OpenMP + MPI Hybrid in C - compilation difficulty - c

Have to compile something like the following code, (OpenMP + MPI in C)
I have very little experience with compilers, and been having major trouble with this
,need clear steps how to compile such code in windows
,very much appreciated!
#include <stdio.h>
#include "mpi.h"
#include <omp.h>
int main(int argc, char *argv[]) {
int numprocs, rank, namelen;
char processor_name[MPI_MAX_PROCESSOR_NAME];
int iam = 0, np = 1;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Get_processor_name(processor_name, &namelen);
#pragma omp parallel default(shared) private(iam, np)
{
np = omp_get_num_threads();
iam = omp_get_thread_num();
printf("Hello from thread %d out of %d from process %d out of %d on %s\n",
iam, np, rank, numprocs, processor_name);
}
MPI_Finalize();
}

You can compile with MPI like any other library, and compilers have a specific OpenMP flag. Assuming you use MS-MPI and the Microsoft C/C++ compiler:
Add MPI include and library paths
/I"C:\Program Files (x86)\Microsoft SDKs\MPI\Include" and /libpath:"C:\Program Files (x86)\Microsoft SDKs\MPI\Lib\x64"
Change "C:\Program Files (x86)\Microsoft SDKs\MPI" for wherever you installed MS-MPI
If on a 32 bit machine, repace Lib\x64 with Lib\x86
Add the MPI library to the command line: "msmpi.lib"
Add the OpenMP flag to the command line: /openmp
cl /I"C:\Program Files (x86)\Microsoft SDKs\MPI\Include" /libpath:"C:\Program Files (x86)\Microsoft SDKs\MPI\Lib\x64" /openmp "msmpi.lib" /out:helloworld.exe helloworld.c
See Microsoft's documentation: How to compile and run a simple MS-MPI program and /openmp (Enable OpenMP 2.0 Support)
If you're using GNU tools (mingw or cygwin) then the same would apply: link MPI and add the OpenMP flag.
gcc -I"/path/to/MPI/include" -L"/path/to/MPI/include" -lmpi --openmp -o helloworld.exe helloworld.c
Note that you may need to change -lmpi to -lmpich if your library is a derivative of MPICH.

Related

MPI_Comm_size always returns 1

MPI_Comm_size command always returns single process, when processors count is 4. I'm using MPI 3.2.1 with CLion on virtual xUbuntu 16.04. Emulator - VMWare. On vm settings processors count is set to 4, nproc in xUbuntu says "4".
Here's output for whereis mpicc:
mpicc: /usr/local/bin/mpicc
I'v tried with mpi 1.**, but it alse did the same. What's wrong?
Here is CMakeLists.txt file:
cmake_minimum_required(VERSION 3.8)
project(primes)
set(CMAKE_C_STANDARD 99)
set(SOURCE_FILES main.c)
add_executable(primes ${SOURCE_FILES})
set(CMAKE_C_COMPILER "/usr/local/bin/mpicc")
Here is the part of code:
int main(int argc, char* argv[]) {
...
int rank, tasksCount;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &tasksCount);
UPD
Running though console:
kamo#kamo:~/prog/primes$ mpicc -o main.o main.c
kamo#kamo:~/prog/primes$ mpiexec -np 4 ./mail.o
gives nothing. Nothing happens and nothing is written to console. Just new line without "kamo#kamo:~/prog/primes$" prefix.
I've tried from the very beginning and it worked fine.
CLion debugger works wrong, so you should use mpicc + mpiexec commands as described in UPD block of my question.

CLion debugger not working when using gcc

