chundoong-lab-ta/SamsungDS22/submissions/HW5/c.w.son/mat_mul.cpp

513 lines
15 KiB
C++

#include "mat_mul.h"
#include <stdio.h>
#include <CL/cl.h>
#include "util.h"
#define MAX_DEVICE_NUM 4
#define CHECK_ERROR(err) \
if (err != CL_SUCCESS) { \
printf("[%s:%d] OpenCL error %d\n", __FILE__, __LINE__, err); \
exit(EXIT_FAILURE); \
}
static int max_device_num = 4;
static cl_device_id device_arr[MAX_DEVICE_NUM];
static cl_command_queue queue_arr[MAX_DEVICE_NUM];
static cl_kernel kernel_arr[MAX_DEVICE_NUM];
static cl_mem bufferA[MAX_DEVICE_NUM];
static cl_mem bufferB[MAX_DEVICE_NUM];
static cl_mem bufferC[MAX_DEVICE_NUM];
static cl_int err;
static cl_platform_id platform;
static cl_device_id device;
static cl_context context;
static cl_command_queue queue;
static cl_program program;
static cl_kernel kernel;
static cl_mem a_d, b_d, c_d;
static float *A, *B, *C;
static int M, N, K;
/*************************************/
/* 2.x main call */
/*************************************/
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;
// 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);
*/
int size_m;
int start_m;
int end_m;
size_m = M / (max_device_num);
for(int i=0; i<max_device_num; i++){
start_m = i*size_m;
if(((i+1) == max_device_num) && ((M%(max_device_num)) != 0)){
end_m = M;
}
else{
end_m = start_m + size_m;
}
int sizeBufA = (end_m - start_m) * K;
int sizeBufC = M * N;
int size_mm = (end_m - start_m);
err = clSetKernelArg(kernel_arr[i], 0, sizeof(cl_mem), &bufferA[i]);
CHECK_ERROR(err);
err = clSetKernelArg(kernel_arr[i], 1, sizeof(cl_mem), &bufferB[i]);
CHECK_ERROR(err);
err = clSetKernelArg(kernel_arr[i], 2, sizeof(cl_mem), &bufferC[i]);
CHECK_ERROR(err);
err = clSetKernelArg(kernel_arr[i], 3, sizeof(int), &M);
CHECK_ERROR(err);
err = clSetKernelArg(kernel_arr[i], 4, sizeof(int), &N);
CHECK_ERROR(err);
err = clSetKernelArg(kernel_arr[i], 5, sizeof(int), &K);
CHECK_ERROR(err);
err = clSetKernelArg(kernel_arr[i], 6, sizeof(int), &start_m);
CHECK_ERROR(err);
err = clSetKernelArg(kernel_arr[i], 7, sizeof(int), &end_m);
CHECK_ERROR(err);
err = clSetKernelArg(kernel_arr[i], 8, sizeof(int), &i);
CHECK_ERROR(err);
}
// Setup global work size and local work size
size_t val1=16;
size_t val2=16;
size_t gws[2] = {(size_t)M, (size_t)N};
size_t lws[2] = {val1, val2};
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 = (int)(2.5) * 16 = 32
gws[i] = (gws[i] + lws[i] - 1) / lws[i] * lws[i];
//printf("%ld %ld\n", gws[i], lws[i]);
}
size_t gws_arr[4][2]= {{(size_t)M/max_device_num, (size_t)N},
{(size_t)M/max_device_num, (size_t)N},
{(size_t)M/max_device_num, (size_t)N},
{(size_t)M/max_device_num, (size_t)N} };
size_t lws_arr[4][2]= {{val1, val2},{val1, val2},{val1, val2},{val1, val2}};
//size_t gws_arr0[2]={(size_t)M/max_device_num, (size_t)N};
//size_t gws_arr1[2]={(size_t)M/max_device_num, (size_t)N};
size_t gws_arr0[2]={(size_t)M, (size_t)N};
size_t gws_arr1[2]={(size_t)M, (size_t)N};
size_t lws_arr0[2] = {32, 32};
size_t lws_arr1[2] = {32, 32};
if(M==8192 && N==8192 && K==8192){
val1 = 1, val2 = 32;
}
else{
val1 = 1, val2 = 1;
}
for (int j = 0 ;j <max_device_num; j++){
for (int i = 0; i < 2; ++i) {
size_m = M/max_device_num;
start_m = j*size_m;
if(((j+1) == max_device_num) && ((M%(max_device_num)) != 0)){
end_m = M;
}
else{
end_m = start_m + size_m;
}
size_t size_mk = (end_m - start_m);
gws_arr[j][i] = size_mk;
// 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_arr[j][i] = (gws_arr[j][i] + lws_arr[j][i] - 1) / lws_arr[j][i] * lws_arr[j][i];
//printf("%ld %ld\n", size_mk, gws_arr[j][i]);
}
}
size_t (*pointer)[2] = gws_arr;
size_t (*pointer2)[2] = lws_arr;
// Run kernel
/*
err = clEnqueueNDRangeKernel(queue, kernel, 2, NULL, gws, lws, 0, NULL, NULL);
CHECK_ERROR(err);
*/
// Modified start
for(int i=0; i<max_device_num; i++){
//err = clEnqueueNDRangeKernel(queue_arr[i], kernel_arr[i], 2, NULL, *(pointer+i), *(pointer2+i), 0, NULL, NULL);
if(i==0){
if(M==8192 && N==8192 && K==8192){
lws_arr0[0] = 1; lws_arr0[1] = 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 = (int)(2.5) * 16 = 32
gws_arr0[i] = (size_t)((gws_arr0[i] + lws_arr0[i] - 1) / lws_arr0[i]) * lws_arr0[i];
//printf("%ld %ld\n", gws_arr0[i], lws_arr0[i]);
}
}
else{
lws_arr0[0] = 1; lws_arr0[1] = 1;
}
err = clEnqueueNDRangeKernel(queue_arr[i], kernel_arr[i], 2, NULL, gws_arr0, lws_arr0, 0, NULL, NULL);
}
else{
if(M==8192 && N==8192 && K==8192){
lws_arr1[0] = 1; lws_arr1[1] = 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 = (int)(2.5) * 16 = 32
gws_arr1[i] = (size_t)((gws_arr1[i] + lws_arr1[i] - 1) / lws_arr1[i]) * lws_arr1[i];
//printf("%ld %ld\n", gws_arr1[i], lws_arr1[i]);
}
}
else{
lws_arr1[0] = 1; lws_arr1[1] = 1;
}
err = clEnqueueNDRangeKernel(queue_arr[i], kernel_arr[i], 2, NULL, gws_arr1, lws_arr1, 0, NULL, NULL);
}
//err = clEnqueueNDRangeKernel(queue_arr[i], kernel_arr[i], 2, NULL, gws, lws, 0, NULL, NULL);
CHECK_ERROR(err);
}
// Modified end
// DO NOT REMOVE; NEEDED FOR TIME MEASURE
/*
err = clFinish(queue);
CHECK_ERROR(err);
*/
for(int i=0; i<max_device_num; i++){
err = clFinish(queue_arr[i]);
CHECK_ERROR(err);
}
}
/* 1.1 main call */
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);
}
/* 1.2 main call */
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);
}
/* 1.3 main call */
static cl_program create_and_build_program_with_source(cl_context context, cl_device_id *device_arr, 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);
// Modified start
for(int i=0; i<max_device_num;i++){
err = clBuildProgram(program, 1, &device_arr[i], "", NULL, NULL);
CHECK_ERROR(err);
}
// Modified end
// 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);
// */
// // Modified start
// for(int i=0; i<max_device_num; i++){
// size_t log_size;
// CHECK_ERROR(clGetProgramBuildInfo(program, device_arr[i], CL_PROGRAM_BUILD_LOG, 0, NULL, &log_size));
// char *log = (char*)malloc(log_size + 1);
// CHECK_ERROR(clGetProgramBuildInfo(program, device_arr[i], CL_PROGRAM_BUILD_LOG, log_size, log, NULL));
// log[log_size] = 0;
// printf("Device(%d) Compile error:\n%s\n", i, log);
// free(log);
// }
// // Modified end
// }
return program;
}
/*************************************/
/* 1.x main call */
/*************************************/
void mat_mul_init(float *A, float *B, float *C, int M, int N, int K) {
if(M==8192 && N==8192 && K==8192){
max_device_num = 4;
}
else{
//max_device_num = 1;
max_device_num = 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);
CHECK_ERROR(err);
print_device_info(device);
*/
// Modified start
err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, max_device_num, device_arr, NULL);
CHECK_ERROR(err);
for(int i=0; i<max_device_num;i++){
print_device_info(device_arr[i]);
}
// Modified end
// Create OpenCL context
/*
context = clCreateContext(NULL, 1, &device, NULL, NULL, &err);
CHECK_ERROR(err);
*/
// Modified start
context = clCreateContext(NULL, max_device_num, device_arr, NULL, NULL, &err);
CHECK_ERROR(err);
// Modified end
// Create OpenCL command queue
/*
queue = clCreateCommandQueue(context, device, 0, &err);
CHECK_ERROR(err);
*/
// Modified start
for(int i=0; i<max_device_num;i++){
queue_arr[i] = clCreateCommandQueue(context, device_arr[i], 0, &err);
CHECK_ERROR(err);
}
// Modified end
// Compile program from "kernel.cl"
/*
program = create_and_build_program_with_source(context, device, "kernel.cl");
*/
// Modified start
program = create_and_build_program_with_source(context, device_arr, "kernel.cl");
// Modified end
// Extract kernel from compiled program
/*
kernel = clCreateKernel(program, "sgemm", &err);
CHECK_ERROR(err);
*/
// Modified start
for(int i=0; i<max_device_num;i++){
kernel_arr[i] = clCreateKernel(program, "sgemm", &err);
CHECK_ERROR(err);
}
// Modified end
// 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, K * N * sizeof(float), NULL, &err);
CHECK_ERROR(err);
c_d = clCreateBuffer(context, CL_MEM_READ_WRITE, M * N * sizeof(float), NULL, &err);
CHECK_ERROR(err);
*/
int size_m;
int start_m;
int end_m;
size_m = M / (max_device_num);
// Modified start
for(int i=0; i<max_device_num;i++){
start_m = i*size_m;
if(((i+1) == max_device_num) && ((M%(max_device_num)) != 0)){
end_m = M;
}
else{
end_m = start_m + size_m;
}
int sizeBufA = (end_m - start_m) * K;
//int sizeBufC = M * N;
int sizeBufC = (end_m - start_m) * N;
//bufferA[i] = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeBufA * sizeof(float), NULL, &err);
bufferA[i] = clCreateBuffer(context, CL_MEM_READ_WRITE, M * K * sizeof(float), NULL, &err);
CHECK_ERROR(err);
bufferB[i] = clCreateBuffer(context, CL_MEM_READ_WRITE, K * N * sizeof(float), NULL, &err);
CHECK_ERROR(err);
//bufferC[i] = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeBufC * sizeof(float), NULL, &err);
bufferC[i] = clCreateBuffer(context, CL_MEM_READ_WRITE, M * N * sizeof(float), NULL, &err);
CHECK_ERROR(err);
}
// Modified end
// 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);
*/
// Modified start
for(int i=0; i<max_device_num;i++){
// CL_TRUE : blocking write
start_m = i*size_m;
if(((i+1) == max_device_num) && ((M%(max_device_num)) != 0)){
end_m = M;
}
else{
end_m = start_m + size_m;
}
int sizeBufA = (end_m - start_m) * K;
int sizeBufC = (end_m - start_m) * N;
//err = clEnqueueWriteBuffer(queue_arr[i], bufferA[i], CL_TRUE, 0, sizeBufA * sizeof(float), &A[start_m*K], 0, NULL, NULL);
err = clEnqueueWriteBuffer(queue_arr[i], bufferA[i], CL_TRUE, 0, M * K * sizeof(float), A, 0, NULL, NULL);
CHECK_ERROR(err);
printf("Dev(%d) Matrix A Start \n", i);
//print_mat(A, M, K);
err = clEnqueueWriteBuffer(queue_arr[i], bufferB[i], CL_TRUE, 0, K * N * sizeof(float), B, 0, NULL, NULL);
CHECK_ERROR(err);
printf("Dev(%d) Matrix B Start\n", i);
//print_mat(B, K, N);
//err = clEnqueueWriteBuffer(queue_arr[i], bufferC[i], CL_TRUE, 0, sizeBufC * sizeof(float), &C[start_m*N], 0, NULL, NULL);
//err = clEnqueueWriteBuffer(queue_arr[i], bufferC[i], CL_TRUE, 0, M * N * sizeof(float), C, 0, NULL, NULL);
//printf("Dev(%d) Matrix C Start\n", i);
//print_mat(C, K, N);
//CHECK_ERROR(err);
}
// Modified end
// DO NOT REMOVE; NEEDED FOR TIME MEASURE
/*
err = clFinish(queue);
CHECK_ERROR(err);
*/
for(int i=0; i<max_device_num;i++){
err = clFinish(queue_arr[i]);
CHECK_ERROR(err);
}
}
/*************************************/
/* 3.x main call */
/*************************************/
void mat_mul_final(float *A, float *B, float *C, int M, int N, int K) {
// 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);
*/
int size_m;
int start_m;
int end_m;
size_m = M / (max_device_num);
for(int i=0; i<max_device_num;i++){
start_m = i*size_m;
if(((i+1) == max_device_num) && ((M%(max_device_num)) != 0)){
end_m = M;
}
else{
end_m = start_m + size_m;
}
int sizeBufA = (end_m - start_m) * K;
int sizeBufC = (end_m - start_m) * N;
err = clEnqueueReadBuffer(queue_arr[i], bufferC[i], CL_TRUE, 0, sizeBufC * sizeof(float), &C[(start_m)*N], 0, NULL, NULL);
CHECK_ERROR(err);
printf("Dev(%d) Matrix C Start, size %d \n", i, sizeBufC);
//print_mat(&C[(start_m)*N], M, N);
}
// DO NOT REMOVE; NEEDED FOR TIME MEASURE
/*
err = clFinish(queue);
CHECK_ERROR(err);
*/
for(int i=0; i<max_device_num;i++){
err = clFinish(queue_arr[i]);
CHECK_ERROR(err);
}
printf("All Matrix C Start\n");
//print_mat(C, M, N);
}