chundoong-lab-ta/SamsungDS22/submissions/HW5/hyowon12.an/kernel.cl

58 lines
1.8 KiB
Common Lisp
Raw Normal View History

2022-09-29 18:01:45 +09:00
#define TS 64
#define WPT 32
#define RTS (TS/WPT)
// super super slow sgemm kernel by heehoon
__kernel void sgemm(__global float *A, __global float *B, __global float *C, int M, int N, int K) {
/*
// Original
int i = get_global_id(0); // row index of C
int j = get_global_id(1); // column index of C
if (i >= M || j >= N) return; // boundary check
C[i * N + j] = 0;
for (int k = 0; k < K; k++) {
C[i * N + j] += A[i * K + k] * B[k * N + j];
}
*/
const int row = get_local_id(0); // Local row ID (max: TS)
const int col = get_local_id(1); // Local col ID (max: TS/WPT == RTS)
const int globalRow = TS*get_group_id(0) + row; // Row ID of C (0..M)
const int globalCol = TS*get_group_id(1) + col; // Col ID of C (0..N)
__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-1)/TS) + 1;
for (int t=0; t<numTiles; t++) {
for (int w=0; w<WPT; w++) {
const int tiledRow = TS*t + row;
const int tiledCol = TS*t + col;
if ((globalRow + w*RTS >= M) || (tiledCol >= K)) Asub[row + w*RTS][col] = 0.0f;
else Asub[row + w*RTS][col] = A[(globalRow + w*RTS)*K + tiledCol];
if ((tiledRow + w*RTS >= K) || (globalCol >= N)) Bsub[row + w*RTS][col] = 0.0f;
else Bsub[row + w*RTS][col] = B[(tiledRow + w*RTS)*N + globalCol];
}
barrier(CLK_LOCAL_MEM_FENCE);
for (int k=0; k<TS; k++) {
for (int w=0; w<WPT; w++) {
acc[w] += Asub[row + w*RTS][k] * Bsub[k][col];
}
}
barrier(CLK_LOCAL_MEM_FENCE);
}
for (int w=0; w<WPT; w++) {
if ((globalRow + w*RTS >= M) || (globalCol >= N)) continue;
C[(globalRow + w*RTS)*N + globalCol] = acc[w];
}
}