#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 NUM_GPUS 4 #define BLOCK_SIZE 16 static cl_int err; static cl_platform_id platform; static cl_device_id device[NUM_GPUS]; static cl_context context; static cl_command_queue queue[NUM_GPUS]; static cl_program program[NUM_GPUS]; static cl_kernel kernel[NUM_GPUS]; static cl_mem a_d[NUM_GPUS], b_d[NUM_GPUS], c_d[NUM_GPUS]; static cl_uint deviceCount; static int workOffset[NUM_GPUS]; static int workSize[NUM_GPUS]; static float *A, *B, *C; static int M, N, K; 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 global work size and local work size size_t gws[2] = {(size_t)N, (size_t)workSize[0]}, lws[2] = {BLOCK_SIZE, BLOCK_SIZE}; // Run kernel for (unsigned int i = 0; i < deviceCount; i++) { gws[0] = ((size_t)N + lws[0] - 1) / lws[0] * lws[0]; gws[1] = ((size_t)workSize[i] + lws[1] - 1) / lws[1] * lws[1]; err = clEnqueueNDRangeKernel(queue[i], kernel[i], 2, NULL, gws, lws, 0, NULL, NULL); CHECK_ERROR(err); } // DO NOT REMOVE; NEEDED FOR TIME MEASURE for (unsigned int i = 0; i < deviceCount; 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, 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) { // 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, 0, NULL, &deviceCount); CHECK_ERROR(err); err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, deviceCount, device, NULL); CHECK_ERROR(err); for (unsigned int i = 0; i < deviceCount; i++) { print_device_info(device[i]); } // Create OpenCL context context = clCreateContext(NULL, deviceCount, device, NULL, NULL, &err); CHECK_ERROR(err); // Create OpenCL command queue for (unsigned int i = 0; i < deviceCount; i++) { queue[i] = clCreateCommandQueue(context, device[i], 0, &err); CHECK_ERROR(err); } // Compile program from "kernel.cl" for (unsigned int i = 0; i < deviceCount; i++) { program[i] = create_and_build_program_with_source(context, device[i], "kernel.cl"); } // Extract kernel from compiled program for (unsigned int i = 0; i < deviceCount; i++) { kernel[i] = clCreateKernel(program[i], "sgemm", &err); CHECK_ERROR(err); } // Create GPU buffers int rounded_N = (N + BLOCK_SIZE - 1) / BLOCK_SIZE * BLOCK_SIZE; int sizePerGPU = M / deviceCount; cl_uint init_mem = 0; workOffset[0] = 0; for (unsigned int i = 0; i < deviceCount; i++) { // input buffer workSize[i] = (i != (deviceCount - 1)) ? sizePerGPU : (M - workOffset[i]); int rounded_M = (workSize[i] + BLOCK_SIZE - 1) / BLOCK_SIZE * BLOCK_SIZE; a_d[i] = clCreateBuffer(context, CL_MEM_READ_ONLY, rounded_M * K * sizeof(float), NULL, &err); CHECK_ERROR(err); b_d[i] = clCreateBuffer(context, CL_MEM_READ_ONLY, K * rounded_N * sizeof(float), NULL, &err); CHECK_ERROR(err); c_d[i] = clCreateBuffer(context, CL_MEM_WRITE_ONLY, rounded_M * rounded_N * sizeof(float), NULL, &err); CHECK_ERROR(err); err = clEnqueueFillBuffer(queue[i], a_d[i], &init_mem, sizeof(cl_uint), 0, rounded_M * K * sizeof(float), 0, NULL, NULL); CHECK_ERROR(err); err = clEnqueueFillBuffer(queue[i], b_d[i], &init_mem, sizeof(cl_uint), 0, K * rounded_N * sizeof(float), 0, NULL, NULL); CHECK_ERROR(err); err = clEnqueueFillBuffer(queue[i], c_d[i], &init_mem, sizeof(cl_uint), 0, rounded_M * rounded_N * sizeof(float), 0, NULL, NULL); CHECK_ERROR(err); // Write to GPU; A (cpu) -> a_d (gpu), B (cpu) -> b_d (gpu) err = clEnqueueWriteBuffer(queue[i], a_d[i], CL_TRUE, 0, workSize[i] * K * sizeof(float), &A[workOffset[i] * K], 0, NULL, NULL); CHECK_ERROR(err); for (int j = 0; j < K; j++) { err = clEnqueueWriteBuffer(queue[i], b_d[i], CL_TRUE, j * rounded_N * sizeof(float), N * sizeof(float), &B[j * N], 0, NULL, NULL); CHECK_ERROR(err); } if (i < deviceCount - 1) { workOffset[i + 1] = workOffset[i] + workSize[i]; } } // Setup kernel arguments for (unsigned int i = 0; i < deviceCount; i++) { int n = 0; err = clSetKernelArg(kernel[i], n++, sizeof(cl_mem), &a_d[i]); CHECK_ERROR(err); err = clSetKernelArg(kernel[i], n++, sizeof(cl_mem), &b_d[i]); CHECK_ERROR(err); err = clSetKernelArg(kernel[i], n++, sizeof(cl_mem), &c_d[i]); CHECK_ERROR(err); err = clSetKernelArg(kernel[i], n++, sizeof(float) * BLOCK_SIZE * BLOCK_SIZE, 0); CHECK_ERROR(err); err = clSetKernelArg(kernel[i], n++, sizeof(float) * BLOCK_SIZE * BLOCK_SIZE, 0); CHECK_ERROR(err); err = clSetKernelArg(kernel[i], n++, sizeof(int), &workSize[i]); CHECK_ERROR(err); err = clSetKernelArg(kernel[i], n++, sizeof(int), &rounded_N); CHECK_ERROR(err); err = clSetKernelArg(kernel[i], n++, sizeof(int), &K); CHECK_ERROR(err); } // DO NOT REMOVE; NEEDED FOR TIME MEASURE for (unsigned int i = 0; i < deviceCount; i++) { err = clFinish(queue[i]); CHECK_ERROR(err); } } void mat_mul_final(float *A, float *B, float *C, int M, int N, int K) { int rounded_N = (N + BLOCK_SIZE - 1) / BLOCK_SIZE * BLOCK_SIZE; // Read from GPU; c_d (gpu) -> C (cpu) for (unsigned int i = 0; i < deviceCount; i++) { for (int j = 0; j < workSize[i]; j++) { err = clEnqueueReadBuffer(queue[i], c_d[i], CL_TRUE, j * rounded_N * sizeof(float), N * sizeof(float), &C[workOffset[i] * N + j * N], 0, NULL, NULL); CHECK_ERROR(err); } } // DO NOT REMOVE; NEEDED FOR TIME MEASURE for (unsigned int i = 0; i < deviceCount; i++) { err = clFinish(queue[i]); CHECK_ERROR(err); } }