chundoong-lab-ta/SamsungDS22/submissions/HW5/jh8846.kim/kernel.cl

192 lines
5.2 KiB
Common Lisp

#define row_col_size_local 64
#define rows_per_local 32
__kernel void sgemm(__global float *A, __global float *B, __global float *C, int M, int N, int K)
{
const int local_row = get_local_id(0); // local row ID
const int local_col = get_local_id(1); // local col ID
const int global_row = row_col_size_local*get_group_id(0) + local_row; // c행렬의 row
const int global_col = row_col_size_local*get_group_id(1) + local_col; // c행렬의 col
//printf("local_id0: %d, local_id1: %d, global_id0: %d, global_id1:%d\n",get_local_id(0),get_local_id(1),get_group_id(0),get_group_id(1));
const int RTS = row_col_size_local/rows_per_local;
//int rows_per_local_var = rows_per_local;
__local float Asub[row_col_size_local][row_col_size_local];
__local float Bsub[row_col_size_local][row_col_size_local];
//int global_row_end = global_row+ ((rows_per_local-1)*RTS) ;
//int global_col_end = global_col;
float temp_val[rows_per_local];
for(int w=0; w<rows_per_local; w++)
{
temp_val[w]=0.0f;
}
const int num_tiles = ((K+row_col_size_local-1)/row_col_size_local) ; //32개씩 몇번 돌건지 정의
for(int t=0;t<num_tiles;t++)
{
const int t_row = row_col_size_local * t + local_row;
const int t_col = row_col_size_local * t + local_col;
for(int w=0; w<rows_per_local; w++)
{
if(global_row + (w*RTS) >=M || t_col >=K)
{
Asub[local_row+w*RTS][local_col] = 0.0f;
}
else
{
Asub[local_row + w*RTS][local_col] = A[(global_row + w*RTS)*K + t_col];
}
if(t_row + (w*RTS) >=K || global_col >=N)
{
Bsub[local_row+w*RTS][local_col] = 0.0f;
}
else
{
Bsub[local_row + w*RTS][local_col] = B[(t_row + w*RTS)*N + global_col];
}
//Asub[local_row+ w*RTS][local_col] = A[(global_row+ w*RTS) * K + t_col];
//Bsub[local_row+ w*RTS][local_col] = B[(t_row+ w*RTS) * N + global_col];
}
barrier(CLK_LOCAL_MEM_FENCE);
for(int k=0; k<row_col_size_local; k++)
{
for(int w=0; w<rows_per_local; w++)
{
temp_val[w] += Asub[local_row+w*RTS][k] * Bsub[k][local_col];
}
}
barrier(CLK_LOCAL_MEM_FENCE);
}
for(int w=0; w<rows_per_local; w++)
{
if(global_row + w*RTS < M && global_col < N)
{
C[(global_row+ w*RTS)*N + global_col] = temp_val[w];
}
}
}
/* 8192 8192 8192 는 15000 달성
#define row_col_size_local 64
#define rows_per_local 32
__kernel void sgemm(__global float *A, __global float *B, __global float *C, int M, int N, int K)
{
const int local_row = get_local_id(0); // local row ID
const int local_col = get_local_id(1); // local col ID
const int global_row = row_col_size_local*get_group_id(0) + local_row; // c행렬의 row
const int global_col = row_col_size_local*get_group_id(1) + local_col; // c행렬의 col
//printf("local_id0: %d, local_id1: %d, global_id0: %d, global_id1:%d\n",get_local_id(0),get_local_id(1),get_group_id(0),get_group_id(1));
const int RTS = row_col_size_local/rows_per_local;
//int rows_per_local_var = rows_per_local;
__local float Asub[row_col_size_local][row_col_size_local];
__local float Bsub[row_col_size_local][row_col_size_local];
//int global_row_end = global_row+ ((rows_per_local-1)*RTS) ;
//int global_col_end = global_col;
float temp_val[rows_per_local];
for(int w=0; w<rows_per_local; w++)
{
temp_val[w]=0.0f;
}
const int num_tiles = (K/row_col_size_local) ; //32개씩 몇번 돌건지 정의
for(int t=0;t<num_tiles;t++)
{
const int t_row = row_col_size_local * t + local_row;
const int t_col = row_col_size_local * t + local_col;
for(int w=0; w<rows_per_local; w++)
{
Asub[local_row+ w*RTS][local_col] = A[(global_row+ w*RTS) * K + t_col];
Bsub[local_row+ w*RTS][local_col] = B[(t_row+ w*RTS) * N + global_col];
}
barrier(CLK_LOCAL_MEM_FENCE);
for(int k=0; k<row_col_size_local; k++)
{
for(int w=0; w<rows_per_local; w++)
{
temp_val[w] += Asub[local_row+w*RTS][k] * Bsub[k][local_col];
}
}
barrier(CLK_LOCAL_MEM_FENCE);
}
for(int w=0; w<rows_per_local; w++)
{
C[(global_row+ w*RTS)*N + global_col] = temp_val[w];
}
}
*/
/* 679 성능
__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); // local row ID
const int col = get_local_id(1); // local col ID
const int global_row = 32*get_group_id(0) + row; // c행렬의 row
const int global_col = 32*get_group_id(1) + col; // c행렬의 col
__local float Asub[32][32];
__local float Bsub[32][32];
float temp_val = 0.0f;
const int num_tiles = K/32;
for(int t=0; t<num_tiles; t++)
{
const int t_row = 32*t + row;
const int t_col = 32*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++)
{
temp_val += Asub[row][k] * Bsub[k][col];
}
barrier(CLK_LOCAL_MEM_FENCE);
}
C[global_row*N + global_col]= temp_val;
}
*/
/* 최초 코드
// 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];
}
}
*/