chundoong-lab-ta/SamsungDS22/submissions/HW5/ty.jeon/mat_mul.cpp

425 lines
16 KiB
C++

#include "mat_mul.h"
#include <stdio.h>
#include <CL/cl.h>
#define CHECK_ERROR(err) \
if (err != CL_SUCCESS) { \
printf("[%s:%d] OpenCL error %d\n", __FILE__, __LINE__, err); \
exit(EXIT_FAILURE); \
}
static cl_int err;
static cl_platform_id platform;
static cl_device_id device_m[4];
static cl_context context;
static cl_command_queue queue_m[4];
static cl_program program;
static cl_kernel kernel_m[4];
static cl_mem a_d[4], b_d[4], c_d[4];
static cl_mem a_d_xl[4], b_d_xl[4], c_d_xl[4];
static cl_mem c_result;
static float *A, *B, *C;
static int M, N, K;
static int M_xl, N_xl, K_xl;
void mat_mul(float *_A, float *_B, float *_C, int _M, int _N, int _K) {
//A = _A, B = _B, C = _C;
//M = _M, N = _N, K = _K;
int M_single = M/4;
int M_last = M - 3*(M/4);
const size_t p_local[2] = {16, 16};
const size_t p_global_a[2] = {(size_t)M_xl, (size_t)K_xl};
const size_t p_global_b[2] = {(size_t)K_xl, (size_t)N_xl};
//const size_t p_global_c[2] = {(size_t)M, (size_t)N};
cl_kernel kernel_zeropadding_a[4];
for(int i = 0; i < 4; i++){
kernel_zeropadding_a[i] = clCreateKernel(program, "paddingAddZeroes", &err);
CHECK_ERROR(err);
err = clSetKernelArg(kernel_zeropadding_a[i], 0, sizeof(int), (void*)&M_single);
err = clSetKernelArg(kernel_zeropadding_a[i], 1, sizeof(int), (void*)&K);
err = clSetKernelArg(kernel_zeropadding_a[i], 2, sizeof(cl_mem), (void*)&a_d[i]);
err = clSetKernelArg(kernel_zeropadding_a[i], 3, sizeof(int), (void*)&M_xl);
err = clSetKernelArg(kernel_zeropadding_a[i], 4, sizeof(int), (void*)&K_xl);
err = clSetKernelArg(kernel_zeropadding_a[i], 5, sizeof(cl_mem), (void*)&a_d_xl[i]);
err = clEnqueueNDRangeKernel(queue_m[i], kernel_zeropadding_a[i], 2, NULL, p_global_a, p_local, 0, NULL, NULL);
CHECK_ERROR(err);
}
kernel_zeropadding_a[3] = clCreateKernel(program, "paddingAddZeroes", &err);
CHECK_ERROR(err);
err = clSetKernelArg(kernel_zeropadding_a[3], 0, sizeof(int), (void*)&M_last);
err = clSetKernelArg(kernel_zeropadding_a[3], 1, sizeof(int), (void*)&K);
err = clSetKernelArg(kernel_zeropadding_a[3], 2, sizeof(cl_mem), (void*)&a_d[3]);
err = clSetKernelArg(kernel_zeropadding_a[3], 3, sizeof(int), (void*)&M_xl);
err = clSetKernelArg(kernel_zeropadding_a[3], 4, sizeof(int), (void*)&K_xl);
err = clSetKernelArg(kernel_zeropadding_a[3], 5, sizeof(cl_mem), (void*)&a_d_xl[3]);
err = clEnqueueNDRangeKernel(queue_m[3], kernel_zeropadding_a[3], 2, NULL, p_global_a, p_local, 0, NULL, NULL);
CHECK_ERROR(err);
cl_kernel kernel_zeropadding_b[4];
for(int i = 0; i < 4; i++){
kernel_zeropadding_b[i] = clCreateKernel(program, "paddingAddZeroes", &err);
CHECK_ERROR(err);
err = clSetKernelArg(kernel_zeropadding_b[i], 0, sizeof(int), (void*)&K);
err = clSetKernelArg(kernel_zeropadding_b[i], 1, sizeof(int), (void*)&N);
err = clSetKernelArg(kernel_zeropadding_b[i], 2, sizeof(cl_mem), (void*)&b_d[i]);
err = clSetKernelArg(kernel_zeropadding_b[i], 3, sizeof(int), (void*)&K_xl);
err = clSetKernelArg(kernel_zeropadding_b[i], 4, sizeof(int), (void*)&N_xl);
err = clSetKernelArg(kernel_zeropadding_b[i], 5, sizeof(cl_mem), (void*)&b_d_xl[i]);
err = clEnqueueNDRangeKernel(queue_m[i], kernel_zeropadding_b[i], 2, NULL, p_global_b, p_local, 0, NULL, NULL);
CHECK_ERROR(err);
}
//cl_kernel kernel_zeropadding_c = clCreateKernel(program, "paddingAddZeroes", &err);
//CHECK_ERROR(err);
//err = clSetKernelArg(kernel_zeropadding_c, 0, sizeof(int), (void*)&M_xl_single);
//CHECK_ERROR(err);
//err = clSetKernelArg(kernel_zeropadding_c, 1, sizeof(int), (void*)&N_xl);
//CHECK_ERROR(err);
//err = clSetKernelArg(kernel_zeropadding_c, 2, sizeof(cl_mem), (void*)&c_d_xl);
//CHECK_ERROR(err);
//err = clSetKernelArg(kernel_zeropadding_c, 3, sizeof(int), (void*)&M);
//CHECK_ERROR(err);
//err = clSetKernelArg(kernel_zeropadding_c, 4, sizeof(int), (void*)&N);
//CHECK_ERROR(err);
//err = clSetKernelArg(kernel_zeropadding_c, 5, sizeof(cl_mem), (void*)&c_d);
//CHECK_ERROR(err);
// Setup kernel arguments
for(int i = 0; i < 4; i++){
err = clSetKernelArg(kernel_m[i], 0, sizeof(cl_mem), &a_d_xl[i]);
CHECK_ERROR(err);
err = clSetKernelArg(kernel_m[i], 1, sizeof(cl_mem), &b_d_xl[i]);
CHECK_ERROR(err);
err = clSetKernelArg(kernel_m[i], 2, sizeof(cl_mem), &c_d_xl[i]);
CHECK_ERROR(err);
err = clSetKernelArg(kernel_m[i], 3, sizeof(int), &M_xl);
CHECK_ERROR(err);
err = clSetKernelArg(kernel_m[i], 4, sizeof(int), &N_xl);
CHECK_ERROR(err);
err = clSetKernelArg(kernel_m[i], 5, sizeof(int), &K_xl);
CHECK_ERROR(err);
}
// Setup global work size and local work size
size_t gws[2] = {(size_t)(M_xl/8), (size_t)N_xl}, lws[2] = {4, 32};
//for (int i = 0; i < 2; ++i) {
// // By OpenCL spec, global work size should be MULTIPLE of local work size
// // Formula below achieve it
// // e.g., gws = 25, lws = 16, then (25 + 16 - 1) / 16 * 16 = 40 / 16 * 16 = 2 * 16 = 32
// gws[i] = (gws[i] + lws[i] - 1) / lws[i] * lws[i];
//}
//size_t gws[2] = {(size_t)M_xl, (size_t)N_xl/8}, lws[2] = {32, 4};
err = clFinish(queue_m[0]);
err = clFinish(queue_m[1]);
err = clFinish(queue_m[2]);
err = clFinish(queue_m[3]);
CHECK_ERROR(err);
for(int i = 0; i < 4; i++){
err = clEnqueueNDRangeKernel(queue_m[i], kernel_m[i], 2, NULL, gws, lws, 0, NULL, NULL);
CHECK_ERROR(err);
}
//err = clEnqueueReadBuffer(queue, a_d_xl, CL_TRUE, 0, M_xl * K_xl * sizeof(float), A_buf, 0, NULL, NULL);
//CHECK_ERROR(err);
// Run kernel
//err = clEnqueueNDRangeKernel(queue, kernel_zeropadding_c, 2, NULL, p_global_c, p_local, 0, NULL, NULL);
// DO NOT REMOVE; NEEDED FOR TIME MEASURE
err = clFinish(queue_m[0]);
err = clFinish(queue_m[1]);
err = clFinish(queue_m[2]);
err = clFinish(queue_m[3]);
CHECK_ERROR(err);
}
static void print_platform_info(cl_platform_id platform) {
size_t sz;
char *buf;
CHECK_ERROR(clGetPlatformInfo(platform, CL_PLATFORM_NAME, 0, NULL, &sz));
buf = (char*)malloc(sz);
CHECK_ERROR(clGetPlatformInfo(platform, CL_PLATFORM_NAME, sz, buf, NULL));
printf("Detected OpenCL platform: %s\n", buf);
free(buf);
}
static void print_device_info(cl_device_id device) {
size_t sz;
char *buf;
CHECK_ERROR(clGetDeviceInfo(device, CL_DEVICE_NAME, 0, NULL, &sz));
buf = (char*)malloc(sz);
CHECK_ERROR(clGetDeviceInfo(device, CL_DEVICE_NAME, sz, buf, NULL));
printf("Detected OpenCL device: %s\n", buf);
free(buf);
}
//static cl_program create_and_build_program_with_source(cl_context context, cl_device_id device, const char *file_name) {
// FILE *file = fopen(file_name, "rb");
// if (file == NULL) {
// printf("Failed to open %s\n", file_name);
// exit(EXIT_FAILURE);
// }
// fseek(file, 0, SEEK_END);
// size_t source_size = ftell(file);
// rewind(file);
// char *source_code = (char*)malloc(source_size + 1);
// size_t ntotal = 0;
// while (ntotal < source_size) {
// int nread = fread(source_code, sizeof(char), source_size, file);
// ntotal += nread;
// }
// source_code[source_size] = '\0';
// fclose(file);
// cl_program program = clCreateProgramWithSource(context, 1, (const char **)&source_code, &source_size, &err);
// CHECK_ERROR(err);
// free(source_code);
// err = clBuildProgram(program, 1, &device, "", NULL, NULL);
// if (err == CL_BUILD_PROGRAM_FAILURE) {
// size_t log_size;
// CHECK_ERROR(clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, 0, NULL, &log_size));
// char *log = (char*)malloc(log_size + 1);
// CHECK_ERROR(clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, log_size, log, NULL));
// log[log_size] = 0;
// printf("Compile error:\n%s\n", log);
// free(log);
// }
// CHECK_ERROR(err);
// return program;
//}
static cl_program create_and_build_program_with_source_4(cl_context context, const char *file_name) {
FILE *file = fopen(file_name, "rb");
if (file == NULL) {
printf("Failed to open %s\n", file_name);
exit(EXIT_FAILURE);
}
fseek(file, 0, SEEK_END);
size_t source_size = ftell(file);
rewind(file);
char *source_code = (char*)malloc(source_size + 1);
size_t ntotal = 0;
while (ntotal < source_size) {
int nread = fread(source_code, sizeof(char), source_size, file);
ntotal += nread;
}
source_code[source_size] = '\0';
fclose(file);
cl_program program = clCreateProgramWithSource(context, 1, (const char **)&source_code, &source_size, &err);
CHECK_ERROR(err);
free(source_code);
err = clBuildProgram(program, 4, device_m, "", NULL, NULL);
if (err == CL_BUILD_PROGRAM_FAILURE) {
printf("Compile error:\n\n");
//size_t log_size;
//CHECK_ERROR(clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, 0, NULL, &log_size));
//char *log = (char*)malloc(log_size + 1);
//CHECK_ERROR(clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, log_size, log, NULL));
//log[log_size] = 0;
//printf("Compile error:\n%s\n", log);
//free(log);
}
CHECK_ERROR(err);
return program;
}
void mat_mul_init(float *_A, float *_B, float *_C, int _M, int _N, int _K) {
// Get OpenCL platform
//
A = _A;
B = _B;
C = _C;
M = _M;
N = _N;
K = _K;
//printf("mat mul init A 0 0 : %f\n", A[0]);
//printf("mat mul init A 0 1 : %f\n", A[1]);
//printf("mat mul init B 0 0 : %f\n", B[0]);
//printf("mat mul init B 1 0 : %f\n", B[N]);
//M_xl = (M + 31) / 32 * 32;
//N_xl = (N + 31) / 32 * 32;
//K_xl = (K + 31) / 32 * 32;
//
M_xl = 8192/4;
N_xl = 8192;
K_xl = 8192;
err = clGetPlatformIDs(1, &platform, NULL);
CHECK_ERROR(err);
print_platform_info(platform);
// Get OpenCL device
err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 4, device_m, NULL);
CHECK_ERROR(err);
print_device_info(device_m[0]);
// Create OpenCL context
context = clCreateContext(NULL, 4, device_m, NULL, NULL, &err);
CHECK_ERROR(err);
int a_single_size = ((M/4) * K) * sizeof(float);
int a_last_size = (M - 3*(M/4)) * K * sizeof(float);
int c_single_size = ((M/4) * N) * sizeof(float);
int c_last_size = (M - 3*(M/4)) * N * sizeof(float);
// Create OpenCL command queue
for(int i = 0; i < 4; i++){
queue_m[i] = clCreateCommandQueue(context, device_m[i], 0, &err);
CHECK_ERROR(err);
a_d_xl[i] = clCreateBuffer(context, CL_MEM_READ_WRITE, M_xl * K_xl * sizeof(float), NULL, &err);
CHECK_ERROR(err);
b_d_xl[i] = clCreateBuffer(context, CL_MEM_READ_WRITE, K_xl * N_xl * sizeof(float), NULL, &err);
CHECK_ERROR(err);
c_d_xl[i] = clCreateBuffer(context, CL_MEM_READ_WRITE, M_xl * N_xl * sizeof(float), NULL, &err);
CHECK_ERROR(err);
}
for(int i = 0; i < 3; i++){
a_d[i] = clCreateBuffer(context, CL_MEM_READ_WRITE, a_single_size, NULL, &err);
CHECK_ERROR(err);
b_d[i] = clCreateBuffer(context, CL_MEM_READ_WRITE, K * N * sizeof(float), NULL, &err);
CHECK_ERROR(err);
c_d[i] = clCreateBuffer(context, CL_MEM_READ_WRITE, c_single_size, NULL, &err);
CHECK_ERROR(err);
}
a_d[3] = clCreateBuffer(context, CL_MEM_READ_WRITE, a_last_size, NULL, &err);
CHECK_ERROR(err);
b_d[3] = clCreateBuffer(context, CL_MEM_READ_WRITE, K * N * sizeof(float), NULL, &err);
CHECK_ERROR(err);
c_d[3] = clCreateBuffer(context, CL_MEM_READ_WRITE, c_last_size, NULL, &err);
CHECK_ERROR(err);
c_result = clCreateBuffer(context, CL_MEM_READ_WRITE, M * N * sizeof(float), NULL, &err);
CHECK_ERROR(err);
// Compile program from "kernel.cl"
program = create_and_build_program_with_source_4(context, "kernel.cl");
// Extract kernel from compiled program
kernel_m[0] = clCreateKernel(program, "sgemm", &err);
kernel_m[1] = clCreateKernel(program, "sgemm", &err);
kernel_m[2] = clCreateKernel(program, "sgemm", &err);
kernel_m[3] = clCreateKernel(program, "sgemm", &err);
CHECK_ERROR(err);
// Create GPU buffers
// Write to GPU; A (cpu) -> a_d (gpu), B (cpu) -> b_d (gpu)
for(int i = 0; i < 3; i++){
err = clEnqueueWriteBuffer(queue_m[i], a_d[i], CL_FALSE, 0, a_single_size, A + i*(a_single_size/sizeof(float)), 0, NULL, NULL);
CHECK_ERROR(err);
err = clEnqueueWriteBuffer(queue_m[i], b_d[i], CL_FALSE, 0, K * N * sizeof(float), B, 0, NULL, NULL);
CHECK_ERROR(err);
}
err = clEnqueueWriteBuffer(queue_m[3], a_d[3], CL_FALSE, 0, a_last_size, A + 3*(a_single_size/sizeof(float)), 0, NULL, NULL);
CHECK_ERROR(err);
err = clEnqueueWriteBuffer(queue_m[3], b_d[3], CL_TRUE, 0, K * N * sizeof(float), B, 0, NULL, NULL);
CHECK_ERROR(err);
// DO NOT REMOVE; NEEDED FOR TIME MEASURE
err = clFinish(queue_m[0]);
err = clFinish(queue_m[1]);
err = clFinish(queue_m[2]);
err = clFinish(queue_m[3]);
CHECK_ERROR(err);
}
void mat_mul_final(float *A, float *B, float *C, int M, int N, int K) {
// Read from GPU; c_d (gpu) -> C (cpu)
int M_single = M/4;
const size_t merge_local[2] = {32, 32};
const size_t merge_global[2] = {(size_t)((M + 31)/32*32), (size_t)((N + 31)/32*32)};
cl_kernel kernel_merge = clCreateKernel(program, "mergeResult", &err);
CHECK_ERROR(err);
err = clSetKernelArg(kernel_merge, 0, sizeof(cl_mem), (void*)&c_d_xl[0]);
err = clSetKernelArg(kernel_merge, 1, sizeof(cl_mem), (void*)&c_d_xl[1]);
err = clSetKernelArg(kernel_merge, 2, sizeof(cl_mem), (void*)&c_d_xl[2]);
err = clSetKernelArg(kernel_merge, 3, sizeof(cl_mem), (void*)&c_d_xl[3]);
err = clSetKernelArg(kernel_merge, 4, sizeof(cl_mem), (void*)&c_result);
err = clSetKernelArg(kernel_merge, 5, sizeof(int), (void*)&M_xl);
err = clSetKernelArg(kernel_merge, 6, sizeof(int), (void*)&N_xl);
err = clSetKernelArg(kernel_merge, 7, sizeof(int), (void*)&M_single);
err = clSetKernelArg(kernel_merge, 8, sizeof(int), (void*)&M);
err = clSetKernelArg(kernel_merge, 9, sizeof(int), (void*)&N);
err = clEnqueueNDRangeKernel(queue_m[0], kernel_merge, 2, NULL, merge_global, merge_local, 0, NULL, NULL);
CHECK_ERROR(err);
err = clFinish(queue_m[0]);
CHECK_ERROR(err);
err = clEnqueueReadBuffer(queue_m[0], c_result, CL_TRUE, 0, M * N * sizeof(float), C, 0, NULL, NULL);
CHECK_ERROR(err);
err = clFinish(queue_m[0]);
CHECK_ERROR(err);
return;
//float * C_xl_0 = (float*)aligned_alloc(32, M_xl * N_xl * sizeof(float));
//float * C_xl_1 = (float*)aligned_alloc(32, M_xl * N_xl * sizeof(float));
//float * C_xl_2 = (float*)aligned_alloc(32, M_xl * N_xl * sizeof(float));
//float * C_xl_3 = (float*)aligned_alloc(32, M_xl * N_xl * sizeof(float));
//err = clEnqueueReadBuffer(queue_m[0], c_d_xl[0], CL_FALSE, 0, M_xl * N_xl * sizeof(float), C_xl_0, 0, NULL, NULL);
//err = clEnqueueReadBuffer(queue_m[1], c_d_xl[1], CL_FALSE, 0, M_xl * N_xl * sizeof(float), C_xl_1, 0, NULL, NULL);
//err = clEnqueueReadBuffer(queue_m[2], c_d_xl[2], CL_FALSE, 0, M_xl * N_xl * sizeof(float), C_xl_2, 0, NULL, NULL);
//err = clEnqueueReadBuffer(queue_m[3], c_d_xl[3], CL_TRUE, 0, M_xl * N_xl * sizeof(float), C_xl_3, 0, NULL, NULL);
//CHECK_ERROR(err);
//int single_size = N*(M/4);
//for(int i = 0; i < M/4; i++){
// for(int j = 0; j < N; j++){
// C[i*N + j] = C_xl_0[i*N_xl + j];
// }
//}
//for(int i = 0; i < M/4; i++){
// for(int j = 0; j < N; j++){
// C[single_size + i*N + j] = C_xl_1[i*N_xl + j];
// }
//}
//for(int i = 0; i < M/4; i++){
// for(int j = 0; j < N; j++){
// C[2*single_size + i*N + j] = C_xl_2[i*N_xl + j];
// }
//}
//for(int i = 0; i < (M-3*(M/4)); i++){
// for(int j = 0; j < N; j++){
// C[3*single_size + i*N + j] = C_xl_3[i*N_xl + j];
// }
//}
//free(C_xl_0);
//free(C_xl_1);
//free(C_xl_2);
//free(C_xl_3);
//// DO NOT REMOVE; NEEDED FOR TIME MEASURE
//err = clFinish(queue_m[0]);
//err = clFinish(queue_m[1]);
//err = clFinish(queue_m[2]);
//err = clFinish(queue_m[3]);
//CHECK_ERROR(err);
}