69 lines
2.3 KiB
Common Lisp
69 lines
2.3 KiB
Common Lisp
#define TS 64
|
|
#define WPT 32
|
|
#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); // Local row ID (max: TS)
|
|
const int col = get_local_id(1); // Local col ID (max: TS/WPT == RTS)
|
|
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 registers
|
|
float tempVal[WPT];
|
|
for (int w=0; w<WPT; w++)
|
|
tempVal[w] = 0.0f;
|
|
|
|
// Loop over all tiles
|
|
const int numTiles = (K + TS - 1) / TS ; // Ceiling to Tile Size
|
|
//const int numTiles = (K / TS) + 1 - (K % TS == 0);
|
|
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;
|
|
|
|
const int orgM = globalRow + w*RTS ;
|
|
const int orgK = tiledRow + w*RTS ;
|
|
|
|
// Copy Global A to local
|
|
if ( (orgM < M) && (tiledCol < K) )
|
|
Asub[row + w*RTS][col] = A[orgM*K + tiledCol] ;
|
|
else
|
|
Asub[row + w*RTS][col] = 0 ;
|
|
|
|
// Copy Global B to local
|
|
if ( (orgK < K) && (globalCol < N) )
|
|
Bsub[row + w*RTS][col] = B[orgK*N + globalCol] ;
|
|
else
|
|
Bsub[row + w*RTS][col] = 0 ;
|
|
}
|
|
|
|
// 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++) {
|
|
tempVal[w] += Asub[row + w*RTS][k] * Bsub[k][col];
|
|
}
|
|
}
|
|
|
|
// Synchronise before loading the next tile
|
|
barrier(CLK_LOCAL_MEM_FENCE);
|
|
}
|
|
|
|
// Store the final results in C
|
|
for (int w=0; w<WPT; w++) {
|
|
const int orgM = globalRow + w*RTS ;
|
|
|
|
if ( (orgM < M) && (globalCol < N) )
|
|
C[orgM * N + globalCol] = tempVal[w];
|
|
}
|
|
}
|