chundoong-lab-ta/SamsungDS22/submissions/HW5/junjip.lee/mat_mul.cpp

332 lines
8.9 KiB
C++
Raw Normal View History

2022-09-29 18:01:45 +09:00
#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 MAX_DEV 4
static cl_int err;
static cl_platform_id platform;
static cl_device_id devices[MAX_DEV];
static cl_device_id device;
static cl_context context;
static cl_command_queue queue[MAX_DEV];
static cl_program program;
static cl_kernel kernel[MAX_DEV];
static cl_mem a_d[MAX_DEV], b_d[MAX_DEV], c_d[MAX_DEV];
#define CURRENT_DEVICE 0
int ndev;
static float *A, *B, *C;
static int M, N, K;
#define TS 32
#define WPT 8 // The amount of work-per-thread, i.e. the thread-coarsening factor
// #define TS 4
// #define WPT 2
#define RTS (TS/WPT) // The reduced tile-size in one dimension
#define KERNEL 3
#define PADDINGX 16
#define PADDINGY 16
#define TRANSPOSEX 16
#define TRANSPOSEY 16
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 i,modi_M,end_M;
// if(M%ndev!=0)
// {
// modi_M=((M/ndev)+1);
// printf("%s %d modi_M %d \n",__func__,__LINE__,modi_M);
// }
modi_M=M/ndev;
// Setup kernel arguments
for (i = 0; i < ndev; i++)
{
err = clSetKernelArg(kernel[i], 0, sizeof(cl_mem), &a_d[i]);
CHECK_ERROR(err);
err = clSetKernelArg(kernel[i], 1, sizeof(cl_mem), &b_d[i]);
CHECK_ERROR(err);
err = clSetKernelArg(kernel[i], 2, sizeof(cl_mem), &c_d[i]);
CHECK_ERROR(err);
if(i==(ndev-1))
{
end_M=M-modi_M*(ndev-1);
err = clSetKernelArg(kernel[i], 3, sizeof(int), &end_M);
CHECK_ERROR(err);
}
else
{
err = clSetKernelArg(kernel[i], 3, sizeof(int), &modi_M);
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);
}
// size_t gws[2] = {(size_t)(M+WPT - 1)/WPT/ndev, (size_t)N}, lws[2] = {TS/WPT, TS};
size_t gws[2] = {(size_t)((M/ndev)+WPT - 1)/WPT, (size_t)N}, lws[2] = {TS/WPT, TS};
// size_t gws[2] = {(size_t)(M/ndev), (size_t)N}, lws[2] = {TS, TS};
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];
}
//printf("gws %ld %ld lws %ld %ld \n",gws[0],gws[1],lws[0],lws[1]);
// Run kernel
for (i = 0; i < ndev; i++)
{
if(i==(ndev-1))
{
gws[0] = (size_t)(end_M+WPT - 1)/WPT;
for (int j = 0; j < 2; ++j) {
// 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[j] = (gws[j] + lws[j] - 1) / lws[j] * lws[j];
}
}
err = clEnqueueNDRangeKernel(queue[i], kernel[i], 2, NULL, gws, lws, 0, NULL, NULL);
CHECK_ERROR(err);
// DO NOT REMOVE; NEEDED FOR TIME MEASURE
}
for (i = 0; i < ndev; i++)
{
err = clFinish(queue[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 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, ndev, devices, "", 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) {
// Get OpenCL platform
int i;
err = clGetPlatformIDs(1, &platform, NULL);
CHECK_ERROR(err);
print_platform_info(platform);
// Get OpenCL device
err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 0, NULL, (unsigned int*) &ndev);
CHECK_ERROR(err);
//printf("%s %d ndev %d\n",__func__,__LINE__,ndev);
err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, ndev, devices, NULL);
CHECK_ERROR(err);
device = devices[CURRENT_DEVICE];
print_device_info(device);
// Create OpenCL context
context = clCreateContext(0, ndev, devices, NULL, NULL, &err);
CHECK_ERROR(err);
//if(M%4 ==0)
// ndev=4;
//else if(M%3 ==0)
// ndev=3;
// else if(M%2 ==0)
// ndev=2;
//else
// ndev=1;
// Create OpenCL command queue
//queue = clCreateCommandQueue(context, device, 0, &err);
//CHECK_ERROR(err);
for (i = 0; i < ndev; i++)
{
queue[i] = clCreateCommandQueue(context, devices[i], 0, &err);
CHECK_ERROR(err);
}
// Compile program from "kernel.cl"
// Extract kernel from compiled program
program = create_and_build_program_with_source(context, device, "kernel.cl");
for (i = 0; i < ndev; i++)
{
kernel[i] = clCreateKernel(program, "sgemm", &err);
CHECK_ERROR(err);
}
// Create GPU buffers
//printf("%s %d jjlee check ndev %d \n",__func__,__LINE__,ndev);
int modi_M=M;
if(M%ndev!=0)
{
modi_M=((M/ndev)+1)*ndev;
// printf("%s %d modi_M %d \n",__func__,__LINE__,modi_M);
}
modi_M=M/ndev;
int end_M;
for (i = 0; i < ndev; i++)
{
if(i==(ndev-1))
{
end_M=M-modi_M*(ndev-1);
a_d[i] = clCreateBuffer(context, CL_MEM_READ_WRITE, end_M * K * sizeof(float), NULL, &err);
CHECK_ERROR(err);
c_d[i] = clCreateBuffer(context, CL_MEM_READ_WRITE, end_M * N * sizeof(float), NULL, &err);
CHECK_ERROR(err);
}
else
{
a_d[i] = clCreateBuffer(context, CL_MEM_READ_WRITE, modi_M * K * sizeof(float), NULL, &err);
CHECK_ERROR(err);
c_d[i] = clCreateBuffer(context, CL_MEM_READ_WRITE, modi_M * N * sizeof(float), NULL, &err);
CHECK_ERROR(err);
}
b_d[i] = clCreateBuffer(context, CL_MEM_READ_WRITE, K * N * sizeof(float), NULL, &err);
CHECK_ERROR(err);
}
for (i = 0; i < ndev; i++)
{
// Write to GPU; A (cpu) -> a_d (gpu), B (cpu) -> b_d (gpu)
if(i==(ndev-1))
{
end_M=M-modi_M*(ndev-1);
err = clEnqueueWriteBuffer(queue[i], a_d[i], CL_TRUE, 0, end_M * K * sizeof(float),(void *) ((size_t)A+(( modi_M * K * sizeof(*A))*i)), 0, NULL, NULL);
CHECK_ERROR(err);
}
else{
err = clEnqueueWriteBuffer(queue[i], a_d[i], CL_TRUE, 0, modi_M * K * sizeof(float),(void *) ((size_t)A+(( modi_M * K * sizeof(*A))*i)), 0, NULL, NULL);
CHECK_ERROR(err);
}
err = clEnqueueWriteBuffer(queue[i], b_d[i], CL_TRUE, 0, K * N * sizeof(float), B, 0, NULL, NULL);
CHECK_ERROR(err);
}
for (i = 0; i < ndev; i++)
{
err = clFinish(queue[i]);
CHECK_ERROR(err);
}
// DO NOT REMOVE; NEEDED FOR TIME MEASURE
}
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 modi_M=M/ndev;
int end_M;
for (int i = 0; i < ndev; i++)
{
// printf("%s %d jjlee check %lf \n",__func__,__LINE__,c_d[i]);
if(i==(ndev-1))
{
end_M=M-modi_M*(ndev-1);
err = clEnqueueReadBuffer(queue[i], c_d[i], CL_TRUE, 0, end_M * N * sizeof(float),(void*) ((size_t) C+((modi_M * N * sizeof(float))*i)), 0, NULL, NULL);
CHECK_ERROR(err);
}
else
{
err = clEnqueueReadBuffer(queue[i], c_d[i], CL_TRUE, 0, modi_M * N * sizeof(float),(void*) ((size_t) C+((modi_M * N * sizeof(float))*i)), 0, NULL, NULL);
//printf("%s %d jjlee check i %d \n",__func__,__LINE__,i);
CHECK_ERROR(err);
}
}
for (int i = 0; i < ndev; i++)
{
err = clFinish(queue[i]);
CHECK_ERROR(err);
}
// DO NOT REMOVE; NEEDED FOR TIME MEASURE
}