Im trying to configure CLion so that I can use openMP. When using the default settings on my Mac, the compiler is clang. Default Apple clang does not support openMP.
When I change my compiler to GCC, the debugger will not stop at breakpoints. The program just runs as it would when executing the compiled file.
The CMakeLists.txt file below works perfectly with CLion debugger. When I uncomment out the compiler flags, the debugger ignores the breakpoints.
cmake_minimum_required(VERSION 3.8)
project(CLionTest)
set(CMAKE_C_STANDARD 99)
#set(CMAKE_C_COMPILER /usr/local/bin/gcc-7)
#set(CMAKE_C_FLAGS -fopenmp)
#set(CMAKE_C_FLAGS_DEBUG "-D_DEBUG")
set(MAIN main.c)
add_executable(CLionTest ${MAIN})
add_custom_target(CLionTestMake COMMAND make all WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
How do I fix this?
Toolchain settings:
CMake executable: Bundeled CMake 3.8.2
Debugger: Bundled LLDB 3.9.0
main.c:
#include <stdio.h>
#include <unistd.h>
#ifdef _OPENMP
#include <omp.h>
#endif
int main() {
printf("Hello, World!\n");
#pragma omp parallel
{
#ifdef _OPENMP
int size = omp_get_num_threads();
int rank = omp_get_thread_num();
#else
int rank = 0;
int size = 1;
#endif
printf("%d/%d\n", rank, size);
};
return 0;
}
set(CMAKE_C_FLAGS -fopenmp)
set(CMAKE_C_FLAGS_DEBUG "-D_DEBUG")
You are replacing the C flags instead of appending them, so you are dropping the builtin -g option that generates debug symbols. Instead, do
set(CMAKE_C_FLAGS "${CMAKE_CFLAGS} -fopenmp")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -D_DEBUG")

Error while compiling hello world program in mpi in C on openSUSE

PROGRAM :
#include <stdio.h>
#include <mpi.h>
int main (argc, argv)
int argc;
char *argv[];
{
int rank, size;
MPI_Init (&argc, &argv); /* starts MPI */
MPI_Comm_rank (MPI_COMM_WORLD, &rank); /* get current process id */
MPI_Comm_size (MPI_COMM_WORLD, &size); /* get number of processes */
printf( "Hello world from process %d of %d\n", rank, size );
MPI_Finalize();
return 0;
}
ERROR :
/usr/lib/gcc/i586-suse-linux/4.4/../../../../i586-suse-linux/bin/ld: cannot find -lopen-rte
collect2: ld returned 1 exit status
command for compilation :mpicc hello.c -o ./hello.
I am trying to build a cluster of openSUSE nodes.
So I am testing if mpich2 programs run on every node.
libopen-rte.so refers to OpenMPI, not MPICH2. Check default MPI implementation using mpi-selector tool. I personally prefer OpenMPI.
It looks like you have two MPI libraries installed at the same time. While this is possible, it's usually a pain to configure and use if you're not very careful. I'd suggest uninstalling either Open MPI or MPICH. That should take care of your problem.

How to compile and execute C program with both MPI and OpenMP on Windows

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;
}
You can use the mpicc compiler with the -openmp option. For example,
mpicc -openmp hello.c -o hello
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.
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

compiling a mpi project with mingw64

i'm trying to get following code running with the "mpiexec -n 4 myprogram" command.
#include <stdio.h>
#include "mpi.h"
#include <omp.h>
int main(int argc, char *argv[]) {
int numprocs, rank, namelen;
char processor_name[MPI_MAX_PROCESSOR_NAME];
int iam = 0, np = 1;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Get_processor_name(processor_name, &namelen);
#pragma omp parallel default(shared) private(iam, np)
{
np = omp_get_num_threads();
iam = omp_get_thread_num();
printf("Hello from thread %d out of %d from process %d out of %d on %s\n",
iam, np, rank, numprocs, processor_name);
}
MPI_Finalize();
}
i'm using win7 x64, mpich2 x64, eclipse x64 and mingw64 (rubenvb build). it compiles well and also runs in eclipse environment (but there only with one process), but on the command line it immediatly closes without a result or an error. if i compile it to a x86 exe it runs as intended. so whats going wrong? is mpi incompatible with programs compiled by mingw64?
If you build it as a console program, the program will run, finish and then immediately close as there's likely no command sent by the program to hold the console open.
If you run it again, this time by going into the console first and running it from the command line, the console will stay open as it's running as a seperate process, instead of being tied to your program (as is the case when double clicking to run the program).
As for not running in parallel, make sure you have the flag -fopenmp in both the compilation and linking stages.

Resources