-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cu
167 lines (139 loc) · 6.67 KB
/
main.cu
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <iostream>
#include <mpi.h>
#include <mpi-ext.h>
#include "cuda_runtime.h"
#define MPI_CHECK_RETURN(error_code) { \
if (error_code != MPI_SUCCESS) { \
char error_string[BUFSIZ]; \
int length_of_error_string; \
int world_rank; \
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank); \
MPI_Error_string(error_code, error_string, &length_of_error_string); \
fprintf(stderr, "%3d: %s\n", world_rank, error_string); \
exit(1); \
}}
#define CUDA_CHECK_RETURN(value) { \
cudaError_t _m_cudaStat = value; \
if (_m_cudaStat != cudaSuccess) { \
int world_rank; \
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank); \
char * name = (char*) malloc (MPI_MAX_PROCESSOR_NAME * sizeof(char)); \
int name_len; \
MPI_Get_processor_name(name, &name_len); \
fprintf(stderr, "%3d %s: CUDA Error %s at line %d in file %s\n", \
world_rank,name,cudaGetErrorString(_m_cudaStat), __LINE__, __FILE__); \
printf("%3d %s: CUDA Error %s at line %d in file %s\n", \
world_rank,name,cudaGetErrorString(_m_cudaStat), __LINE__, __FILE__); \
if(value == 2) exit(2); \
exit(1); \
} }
int main() {
int test = 0;
int N = 100;
printf("Compile time check:\n");
#if defined(MPIX_CUDA_AWARE_SUPPORT) && MPIX_CUDA_AWARE_SUPPORT
printf("This MPI library has CUDA-aware support.\n");
#elif defined(MPIX_CUDA_AWARE_SUPPORT) && !MPIX_CUDA_AWARE_SUPPORT
printf("This MPI library does not have CUDA-aware support.\n");
#else
printf("This MPI library cannot determine if there is CUDA-aware support.\n");
#endif /* MPIX_CUDA_AWARE_SUPPORT */
printf("Run time check:\n");
#if defined(MPIX_CUDA_AWARE_SUPPORT)
if (1 == MPIX_Query_cuda_support()) {
printf("This MPI library has CUDA-aware support.\n");
} else {
printf("This MPI library does not have CUDA-aware support.\n");
}
#else /* !defined(MPIX_CUDA_AWARE_SUPPORT) */
printf("This MPI library cannot determine if there is CUDA-aware support.\n");
#endif /* MPIX_CUDA_AWARE_SUPPORT */
MPI_CHECK_RETURN(MPI_Init_thread(NULL, NULL,MPI_THREAD_FUNNELED, &test));
if(test != MPI_THREAD_FUNNELED){
std::cout << "Somethings is wrong with the mpi init " << test << " != MPI_THREAD_FUNNELED (1)\n";
}
int world_size;
int world_rank;
MPI_CHECK_RETURN(MPI_Comm_size(MPI_COMM_WORLD, &world_size));
MPI_CHECK_RETURN(MPI_Comm_rank(MPI_COMM_WORLD, &world_rank));
int *a,*check;
int *d_a;
// Allocate host memory
a = (int*)malloc(sizeof(int) * N);
check = (int*)malloc(sizeof(int) * N);
int offset = N/world_size*world_rank;
int sizedit = N/world_size;
// Initialize host arrays
for(int i = 0; i < N ; i++) {
a[i] = 0;
check[i] = i;
}
for(int i = offset; i < N/world_size + offset; i++){
a[i] = i;
}
// Allocate device memory
CUDA_CHECK_RETURN(cudaMalloc((void**)&d_a, sizeof(float) * N));
// Transfer data from host to device memory
CUDA_CHECK_RETURN(cudaMemcpy(d_a, a, sizeof(float) * N, cudaMemcpyHostToDevice));
CUDA_CHECK_RETURN(cudaDeviceSynchronize());
int number;
std::cout << world_rank << " :cpu send receive \n";
if (world_rank == 0) {
MPI_Send(&a[5], 1, MPI_INT, 1, 0, MPI_COMM_WORLD);
} else if (world_rank == 1) {
MPI_Recv(&number, 1, MPI_INT, 0, 0, MPI_COMM_WORLD,
MPI_STATUS_IGNORE);
printf("Process 1 received number %d from process 0\n",
number);
}
MPI_CHECK_RETURN(MPI_Barrier(MPI_COMM_WORLD));
MPI_CHECK_RETURN(MPI_Barrier(MPI_COMM_WORLD));
std::cout << world_rank << " :check the cpu allgather " << a << "\n";
MPI_CHECK_RETURN(MPI_Allgather(
&a[offset], //sendbuffer
sizedit, //sendcount
MPI_INT, //type
a, //receivebuffer
sizedit, //recvcount (from any process)
MPI_INT, //type
MPI_COMM_WORLD)); //handle
for(int i = 0; i < N ; i++) {
if(a[i] != check[i]){
printf("ERROR");
exit(0);
}
}
MPI_CHECK_RETURN(MPI_Barrier(MPI_COMM_WORLD));
std::cout << "\n";
MPI_CHECK_RETURN(MPI_Barrier(MPI_COMM_WORLD));
std::cout << world_rank << " :GPU send receive \n";
if (world_rank == 0) {
MPI_Send(&d_a[5], 1, MPI_INT, 1, 0, MPI_COMM_WORLD);
} else if (world_rank == 1) {
MPI_Recv(&number, 1, MPI_INT, 0, 0, MPI_COMM_WORLD,
MPI_STATUS_IGNORE);
printf("Process 1 received number %d from process 0\n",
number);
}
MPI_CHECK_RETURN(MPI_Barrier(MPI_COMM_WORLD));
std::cout << world_rank << " : check the GPU allgather " << d_a << "\t" << "\n";
MPI_CHECK_RETURN(MPI_Allgather(
&d_a[offset], //sendbuffer
sizedit, //sendcount
MPI_INT, //type
d_a, //receivebuffer
sizedit, //recvcount (from any process)
MPI_INT, //type
MPI_COMM_WORLD)); //handle
CUDA_CHECK_RETURN(cudaMemcpy(a, d_a, sizeof(float) * N, cudaMemcpyDeviceToHost));
CUDA_CHECK_RETURN(cudaDeviceSynchronize());
for(int i = 0; i < N ; i++) {
if(a[i] != check[i]){
printf("ERROR");
exit(0);
}
}
MPI_Finalize();
exit(0);
return true;
}