0
votes

I have a program on C that uses both MPI and OpenMP. In order to compile such program on Windows system I have downloaded and installed a gcc compiler provided by MinGW. Using this compiler I can compile and execute C programs with OpenMP using the key -fopenmp for gcc. Such programs run without problems. In order to compile and execute C programs with MPI I have downloaded and installed MPICH2. Now I can compile and run such programs without problems, specifying additional parameters for gcc, provided by MinGW. But when I want to compile and run a program that uses both OpenMP and MPI I have a problem. I specified both keys -fopenmp and keys for MPI program for gcc compiler. Compilator didn't give me any error. I tried to launch my program by mpiexec, provided by MPICH2. My program didn't want to work (It was a HelloWorld program and it didn't print anything to output). Please help me to compile and launch such programs correctly.

Here is my HelloWorld program, that doesn't produce any output.

#include <stdio.h>
#include <mpi.h>

int main(int argc, char ** argv)
{
    int thnum, thtotal;
    int pid, np;

    MPI_Init(&argc,&argv);
    MPI_Comm_rank(MPI_COMM_WORLD,&pid);
    MPI_Comm_size(MPI_COMM_WORLD,&np);

    printf("Sequental %d out of %d!\n",pid,np);
    MPI_Barrier(MPI_COMM_WORLD);

    #pragma omp parallel private(thnum,thtotal)
    {
        thnum = omp_get_thread_num();
        thtotal = omp_get_num_threads();
        printf("parallel: %d out of %d from proc %d out of %d\n",thnum,thtotal,pid,np);

    }
    MPI_Barrier(MPI_COMM_WORLD);
    MPI_Finalize();
    return 0;
}
3
Can you get an MPI-only program to work (without OpenMP)? What are the exact steps you take to build and run the program? - Greg Inozemtsev
@GregInozemtsev MPI-only program works good. To build and run MPI-only program I use: gcc -c -o main.o main.c -I"C:\...\MPHICH2\include"; gcc -o main.exe main.o -L"C:\...\MPICH2\lib" -lmpi; mpiexec -n 4 main.exe - Appliqué

3 Answers

2
votes

You can use the mpicc compiler with the -openmp option. For example,

mpicc -openmp hello.c -o hello
0
votes

This might not be the root cause of your problem, but the MPI standard mandates that threaded programs use MPI_Init_thread() instead of MPI_Init(). In your case there are no MPI calls from within the parallel region so threading level of MPI_THREAD_FUNNELED should suffice. You should replace the call to MPI_Init() with:

int provided;

MPI_Init_thread(&argc, &argv, MPI_THREAD_FUNNELED, &provided);
if (provided < MPI_THREAD_FUNNELED)
{
    MPI_Abort(MPI_COMM_WORLD, 1);
    return 1; // Usually not reached
}

Although some MPI libraries might not advertise threading support (provided as returned is MPI_THREAD_SINGLE) they still work fine with hybrid OpenMP/MPI codes if one does not make MPI calls from within parallel regions.

0
votes

The OpenMP portion of your program might require #include <omp.h> :

parallel: 0 out of 2 from proc 0 out of 0
parallel: 1 out of 2 from proc 0 out of 0