Initializing...

SimpleOMP Examples

Lightweight OpenMP for Emscripten

What is SimpleOMP?

SimpleOMP enables basic parallel programming capabilities in WebAssembly applications through a minimal OpenMP runtime for Emscripten-compiled projects.

Parallel For Loop

Demonstrates basic #pragma omp parallel for usage with heavy computation. Compare execution time with and without parallelization to see the performance benefits.

Conditional Parallelization (if clause)

Shows how to use if(condition) clause to dynamically enable or disable parallelization based on runtime conditions. Serial execution when n=100, parallel execution when n=5000.

Master Thread Construct

Demonstrates #pragma omp master to execute code only on the master thread (thread 0). Useful for initialization, I/O operations, or coordinating parallel work.

Critical Section

Shows #pragma omp critical for mutual exclusion. Ensures only one thread executes the critical section at a time, preventing race conditions.

Barrier Synchronization

Demonstrates #pragma omp barrier to synchronize all threads at specific points. All threads wait at the barrier until every thread in the team arrives.

Single Thread Execution

Shows #pragma omp single where only one thread (first to arrive) executes the block. Useful for one-time initialization in parallel regions.

Loop Scheduling Strategies

Demonstrates schedule(static), schedule(dynamic), schedule(guided), and schedule(runtime) with clear, verifiable output showing how iterations are assigned to threads. Includes validation tests. Runtime schedule can be configured via OMP_SCHEDULE environment variable.

Atomic Operations

Shows #pragma omp atomic for thread-safe variable updates without explicit locks. Demonstrates atomic add for integers and floating-point numbers, preventing race conditions in concurrent updates.

Nowait Clause

Demonstrates nowait clause to skip implicit barriers at the end of worksharing constructs. Compare execution time and thread behavior with and without nowait on single and for constructs.

OpenMP Locks

Demonstrates OpenMP lock API: omp_lock_t for simple locks and omp_nest_lock_t for nestable locks. Shows both blocking (omp_set_lock) and non-blocking (omp_test_lock) lock acquisition patterns.

Data-Sharing Clauses

Demonstrates private, shared, firstprivate, and lastprivate clauses. Shows how variables are scoped and shared between threads in parallel regions and loops. All these clauses are compiler-handled with no runtime overhead.

Cancellation

Shows #pragma omp cancel and #pragma omp cancellation point for early termination of parallel regions and loops. Demonstrates parallel search that stops all threads once the target is found, and error handling that cancels remaining work.

Reduction Operations

Demonstrates #pragma omp reduction for parallel aggregation of variables. Covers common operations: sum (+), product (*), min/max, logical (&&, ||), and bitwise (&, |, ^). Shows how to compute statistics like mean and standard deviation in parallel.

Built with SimpleOMP | Learn More