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

264 lines
8.8 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;
static cl_device_id devices[4];
//static cl_context context;
static cl_context contexts[4];
//static cl_command_queue queue;
static cl_command_queue queues[4];
//static cl_program program;
static cl_program programs[4];
//static cl_kernel kernel;
static cl_kernel kernels[4];
//static cl_mem a_d, b_d, c_d;
static cl_mem a_d[4], b_d[4], c_d[4];
static float *A, *B, *C;
static int M, N, K;
static int CORESIZE;
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;
// Split M for Multicore
int M_split = (M / CORESIZE) + ((M % CORESIZE) > 0);
// Setup kernel arguments
//err = clSetKernelArg(kernel, 0, sizeof(cl_mem), &a_d);
//CHECK_ERROR(err);
//err = clSetKernelArg(kernel, 1, sizeof(cl_mem), &b_d);
//CHECK_ERROR(err);
//err = clSetKernelArg(kernel, 2, sizeof(cl_mem), &c_d);
//CHECK_ERROR(err);
//err = clSetKernelArg(kernel, 3, sizeof(int), &M);
//CHECK_ERROR(err);
//err = clSetKernelArg(kernel, 4, sizeof(int), &N);
//CHECK_ERROR(err);
//err = clSetKernelArg(kernel, 5, sizeof(int), &K);
//CHECK_ERROR(err);
for (int d = 0; d < CORESIZE; d++) {
err = clSetKernelArg(kernels[d], 0, sizeof(cl_mem), &a_d[d]);
CHECK_ERROR(err);
err = clSetKernelArg(kernels[d], 1, sizeof(cl_mem), &b_d[d]);
CHECK_ERROR(err);
err = clSetKernelArg(kernels[d], 2, sizeof(cl_mem), &c_d[d]);
CHECK_ERROR(err);
err = clSetKernelArg(kernels[d], 3, sizeof(int), &M_split);
CHECK_ERROR(err);
err = clSetKernelArg(kernels[d], 4, sizeof(int), &N);
CHECK_ERROR(err);
err = clSetKernelArg(kernels[d], 5, sizeof(int), &K);
CHECK_ERROR(err);
}
// Setup global work size and local work size
//size_t gws[2] = {(size_t)M, (size_t)N}, lws[2] = {1, 1};
size_t gws[2] = {(size_t)M_split, (size_t)N}, lws[2] = {2, 128};
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];
}
// Run kernel
//err = clEnqueueNDRangeKernel(queue, kernel, 2, NULL, gws, lws, 0, NULL, NULL);
//CHECK_ERROR(err);
for (int d = 0; d < CORESIZE; d++) {
err = clEnqueueNDRangeKernel(queues[d], kernels[d], 2, NULL, gws, lws, 0, NULL, NULL);
CHECK_ERROR(err);
}
// DO NOT REMOVE; NEEDED FOR TIME MEASURE
//err = clFinish(queue);
//CHECK_ERROR(err);
for (int d = 0; d < CORESIZE; d++) {
err = clFinish(queues[d]);
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;
}
void mat_mul_init(float *A, float *B, float *C, int M, int N, int K) {
// Set Core Size
CORESIZE = 4;
// 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, 1, &device, NULL); // Device 1
err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, CORESIZE, devices, NULL); // Multi device
CHECK_ERROR(err);
//print_device_info(device);
for (int d = 0; d < CORESIZE; d++) {
print_device_info(devices[d]);
}
// Create OpenCL context
//context = clCreateContext(NULL, 1, &device, NULL, NULL, &err);
//context = clCreateContext(NULL, CORESIZE, devices, NULL, NULL, &err); // Use 1 context
for (int d = 0; d < CORESIZE; d++) {
// Create OpenCL context
contexts[d] = clCreateContext(NULL, 1, &devices[d], NULL, NULL, &err);
CHECK_ERROR(err);
}
// Create OpenCL command queue
//queue = clCreateCommandQueue(context, device, 0, &err);
//CHECK_ERROR(err);
for (int d = 0; d < CORESIZE; d++) {
queues[d] = clCreateCommandQueue(contexts[d], devices[d], 0, &err);
CHECK_ERROR(err);
}
// Compile program from "kernel.cl"
//program = create_and_build_program_with_source(context, device, "kernel.cl");
for (int d = 0; d < CORESIZE; d++) {
programs[d] = create_and_build_program_with_source(contexts[d], devices[d], "kernel.cl");
}
// Extract kernel from compiled program
//kernel = clCreateKernel(program, "sgemm", &err);
//CHECK_ERROR(err);
for (int d = 0; d < CORESIZE; d++) {
kernels[d] = clCreateKernel(programs[d], "sgemm", &err);
CHECK_ERROR(err);
}
// Split M for Multicore
int M_split = (M / CORESIZE) + ((M % CORESIZE) > 0);
// Create GPU buffers
//a_d = clCreateBuffer(context, CL_MEM_READ_WRITE, M * K * sizeof(float), NULL, &err);
//CHECK_ERROR(err);
//b_d = clCreateBuffer(context, CL_MEM_READ_WRITE, M * K * sizeof(float), NULL, &err);
//CHECK_ERROR(err);
//c_d = clCreateBuffer(context, CL_MEM_READ_WRITE, M * N * sizeof(float), NULL, &err);
//CHECK_ERROR(err);
for (int d = 0; d < CORESIZE; d++) {
a_d[d] = clCreateBuffer(contexts[d], CL_MEM_READ_ONLY, M_split * K * sizeof(float), NULL, &err);
CHECK_ERROR(err);
b_d[d] = clCreateBuffer(contexts[d], CL_MEM_READ_ONLY, K * N * sizeof(float), NULL, &err);
CHECK_ERROR(err);
c_d[d] = clCreateBuffer(contexts[d], CL_MEM_READ_WRITE, M_split * N * sizeof(float), NULL, &err);
CHECK_ERROR(err);
}
// Write to GPU; A (cpu) -> a_d (gpu), B (cpu) -> b_d (gpu)
//err = clEnqueueWriteBuffer(queue, a_d, CL_TRUE, 0, M * K * sizeof(float), A, 0, NULL, NULL);
//CHECK_ERROR(err);
//err = clEnqueueWriteBuffer(queue, b_d, CL_TRUE, 0, K * N * sizeof(float), B, 0, NULL, NULL);
//CHECK_ERROR(err);
for (int d = 0; d < CORESIZE; d++) {
err = clEnqueueWriteBuffer(queues[d], a_d[d], CL_TRUE, 0, M_split * K * sizeof(float), &A[d * M_split * K], 0, NULL, NULL);
CHECK_ERROR(err);
err = clEnqueueWriteBuffer(queues[d], b_d[d], CL_TRUE, 0, K * N * sizeof(float), B, 0, NULL, NULL);
CHECK_ERROR(err);
}
// DO NOT REMOVE; NEEDED FOR TIME MEASURE
//err = clFinish(queue);
//CHECK_ERROR(err);
for (int d = 0; d < CORESIZE; d++) {
err = clFinish(queues[d]);
CHECK_ERROR(err);
}
}
void mat_mul_final(float *A, float *B, float *C, int M, int N, int K) {
// Split M for Multicore
int M_split = (M / CORESIZE) + ((M % CORESIZE) > 0);
// Read from GPU; c_d (gpu) -> C (cpu)
//err = clEnqueueReadBuffer(queue, c_d, CL_TRUE, 0, M * N * sizeof(float), C, 0, NULL, NULL);
//CHECK_ERROR(err);
for (int d = 0; d < CORESIZE; d++) {
if (d == CORESIZE - 1) {
int M_last = M - (M_split * d);
err = clEnqueueReadBuffer(queues[d], c_d[d], CL_TRUE, 0, M_last * N * sizeof(float), &C[d * M_split * N], 0, NULL, NULL);
}
else {
err = clEnqueueReadBuffer(queues[d], c_d[d], CL_TRUE, 0, M_split * N * sizeof(float), &C[d * M_split * N], 0, NULL, NULL);
}
CHECK_ERROR(err);
}
// DO NOT REMOVE; NEEDED FOR TIME MEASURE
//err = clFinish(queue);
//CHECK_ERROR(err);
for (int d = 0; d < CORESIZE; d++) {
err = clFinish(queues[d]);
CHECK_ERROR(err);
}
}