forked from HLRA-JHPCN/comm_bench
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cpu2cpu.c
83 lines (74 loc) · 2.23 KB
/
cpu2cpu.c
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <mpi.h>
#define min(a,b) (a<b?a:b)
int main(int argc, char **argv)
{
int i;
int myid, nprocs, ierr, provided;
MPI_Status status;
int N = 1000, loops;
double time, t_min=999999.99, t_max=0.0, t_sum=0.0;
double *data;
if(argc!=3){
printf("usage: %s length loops\n", argv[0]);
return -1;
}
N = atoi(argv[1]);
loops = atoi(argv[2]);
//ierr = MPI_Init_thread(&argc,&argv,MPI_THREAD_FUNNELED,&provided);
//if(provided!=MPI_THREAD_FUNNELED)printf("MPI_THREAD_FUNNELED is not provided.\n");
MPI_Init(&argc,&argv);
ierr = MPI_Comm_rank(MPI_COMM_WORLD, &myid);
ierr = MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
if(nprocs!=2){
printf("2 processes are required.\n");
return -1;
}
data = (double*)malloc(sizeof(double)*N*2);
for(i=0;i<N*2;i++)data[i] = (double)i;
ierr = MPI_Barrier(MPI_COMM_WORLD);
for(i=0; i<10; i++){
if(myid==0){
ierr = MPI_Send(data, N, MPI_DOUBLE, 1, 0, MPI_COMM_WORLD);
ierr = MPI_Recv(data, N, MPI_DOUBLE, 1, 0, MPI_COMM_WORLD, &status);
}else if(myid==1){
ierr = MPI_Recv(data, N, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, &status);
ierr = MPI_Send(&data[N], N, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD);
}
}
ierr = MPI_Barrier(MPI_COMM_WORLD);
for(i=0; i<loops; i++){
if(myid==0){
time = MPI_Wtime();
ierr = MPI_Send(data, N, MPI_DOUBLE, 1, 0, MPI_COMM_WORLD);
ierr = MPI_Recv(data, N, MPI_DOUBLE, 1, 0, MPI_COMM_WORLD, &status);
time = MPI_Wtime() - time;
if(time>t_max)t_max=time;
if(time<t_min)t_min=time;
t_sum += time;
}else if(myid==1){
ierr = MPI_Recv(data, N, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, &status);
ierr = MPI_Send(&data[N], N, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD);
}
}
ierr = MPI_Barrier(MPI_COMM_WORLD);
if(myid==0){
printf("TIME %d : %e (average %e msec, min %e msec, max %e msec, average %f GByte/sec)\n",
myid, t_sum,
t_sum/(double)loops*1000.0,
t_min*1000.0,
t_max*1000.0,
(double)(sizeof(double)*loops*N)*2.0/t_sum/1024.0/1024.0/1024.0
);
printf("result:");
for(i=0;i<min(N,10);i++){
printf(" %f", data[i]);
}
printf("\n\n");
}
free(data);
ierr = MPI_Finalize();
return 0;
}