58 lines
1.4 KiB
Common Lisp
58 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) {
|
|
const int i = get_local_id(0); // row index of C
|
|
const int j = get_local_id(1); // column index of C
|
|
const int gi = TS * get_group_id(0) + i;
|
|
const int gj = TS * get_group_id(1) + j;
|
|
|
|
|
|
__local float Asub[TS][TS];
|
|
__local float Bsub[TS][TS];
|
|
|
|
float intermediate_val[WPT];
|
|
for(int w=0;w<WPT;w++){
|
|
intermediate_val[w] = 0.0f;
|
|
}
|
|
|
|
const int num_tile = (K+TS-1) / TS;
|
|
|
|
for(int t=0;t<num_tile; t++){
|
|
for(int w=0;w<WPT;w++){
|
|
const int t_row = TS * t + i;
|
|
const int t_col = TS * t + j;
|
|
if(M-1<(gi+w*RTS)){
|
|
Asub[i + w * RTS][j] = 0.0f;}
|
|
else if(K-1<(t_col)){
|
|
Asub[i + w * RTS][j] = 0.0f;}
|
|
else {
|
|
Asub[i + w * RTS][j] = A[(gi + w * RTS) * K + t_col];}
|
|
|
|
if(K-1<(t_row+w*RTS)){
|
|
Bsub[i + w * RTS][j] = 0.0f;}
|
|
else if(N-1<(gj)){
|
|
Bsub[i + w * RTS][j] = 0.0f;}
|
|
else{
|
|
Bsub[i + w * RTS][j] = B[(t_row + w * RTS) * N + gj];}
|
|
}
|
|
barrier(CLK_LOCAL_MEM_FENCE);
|
|
|
|
for(int k=0;k<TS;k++){
|
|
for(int w=0;w<WPT;w++){
|
|
intermediate_val[w] += Asub[i + w * RTS][k] * Bsub[k][j];
|
|
}
|
|
}
|
|
|
|
barrier(CLK_LOCAL_MEM_FENCE);
|
|
}
|
|
for(int w=0;w<WPT;w++){
|
|
if(M>(gi+w*RTS) && N>gj)
|
|
C[(gi + w * RTS)* N + gj] = intermediate_val[w];
|
|
}
|
|
|
|
}
|