#include "mat_mul.h" #include #include #define CHECK_ERROR(err) \ if (err != CL_SUCCESS) { \ printf("[%s:%d] OpenCL error %d\n", __FILE__, __LINE__, err); \ exit(EXIT_FAILURE); \ } #define MAX_DEVICES 4 #define TS 32 #define WPT 32 #define TSM TS #define TSN TS #define TSK TS #define CEIL_DIV(x,y) (((x) + (y) - 1) / (y)) #define PADDINGX TS #define PADDINGY TS static cl_int err; static cl_platform_id platform; static cl_device_id device[MAX_DEVICES]; static cl_context context; static cl_command_queue queue[MAX_DEVICES]; static cl_program program[MAX_DEVICES]; static cl_kernel padakernel[MAX_DEVICES], padbkernel[MAX_DEVICES], kernel[MAX_DEVICES], removekernel[MAX_DEVICES]; static cl_mem a_d[MAX_DEVICES], b_d[MAX_DEVICES], c_d[MAX_DEVICES]; static cl_mem a_ad[MAX_DEVICES], b_ad[MAX_DEVICES], c_ad[MAX_DEVICES]; static float *A, *B, *C; static int M, N, K; static int K_XL; static int M_XL[MAX_DEVICES]; static int N_XL; static int startM[MAX_DEVICES], endM[MAX_DEVICES]; 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_size; size_t gws[2] = {(size_t)(M + WPT - 1)/ WPT, (size_t)N}, lws[2] = {TS/WPT, TS}; for (int k = 0; k < MAX_DEVICES; k++) { m_size = endM[k] - startM[k]; // Setup kernel arguments for adding zero padding of A err = clSetKernelArg(padakernel[k], 0, sizeof(int), &m_size); CHECK_ERROR(err); err = clSetKernelArg(padakernel[k], 1, sizeof(int), &K); CHECK_ERROR(err); err = clSetKernelArg(padakernel[k], 2, sizeof(cl_mem), &a_d[k]); CHECK_ERROR(err); err = clSetKernelArg(padakernel[k], 3, sizeof(int), &M_XL[k]); CHECK_ERROR(err); err = clSetKernelArg(padakernel[k], 4, sizeof(int), &K_XL); CHECK_ERROR(err); err = clSetKernelArg(padakernel[k], 5, sizeof(cl_mem), &a_ad[k]); CHECK_ERROR(err); // Setup kernel arguments for adding zero padding of B err = clSetKernelArg(padbkernel[k], 0, sizeof(int), &K); CHECK_ERROR(err); err = clSetKernelArg(padbkernel[k], 1, sizeof(int), &N); CHECK_ERROR(err); err = clSetKernelArg(padbkernel[k], 2, sizeof(cl_mem), &b_d[k]); CHECK_ERROR(err); err = clSetKernelArg(padbkernel[k], 3, sizeof(int), &K_XL); CHECK_ERROR(err); err = clSetKernelArg(padbkernel[k], 4, sizeof(int), &N_XL); CHECK_ERROR(err); err = clSetKernelArg(padbkernel[k], 5, sizeof(cl_mem), &b_ad[k]); CHECK_ERROR(err); // Setup kernel arguments for calculating matrix multiplication err = clSetKernelArg(kernel[k], 0, sizeof(cl_mem), &a_ad[k]); CHECK_ERROR(err); err = clSetKernelArg(kernel[k], 1, sizeof(cl_mem), &b_ad[k]); CHECK_ERROR(err); err = clSetKernelArg(kernel[k], 2, sizeof(cl_mem), &c_ad[k]); CHECK_ERROR(err); err = clSetKernelArg(kernel[k], 3, sizeof(int), &M_XL[k]); CHECK_ERROR(err); err = clSetKernelArg(kernel[k], 4, sizeof(int), &N_XL); CHECK_ERROR(err); err = clSetKernelArg(kernel[k], 5, sizeof(int), &K_XL); CHECK_ERROR(err); // Setup kernel arguments for removing zero padding of C err = clSetKernelArg(removekernel[k], 0, sizeof(int), &M_XL[k]); CHECK_ERROR(err); err = clSetKernelArg(removekernel[k], 1, sizeof(int), &N_XL); CHECK_ERROR(err); err = clSetKernelArg(removekernel[k], 2, sizeof(cl_mem), &c_ad[k]); CHECK_ERROR(err); err = clSetKernelArg(removekernel[k], 3, sizeof(int), &m_size); CHECK_ERROR(err); err = clSetKernelArg(removekernel[k], 4, sizeof(int), &N); CHECK_ERROR(err); err = clSetKernelArg(removekernel[k], 5, sizeof(cl_mem), &c_d[k]); CHECK_ERROR(err); size_t agws[2] = {(size_t)M_XL[k], (size_t)K_XL}, alws[2] = {PADDINGX, PADDINGY}; // Run kernel for adding zero padding of A err = clEnqueueNDRangeKernel(queue[k], padakernel[k], 2, NULL, agws, alws, 0, NULL, NULL); CHECK_ERROR(err); size_t bgws[2] = {(size_t)K_XL, (size_t)N_XL}, blws[2] = {PADDINGX, PADDINGY}; // Run kernel for adding zero padding of B err = clEnqueueNDRangeKernel(queue[k], padbkernel[k], 2, NULL, bgws, blws, 0, NULL, NULL); CHECK_ERROR(err); // Setup global work size and local work size gws[0] = (size_t)(m_size + WPT - 1)/ WPT; 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 for calculating matrix multiplication err = clEnqueueNDRangeKernel(queue[k], kernel[k], 2, NULL, gws, lws, 0, NULL, NULL); CHECK_ERROR(err); size_t cgws[2] = {(size_t)M_XL[k], (size_t)N_XL}, clws[2] = {PADDINGX, PADDINGY}; // Run kernel for removing zero padding of C err = clEnqueueNDRangeKernel(queue[k], removekernel[k], 2, NULL, cgws, clws, 0, NULL, NULL); CHECK_ERROR(err); // DO NOT REMOVE; NEEDED FOR TIME MEASURE err = clFinish(queue[k]); 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_n_program_with_source(cl_context context, cl_device_id device, int ndev, 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) { int slice = M / MAX_DEVICES, m_size; K_XL = CEIL_DIV(K, TSK) * TSK; N_XL = CEIL_DIV(N, TSN) * TSN; // 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, MAX_DEVICES, device, NULL); CHECK_ERROR(err); // Create OpenCL context context = clCreateContext(NULL, MAX_DEVICES, device, NULL, NULL, &err); CHECK_ERROR(err); for (int i = 0; i < MAX_DEVICES; i++) { print_device_info(device[i]); // Create OpenCL command queue queue[i] = clCreateCommandQueue(context, device[i], 0, &err); CHECK_ERROR(err); // Compile program from "kernel.cl" program[i] = create_and_build_program_with_source(context, device[i], "kernel.cl"); padakernel[i] = clCreateKernel(program[i], "paddingAddZeroes", &err); CHECK_ERROR(err); padbkernel[i] = clCreateKernel(program[i], "paddingAddZeroes", &err); CHECK_ERROR(err); // Extract kernel from compiled program kernel[i] = clCreateKernel(program[i], "sgemm", &err); CHECK_ERROR(err); removekernel[i] = clCreateKernel(program[i], "paddingRemoveZeroes", &err); CHECK_ERROR(err); startM[i] = i * slice; endM[i] = (i == MAX_DEVICES - 1) ? M : (i + 1) * slice; m_size = endM[i] - startM[i]; M_XL[i] = CEIL_DIV(m_size, TSM) * TSM; // Create GPU buffers a_d[i] = clCreateBuffer(context, CL_MEM_READ_ONLY, m_size * K * sizeof(float), NULL, &err); CHECK_ERROR(err); b_d[i] = clCreateBuffer(context, CL_MEM_READ_ONLY, K * N * sizeof(float), NULL, &err); CHECK_ERROR(err); c_d[i] = clCreateBuffer(context, CL_MEM_READ_WRITE, m_size * N * sizeof(float), NULL, &err); CHECK_ERROR(err); a_ad[i] = clCreateBuffer(context, CL_MEM_READ_ONLY, M_XL[i] * K_XL * sizeof(float), NULL, &err); CHECK_ERROR(err); b_ad[i] = clCreateBuffer(context, CL_MEM_READ_ONLY, K_XL * N_XL * sizeof(float), NULL, &err); CHECK_ERROR(err); c_ad[i] = clCreateBuffer(context, CL_MEM_READ_WRITE, M_XL[i] * N_XL * sizeof(float), NULL, &err); CHECK_ERROR(err); // Write to GPU; A (cpu) -> a_d (gpu), B (cpu) -> b_d (gpu) err = clEnqueueWriteBuffer(queue[i], a_d[i], CL_FALSE, 0, m_size * K * sizeof(float), &A[startM[i] * K], 0, NULL, NULL); CHECK_ERROR(err); err = clEnqueueWriteBuffer(queue[i], b_d[i], CL_FALSE, 0, K * N * sizeof(float), B, 0, NULL, NULL); CHECK_ERROR(err); } for (int i = 0; i < MAX_DEVICES; i++) { // DO NOT REMOVE; NEEDED FOR TIME MEASURE err = clFinish(queue[i]); CHECK_ERROR(err); } } void mat_mul_final(float *A, float *B, float *C, int M, int N, int K) { int m_size; for (int i = 0; i < MAX_DEVICES; i++) { m_size = endM[i] - startM[i]; // Read from GPU; c_d (gpu) -> C (cpu) err = clEnqueueReadBuffer(queue[i], c_d[i], CL_FALSE, 0, m_size * N * sizeof(float), &C[startM[i] * N], 0, NULL, NULL); CHECK_ERROR(err); } for (int i = 0; i < MAX_DEVICES; i++) { // DO NOT REMOVE; NEEDED FOR TIME MEASURE err = clFinish(queue[i]); CHECK_ERROR(err); } }