57 lines
1.3 KiB
Common Lisp
57 lines
1.3 KiB
Common Lisp
|
// version 3
|
||
|
#define TS 32
|
||
|
#define WPT 8
|
||
|
#define RTSF (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 globalRow = TS*get_group_id(0)+row;
|
||
|
const int globalCol = TS*get_group_id(1)+col;
|
||
|
|
||
|
__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 num_tiles = (K+TS-1)/TS;
|
||
|
|
||
|
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(globalRow + w*RTSF >= M || tiledCol >= K) {
|
||
|
Asub[row+w*RTSF][col]=0.0f;
|
||
|
}
|
||
|
else {
|
||
|
Asub[row+w*RTSF][col]=A[(globalRow+w*RTSF)*K+tiledCol];
|
||
|
}
|
||
|
if(tiledRow + w*RTSF >= K || globalCol >= N) {
|
||
|
Bsub[row+w*RTSF][col]=0.0f;
|
||
|
}
|
||
|
else {
|
||
|
Bsub[row+w*RTSF][col]=B[(tiledRow+w*RTSF)*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*RTSF][k]*Bsub[k][col];
|
||
|
}
|
||
|
}
|
||
|
|
||
|
barrier(CLK_LOCAL_MEM_FENCE);
|
||
|
}
|
||
|
|
||
|
for(int w=0;w<WPT;w++) {
|
||
|
if(globalRow + w*RTSF >= M || globalCol >= N) continue;
|
||
|
C[(globalRow+w*RTSF)*N+globalCol]=acc[w];
|
||
|
}
|
||
|
}
|
||
|
|