Skip to main content
skill CX Codex

openmp — Claude Code skill: what it does, how to install

Teaches the agent to write and fix OpenMP code: parallel loops, reductions, tasks, SIMD hints and GPU offload.

MIT 371 installs ★ 298 updated 2026-06-27

Install into Claude Code · Skills CLI (Vercel) All methods and platforms ↓
npx skills add mohitmishra786/low-level-dev-skills --skill openmp -a claude-code
Open on GitHub

What openmp does

The skill walks the agent through shared-memory parallelism: #pragma omp parallel for with a scheduling clause, reductions, data-sharing attributes, SIMD hints and target offload to a GPU. Tasks get their own section, for recursive code where loop parallelism does not fit. Samples are in C, and the tables map each schedule option to the workload it suits.

Failures get equal space. False sharing is fixed by padding per-thread arrays to 64 bytes, and a race on a variable left out of reduction moves to atomic. Thread tuning is spelled out through OMP_NUM_THREADS, OMP_PROC_BIND and OMP_PLACES, with commands for profiling a run under Score-P or TAU.

You need a compiler that supports OpenMP, since the samples build with gcc -fopenmp. GPU offload also wants a toolchain with offload support (GCC offload, Clang/OpenMP, NVIDIA HPC SDK) and a real target device — without one the target region has nowhere to run.

What it helps with

  • parallelise a C loop without data races
  • pick a schedule for uneven iteration cost
  • track down false sharing that slows a run
  • move a kernel to a GPU with target
  • profile an OpenMP build under Score-P

Example prompts for the agent

  • Parallelise this summation loop with #pragma omp parallel for and reduction, then build it with gcc -fopenmp.
  • More threads made it slower — check for false sharing and set OMP_PROC_BIND.
  • Port this loop to #pragma omp target teams distribute parallel for.

How to install openmp

1Skills CLI (Vercel)

npx skills add mohitmishra786/low-level-dev-skills --skill openmp -a claude-code

2Manually: put the files in place

~/.claude/skills/openmp/SKILL.md

Copy the whole skill folder; for one project use .claude/skills/openmp/

Step by step, with commands for each platform: How to install skills in Claude Code: five ways

Questions about openmp

How do I install openmp in Claude Code?

Run npx skills add mohitmishra786/low-level-dev-skills --skill openmp -a claude-code. The skill file lands at ~/.claude/skills/openmp/SKILL.md.

What does the openmp skill do?

It guides the agent through OpenMP code: parallel loops, reductions, tasks, SIMD hints and target offload. It also points at the scheduling clause that fits a loop and at what breaks a reduction result.

What do I need for GPU offload?

A compiler with offload support, such as GCC offload, Clang/OpenMP or the NVIDIA HPC SDK, plus an actual target device. The build example in the skill is gcc -fopenmp -foffload=-march=sm_80.

Source text

SKILL.md · MIT
---
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

```c
#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;
}
```

```bash
gcc -fopenmp -O3 -o omp_sum omp_sum.c
export OMP_NUM_THREADS=8
./omp_sum
```

### 2. Schedule clauses

```c
#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

```c
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

```c
#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

```c
#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

```c
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)

```c
#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;
```

```bash
# 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

```bash
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

```bash
# 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.

```c
// 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**:

```bash
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

© mohitmishra786 · MIT · mohitmishra786/low-level-dev-skills

Similar skills

All skills for Programming languages →

From the same set140

The whole set →
writing-char-drivers writing-char-drivers — a skill for writing Linux character drivers: cdev registration, file_operations implementation, safe userspace copies, ioctl and mmap. CX Codex +1 378installs adc-dac-baremetal adc-dac-baremetal — a skill for bare-metal ADC and DAC setup on MCUs: channel selection, sampling time, calibration, DMA buffers, and voltage output. Essential when working with analog sensors, audio signals, or control voltages. CX Codex +1 378installs resource-optimization-lowend The resource-optimization-lowend skill helps trim flash and RAM use in MCU firmware, inspect linker maps, and measure stack depth when embedded resources are tight. CX Codex +1 376installs llvm-passes llvm-passes guides coding agents in creating, building, and verifying LLVM optimization passes with the New Pass Manager. CX Codex +1 373installs numa-programming Skill teaches the agent NUMA programming: detect topology with numactl and lstopo, bind processes to local memory, use libnuma API, build per-node data structures, and diagnose remote access penalties. CX Codex +1 369installs dpdk Skill for DPDK work: EAL init, poll-mode driver setup, mbuf pools, rte_ring, huge pages, RSS, and testpmd validation. CX Codex +1 369installs timers-pwm-baremetal timers-pwm-baremetal — a skill to configure MCU timers without an OS: PWM for LED dimming and motor control, input capture for pulse measurement, and periodic interrupts via SysTick. Works on STM32 and other bare-metal platforms. CX Codex +1 367installs mlir Skill for working with MLIR, a compiler infrastructure where each abstraction level lives in its own dialect. Useful when building domain-specific compilers and optimizers. CX Codex +1 366installs

Project sponsors

Companies that keep this analytics open