Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

StaMp — Simple Threading and Multiprocessing Library

StaMp is a lightweight C++11 parallel programming library built on top of POSIX Threads (Pthreads). It provides a simple, high-level API for parallelizing loops and executing tasks concurrently using C++11 lambda functions — no OpenMP, no TBB, no heavy dependencies.

Features

  • execute_tuple — Run two lambda functions in parallel on separate threads
  • parallel_for (1D) — Parallelize a single for-loop across multiple threads
  • parallel_for (2D) — Parallelize nested for-loops (outer loop partitioned, full inner loop per thread)
  • Automatic work partitioning with remainder handling
  • Supports custom loop bounds (low, high) and stride
  • Built-in execution time statistics

Project Structure

StaMp/
├── library/
│   ├── stamp.h              # Public API header
│   ├── stamp.cpp            # Library implementation
│   └── Makefile             # Builds libstamp.so
├── examples/
│   ├── fib.cpp              # Parallel Fibonacci (execute_tuple)
│   ├── vector.cpp           # Parallel vector addition (1D parallel_for)
│   ├── matrix.cpp           # Parallel matrix multiplication (2D parallel_for)
│   └── Makefile             # Builds and runs examples
├── semaphore_examples/
│   ├── sem_create.cpp       # POSIX named semaphore creation
│   ├── sem_counter_pid1.cpp # Semaphore-based counter (process 1)
│   ├── sem_counter_pid2.cpp # Semaphore-based counter (process 2)
│   ├── sem_async.cpp        # Async semaphore sync using std::future
│   └── Makefile             # Builds and runs semaphore examples
├── .gitignore
└── README.md

API Reference

All functions are in the stamp namespace.

stamp::execute_tuple

void execute_tuple(std::function<void()> &&lambda1, std::function<void()> &&lambda2);

Runs two lambda functions in parallel on separate threads. Blocks until both complete.

Example:

int x, y;
stamp::execute_tuple(
    [&]() { x = fib(n - 1); },
    [&]() { y = fib(n - 2); }
);
int result = x + y;

stamp::parallel_for (1D)

// Full form
void parallel_for(int low, int high, int stride,
                  std::function<void(int)> &&lambda, int numThreads);

// Shorthand (low=0, stride=1)
void parallel_for(int high, std::function<void(int)> &&lambda, int numThreads);

Parallelizes a for-loop by partitioning [low, high) across numThreads threads.

Example:

stamp::parallel_for(0, size, 1, [&](int i) {
    C[i] = A[i] + B[i];
}, numThreads);

stamp::parallel_for (2D)

// Full form
void parallel_for(int low1, int high1, int stride1,
                  int low2, int high2, int stride2,
                  std::function<void(int, int)> &&lambda, int numThreads);

// Shorthand (low=0, stride=1 for both loops)
void parallel_for(int high1, int high2,
                  std::function<void(int, int)> &&lambda, int numThreads);

Parallelizes a nested for-loop. The outer loop is partitioned across threads; each thread executes the full inner loop for its assigned rows.

Example:

stamp::parallel_for(0, size, 1, 0, size, 1, [&](int i, int j) {
    for (int k = 0; k < size; k++) {
        C[i][j] += A[i][k] * B[k][j];
    }
}, numThreads);

Building

Prerequisites

  • g++ with C++11 support
  • POSIX Threads (pthreads) — available on Linux and macOS

Build the Library

cd library
make all

This produces libstamp.so in the library/ directory.

Build the Examples

cd examples
make all

Running

Set the Library Path

From the project root:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$(pwd)/library

Run Examples

cd examples

# Parallel Fibonacci (uses execute_tuple)
make run-fib

# Parallel vector addition (1D parallel_for, default 1 thread)
make run-vector

# Parallel matrix multiplication (2D parallel_for, default 1 thread)
make run-matrix

Specify Thread Count and Problem Size

The vector and matrix examples accept command-line arguments:

# vector <numThreads> <vectorSize>
./vector 4 48000000

# matrix <numThreads> <matrixSize>
./matrix 4 1024

Clean Build Artifacts

cd library && make clean
cd examples && make clean

Using StaMp in Your Own Code

  1. Include the header:

    #include "stamp.h"
  2. Compile with the library:

    g++ -std=c++11 -Wall your_code.cpp -o your_program \
        -L/path/to/StaMp/library -I/path/to/StaMp/library \
        -lstamp -pthread
  3. Set the library path before running:

    export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/path/to/StaMp/library
    ./your_program

Semaphore Examples

The semaphore_examples/ directory contains standalone POSIX semaphore examples, independent of the StaMp library. They demonstrate inter-process and inter-thread synchronization using named semaphores.

cd semaphore_examples
make all

# Create a named semaphore
make run-create

# Run two processes with semaphore synchronization
make run-counters

# Run async semaphore sync using std::future
make run-async

# Clean up
make clean

License

This project is provided as-is for educational purposes.

About

StaMp: Runtime for Static Mapping of Tasks to Threads

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages