Skip to content
Snippets Groups Projects
threads.cpp 751 B
Newer Older
  • Learn to ignore specific revisions
  • Ondrej Meca's avatar
    Ondrej Meca committed
    
    #include "mpi.h"
    #include "omp.h"
    #include <sstream>
    #include <unistd.h>
    
    int main(int argc, char **argv) {
    	int threads;
    	std::stringstream env(getenv("OMP_NUM_THREADS"));
    	env >> threads;
    
    	int provided;
    	MPI_Init_thread(&argc, &argv, MPI_THREAD_FUNNELED, &provided);
    	if (provided < MPI_THREAD_FUNNELED) {
    		printf("MPI does not support required MPI / THREAD combination [provided=%d].\n", provided);
    		return 0;
    	}
    
    	int rank, size;
    	MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    	MPI_Comm_size(MPI_COMM_WORLD, &size);
    
    	#pragma omp parallel for
    	for (int t = 0; t < threads; t++) {
    		double start = omp_get_wtime();
    		while (omp_get_wtime() - start < 5);
    		printf("Hello world from rank: %d-%d / %d\n", rank, t, size);
    	}
    
    	MPI_Finalize();
    	return 0;
    }