chundoong-lab-ta/SamsungDS22/submissions/HW5/h0.kim/kernel.cl

47 lines
1.3 KiB
Common Lisp

#define TS 32
#define WPT 16
#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 = get_group_id(0) * TS + row; // row index of C
int global_col = get_group_id(1) * TS + col; // column index of C
__local float A_SUB[TS][TS];
__local float B_SUB[TS][TS];
float iv[WPT];
for (int w = 0; w < WPT; w++) {
iv[w] = 0.0f;
}
int _K = (K + TS - 1) / TS * TS;
for (int t = 0; t < _K / TS; t++) {
for (int w = 0; w < WPT; w++) {
const int t_row = TS * t + row;
const int t_col = TS * t + col;
const int indexA = (global_row + w*RTS) * K + t_col;
const int indexB = (t_row + w*RTS) * N + global_col;
A_SUB[row + w*RTS][col] = (global_row + w*RTS < M && t_col < K) ? A[indexA] : 0.0f;
B_SUB[row + w*RTS][col] = (t_row + w*RTS < K && global_col < N) ? B[indexB] : 0.0f;
}
barrier(CLK_LOCAL_MEM_FENCE);
for (int k = 0; k < TS; k++) {
for (int w = 0; w < WPT; w++) {
iv[w] += A_SUB[row + w * RTS][k] * B_SUB[k][col];
}
}
barrier(CLK_LOCAL_MEM_FENCE);
}
if (global_row >= M || global_col >= N) return; // boundary check
for (int w = 0; w < WPT; w++) {
C[(global_row + w * RTS)* N + global_col] = iv[w];
}
}