-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdata_test.cpp
More file actions
117 lines (92 loc) · 2.54 KB
/
data_test.cpp
File metadata and controls
117 lines (92 loc) · 2.54 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <cstdio>
#include <cstdlib>
#include <string>
#include "mpi.h"
#include "cuda_runtime.h"
#include "helper.h"
#define DEVICE_BUFFER 1
// some analytical function to assign values to the memory buffer
double f(int i, int rank){
return rank*1000 + i%1000;
}
void datatest(int size0, int size1){
int myid, numprocs, i, j;
int size;
int namelen;
MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
MPI_Comm_rank(MPI_COMM_WORLD, &myid);
int *s_buf;
int *r_buf;
if(numprocs < 2){
printf("use more than 2 ranks!\n");
return;
}
int *cpu_data = new int[size1];
// initialize data on the CPU
if(myid == 0){
for(int i=0; i<size1; i++){
cpu_data[i] = f(i,myid);
}
}
// allocat/copy the data to the GPU
#ifdef DEVICE_BUFFER
HANDLE_ERROR(cudaMalloc(&s_buf, size1*sizeof(int)));
HANDLE_ERROR(cudaMalloc(&r_buf, size1*sizeof(int)));
HANDLE_ERROR(cudaMemcpy(s_buf, cpu_data, size1*sizeof(int), cudaMemcpyHostToDevice));
#else
s_buf = new int[size1];
r_buf = new int[size1];
for(int i=0; i<size1; i++){
s_buf[i] = cpu_data[i];
}
#endif
MPI_Request request;
MPI_Status reqstat;
if(myid == 0){
printf("\n ----- Checking data integrity from rank 0 to all other ranks ------- \n\n");
}
// loop over the proc that will get data sent to
for(int p=1; p<numprocs; p++){
// loop over the packet size
for(size = size0; size<=size1; size*=4){
if(myid == 0){ // rank 0 always sends
MPI_Send(s_buf, size, MPI_INT, p, 100, MPI_COMM_WORLD);
} else if(myid == p){ // rank p receives
MPI_Recv(r_buf, size, MPI_INT, 0, 100, MPI_COMM_WORLD, &reqstat);
}
if(myid == p){
// copy data back to cpu memory
#ifdef DEVICE_BUFFER
HANDLE_ERROR(cudaMemcpy(cpu_data, r_buf, size*sizeof(int), cudaMemcpyDeviceToHost));
#else
for(int i=0; i<size; i++){
cpu_data[i] = r_buf[i];
}
#endif
// check the data is correct
bool error=false;
for(int i=0; i<size; i++){
if(cpu_data[i] != f(i,0)){
error = true;
}
}
if(error){
printf("ERROR with N=%10d, rank=%3d\n", size, myid);
} else {
printf("SUCCESS with N=%10d, rank=%3d\n", size, myid);
}
}
}
}
if(myid == numprocs-1){
printf("------------------------------------------\n");
}
#ifdef DEVICE_BUFFER
HANDLE_ERROR(cudaFree(s_buf));
HANDLE_ERROR(cudaFree(r_buf));
#else
delete[] s_buf;
delete[] r_buf;
#endif
delete[] cpu_data;
}