-
Notifications
You must be signed in to change notification settings - Fork 0
/
example-5a.cpp
225 lines (164 loc) · 5.17 KB
/
example-5a.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
/*
please note that the series of optmiztion technology is not in official document.
All the tests are based on AMD MI25 radeon instict and AMD ROCm.
*/
#include <assert.h>
#include <stdio.h>
#include <algorithm>
#include <stdlib.h>
#include<iostream>
#include "hip/hip_runtime.h"
#define HIP_ASSERT(x) (assert((x)==hipSuccess))
#define NUM 1
#define MAX_BLOCKS 1024
#define THREADS_PER_BLOCK_X 256
#define THREADS_PER_BLOCK_Y 1
#define THREADS_PER_BLOCK_Z 1
#define FMA_PER_THREDS 1000000
__global__ void
test_kernel(hipLaunchParm lp,
float* __restrict__ a)
{
int x = hipBlockDim_x * hipBlockIdx_x + hipThreadIdx_x;
int y = hipBlockDim_y * hipBlockIdx_y + hipThreadIdx_y;
float t0 = (float)x / (float) (x + 1);
float t1 = float(y + 1) / (float)(y + 100000000);
float sum=0.0;
for(int i =0; i < FMA_PER_THREDS;i++)
{
sum = t0 *sum + t1;
}
if( (float(x)+sum) < -1.0f)
{
a[0] = sum;
}
}
__global__ void
test_kernel_divergence(hipLaunchParm lp,
float* __restrict__ a)
{
int x = hipBlockDim_x * hipBlockIdx_x + hipThreadIdx_x;
int y = hipBlockDim_y * hipBlockIdx_y + hipThreadIdx_y;
float t0 = (float)x / (float)(x + 1);
float t1 = float(y + 1) / (float)(y + 100000000);
float sum = 0.0;
if (hipThreadIdx_x == 0) {
for (int i = 0; i < FMA_PER_THREDS; i++){
sum = t0 * sum + t1;
}
}
else {
for (int i = 0; i < FMA_PER_THREDS; i++){
sum = t1 * sum + t0;
}
}
if ((float(x) + sum) < -1.0f)
{
a[0] = sum;
}
}
__global__ void
test_kernel_optimize(hipLaunchParm lp,
float* __restrict__ a)
{
int x = hipBlockDim_x * hipBlockIdx_x + hipThreadIdx_x;
int y = hipBlockDim_y * hipBlockIdx_y + hipThreadIdx_y;
float t0 = (float)x / (float)(x + 1);
float t1 = float(y + 1) / (float)(y + 100000000);
float sum = 0.0;
if (hipThreadIdx_x == 0) {
float t = t0;
t1 = t0;
t0 = t;
}
for (int i = 0; i < FMA_PER_THREDS ; i++)
{
sum = t0 * sum + t1;
}
if ((float(x) + sum) < -1.0f)
{
a[0] = sum;
}
}
using namespace std;
int main() {
float* hostA;
float* deviceA;
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 i;
int errors;
hostA = (float*)malloc(NUM * sizeof(float));
HIP_ASSERT(hipMalloc((void**)&deviceA, NUM * sizeof(float)));
hipLaunchKernel(test_kernel,
dim3(1, 1),
dim3(THREADS_PER_BLOCK_X, THREADS_PER_BLOCK_Y, THREADS_PER_BLOCK_Z),
0, 0,
deviceA);
hipLaunchKernel(test_kernel_divergence,
dim3(1, 1),
dim3(THREADS_PER_BLOCK_X, THREADS_PER_BLOCK_Y, THREADS_PER_BLOCK_Z),
0, 0,
deviceA);
hipLaunchKernel(test_kernel_optimize,
dim3(1, 1),
dim3(THREADS_PER_BLOCK_X, THREADS_PER_BLOCK_Y, THREADS_PER_BLOCK_Z),
0, 0,
deviceA);
printf("execute test kernel\n");
for (int i = 1; i < 2; i = i + 1) {
hipEventRecord(start, NULL);
hipLaunchKernel(test_kernel,
dim3(i, 1,1),
dim3(THREADS_PER_BLOCK_X, 1, 1),
0, 0,
deviceA);
hipEventRecord(stop, NULL);
hipEventSynchronize(stop);
hipEventElapsedTime(&eventMs, start, stop);
printf("kernel_time (hipEventElapsedTime) =%6.3fms\n", eventMs);
double FMA_per_cycle = double(THREADS_PER_BLOCK_X) * i * double(FMA_PER_THREDS) / eventMs / (1.536 * 1e6) + 0.5;
printf("Total Threads = %d * 256, FMA_per_cycle for Vega10 - 1.536GHz = %6d\n", i, (int)FMA_per_cycle);
}
printf("execute divergence kernel\n");
for (int i = 1; i < 2; i = i + 1) {
hipEventRecord(start, NULL);
hipLaunchKernel(test_kernel_divergence,
dim3(i, 1, 1),
dim3(THREADS_PER_BLOCK_X, 1, 1),
0, 0,
deviceA);
hipEventRecord(stop, NULL);
hipEventSynchronize(stop);
hipEventElapsedTime(&eventMs, start, stop);
printf("kernel_time (hipEventElapsedTime) =%6.3fms\n", eventMs);
double FMA_per_cycle = double(THREADS_PER_BLOCK_X) * i * double(FMA_PER_THREDS) / eventMs / (1.536 * 1e6) + 0.5;
printf("Total Threads = %d * 256, FMA_per_cycle for Vega10 - 1.536GHz = %6d\n", i, (int)FMA_per_cycle);
}
printf("execute optimized kernel\n");
for (int i = 1; i < 2; i = i + 1) {
hipEventRecord(start, NULL);
hipLaunchKernel(test_kernel_optimize,
dim3(i, 1, 1),
dim3(THREADS_PER_BLOCK_X, 1, 1),
0, 0,
deviceA);
hipEventRecord(stop, NULL);
hipEventSynchronize(stop);
hipEventElapsedTime(&eventMs, start, stop);
printf("kernel_time (hipEventElapsedTime) =%6.3fms\n", eventMs);
double FMA_per_cycle = double(THREADS_PER_BLOCK_X) * i * double(FMA_PER_THREDS) / eventMs / (1.536 * 1e6) + 0.5;
printf("Total Threads = %d * 256, FMA_per_cycle for Vega10 - 1.536GHz = %6d\n", i, (int)FMA_per_cycle);
}
HIP_ASSERT(hipFree(deviceA));
free(hostA);
return errors;
}