71 lines
2.1 KiB
Common Lisp
71 lines
2.1 KiB
Common Lisp
#define TS 32
|
|
#define WPT 8
|
|
#define RTS (TS/WPT)
|
|
|
|
//// super super slow sgemm kernel by heehoon
|
|
//__kernel void sgemm(__global float *A, __global float *B, __global float *C, int M, int N, 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];
|
|
// }
|
|
__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];
|
|
}
|
|
}
|