hello_hybrid.c 1.05 KB
Newer Older
hautreux's avatar
hautreux committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <omp.h>
#include <stdio.h>
#include <mpi.h>
#include <stdlib.h>
#include <sys/unistd.h>

int main(int argc,char**argv)
{
  int mpiRank, mpiSize;
  char hostname[128];
  int nthreads, tid, cpuid;  
	int i, j=0;

  MPI_Init(&argc,&argv);
  MPI_Comm_rank(MPI_COMM_WORLD, &mpiRank);
  MPI_Comm_size(MPI_COMM_WORLD, &mpiSize);

  gethostname(hostname, sizeof hostname);

	#pragma omp parallel
	{
 	nthreads = omp_get_num_threads();
	}

  if ( mpiRank== 0) 
		printf("Run executed using %d MPI processes, with %d threads per process \n", mpiSize, nthreads);

	for(i = 0; i < mpiSize; i++) {
    MPI_Barrier(MPI_COMM_WORLD);
    if (i == mpiRank) {
  		printf("%s: MPI n° %d -> cpuid %d \n",hostname, mpiRank,sched_getcpu());
		#pragma omp parallel private(tid, nthreads, cpuid) shared(i)
  	{
    	tid=omp_get_thread_num();
    	nthreads = omp_get_num_threads();
    	cpuid = sched_getcpu();

			while(j < tid){
				#pragma omp flush(j)
			}  
   		printf("\t thread n° %d -> cpuid %d \n", tid, cpuid);
			j++;
			#pragma omp flush(j)
		}
    }
	}
  MPI_Finalize();
}