/openmp
OpenMP skill for shared-memory parallel programming. Use when writing parallel for loops, reductions, task parallelism, SIMD directives, GPU offloading, or profiling with Score-P/TAU. Activates on queries about OpenMP, pragma omp, schedule static dynamic, reduction, false
$ npx -y skills add mohitmishra786/low-level-dev-skills --skill openmp --agent claude-codeHow it fires
How this skill gets triggered: by you, by Claude, or both.
- Fires itselfAuto-invocation. Claude auto-loads it when your prompt matches the work.Auto-invocation is when the right skill fires by itself at the right moment, driven by a FLOW.md router and a hook, instead of you invoking it by name. It is the difference between a skill being installed and a skill actually getting used.Read the full definition →
- You can call itInvoke it directly when you want it.
- Slash command
/openmp
Context preview
The summary Claude sees to decide when to auto-load this skill.
OpenMP skill for shared-memory parallel programming. Use when writing parallel for loops, reductions, task parallelism, SIMD directives, GPU offloading, or profiling with Score-P/TAU. Activates on queries about OpenMP, pragma omp, schedule static dynamic, reduction, false
SKILL.md
openmp.SKILL.mdname: openmp
description: OpenMP skill for shared-memory parallel programming. Use when writing parallel for loops, reductions, task parallelism, SIMD directives, GPU offloading, or profiling with Score-P/TAU. Activates on queries about OpenMP, pragma omp, schedule static dynamic, reduction, false sharing, or OMP_NUM_THREADS.
OpenMP
Purpose
Guide agents through OpenMP shared-memory parallelism: `#pragma omp parallel for` with scheduling clauses, reductions, data-sharing attributes, SIMD hints, task parallelism, OpenMP 5.x GPU `target` offloading, common pitfalls (false sharing, data races), environment tuning, and profiling with Score-P or TAU.
When to Use
- Parallelizing C/C++/Fortran loops on multicore CPUs
- Implementing reductions (sum, max, custom)
- Task parallelism for irregular workloads
- Offloading compute to GPU with OpenMP target directives
- Diagnosing scaling failures (false sharing, load imbalance)
- Tuning thread count and spin behavior
Workflow
1. Basic parallel for
#include <omp.h>
#include <stdio.h>
int main(void) {
const int n = 1000000;
double sum = 0.0;
#pragma omp parallel for reduction(+:sum)
for (int i = 0; i < n; i++)
sum += i * 0.001;
printf("sum = %f, threads = %d\n", sum, omp_get_max_threads());
return 0;
}gcc -fopenmp -O3 -o omp_sum omp_sum.c
export OMP_NUM_THREADS=8
./omp_sum
2. Schedule clauses
#pragma omp parallel for schedule(static) // equal chunks, low overhead
#pragma omp parallel for schedule(dynamic, 64) // dynamic chunks of 64
#pragma omp parallel for schedule(guided) // decreasing chunk size
#pragma omp parallel for schedule(auto) // compiler/runtime decides
| Schedule | Best for | |----------|----------| | `static` | Uniform work per iteration | | `dynamic` | Variable iteration cost | | `guided` | Decreasing iteration cost | | `static,1` | Cache blocking with interleaved chunks |
3. Data sharing attributes
int shared_var = 0;
#pragma omp parallel private(i) shared(shared_var)
{
int i = omp_get_thread_num();
#pragma omp atomic
shared_var += i;
}
// firstprivate — copy in; lastprivate — copy out after loop
#pragma omp parallel for firstprivate(offset) lastprivate(result)
for (int i = 0; i < n; i++) { ... }| Clause | Meaning | |--------|---------| | `private` | Uninitialized per-thread copy | | `shared` | One variable, all threads | | `reduction(op:var)` | Combine at end (+, *, max, &&, \|\|) | | `firstprivate` | Initialize from master | | `lastprivate` | Master gets last iteration value |
4. SIMD vectorization hint
#pragma omp simd
for (int i = 0; i < n; i++)
c[i] = a[i] + b[i];
// SIMD + parallel
#pragma omp parallel for simd
for (int i = 0; i < n; i++)
c[i] = a[i] * b[i];Requires `-fopenmp-simd` or `-fopenmp` with compiler SIMD support. Check with `-fopt-info-vec`.
5. Task parallelism
#pragma omp parallel
{
#pragma omp single
{
for (int i = 0; i < 10; i++) {
#pragma omp task firstprivate(i)
process_subtree(i);
}
#pragma omp taskwait
}
}Tasks suit recursive algorithms (quicksort, tree traversal) where loop parallelism doesn't fit.
6. Timing
double start = omp_get_wtime();
#pragma omp parallel for
for (int i = 0; i < n; i++) work(i);
double elapsed = omp_get_wtime() - start;
printf("elapsed: %f s\n", elapsed);7. GPU target offloading (OpenMP 5.x)
#pragma omp target teams distribute parallel for map(to:a[0:n]) map(from:c[0:n])
for (int i = 0; i < n; i++)
c[i] = a[i] * 2.0f;# NVIDIA offload
gcc -fopenmp -foffload=-march=sm_80 -o offload offload.c
# Check device
export OMP_DEFAULT_TARGET_DEVICE=1
Requires compiler offload support (GCC offload, Clang/OpenMP, NVIDIA HPC SDK).
8. Environment variables
export OMP_NUM_THREADS=16
export OMP_PROC_BIND=close # bind threads to nearby cores
export OMP_PLACES=cores
export GOMP_SPINCOUNT=2000 # spin before sleep
export OMP_WAIT_POLICY=active # active vs passive waiting
export OMP_DISPLAY_ENV=true # print config at startup
9. Profiling
# Score-P (compile with wrapper)
scorep gcc -fopenmp -o app app.c
export SCOREP_METRIC_MANAGER=1
scorep ./app
scorep-score -f scorep_*/profile.cubex
# TAU
tau_cc.sh -fopenmp -o app app.c
export TAU_TRACE=1
./app
pprof app profile.*
10. Pitfalls
**False sharing**: threads modify adjacent cache lines.
// Bad: sum_array[tid] on same cache line
#pragma omp parallel
{
int tid = omp_get_thread_num();
sum_array[tid] += local_sum; // pad to 64 bytes between elements
}
// Fix: padding
double sum_padded[MAX_THREADS][8]; // 8 doubles = 64 bytes**Nested parallelism**:
export OMP_MAX_ACTIVE_LEVELS=2
export OMP_NESTED=true # deprecated; use MAX_ACTIVE_LEVELS
Common Problems
| Symptom | Cause | Fix | |---------|-------|-----| | No speedup | Loop too small | Increase work; check `if` clause threshold | | Wrong reduction result | Race on non-reduction var | Use `reduction` or `atomic` | | Slower with more threads | False sharing | Pad per-thread arrays | | GPU offload fails | No target device | Check `-foffload`; `nvidia-smi` | | Threads not bound | Default spread | `OMP_PROC_BIND=close` | | Nested deadlock | Oversubscription | Limit `OMP_NUM_THREADS` per level |
Related Skills
- `skills/hpc/mpi` — distributed memory complement
- `skills/low-level-programming/cpu-cache-opt` — false sharing deep dive
- `skills/gpu/cuda` — GPU programming alternative to target offload
- `skills/profilers/intel-vtune-amd-uprof` — OpenMP region analysis in VTune
- `skills/compilers/gcc` — `-fopenmp` flags
- `skills/allocators/numa-programming` — NUMA-aware thread binding
Read more
name: openmp description: OpenMP skill for shared-memory parallel programming. Use when writing parallel for loops, reductions, task parallelism, SIMD directives, GPU offloading, or profiling with Score-P/TAU. Activates on queries about OpenMP, pragma omp, schedule static dynamic, reduction, false sharing, or OMP_NUM_THREADS.
OpenMP
Purpose
Guide agents through OpenMP shared-memory parallelism: `#pragma omp parallel for` with scheduling clauses, reductions, data-sharing attributes, SIMD hints, task parallelism, OpenMP 5.x GPU `target` offloading, common pitfalls (false sharing, data races), environment tuning, and profiling with Score-P or TAU.
When to Use
- Parallelizing C/C++/Fortran loops on multicore CPUs
- Implementing reductions (sum, max, custom)
- Task parallelism for irregular workloads
- Offloading compute to GPU with OpenMP target directives
- Diagnosing scaling failures (false sharing, load imbalance)
- Tuning thread count and spin behavior
Workflow
1. Basic parallel for
#include <omp.h>
#include <stdio.h>
int main(void) {
const int n = 1000000;
double sum = 0.0;
#pragma omp parallel for reduction(+:sum)
for (int i = 0; i < n; i++)
sum += i * 0.001;
printf("sum = %f, threads = %d\n", sum, omp_get_max_threads());
return 0;
}gcc -fopenmp -O3 -o omp_sum omp_sum.c export OMP_NUM_THREADS=8 ./omp_sum
2. Schedule clauses
#pragma omp parallel for schedule(static) // equal chunks, low overhead #pragma omp parallel for schedule(dynamic, 64) // dynamic chunks of 64 #pragma omp parallel for schedule(guided) // decreasing chunk size #pragma omp parallel for schedule(auto) // compiler/runtime decides
| Schedule | Best for | |----------|----------| | `static` | Uniform work per iteration | | `dynamic` | Variable iteration cost | | `guided` | Decreasing iteration cost | | `static,1` | Cache blocking with interleaved chunks |
3. Data sharing attributes
int shared_var = 0;
#pragma omp parallel private(i) shared(shared_var)
{
int i = omp_get_thread_num();
#pragma omp atomic
shared_var += i;
}
// firstprivate — copy in; lastprivate — copy out after loop
#pragma omp parallel for firstprivate(offset) lastprivate(result)
for (int i = 0; i < n; i++) { ... }| Clause | Meaning | |--------|---------| | `private` | Uninitialized per-thread copy | | `shared` | One variable, all threads | | `reduction(op:var)` | Combine at end (+, *, max, &&, \|\|) | | `firstprivate` | Initialize from master | | `lastprivate` | Master gets last iteration value |
4. SIMD vectorization hint
#pragma omp simd
for (int i = 0; i < n; i++)
c[i] = a[i] + b[i];
// SIMD + parallel
#pragma omp parallel for simd
for (int i = 0; i < n; i++)
c[i] = a[i] * b[i];Requires `-fopenmp-simd` or `-fopenmp` with compiler SIMD support. Check with `-fopt-info-vec`.
5. Task parallelism
#pragma omp parallel
{
#pragma omp single
{
for (int i = 0; i < 10; i++) {
#pragma omp task firstprivate(i)
process_subtree(i);
}
#pragma omp taskwait
}
}Tasks suit recursive algorithms (quicksort, tree traversal) where loop parallelism doesn't fit.
6. Timing
double start = omp_get_wtime();
#pragma omp parallel for
for (int i = 0; i < n; i++) work(i);
double elapsed = omp_get_wtime() - start;
printf("elapsed: %f s\n", elapsed);7. GPU target offloading (OpenMP 5.x)
#pragma omp target teams distribute parallel for map(to:a[0:n]) map(from:c[0:n])
for (int i = 0; i < n; i++)
c[i] = a[i] * 2.0f;# NVIDIA offload gcc -fopenmp -foffload=-march=sm_80 -o offload offload.c # Check device export OMP_DEFAULT_TARGET_DEVICE=1
Requires compiler offload support (GCC offload, Clang/OpenMP, NVIDIA HPC SDK).
8. Environment variables
export OMP_NUM_THREADS=16 export OMP_PROC_BIND=close # bind threads to nearby cores export OMP_PLACES=cores export GOMP_SPINCOUNT=2000 # spin before sleep export OMP_WAIT_POLICY=active # active vs passive waiting export OMP_DISPLAY_ENV=true # print config at startup
9. Profiling
# Score-P (compile with wrapper) scorep gcc -fopenmp -o app app.c export SCOREP_METRIC_MANAGER=1 scorep ./app scorep-score -f scorep_*/profile.cubex # TAU tau_cc.sh -fopenmp -o app app.c export TAU_TRACE=1 ./app pprof app profile.*
10. Pitfalls
**False sharing**: threads modify adjacent cache lines.
// Bad: sum_array[tid] on same cache line
#pragma omp parallel
{
int tid = omp_get_thread_num();
sum_array[tid] += local_sum; // pad to 64 bytes between elements
}
// Fix: padding
double sum_padded[MAX_THREADS][8]; // 8 doubles = 64 bytes**Nested parallelism**:
export OMP_MAX_ACTIVE_LEVELS=2 export OMP_NESTED=true # deprecated; use MAX_ACTIVE_LEVELS
Common Problems
| Symptom | Cause | Fix | |---------|-------|-----| | No speedup | Loop too small | Increase work; check `if` clause threshold | | Wrong reduction result | Race on non-reduction var | Use `reduction` or `atomic` | | Slower with more threads | False sharing | Pad per-thread arrays | | GPU offload fails | No target device | Check `-foffload`; `nvidia-smi` | | Threads not bound | Default spread | `OMP_PROC_BIND=close` | | Nested deadlock | Oversubscription | Limit `OMP_NUM_THREADS` per level |
Related Skills
- `skills/hpc/mpi` — distributed memory complement
- `skills/low-level-programming/cpu-cache-opt` — false sharing deep dive
- `skills/gpu/cuda` — GPU programming alternative to target offload
- `skills/profilers/intel-vtune-amd-uprof` — OpenMP region analysis in VTune
- `skills/compilers/gcc` — `-fopenmp` flags
- `skills/allocators/numa-programming` — NUMA-aware thread binding
A curated suite of AI agent skills for systems and low-level programming — C/C++, Rust, Zig, GPU, bare-metal firmware, Linux kernel/driver development, computer architecture, compiler internals, HPC, and more.
Repo: mohitmishra786/low-level-dev-skills
Other skills on low-level-dev-skills.
- /custom-allocators
Custom allocator skill for memory allocation strategies. Use when implementing pool/slab/arena allocators, tuning jemalloc/mimalloc, writing Rust GlobalAlloc, or benchmarking allocator performance. Activates on queries about jemalloc, mimalloc, tcmalloc, arena allocator,
Open skill - /numa-programming
NUMA programming skill for multi-socket memory locality. Use when detecting NUMA topology, binding processes with numactl, using libnuma API, building NUMA-aware data structures, or measuring remote access penalties. Activates on queries about numactl, libnuma, NUMA topology,
Open skill - /af-xdp
AF_XDP skill for high-performance XDP sockets. Use when creating AF_XDP sockets, configuring UMEM and XSK rings, XDP_REDIRECT programs, copy vs zero-copy mode, or comparing with DPDK. Activates on queries about AF_XDP, xsk_umem, XDP_REDIRECT, libbpf xsk, or zero-copy XDP.
Open skill - /dpdk
DPDK skill for userspace packet I/O. Use when initializing EAL, configuring PMD drivers, using mbuf pools and rte_ring, setting up huge pages, RSS, or testpmd validation. Activates on queries about DPDK, EAL, rte_eth_rx_burst, hugepages, PMD, or testpmd.
Open skill - /io-uring
io_uring skill for Linux async I/O. Use when building high-performance servers with liburing, multi-shot operations, provided buffers, fixed files, zero-copy send, or tokio-uring. Activates on queries about io_uring, SQE/CQE, liburing, IORING_OP_PROVIDE_BUFFERS, or io_uring vs
Open skill - /adc-dac-baremetal
Bare-metal ADC and DAC skill. Use when configuring analog sampling, DMA-driven ADC, calibration, or DAC output on MCUs. Activates on queries about ADC bare-metal, sampling time, DMA ADC, or DAC channel setup.
Open skill

