Hello World in OpenMP

Using MPC to compile and run OpenMP programs is relatively simple. In most cases, you simply compile your code with the MPC compiler (mpc_cc, mpc_cxx, mpc_f77, etc.).

Here’s our example code: {{attachment:hello_world.c}}

Compile hello_world.c

Use the MPC C compiler command, mpc_cc, to compile the OpenMP code:

mpc_cc -fopenmp hello_world.c -o hello

Execute with mpcrun

Use the mpcrun command to launch the executable:

mpcrun ./hello

You should see output similar to this:

Running MPC with HYDRA job manager
MPC version 2.4.1 C/C++ (1 tasks 1 processes 4 cpus (2.39GHz) ethread_mxn) MPC allocator  none
Initialization time: 0.1s - Memory used: 3MB
Warning: Number of microVPs set to <0> -> Switching to default value 4
MPC OpenMP version 2.5 (DEV)
        OMP_SCHEDULE 1
        OMP_NUM_THREADS 4
        OMP_DYNAMIC 0
        OMP_NESTED 0
        4 VPs (OMP_VP_NUMBER)
Hello, world from thread 3 of 4
Hello, world from thread 2 of 4
Hello, world from thread 1 of 4
Hello, world from thread 0 of 4

To execute on multiple nodes, use the -N parameter:

mpcrun -n=1 -N=4 -net=tcp ./hello

We’ve also used the -net parameter to specify our network fabric. The default in most cases in InfiBand, but you can select TCP/IP with the tcp parameter.

You should see output similar to this:

Running MPC with HYDRA job manager
MPC version 2.4.1 C/C++ (1 tasks 1 processes 4 cpus (2.39GHz) ethread_mxn) MPC allocator  none
Initialization time: 0.1s - Memory used: 3MB
Warning: Number of microVPs set to <0> -> Switching to default value 4
MPC OpenMP version 2.5 (DEV)
        OMP_SCHEDULE 1
        OMP_NUM_THREADS 4
        OMP_DYNAMIC 0
        OMP_NESTED 0
        4 VPs (OMP_VP_NUMBER)
Hello, world from thread 3 of 4
Hello, world from thread 2 of 4
Hello, world from thread 1 of 4
Hello, world from thread 0 of 4

The value of the OMP_NUM_THREADS environment variable is still observed:

export OMP_NUM_THREADS=2
Running MPC with HYDRA job manager
MPC version 2.4.1 C/C++ (1 tasks 1 processes 4 cpus (2.39GHz) ethread_mxn) MPC allocator  none
Initialization time: 0.1s - Memory used: 3MB
Warning: Number of microVPs set to <0> -> Switching to default value 4
MPC OpenMP version 2.5 (DEV)
        OMP_SCHEDULE 1
        OMP_NUM_THREADS 2
        OMP_DYNAMIC 0
        OMP_NESTED 0
        4 VPs (OMP_VP_NUMBER)
Hello, world from thread 1 of 2
Hello, world from thread 0 of 2