chundoong-lab-ta/SamsungDS22/submissions/HW5/sung_min.kim/mat_mul.cpp

227 lines
7.4 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); \
}
#define TS 64
#define WPT 32
#define RTS (TS/WPT)
#define ENTRYNUM 4
// static int num_threads;
static cl_int err;
static cl_platform_id platform;
static cl_device_id *devices;
static cl_context context;
static cl_command_queue *queues;
static cl_program program;
static cl_kernel *kernel;
static cl_mem *a_ds, *b_ds, *c_ds;
static cl_uint num_of_gpu;
static int M, N, K;
int split[ENTRYNUM];
void mat_mul(float *_A, float *_B, float *_C, int _M, int _N, int _K) {
M = _M, N = _N, K = _K;
int i;
for(i=0;i<(int)num_of_gpu;i++){
// Setup kernel arguments
err = clSetKernelArg(kernel[i], 0, sizeof(cl_mem), &(a_ds[i]));
CHECK_ERROR(err);
err = clSetKernelArg(kernel[i], 1, sizeof(cl_mem), &(b_ds[i]));
CHECK_ERROR(err);
err = clSetKernelArg(kernel[i], 2, sizeof(cl_mem), &(c_ds[i]));
CHECK_ERROR(err);
err = clSetKernelArg(kernel[i], 3, sizeof(int), &split[i]);
CHECK_ERROR(err);
err = clSetKernelArg(kernel[i], 4, sizeof(int), &N);
CHECK_ERROR(err);
err = clSetKernelArg(kernel[i], 5, sizeof(int), &K);
CHECK_ERROR(err);
// Setup global work size and local work size
size_t gws[2] = {(size_t)(split[i]/WPT + ((split[i]%WPT)?1:0)), (size_t)N}, lws[2] = {RTS, TS};
/* debug */// printf("\ngws[0] = %ld, gws[1] = %ld\n", gws[0], gws[1]);
for (int i = 0; i < 2; ++i) {
gws[i] = (gws[i] + lws[i] - 1) / lws[i] * lws[i];
/* debug */// printf("gws[%d] = %ld\n", i, gws[i]);
}
// Run kernel
err = clEnqueueNDRangeKernel(queues[i], kernel[i], 2, NULL, gws, lws, 0, NULL, NULL);
CHECK_ERROR(err);
}
// DO NOT REMOVE; NEEDED FOR TIME MEASURE
for(i=0;i<(int)num_of_gpu;i++){
err = clFinish(queues[i]);
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 *devices, 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, num_of_gpu, devices, "", NULL, NULL);
if (err == CL_BUILD_PROGRAM_FAILURE) {
size_t log_size;
CHECK_ERROR(clGetProgramBuildInfo(program, devices[0], CL_PROGRAM_BUILD_LOG, 0, NULL, &log_size));
char *log = (char*)malloc(log_size + 1);
CHECK_ERROR(clGetProgramBuildInfo(program, devices[0], 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) {
int i;
devices = (cl_device_id *)malloc(sizeof(cl_device_id)*ENTRYNUM);
queues = (cl_command_queue *)malloc(sizeof(cl_command_queue)*ENTRYNUM);
a_ds = (cl_mem *)malloc(sizeof(cl_mem)*ENTRYNUM);
b_ds = (cl_mem *)malloc(sizeof(cl_mem)*ENTRYNUM);
c_ds = (cl_mem *)malloc(sizeof(cl_mem)*ENTRYNUM);
// Get OpenCL platform
err = clGetPlatformIDs(1, &platform, NULL);
CHECK_ERROR(err);
print_platform_info(platform);
// Get OpenCL device
err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, ENTRYNUM, devices, &num_of_gpu);
CHECK_ERROR(err);
/* debug */// printf("num_of_gpu: %d\n", num_of_gpu);
for(i=0;i<(int)num_of_gpu;i++){
printf("%d: ", i);
print_device_info(devices[i]);
}
// Create OpenCL context
context = clCreateContext(NULL, num_of_gpu, devices, NULL, NULL, &err);
CHECK_ERROR(err);
// Create OpenCL command queue
for(i=0;i<(int)num_of_gpu;i++){
queues[i] = clCreateCommandQueue(context, devices[i], 0, &err);
CHECK_ERROR(err);
}
// Compile program from "kernel.cl"
program = create_and_build_program_with_source(context, devices, "kernel.cl");
// Extract kernel from compiled program
kernel = (cl_kernel *)malloc(sizeof(cl_kernel)*ENTRYNUM);
for(i=0;i<(int)num_of_gpu;i++){
kernel[i] = clCreateKernel(program, "sgemm", &err);
}
// kernel = clCreateKernel(program, "sgemm", &err);
CHECK_ERROR(err);
// Create GPU buffers
int r = M%num_of_gpu;
int q = M/num_of_gpu + (r?1:0);
/* debug */// printf("q = %d, r = %d\n", q, r);
for(i=0;i<(int)num_of_gpu;i++){
if(q*(i+1)<M)
split[i] = q;
else
split[i] = M-q*i<=0?1:M-q*i;
/* debug */// printf("split[%d] = %d\n", i, split[i]);
}
int sum = 0;
for(i=0;i<(int)num_of_gpu;i++){
b_ds[i] = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, K * N * sizeof(float), B, &err);
CHECK_ERROR(err);
if(sum+split[i]>M){
/* debug */// printf("a_ds[%d] dummy\n", i);
a_ds[i] = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, split[i] * K * sizeof(float), A, &err);
CHECK_ERROR(err);
}else{
a_ds[i] = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, split[i] * K * sizeof(float), A+sum*K, &err);
CHECK_ERROR(err);
}
sum += split[i];
c_ds[i] = clCreateBuffer(context, CL_MEM_WRITE_ONLY, split[i] * N * sizeof(float), NULL, &err);
CHECK_ERROR(err);
}
// DO NOT REMOVE; NEEDED FOR TIME MEASURE
for(i=0;i<(int)num_of_gpu;i++){
err = clFinish(queues[i]);
CHECK_ERROR(err);
}
}
void mat_mul_final(float *A, float *B, float *C, int M, int N, int K) {
int i;
int sum = 0;
// Read from GPU; c_d (gpu) -> C (cpu)
for(i=0;i<(int)num_of_gpu;i++){
err = clEnqueueReadBuffer(queues[i], c_ds[i], CL_TRUE, 0, split[i] * N * sizeof(float), C+sum*N, 0, NULL, NULL);
CHECK_ERROR(err);
sum+=split[i];
if(sum>M){
/* debug */// printf("Final %d: break!\n", i);
break;
}
}
// DO NOT REMOVE; NEEDED FOR TIME MEASURE
for(i=0;i<(int)num_of_gpu;i++){
err = clFinish(queues[i]);
CHECK_ERROR(err);
}
}