64 lines
1.6 KiB
Common Lisp
64 lines
1.6 KiB
Common Lisp
#define TILE_ROW 32
|
|
#define TILE_COL (TILE_ROW/WIDTH)
|
|
|
|
#define TS 32
|
|
#define WPT 8
|
|
#define RTS 4
|
|
|
|
__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(TS/WPT IDs)
|
|
const int col = get_local_id(1); // local col ID(0~31)
|
|
const int global_row = TS * get_group_id(0) + row; // row ID of C(0~M)
|
|
const int global_col = TS * get_group_id(1) + col; // col ID of C(0~N)
|
|
|
|
__local float Asub[TS][TS];
|
|
__local float Bsub[TS][TS];
|
|
|
|
float acc[WPT];
|
|
for (int w=0; w<WPT; w++) {
|
|
acc[w] = 0.0f;
|
|
}
|
|
// Loop over all tiles
|
|
const int num_tiles = (K+TS-1) / TS;
|
|
for (int t = 0; t<num_tiles; t++) {
|
|
|
|
// Load one tile of A and B into local memory
|
|
for (int w = 0; w <WPT; w++){
|
|
const int t_row = TS * t + row;
|
|
const int t_col = TS * t + col;
|
|
|
|
if (global_row + w*RTS >= M || t_col >= K) {
|
|
Asub[row + w*RTS][col] = 0.0f;
|
|
} else{
|
|
Asub[row + w*RTS][col] = A[(global_row + w*RTS)*K +t_col];
|
|
}
|
|
if (t_row + w*RTS >= K || global_col >= N) {
|
|
Bsub[row + w*RTS][col] = 0.0f;
|
|
} else {
|
|
Bsub[row + w*RTS][col] = B[(t_row + w*RTS)*N + global_col];
|
|
}
|
|
}
|
|
|
|
// Synchronize 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[row + w*RTS][k] * Bsub[k][col];
|
|
}
|
|
}
|
|
|
|
// Synchronize before loading the next tile
|
|
barrier(CLK_LOCAL_MEM_FENCE);
|
|
}
|
|
|
|
// Store the final results in C
|
|
for (int w=0; w<WPT; w++) {
|
|
if (global_row + w*RTS >= M || global_col >= N) continue;
|
|
C[(global_row + w*RTS)*N + global_col] = acc[w];
|
|
}
|
|
}
|
|
|