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.
execute_tuple— Run two lambda functions in parallel on separate threadsparallel_for(1D) — Parallelize a single for-loop across multiple threadsparallel_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) andstride - Built-in execution time statistics
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
All functions are in the stamp namespace.
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;// 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);// 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);- g++ with C++11 support
- POSIX Threads (pthreads) — available on Linux and macOS
cd library
make allThis produces libstamp.so in the library/ directory.
cd examples
make allFrom the project root:
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$(pwd)/librarycd 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-matrixThe vector and matrix examples accept command-line arguments:
# vector <numThreads> <vectorSize>
./vector 4 48000000
# matrix <numThreads> <matrixSize>
./matrix 4 1024cd library && make clean
cd examples && make clean-
Include the header:
#include "stamp.h"
-
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 -
Set the library path before running:
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/path/to/StaMp/library ./your_program
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 cleanThis project is provided as-is for educational purposes.