chundoong-lab-ta/SamsungDS22/submissions/HW5/dk.han/kernel.cl

86 lines
2.2 KiB
Common Lisp

// version 3
#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) {
const int row = get_local_id(0); // row index of C
const int col = get_local_id(1); // row index of C
const int global_row = TS*get_group_id(0)+row;
const int global_col = TS*get_group_id(1)+col;
//printf("row, col grow, gcol %d, %d, %d, %d \n", row,col, global_row, global_col);
__local float Asub[TS][TS];
__local float Bsub[TS][TS];
//if (globalRow >= M || globalCol >= N) return;
/*
int line_no = 0;
if (line_no < 100){
printf("# %d ######### row = %d, col = %d, get_locl_id : %d, %d, get_global_id : %d, %d\n", line_no,row, col, get_local_id(0), get_local_id(1), get_group_id(0), get_group_id(1));
printf("## get_global_row/col : %d, %d \n", globalRow, globalCol);
line_no++;
}
*/
/*
int i = get_global_id(0); // row index of C
int j = get_global_id(1);
int m = get_group_id(0);
int n = get_group_id(1);
if (globalRow > 63 || globalCol > 63) {
printf("## global i %d, j %d, m %d, n %d, group %d(%d), %d(%d) \n",i,j,row,col,m,globalRow,n,globalCol);
}
*/
float acc[WPT];
for(int w=0; w<WPT; w++) {
acc[w] = 0.0f;
}
//const int num_tiles = (K%TS)>0 ? K/TS+1 : K/TS;
const int num_tiles = (K+TS-1)/TS;
//printf("K, K %% TS, numtile %d %d %d\n", K,K%TS,num_tiles);
for(int t=0; t<num_tiles; t++){
for(int w=0; w<WPT; w++){
const int tiledRow = TS*t + row;
const int tiledCol = TS*t + col;
if(global_row + w*RTS >= M || tiledCol >= K) {
Asub[row + w*RTS][col] = 0.0f;
}
else {
Asub[row + w*RTS][col] = A[(global_row + w*RTS)*K + tiledCol];
}
if(tiledRow + w*RTS >= K || global_col >= N) {
Bsub[row + w*RTS][col]=0.0f;
}
else {
Bsub[row + w*RTS][col]=B[(tiledRow + w*RTS)*N + global_col];
}
}
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(global_row + w*RTS >= M || global_col >=N) continue;
else C[(global_row + w*RTS)*N + global_col] = acc[w];
}
}