-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpi-send-m-msg-s-barrier-s-timer.c
74 lines (65 loc) · 2.21 KB
/
mpi-send-m-msg-s-barrier-s-timer.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
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
#include <time.h>
#define call_mpi( func, ...) { \
res = func(__VA_ARGS__); \
if( res != MPI_SUCCESS ) { \
fprintf(stderr, "error on line[%d]\n", __LINE__ ); \
exit(1); \
} \
}
#define MAX_MSG_SIZE 4194304 // 4 MB
void cbarrier(void) {
MPI_Barrier(MPI_COMM_WORLD);
}
int main(int argc, char** argv) {
int res, size, proc_name_len, rank, msg_size, dest, tag, num_of_iterations, iteration;
char hostname[MPI_MAX_PROCESSOR_NAME];
char* msg_buf;
MPI_Status recv_status;
struct timespec t_start, t_end;
double xfer_time_usecs;
num_of_iterations = 10000;
msg_buf = (char*)malloc( sizeof(char)*MAX_MSG_SIZE );
if( msg_buf == NULL ) {
fprintf(stderr, "error: failed to malloc at %d\n", __LINE__);
return 1;
}
res = MPI_Init(&argc, &argv);
call_mpi(MPI_Comm_size, MPI_COMM_WORLD, &size ) ;
call_mpi(MPI_Comm_rank, MPI_COMM_WORLD, &rank);
call_mpi(MPI_Get_processor_name, hostname, &proc_name_len);
printf("Rank [%d/%d] runs on [%s]\n", rank, size, hostname);
tag = 81;
for(msg_size = 0; msg_size <= MAX_MSG_SIZE; msg_size = (msg_size ? msg_size*2 : 1) ) {
cbarrier();
if( rank == 0 ) {
res = clock_gettime(CLOCK_MONOTONIC, &t_start );
if( res != 0 ) {
fprintf(stderr, "error: failed at clock_gettime %d\n", __LINE__);
return 1;
}
for(iteration = 0; iteration < num_of_iterations; ++iteration) {
call_mpi(MPI_Send, msg_buf, msg_size, MPI_CHAR, 1, tag, MPI_COMM_WORLD);
}
res = clock_gettime(CLOCK_MONOTONIC, &t_end );
if( res != 0 ) {
fprintf(stderr, "error: failed at clock_gettime %d\n", __LINE__);
return 1;
}
} else { // rank = 1
for(iteration = 0; iteration < num_of_iterations; ++iteration) {
call_mpi(MPI_Recv, msg_buf, msg_size, MPI_CHAR, 0, tag, MPI_COMM_WORLD, &recv_status);
}
}
xfer_time_usecs = (t_end.tv_sec - t_start.tv_sec)*1000000.0 + (t_end.tv_nsec - t_start.tv_nsec)/1000.0;
xfer_time_usecs = xfer_time_usecs/num_of_iterations;
if( rank == 0) {
printf("[%s]: avg xfer time: size=%d iters=[%d] avg_latency=%lf usecs\n", argv[0], msg_size, num_of_iterations, xfer_time_usecs);
}
}
free( msg_buf );
call_mpi( MPI_Finalize );
return 0;
}