-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathkernel_null.cpp
158 lines (116 loc) · 3.69 KB
/
kernel_null.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
//hipcc --amdgpu-target=gfx900 kernel_null.cpp -o kernel_null
#include <assert.h>
#include <stdio.h>
#include <algorithm>
#include <stdlib.h>
#include <unistd.h>
#include<iostream>
#include "hip/hip_runtime.h"
#define HIP_ASSERT(x) (assert((x)==hipSuccess))
#define M 8192
#define N 8192
#define K (8192)
#define NUM (M*K)
#define THREADS_PER_BLOCK_X 16
#define THREADS_PER_BLOCK_Y 16
#define THREADS_PER_BLOCK_Z 1
typedef float Float4 __attribute__((ext_vector_type(4)));
/*
inline __device__ void Matrix8x1X8(float* a, float* b, float* c){
for(int i = 0; i < 8; i++)
for(int j = 0; j < 8; j++)
{
asm volatile("\n \
v_fma_f32 %0, %1, %2, %0 \n \
"
:
:"v"(c[i*8+j]), "v"(a[i]), "v"(b[j])
);
}
}
*/
#define UNROLL_SIZE 8
__global__ void sgemm_null(const float* a, const float* b, float* __restrict__ c, const int m, const int n, const int k, const int lda, const int ldb, const double alpha, double beta ){
int wk_tile_m = hipBlockIdx_y * 128 ;
int wk_tile_n = hipBlockIdx_x * 128 ;
int local_id = hipThreadIdx_y * 16 + hipThreadIdx_x;
__shared__ float a_shared[128*UNROLL_SIZE];
__shared__ float b_shared[128*UNROLL_SIZE];
float sum[8*8];
a_shared[local_id] = (local_id >> 6) * 1.0f;
__syncthreads();
for(int i=0; i <=64; i++){
sum[i] = a_shared[i];
}
asm volatile("\n \
v_fma_f32 %0, %1, %2, %0 \n \
"
:
:"v"(wk_tile_m), "v"(wk_tile_n), "v"(local_id)
);
#pragma unroll
for(int i=0; i <=64; i++){
asm volatile("\n \
v_fma_f32 %0, %1, %2, %0 \n \
"
:
:"v"(c[i]), "v"(c[i]), "v"(wk_tile_n)
);
}
asm volatile ("\nds_read_b32 v2, v1\n");
asm volatile ("\nv_mov_b32 v199, 0\n");
asm volatile ("\ns_mov_b32 s17, 0\n");
}
using namespace std;
int main() {
float* hostA;
float* hostB;
float* hostC;
float* deviceA;
float* deviceB;
float* deviceC;
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 ;
int i;
int errors;
hostA = (float*)malloc(NUM * sizeof(float));
hostB = (float*)malloc(NUM * sizeof(float));
hostC = (float*)malloc(NUM * sizeof(float));
// initialize the input data
for (i = 0; i < NUM; i++) {
hostA[i] = (float)sin(i);
hostB[i] = (float)cos(i);
}
HIP_ASSERT(hipMalloc((void**)&deviceA, NUM * sizeof(float)));
HIP_ASSERT(hipMalloc((void**)&deviceB, NUM * sizeof(float)));
HIP_ASSERT(hipMalloc((void**)&deviceC, NUM * sizeof(float)));
HIP_ASSERT(hipMemcpy(deviceA, hostA, NUM*sizeof(float), hipMemcpyHostToDevice));
HIP_ASSERT(hipMemcpy(deviceB, hostB, NUM*sizeof(float), hipMemcpyHostToDevice));
hipEvent_t start, stop;
hipEventCreate(&start);
hipEventCreate(&stop);
float eventMs = 1.0f;
hipEventRecord(start, NULL);
hipLaunchKernelGGL(sgemm_null,
dim3(M/128, N/128 ),
dim3(THREADS_PER_BLOCK_X, THREADS_PER_BLOCK_Y),
0, 0,
deviceA ,deviceB ,deviceC, M, N, K, 0,0,0.0,0.0);
hipEventRecord(stop, NULL);
hipEventSynchronize(stop);
hipEventElapsedTime(&eventMs, start, stop);
HIP_ASSERT(hipMemcpy(hostC, deviceC, NUM*sizeof(float), hipMemcpyDeviceToHost));
// verify the results
HIP_ASSERT(hipFree(deviceA));
HIP_ASSERT(hipFree(deviceB));
HIP_ASSERT(hipFree(deviceC));
free(hostA);
free(hostB);
free(hostC);
//hipResetDefaultAccelerator();
return errors;
}