121 lines
4.0 KiB
Common Lisp
121 lines
4.0 KiB
Common Lisp
|
/* 3 */
|
||
|
#define TS 32
|
||
|
#define WPT 8
|
||
|
#define RTS 4 // (TS/WPT)
|
||
|
|
||
|
|
||
|
// super super slow sgemm kernel by heehoon
|
||
|
// SGEMM(single-precision matrix-multiplication)
|
||
|
// /* 2 */__kernel void sgemm(__global float *A, __global float *B, __global float *C, int M, int N, int K, int TS) {
|
||
|
__kernel void sgemm(__global float *A, __global float *B, __global float *C, int M, int N, int K) {
|
||
|
/* 3 */
|
||
|
// Thread identifiers
|
||
|
const int row = get_local_id(0); // local row (max = TS)
|
||
|
const int col = get_local_id(1); // local col (max = TS/WPT = RTS)
|
||
|
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 intermediate_val[WPT];
|
||
|
for (int w=0; w<WPT; w++) {
|
||
|
intermediate_val[w] = 0.0f;
|
||
|
}
|
||
|
|
||
|
//const int num_tiles = K / TS;
|
||
|
const int num_tiles = (K + TS - 1) / TS;
|
||
|
//printf("\nnum_tilesi: (K + TS -1)/TS = %d\n", num_tiles);
|
||
|
for (int t = 0; t < num_tiles; t++) {
|
||
|
for (int w = 0; w < WPT; w++) {
|
||
|
const int t_row = TS * t + row;
|
||
|
const int t_col = TS * t + col;
|
||
|
|
||
|
//Asub[row + w*RTS][col] = A[global_row * K + t_col];
|
||
|
//Bsub[row + w*RTS][col] = B[t_row * N + global_col];
|
||
|
if (global_row + w*RTS >= M || t_row >= 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];
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
barrier(CLK_LOCAL_MEM_FENCE);
|
||
|
|
||
|
for (int k = 0; k < TS; k++) {
|
||
|
for (int w = 0; w < WPT; w++) {
|
||
|
intermediate_val[w] += Asub[row + w*RTS][k] * Bsub[k][col];
|
||
|
}
|
||
|
}
|
||
|
|
||
|
barrier(CLK_LOCAL_MEM_FENCE);
|
||
|
}
|
||
|
|
||
|
// Store
|
||
|
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] = intermediate_val[w];
|
||
|
}
|
||
|
|
||
|
/* 1. original code
|
||
|
int i = get_global_id(0); // row index of C (max = TS)
|
||
|
int j = get_global_id(1); // column index of C (max = TS/WPT = RTS)
|
||
|
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];
|
||
|
}
|
||
|
*/
|
||
|
|
||
|
/* 2. TS = 8 -> run_performance.sh: 922.67 GFLOPS but run_validate.sh -> INVALID
|
||
|
//printf("\n[hong] TS = %d\n", TS);
|
||
|
// Thread identifiers
|
||
|
const int row = get_local_id(0); // local row
|
||
|
const int col = get_local_id(1); // local col
|
||
|
//const int global_row = 32 * get_group_id(0) + row; // row ID of C
|
||
|
//const int global_col = 32 * get_group_id(1) + col; // col ID of C
|
||
|
const int global_row = TS * get_group_id(0) + row; // row ID of C
|
||
|
const int global_col = TS * get_group_id(1) + col; // col ID of C
|
||
|
|
||
|
__local float Asub[32][32];
|
||
|
__local float Bsub[32][32];
|
||
|
//__local float Asub[8][8];
|
||
|
//__local float Bsub[8][8];
|
||
|
// __local float Asub[TS][TS]; // variable length arrays are not supported in OpenCL
|
||
|
// __local float Bsub[TS][TS]; // variable length arrays are not supported in OpenCL
|
||
|
|
||
|
float intermediate_val = 0.0f;
|
||
|
//const int num_tiles = K / 32;
|
||
|
const int num_tiles = K / TS;
|
||
|
for (int t = 0; t < num_tiles; t++) {
|
||
|
//const int t_row = 32 * t + row;
|
||
|
//const int t_col = 32 * t + col;
|
||
|
const int t_row = TS * t + row;
|
||
|
const int t_col = TS * t + col;
|
||
|
Asub[row][col] = A[global_row * K + t_col];
|
||
|
Bsub[row][col] = B[t_row * N + global_col];
|
||
|
|
||
|
barrier(CLK_LOCAL_MEM_FENCE);
|
||
|
|
||
|
//for (int k = 0; k < 32; k++) {
|
||
|
for (int k = 0; k < TS; k++) {
|
||
|
intermediate_val += Asub[row][k] * Bsub[k][col];
|
||
|
}
|
||
|
|
||
|
barrier(CLK_LOCAL_MEM_FENCE);
|
||
|
}
|
||
|
|
||
|
C[global_row * N + global_col] = intermediate_val;
|
||
|
*/
|
||
|
|
||
|
}
|