0

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;
}
2
  • Can you get an MPI-only program to work (without OpenMP)? What are the exact steps you take to build and run the program? Commented Oct 10, 2012 at 4:21
  • @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 Commented Oct 10, 2012 at 9:07

3 Answers 3

3

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

mpicc -openmp hello.c -o hello
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for answer! Unfortunately, MPICH2 for Windows doesn't contain such compilator (At least I can't find it in "bin" directory, but I have it on my Unix(AIX) server thanks to MPICH).
Can you add more details about your errors. What is the output of your errors?
0

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.

1 Comment

Thank you for answer! I replaced in my program the call to MPI_Init by your code. The inside section of if is not reached when I compile without -fopenmp key. But when I compile my program identically to the case of the MPI-only program but with -fopenmp key I receive a program that doesn't produce any output again.
0

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

1 Comment

I included this header file but there are no output again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.