#define TS 32 #define WPT 8 #define RTS (TS/WPT) __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; __local float subA[TS][TS]; __local float subB[TS][TS]; float accum[WPT]; for (int w = 0; w < WPT; w++) { accum[w] = 0.0f; } const int num_tiles = (K+TS-1)/TS; for (int t = 0; t < num_tiles; t++) { for (int w = 0; w < WPT; w++) { const int tiledRow = TS*t + row; const int tiledCol = TS*t + col; /*if (M <= global_row) subA[row + w*RTS][col] = 0.0f; else*/ if (K <= tiledCol) subA[row + w*RTS][col] = 0.0f; else if (M <= (global_row + w*RTS)) subA[row + w*RTS][col] = 0.0f; else subA[row + w*RTS][col] = A[(global_row + w*RTS)*K + tiledCol]; if (N <= global_col) subB[row + w*RTS][col] = 0.0f; //else if (K <= tiledRow) subB[row + w*RTS][col] = 0.0f; else if (K <= (tiledRow + w*RTS)) subB[row + w*RTS][col] = 0.0f; else subB[row + w*RTS][col] = B[(tiledRow + w*RTS)*N + global_col]; } barrier(CLK_LOCAL_MEM_FENCE); for (int k = 0; k < TS; k++) { for (int w = 0; w < WPT; w++) { accum[w] += subA[row + w*RTS][k] * subB[k][col]; } } barrier(CLK_LOCAL_MEM_FENCE); } for (int w = 0; w < WPT; w++) { if (M <= (global_row + w*RTS));// C[(global_row + w*RTS)*N + global_col] = 0.0f; else if (N <= global_col);// C[(global_row + w*RTS)*N + global_col] = 0.0f; else C[(global_row + w*RTS)*N + global_col] = accum[w]; } }