chundoong-lab-ta/SamsungDS22/submissions/HW5/yh1597.yang/kernel.cl

56 lines
1.5 KiB
Common Lisp

// Select a kernel
#define TSM 32
#define TSN 32
#define TSK TSN
#define WPT 32
#define RTSM (TSM/WPT)
#define RTSN (TSN/WPT)
#define RTSK (TSK/WPT)
__kernel void sgemm(__global float *A, __global float *B, __global float *C, int M, int N, int K) {
// Thread identifiers
const int row = get_local_id(0);
const int col = get_local_id(1);
const int global_row = TSM * get_group_id(0) + row;
const int global_col = TSN * get_group_id(1) + col;
__local float Asub[TSM][TSK];
__local float Bsub[TSK][TSN];
float intermediate_val[WPT];
for (int w=0; w<WPT; w++) {
intermediate_val[w] = 0.0f;
}
const int num_tiles = (K+TSK-1)/TSK;
for(int t = 0; t < num_tiles; t++) {
for (int w=0; w<WPT; w++) {
const int t_row = TSK * t + row;
const int t_col = TSK * t + col;
if((t_col>=K) || ((global_row+w*RTSM)>=M))
Asub[row + w*RTSM][col] = 0;
else
Asub[row + w*RTSM][col] = A[(global_row + w*RTSM) * K + t_col];
if(((t_row+w*RTSK)>=K) || (global_col>=N))
Bsub[row + w*RTSK][col] = 0;
else
Bsub[row + w*RTSK][col] = B[(t_row + w*RTSK) * N + global_col];
}
barrier(CLK_LOCAL_MEM_FENCE);
for(int k = 0; k < TSK; k++) {
for (int w=0; w<WPT; w++) {
intermediate_val[w] += Asub[row + w*RTSM][k] * Bsub[k][col];
}
}
barrier(CLK_LOCAL_MEM_FENCE);
}
for (int w=0; w<WPT; w++) {
if(((global_row + w*RTSM) < M) && (global_col < N))
C[(global_row + w*RTSM)*N + global_col] = intermediate_val[w];
}
}