skills/hpc/openmp/SKILL.md
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.
npx skillsauth add mohitmishra786/low-level-dev-skills openmpInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
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.
#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
#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 |
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 |
#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.
#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.
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);
#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).
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
# 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.*
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
| 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 |
skills/hpc/mpi — distributed memory complementskills/low-level-programming/cpu-cache-opt — false sharing deep diveskills/gpu/cuda — GPU programming alternative to target offloadskills/profilers/intel-vtune-amd-uprof — OpenMP region analysis in VTuneskills/compilers/gcc — -fopenmp flagsskills/allocators/numa-programming — NUMA-aware thread bindingdevelopment
QEMU/KVM skill for virtualization and kernel development. Use when running qemu-system-x86_64 with KVM, configuring virtio devices, VFIO passthrough, QMP monitor, libvirt, or booting custom kernels. Activates on queries about QEMU, KVM, virtio, VFIO, virsh, virt-install, or -kernel -append.
development
Hardware virtualization internals skill for Intel VT-x and AMD-V. Use when studying VMCS/VMCB, EPT/NPT page tables, VMEXIT handling, APIC virtualization, or building minimal hypervisors. Activates on queries about VMX, SVM, VMCS, EPT, NPT, VMEXIT, or type-1 hypervisor.
testing
Linux containers internals skill for namespaces, cgroups, and OCI. Use when understanding clone/unshare namespaces, cgroups v2 limits, overlayfs, runc, seccomp profiles, capabilities, or escape mitigations. Activates on queries about namespaces, cgroups, overlayfs, runc, seccomp-bpf, OCI spec, or container escape.
tools
Reverse engineering skill for binary analysis. Use when decompiling with Ghidra, analyzing with radare2, scripting RE tools, triaging with strings/file/xxd, or diffing binaries. Activates on queries about Ghidra, radare2, r2, decompiler, Binary Ninja, Diaphora, or stripped binary analysis.