73 lines
1.8 KiB
Common Lisp
73 lines
1.8 KiB
Common Lisp
#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) {
|
|
|
|
// Thread identifiers
|
|
const int row = get_local_id(0); // row index of C
|
|
const int col = get_local_id(1); // column index of C
|
|
const int globalRow = TS * get_group_id(0) + row;
|
|
const int globalCol = TS * get_group_id(1) + col;
|
|
|
|
// Local memory to fit a tile of TS*TS elements of A and B
|
|
__local float Asub[TS][TS];
|
|
__local float Bsub[TS][TS];
|
|
|
|
// Initialise the accumulation array
|
|
float intermediateVal[WPT];
|
|
for(int w = 0; w < WPT; w++){
|
|
intermediateVal[w] = 0.0f;
|
|
}
|
|
|
|
// Loop over all tiles
|
|
int numTiles;
|
|
if(K % TS == 0){
|
|
numTiles = K / TS;
|
|
}
|
|
else{
|
|
numTiles = K / 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] = A[(globalRow + w * RTS) * K + tiledCol];
|
|
}
|
|
else{
|
|
Asub[row + w * RTS][col] = 0.0f;
|
|
}
|
|
|
|
if( (tiledRow + w * RTS) < K && globalCol < N ){
|
|
Bsub[row + w * RTS][col] = B[(tiledRow + w * RTS) * N + globalCol];
|
|
}
|
|
else{
|
|
Bsub[row + w * RTS][col] = 0.0f;
|
|
}
|
|
}
|
|
|
|
// Synchronise to make sure the tile is loaded
|
|
barrier(CLK_LOCAL_MEM_FENCE);
|
|
|
|
// Perform the computation
|
|
for(int k = 0; k < TS; k++){
|
|
for(int w = 0; w < WPT; w++){
|
|
intermediateVal[w] += Asub[row + w * RTS][k] * Bsub[k][col];
|
|
}
|
|
}
|
|
|
|
// Synchronise before loading the next tile
|
|
barrier(CLK_LOCAL_MEM_FENCE);
|
|
}
|
|
|
|
for(int w = 0; w < WPT; w++){
|
|
if( (globalRow + w * RTS) < M && globalCol < N){
|
|
C[(globalRow + w * RTS) * N + globalCol] = intermediateVal[w];
|
|
}
|
|
}
|
|
}
|
|
|