-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShuffle-on-Vega.cpp
275 lines (200 loc) · 6.42 KB
/
Shuffle-on-Vega.cpp
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#include <assert.h>
#include <stdio.h>
#include <algorithm>
#include <stdlib.h>
#include <iostream>
#include "hip/hip_runtime.h"
#include <math.h>
#define HIP_ASSERT(x) (assert((x)==hipSuccess))
template<int KK, int CC, int HH, int WW>
__global__ void convert_nhwc_to_nchw(hipLaunchParm lp, float* source, float* dest)
{
int global_idx = hipBlockIdx_x * hipBlockDim_x + hipThreadIdx_x;
int k, c, h, w;
w = global_idx % WW;
int temp = global_idx / WW;
h = temp % HH;
temp = temp /HH;
c = temp % CC;
temp = temp / CC;
k = temp;
//NHWC
int soruce_idx = k * HH * WW *CC + h * WW * CC + w *CC + c;
dest[global_idx] = source[soruce_idx];
}
template<int KK, int CC, int HH, int WW>
__global__ void convert_nhwc_to_nchw_optimized(hipLaunchParm lp, float* source, float* dest)
{
//Padding 1
const int stride = 257;
__shared__ float shared_data[stride * 9];
//global_idx = KK * CC;
int global_idx = hipBlockIdx_x * hipBlockDim_x + hipThreadIdx_x;
int k, c;
c = global_idx % CC;
k = global_idx / CC;
int source_base = k * HH * WW * CC + c;
for(int i=0; i < HH; i++)
for (int j = 0; j < WW; j++)
{
int h = i;
int w = j;
int soruce_idx = source_base + h * WW * CC + w * CC ;
int shared_idx = (c&0xff) *HH *WW + h * WW + w;
shared_data[shared_idx] = source[soruce_idx];
}
__syncthreads();
//Scatter 256xH*W to different thread
int tile_c_offset = c & (~0xff);
int dest_base = k * CC * HH * WW + tile_c_offset * HH * WW + hipThreadIdx_x;
int shared_Idx2 = hipThreadIdx_x;
for (int i = 0; i < HH; i++)
for (int j = 0; j < WW; j++)
{
dest[dest_base] = shared_data[shared_Idx2];
shared_Idx2 += 256;
dest_base += 256;
}
}
using namespace std;
#define KC_VAL 4096
void test(unsigned int k= KC_VAL, int c= KC_VAL, int h=3, int w=3)
{
int inlength = k * c * h * w;
unsigned int A_NUM = inlength;
unsigned int B_NUM = inlength;
unsigned int C_NUM = inlength;
float* hostA;
float* hostB;
float* hostC;
hipDeviceProp_t devProp;
hipGetDeviceProperties(&devProp, 0);
cout << " System minor " << devProp.minor << endl;
cout << " System major " << devProp.major << endl;
cout << " agent prop name " << devProp.name << endl;
cout << "hip Device prop succeeded " << endl;
hipEvent_t start, stop;
hipEventCreate(&start);
hipEventCreate(&stop);
float eventMs = 1.0f;
int errors;
//hostA = (float*)malloc(A_NUM * sizeof(float));
hipHostMalloc((void**)& hostA, A_NUM * sizeof(float));
hostB = (float*)malloc(B_NUM * sizeof(float));
hostC = (float*)malloc(C_NUM * sizeof(float));
for (unsigned int i = 0; i < inlength; i = i + 1) {
hostA[i] = i;
hostB[i] = i;
hostC[i] = i;
}
float* deviceA;
float* deviceB;
float* deviceC;
cout << "host allocated \n";
HIP_ASSERT(hipMalloc((void**)& deviceA, A_NUM * sizeof(float)));
HIP_ASSERT(hipMalloc((void**)& deviceB, B_NUM * sizeof(float)));
HIP_ASSERT(hipMalloc((void**)& deviceC, C_NUM * sizeof(float)));
cout << "device allocated \n";
HIP_ASSERT(hipMemcpy(deviceA, hostA, A_NUM * sizeof(float), hipMemcpyHostToDevice));
HIP_ASSERT(hipMemcpy(deviceB, hostB, B_NUM * sizeof(float), hipMemcpyHostToDevice));
HIP_ASSERT(hipMemcpy(deviceC, hostC, C_NUM * sizeof(float), hipMemcpyHostToDevice));
cout << "Host to Device Copied\n";
{
int localThreads = 256;
int gloal_blocks = (inlength + localThreads - 1) / localThreads;
hipLaunchKernel(convert_nhwc_to_nchw<KC_VAL, KC_VAL, 3, 3>,
dim3(gloal_blocks),
dim3(localThreads),
0, 0,
hostA, deviceB);
hipEventRecord(start, NULL);
int iterations = 10;
for (int i = 0; i < iterations; i++)
{
hipLaunchKernel(convert_nhwc_to_nchw<KC_VAL, KC_VAL, 3, 3>,
dim3(gloal_blocks),
dim3(localThreads),
0, 0,
hostA, deviceB);
}
hipEventRecord(stop, NULL);
hipEventSynchronize(stop);
hipEventElapsedTime(&eventMs, start, stop);
float bandwidth = float(inlength) * 1 / 1e9;
bandwidth = bandwidth / (eventMs / iterations / 1000.0);
printf("Shuffle_simple : host A, Inlen:[%d], costs %f millseconds , Bandwidth = %f Giga Data/s\n", inlength, eventMs / iterations, bandwidth);
}
{
int localThreads = 256;
int gloal_blocks = (inlength + localThreads -1) / localThreads;
hipLaunchKernel(convert_nhwc_to_nchw<KC_VAL, KC_VAL,3,3>,
dim3(gloal_blocks),
dim3(localThreads),
0, 0,
deviceA, deviceB);
hipEventRecord(start, NULL);
int iterations = 10;
for (int i = 0; i < iterations; i++)
{
hipLaunchKernel(convert_nhwc_to_nchw<KC_VAL, KC_VAL, 3, 3>,
dim3(gloal_blocks),
dim3(localThreads),
0, 0,
deviceA, deviceB);
}
hipEventRecord(stop, NULL);
hipEventSynchronize(stop);
hipEventElapsedTime(&eventMs, start, stop);
float bandwidth = float(inlength) * 1 / 1e9 ;
bandwidth = bandwidth / (eventMs / iterations / 1000.0);
printf("Shuffle_simple, Inlen:[%d], costs %f millseconds , Bandwidth = %f Giga Data/s\n", inlength, eventMs / iterations, bandwidth);
}
{
int localThreads = 256;
//9*256 once
int gloal_blocks = (inlength/9 + localThreads - 1) / localThreads;
hipLaunchKernel(convert_nhwc_to_nchw_optimized<KC_VAL, KC_VAL, 3, 3>,
dim3(gloal_blocks),
dim3(localThreads),
0, 0,
deviceA, deviceB);
hipEventRecord(start, NULL);
int iterations = 10;
for (int i = 0; i < iterations; i++)
{
hipLaunchKernel(convert_nhwc_to_nchw_optimized<KC_VAL, KC_VAL, 3, 3>,
dim3(gloal_blocks),
dim3(localThreads),
0, 0,
deviceA, deviceB);
}
hipEventRecord(stop, NULL);
hipEventSynchronize(stop);
hipEventElapsedTime(&eventMs, start, stop);
float bandwidth = float(inlength) * 1 / 1e9;
bandwidth = bandwidth / (eventMs / iterations / 1000.0);
printf("Shuffle_optimized, Inlen:[%d], costs %f millseconds , Bandwidth = %f Giga Data/s\n", inlength, eventMs / iterations, bandwidth);
}
//CPU Verifying here
HIP_ASSERT(hipFree(deviceA));
HIP_ASSERT(hipFree(deviceB));
HIP_ASSERT(hipFree(deviceC));
//free(hostA);
hipFree(hostA);
free(hostB);
free(hostC);
//return errors;
}
int main(int argc, char** argv)
{
if (argc != 2)
{
//printf(" please input length\n\n");
return 0;
}
unsigned int packedLength = 0;
//packedLength = atoi(argv[1]);
//packedLength = 256 * 256 * 255;
test();
return 0;
}