-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmybcast.c
188 lines (165 loc) · 5.77 KB
/
mybcast.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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include "mpi.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>
#include <cuda_runtime.h>
#include <dlfcn.h>
int main(int argc, char** argv) {
// Initialize the MPI environment
MPI_Init(NULL, NULL);
// Get the number of processes
int world_size;
MPI_Comm_size(MPI_COMM_WORLD, &world_size);
// Get the rank of the process
int my_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
// Get the name of the processor
char processor_name[MPI_MAX_PROCESSOR_NAME];
int name_len;
MPI_Get_processor_name(processor_name, &name_len);
opterr = 0;
int c;
int j, k;
size_t length;
char *buffer_cuda = NULL;
char *buffer_host = NULL;
char *buffer_tmp = NULL;
int root = 0;
MPI_Datatype root_type;
int tag = 0;
size_t root_size;
double t1, t2;
cudaError_t err;
MPI_Status status;
int ierr;
int nb_segs;
int seg_size = 2000000;
MPI_Request req[1000];
MPI_Status sta_array[1000];
while ((c = getopt (argc, argv, "l:s:")) != -1) {
switch (c) {
case 'l':
length = atoi(optarg);
break;
case 's':
seg_size = atoi(optarg);
break;
case '?':
if (optopt == 'l')
fprintf (stderr, "Option -%c requires an argument.\n", optopt);
else if (optopt == 's')
fprintf (stderr, "Option -%c requires an argument.\n", optopt);
else if (isprint (optopt))
fprintf (stderr, "Unknown option `-%c'.\n", optopt);
else
fprintf (stderr,"Unknown option character `\\x%x'.\n", optopt);
exit(1);
default:
exit(1);
}
}
// Print off a hello world message
printf("Hello world from processor %s, rank %d"
" out of %d processors, size %ld, seg_size %ld\n",
processor_name, my_rank, world_size, length, seg_size);
/*
for (k = 0; k < world_size; k++) {
cudaSetDevice(k);
for (j = 0; j < world_size; j++) {
if (j != k) {
err = cudaDeviceEnablePeerAccess(j, 0);
if (err != cudaSuccess && err != cudaErrorPeerAccessAlreadyEnabled) {
printf("peer access error\n");
exit(0);
}
}
}
} */
cudaSetDevice(my_rank);
cudaMalloc((void **)&buffer_cuda, sizeof(char)*length);
cudaMallocHost((void **)&buffer_host, sizeof(char)*length);
buffer_tmp = buffer_cuda;
nb_segs = length / seg_size;
MPI_Bcast(buffer_cuda, length, MPI_CHAR, root, MPI_COMM_WORLD);
cudaMemset(buffer_cuda, 0, sizeof(char)*length);
cudaDeviceSynchronize();
if (my_rank == 0) {
for (j = 0; j < length; j++) {
buffer_host[j] = 'a';
}
cudaMemcpy(buffer_cuda, buffer_host, sizeof(char)*length, cudaMemcpyHostToDevice);
}
MPI_Barrier(MPI_COMM_WORLD);
if (my_rank == 0) {
t1 = MPI_Wtime();
}
printf("nb_segs %d\n", nb_segs);
if (my_rank == 0) {
for (j = 0; j < nb_segs; j++) {
tag = j;
printf("send_buffer %p, size %d\n", buffer_tmp, seg_size);
ierr = MPI_Isend(buffer_tmp, seg_size, MPI_CHAR, my_rank+1, tag, MPI_COMM_WORLD, &req[j]);
if (ierr != MPI_SUCCESS) {
printf("MPI_Send() returned %d", ierr);
}
buffer_tmp += seg_size;
}
MPI_Waitall(nb_segs, req, sta_array);
} else if (my_rank == (world_size - 1)){
for (j = 0; j < nb_segs; j++) {
tag = j;
ierr = MPI_Irecv(buffer_tmp, seg_size, MPI_CHAR, my_rank-1, tag, MPI_COMM_WORLD, &req[j]);
if (ierr != MPI_SUCCESS) {
printf("MPI_Send() returned %d", ierr);
}
buffer_tmp += seg_size;
}
MPI_Waitall(nb_segs, req, sta_array);
} else {
tag = 0;
ierr = MPI_Recv(buffer_tmp, seg_size, MPI_CHAR, my_rank-1, tag, MPI_COMM_WORLD, &status);
if (ierr != MPI_SUCCESS) {
printf("MPI_Send() returned %d", ierr);
}
tag = 0;
ierr = MPI_Isend(buffer_tmp, seg_size, MPI_CHAR, my_rank+1, tag, MPI_COMM_WORLD, &req[0]);
if (ierr != MPI_SUCCESS) {
printf("MPI_Send() returned %d", ierr);
}
buffer_tmp += seg_size;
for (j = 1; j < nb_segs; j++) {
tag = j;
ierr = MPI_Irecv(buffer_tmp, seg_size, MPI_CHAR, my_rank-1, tag, MPI_COMM_WORLD, &req[1]);
if (ierr != MPI_SUCCESS) {
printf("MPI_Send() returned %d", ierr);
}
MPI_Waitall(2, req, sta_array);
ierr = MPI_Isend(buffer_tmp, seg_size, MPI_CHAR, my_rank+1, tag, MPI_COMM_WORLD, &req[0]);
if (ierr != MPI_SUCCESS) {
printf("MPI_Send() returned %d", ierr);
}
buffer_tmp += seg_size;
}
}
MPI_Barrier(MPI_COMM_WORLD);
if (my_rank == 0) {
t2 = MPI_Wtime();
printf("rank %d send&recv time %fs, BW %f GB/s\n", my_rank, (t2-t1), length*sizeof(char)/1.0E9/(t2-t1));
}
if (my_rank != 0) {
cudaMemcpy(buffer_host, buffer_cuda, sizeof(char)*length, cudaMemcpyDeviceToHost);
for (j = 0; j < length; j++) {
if (buffer_host[j] != 'a') {
printf("error find , val %c\n", buffer_host[j]);
goto cleanup;
}
}
printf("no error is found\n");
}
cleanup:
if (buffer_cuda != NULL) cudaFree(buffer_cuda);
if (buffer_host != NULL) cudaFreeHost(buffer_host);
MPI_Finalize();
}