96 lines
2.9 KiB
Common Lisp
96 lines
2.9 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) {
|
||
|
//__kernel void sgemm(const __global float *A, const __global float *B, __global float *C, const int M, const int N, const int K) {
|
||
|
|
||
|
// int i = get_global_id(0); // row index of C
|
||
|
// int j = get_global_id(1); // column index of C
|
||
|
// if (i >= M || j >= N) return; // boundary check
|
||
|
//
|
||
|
// C[i * N + j] = 0;
|
||
|
// for (int k = 0; k < K; k++) {
|
||
|
// C[i * N + j] += A[i * K + k] * B[k * N + j];
|
||
|
// }
|
||
|
|
||
|
// Thread identifiers
|
||
|
const int row = get_local_id(0); // Local row ID (max: TS)
|
||
|
const int col = get_local_id(1); // Local col ID (max: TS)
|
||
|
const int globalRow = TS*get_group_id(0) + row; // Row ID of C (0..M)
|
||
|
const int globalCol = TS*get_group_id(1) + col; // Col ID of C (0..N)
|
||
|
|
||
|
|
||
|
// 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 register
|
||
|
float acc[WPT];
|
||
|
for (int w=0; w<WPT; w++) {
|
||
|
acc[w] = 0.0f;
|
||
|
}
|
||
|
|
||
|
// Loop over all tiles
|
||
|
//const int numTiles = K/TS;
|
||
|
const int numTiles = (K+TS-1)/TS;
|
||
|
for (int t=0; t<numTiles; t++) {
|
||
|
|
||
|
// Load one tile of A and B into local memory
|
||
|
for (int w=0; w<WPT; w++) {
|
||
|
const int tiledRow = TS*t + row;
|
||
|
const int tiledCol = TS*t + col;
|
||
|
//Asub[col + w*RTS][row] = A[(tiledCol + w*RTS)*M + globalRow];
|
||
|
//Bsub[col + w*RTS][row] = B[(globalCol + w*RTS)*K + tiledRow];
|
||
|
//Asub[row + w*RTS][col] = A[(globalRow + w*RTS)*K + tiledCol];
|
||
|
//Bsub[row + w*RTS][col] = B[(tiledRow + w*RTS)*N + globalCol];
|
||
|
if(globalRow + w*RTS >= M || tiledCol >= K)
|
||
|
{
|
||
|
Asub[row + w*RTS][col] = 0.0f;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
Asub[row + w*RTS][col] = A[(globalRow + w*RTS)*K + tiledCol];
|
||
|
}
|
||
|
if(globalCol >= N || tiledRow + w*RTS >= K)
|
||
|
{
|
||
|
Bsub[row + w*RTS][col] = 0.0f;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
Bsub[row + w*RTS][col] = B[(tiledRow + w*RTS)*N + globalCol];
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Synchronise to make sure the tile is loaded
|
||
|
barrier(CLK_LOCAL_MEM_FENCE);
|
||
|
|
||
|
// Perform the computation for a single tile
|
||
|
for (int k=0; k<TS; k++) {
|
||
|
for (int w=0; w<WPT; w++) {
|
||
|
//acc[w] += Asub[k][row] * Bsub[col + w*RTS][k];
|
||
|
acc[w] += Asub[row+w*RTS][k] * Bsub[k][col];
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Synchronise before loading the next tile
|
||
|
barrier(CLK_LOCAL_MEM_FENCE);
|
||
|
}
|
||
|
|
||
|
// Store the final result in C
|
||
|
for (int w=0; w<WPT; w++) {
|
||
|
//C[(globalCol + w*RTS)*M + globalRow] = acc[w];
|
||
|
//C[(globalRow + w*RTS)*N + globalCol] = acc[w];
|
||
|
if(globalRow + w*RTS >= M || globalCol >= N)
|
||
|
{
|
||
|
continue;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
C[(globalRow + w*RTS)*N + globalCol] = acc[w];
|
||
|
}
|
||
|
}
|
||
|
}
|