185 lines
5.1 KiB
C
185 lines
5.1 KiB
C
// vim: syntax=c
|
|
// super super slow sgemm kernel by heehoon
|
|
#define TS 32
|
|
#define WPT 16
|
|
#define RTS (TS/WPT)
|
|
|
|
|
|
void __print_mat(float *m, int R, int C)
|
|
{
|
|
for (int i = 0; i < R; ++i)
|
|
{
|
|
for (int j = 0; j < C; ++j)
|
|
{
|
|
printf("%+.3f ", m[i * C + j]);
|
|
}
|
|
printf("\n");
|
|
}
|
|
return;
|
|
}
|
|
|
|
// step 1: naive implementation
|
|
/*
|
|
__kernel void sgemm(__global float *A, __global float *B, __global float *C, int M, int N, int K) {
|
|
const int global_row = get_global_id(0); // row index of C
|
|
const int global_col = get_global_id(1); // column index of C
|
|
|
|
float acc = 0.0f;
|
|
float AikBkj;
|
|
printf("(info) {row, col} = {%2d, %2d}\n", global_row, global_col);
|
|
for(int k=0; k<K; ++k)
|
|
{
|
|
// acc += A[global_row * K + k] * B[k * N + global_col];
|
|
AikBkj = A[global_row * K + k] * B[k * N + global_col];
|
|
acc += AikBkj;
|
|
|
|
// if( (global_row == 0) && (global_col == 1) )
|
|
// {
|
|
// printf("(info) A[%2d, %2d] = %f, B[%2d, %2d] = %f\n",
|
|
// global_row, k, A[global_row * K + k], k, global_col, B[k * N + global_col]);
|
|
// printf("(info) A[%2d, %2d]*B[%2d, %2d] = %f\n", global_row, k, k, global_col, AikBkj);
|
|
// printf("(info) acc = %f\n", acc);
|
|
// }
|
|
}
|
|
|
|
C[global_row * N + global_col] = acc;
|
|
// printf("(info) C[%2d, %2d] = %f\n", global_row, global_col, C[global_col*M + global_row]);
|
|
}
|
|
*/
|
|
|
|
/*
|
|
// step 2: local tiling
|
|
__kernel void sgemm(__global float *A, __global float *B, __global float *C, int M, int N, int K) {
|
|
const int row = get_local_id(0); // 0 <= row < TS
|
|
const int col = get_local_id(1); // 0 <= col < TS
|
|
const int tid_row = get_group_id(0); // row index of tile
|
|
const int tid_col = get_group_id(1); // col index of tile
|
|
const int global_row = TS * tid_row + row; // row index of C
|
|
const int global_col = TS * tid_col + col; // col index of C
|
|
|
|
__local float Asub[TS*TS];
|
|
__local float Bsub[TS*TS];
|
|
|
|
float acc = 0.0f;
|
|
|
|
// printf("(info) {row, col, tid_row, tid_col} = {%2d, %2d, %2d, %2d}\n",
|
|
// row, col, tid_row, tid_col);
|
|
|
|
const int numTiles = K / TS;
|
|
for(int t=0; t<numTiles; ++t)
|
|
{
|
|
// load one tile of A and B into local memory
|
|
const int tile_row = TS * t + row;
|
|
const int tile_col = TS * t + col;
|
|
|
|
// Asub[row*TS + col] = A[global_row * K + global_col];
|
|
// Bsub[row*TS + col] = B[global_row * N + global_col];
|
|
Asub[row*TS + col] = A[global_row * K + tile_col];
|
|
Bsub[row*TS + col] = B[tile_row * N + global_col];
|
|
// Asub[row*TS + col] = A[tile_row * K + global_col];
|
|
// Bsub[row*TS + col] = B[global_row * N + tile_col];
|
|
|
|
barrier(CLK_LOCAL_MEM_FENCE);
|
|
|
|
// if( (tid_row == 0) && (tid_col == 0) )
|
|
// {
|
|
printf("(info) global (%d, %d), tile (%d, %d, %d) Asub[%d, %d] = %+.3f\n",
|
|
global_row, global_col, t, tid_row, tid_col, row, col, Asub[row*TS + col]);
|
|
printf("(info) global (%d, %d), tile (%d, %d, %d) Bsub[%d, %d] = %+.3f\n",
|
|
global_row, global_col, t, tid_row, tid_col, row, col, Bsub[row*TS + col]);
|
|
// }
|
|
|
|
|
|
for(int k=0; k<TS; ++k)
|
|
{
|
|
acc += Asub[row * TS + k] * Bsub[k * TS + col];
|
|
// printf("(info) Asub[%2d, %2d] = %+.3f\n", row, k, Asub[row*TS + k]);
|
|
// printf("(info) Bsub[%2d, %2d] = %+.3f\n", k, col, Bsub[k*TS + col]);
|
|
}
|
|
|
|
barrier(CLK_LOCAL_MEM_FENCE);
|
|
}
|
|
|
|
C[global_row * N + global_row] = acc;
|
|
// if( (t == 0) && (row == 0) && (col == 0) )
|
|
// {
|
|
// printf("A = \n");
|
|
// __print_mat(Asub, TS, TS);
|
|
// printf("B = \n");
|
|
// __print_mat(Bsub, TS, TS);
|
|
// }
|
|
|
|
}
|
|
*/
|
|
|
|
// step 3: tiling
|
|
__kernel void sgemm(__global float *A, __global float *B, __global float *C, int M, int N, int K) {
|
|
const int row = get_local_id(0); // row index of C
|
|
const int col = get_local_id(1); // column index of C
|
|
const int global_row = TS * get_group_id(0) + row;
|
|
const int global_col = TS * get_group_id(1) + col;
|
|
|
|
// printf("(info) row, col, global_row, global_col = {%02d, %02d, %02d, %02d}\n",
|
|
// row, col, global_row, global_col);
|
|
|
|
__local float Asub[TS][TS];
|
|
__local float Bsub[TS][TS];
|
|
|
|
float acc[WPT];
|
|
for(int w=0; w<WPT; ++w)
|
|
{
|
|
acc[w] = 0.0f;
|
|
}
|
|
|
|
const int num_tiles = (int) ((K + TS - 1)/TS);
|
|
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;
|
|
|
|
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];
|
|
}
|
|
}
|
|
|
|
barrier(CLK_LOCAL_MEM_FENCE);
|
|
|
|
for(int k=0; k<TS; ++k)
|
|
{
|
|
for(int w=0; w<WPT; ++w)
|
|
{
|
|
acc[w] += Asub[row + w*RTS][k] * Bsub[k][col];
|
|
}
|
|
}
|
|
|
|
barrier(CLK_LOCAL_MEM_FENCE);
|
|
}
|
|
|
|
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];
|
|
}
|
|
// C[i * N + j] = 0;
|
|
// for (int k = 0; k < K; k++) {
|
|
// C[i * N + j] += A[i * K + k] * B[k * N + j];
|
|
// }
|
|
}
|
|
|