chundoong-lab-ta/SamsungDS22/submissions/HW5/inseok.bae/kernel.cl

64 lines
1.4 KiB
Common Lisp

// super super slow sgemm kernel by heehoon
#define TS 32
#define WPT 8
#define RTS TS/WPT
__kernel void sgemm(__global float *A, __global float *B, __global float *C, int M, int N, int K) {
int row = get_local_id(0);
int col = get_local_id(1);
int global_row = TS*get_group_id(0) + row ; // row index of C
int global_col = TS*get_group_id(1) + col ; // column index of C
__local float Asub[TS*TS];
__local float Bsub[TS*TS];
float acc[WPT];
for (int w=0; w<WPT; w++) {
acc[w] = 0.0f;
}
const int numTiles = (K+TS-1)/TS;
for (int t=0; t<numTiles; t++) {
for (int w=0; w < WPT; w++) {
const int tiled_row = TS*t + row;
const int tiled_col = TS*t + col;
if(global_row + w*RTS >=M || tiled_col >=K) {
Asub[(row + w*RTS)*TS + col] = 0.0f;
}
else {
Asub[(row + w*RTS)*TS + col] = A[(global_row + w*RTS)*K + tiled_col];
}
if(tiled_row + w*RTS >=K || global_col >= N){
Bsub[(row + w*RTS)*TS + col] = 0.0f;
}
else {
Bsub[(row + w*RTS)*TS + col] = B[(tiled_row + w*RTS)*N + global_col];
}
}
barrier(CLK_LOCAL_MEM_FENCE);
for (int k = 0; k < TS; k++) {
//acc += Asub[i * K + k] * B[k * N + j];
for (int w = 0; w< WPT; w++) {
acc[w] += Asub[(row + w*RTS)* TS + k] * Bsub[k * TS + col];
}
}
barrier(CLK_LOCAL_MEM_FENCE);
}
for (int w=0; w<WPT; w++){
if((global_row+w*RTS < M) && (global_col < N)) {
C[(global_row+w*RTS)*N + global_col] = acc[w];
}
}
}