In this page we will compile a simple OpenMP program with MPC openMP. The sample code to be used is present at the bottom of the page.
Compile an OpenMP COde
MPC uses the regular OpenMP flag from its underlying GCC:
# Compile test.c with OpenMP support mpc_cc -fopenmp ./test.c -o test
Run an OpenMP Code
You can now run your OpenMP program in various configuration.
# A single process mpcrun ./test # or simply ./test
With an output similar to:
MPC version 3.3.0 C/C++ (1 tasks 1 processes 4 cpus (3.59GHz)
(...)
Hello World Thread 0 / 4
Hello World Thread 3 / 4
Hello World Thread 1 / 4
Hello World Thread 2 / 4
Note that the OpenMP environment variable OMP_NUM_THREADS is supported:
# Run with two OpenMP threads OMP_NUM_THREADS=2 ./test #Produces as output: MPC version 3.3.0 C/C++ (1 tasks 1 processes 4 cpus (3.59GHz) (...) Hello World Thread 1 / 2 Hello World Thread 2 / 2
Sample Code
#include <omp.h> #include <stdio.h> #include <stdlib.h> int main (int argc, char *argv[]) { int nthreads, tid; #pragma omp parallel private(nthreads, tid) { tid = omp_get_thread_num(); nthreads = omp_get_num_threads(); printf("Hello World Thread %d / %d\n", tid, nthreads); } return 0; }