-
Notifications
You must be signed in to change notification settings - Fork 1
/
mpi_avg_pooling.cu
188 lines (152 loc) · 5.72 KB
/
mpi_avg_pooling.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<time.h>
#include<cuda.h>
#include<mpi.h>
using namespace std;
MPI_Status status;
__global__ void avg_pooling(float* gpu_input, float* gpu_output_data, int input_h_size, int input_w_size, int pool_h_size, int pool_w_size, int pool_h_stride, int pool_w_stride, int width, int before_height, int height)
{
int x = blockIdx.x;
int y = blockIdx.y;
if((x < width) && (before_height <= y) && (y < height))
{
int sum;
float avg;
int pooled_size = ((input_w_size - pool_w_size) / pool_w_stride) + 1;
int h_start = y * pool_h_stride;
int w_start = x * pool_w_stride;
int h_end = min(h_start + pool_h_size, input_h_size);
int w_end = min(w_start + pool_w_size, input_w_size);
h_start = max(h_start, 0);
w_start = max(w_start, 0);
sum = 0;
avg = 0;
int pool_index = (y * pooled_size) + x;
for (int h = h_start; h < h_end; h++)
{
for (int w = w_start; w < w_end; w++)
{
int index = (h * input_w_size) + w;
sum += gpu_input[index];
}
avg = (float)sum / (pool_h_size * pool_w_size);
gpu_output_data[pool_index] = avg;
}
}
}
void Init_input(float* input, int input_h_size, int input_w_size, int num)
{
srand(time(NULL));
for (int h = 0; h < input_h_size; h++)
{
for (int w = 0; w < input_w_size; w++)
{
input[(h * input_w_size) + w] = rand() % num;
}
}
}
void print(float* data, int h_size, int w_size)
{
for (int h = 0; h < h_size; h++)
{
for (int w = 0; w < w_size; w++)
{
printf("%.2f ", data[(h * w_size) + w]);
}
printf("\n");
}
printf("\n");
}
int main(int argc, char** argv)
{
int procs, myrank;
int offset;
int before_offset = 0;
int input_h_size = 8192;
int input_w_size = 8192;
int pool_w_size = 16;
int pool_h_size = 16;
int pool_w_stride = 1;
int pool_h_stride = 1;
int pooled_h = ((input_h_size - pool_h_size) / pool_h_stride) + 1;
int pooled_w = ((input_w_size - pool_w_size) / pool_w_stride) + 1;
float* input = (float*)malloc(sizeof(float) * input_h_size * input_w_size);
float* result = (float*)malloc(sizeof(float) * pooled_h * pooled_w);
float* slave_input = (float*)malloc(sizeof(float) * input_h_size * input_w_size);
float* slave_result = (float*)malloc(sizeof(float) * pooled_h * pooled_w);
float* gpu_output_data;
float* gpu_input;
clock_t start, end;
float result_time = 0;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
MPI_Comm_size(MPI_COMM_WORLD, &procs);
Init_input(input, input_h_size, input_w_size, 10);
// if(myrank == 0)
// {
// print(input, input_h_size, input_w_size);
// }
cudaMalloc((void**)&gpu_input, sizeof(float) * input_h_size * input_w_size);
cudaMalloc((void**)&gpu_output_data, sizeof(float) * pooled_h * pooled_w);
if(myrank == 0)
{
int width = pooled_w;
int height = (pooled_h / procs) * (myrank + 1);
int before_height = (pooled_h / procs) * myrank;
for(int i = 1; i < procs; i++)
{
MPI_Send(input, input_h_size * input_w_size, MPI_FLOAT, i, 0, MPI_COMM_WORLD);
}
cudaMemcpy(gpu_input, input, sizeof(float) * input_h_size * input_w_size, cudaMemcpyHostToDevice);
dim3 dimGrid(pooled_h, pooled_w);
start = clock();
avg_pooling<<<dimGrid,1>>>(gpu_input, gpu_output_data, input_h_size, input_w_size, pool_h_size, pool_w_size, pool_h_stride, pool_w_stride, width, before_height, height);
cudaDeviceSynchronize();
cudaMemcpy(result, gpu_output_data, sizeof(float) * pooled_h * pooled_w, cudaMemcpyDeviceToHost);
offset = (pooled_h / procs);
for(int i = 1; i < procs; i++)
{
MPI_Recv(slave_result, pooled_h * pooled_w, MPI_FLOAT, i, 1, MPI_COMM_WORLD, &status);
before_offset = offset;
offset += (pooled_h / procs);
for(int h = before_offset; h < offset; h++)
{
for(int w = 0; w < pooled_h; w++)
{
result[(h * pooled_h + w)] = result[(h * pooled_h + w)] + slave_result[(h * pooled_h + w)];
}
}
}
end = clock();
}
else if(myrank > 0)
{
int width = pooled_w;
int height = (pooled_h / procs) * (myrank + 1);
int before_height = (pooled_h / procs) * myrank;
MPI_Recv(slave_input, input_h_size * input_w_size, MPI_FLOAT, 0, 0, MPI_COMM_WORLD, &status);
cudaMemcpy(gpu_input, slave_input, sizeof(float) * input_h_size * input_w_size, cudaMemcpyHostToDevice);
dim3 dimGrid(pooled_h, pooled_w);
avg_pooling<<<dimGrid,1>>>(gpu_input, gpu_output_data, input_h_size, input_w_size, pool_h_size, pool_w_size, pool_h_stride, pool_w_stride, width, before_height, height);
cudaDeviceSynchronize();
cudaMemcpy(slave_result, gpu_output_data, sizeof(float) * pooled_h * pooled_w, cudaMemcpyDeviceToHost);
MPI_Send(slave_result, pooled_h * pooled_w, MPI_FLOAT, 0, 1, MPI_COMM_WORLD);
}
result_time = (float)(end-start)/CLOCKS_PER_SEC;
if(myrank == 0)
{
//print(result, pooled_h, pooled_w);
printf("rank = %d time %.4f\n", myrank,result_time);
}
free(input);
free(result);
free(slave_input);
free(slave_result);
cudaFree(gpu_output_data);
cudaFree(gpu_input);
MPI_Finalize();
return 0;
}