#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); \ } static cl_int err; static cl_platform_id platform; static cl_device_id device[4]; static cl_context context; static cl_command_queue queue[4]; static cl_program program1; static cl_program program2; static cl_program program3; static cl_program program4; static cl_kernel kernel1; static cl_kernel kernel2; static cl_kernel kernel3; static cl_kernel kernel4; static cl_mem a1_d, b1_d, c1_d; static cl_mem a2_d, b2_d, c2_d; static cl_mem a3_d, b3_d, c3_d; static cl_mem a4_d, b4_d, c4_d; 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 kernel arguments err = clSetKernelArg(kernel1, 0, sizeof(cl_mem), &a1_d); CHECK_ERROR(err); err = clSetKernelArg(kernel1, 1, sizeof(cl_mem), &b1_d); CHECK_ERROR(err); err = clSetKernelArg(kernel1, 2, sizeof(cl_mem), &c1_d); CHECK_ERROR(err); err = clSetKernelArg(kernel1, 3, sizeof(int), &M); CHECK_ERROR(err); err = clSetKernelArg(kernel1, 4, sizeof(int), &N); CHECK_ERROR(err); err = clSetKernelArg(kernel1, 5, sizeof(int), &K); CHECK_ERROR(err); err = clSetKernelArg(kernel2, 0, sizeof(cl_mem), &a2_d); CHECK_ERROR(err); err = clSetKernelArg(kernel2, 1, sizeof(cl_mem), &b2_d); CHECK_ERROR(err); err = clSetKernelArg(kernel2, 2, sizeof(cl_mem), &c2_d); CHECK_ERROR(err); err = clSetKernelArg(kernel2, 3, sizeof(int), &M); CHECK_ERROR(err); err = clSetKernelArg(kernel2, 4, sizeof(int), &N); CHECK_ERROR(err); err = clSetKernelArg(kernel2, 5, sizeof(int), &K); CHECK_ERROR(err); err = clSetKernelArg(kernel3, 0, sizeof(cl_mem), &a3_d); CHECK_ERROR(err); err = clSetKernelArg(kernel3, 1, sizeof(cl_mem), &b3_d); CHECK_ERROR(err); err = clSetKernelArg(kernel3, 2, sizeof(cl_mem), &c3_d); CHECK_ERROR(err); err = clSetKernelArg(kernel3, 3, sizeof(int), &M); CHECK_ERROR(err); err = clSetKernelArg(kernel3, 4, sizeof(int), &N); CHECK_ERROR(err); err = clSetKernelArg(kernel3, 5, sizeof(int), &K); CHECK_ERROR(err); err = clSetKernelArg(kernel4, 0, sizeof(cl_mem), &a4_d); CHECK_ERROR(err); err = clSetKernelArg(kernel4, 1, sizeof(cl_mem), &b4_d); CHECK_ERROR(err); err = clSetKernelArg(kernel4, 2, sizeof(cl_mem), &c4_d); CHECK_ERROR(err); err = clSetKernelArg(kernel4, 3, sizeof(int), &M); CHECK_ERROR(err); err = clSetKernelArg(kernel4, 4, sizeof(int), &N); CHECK_ERROR(err); err = clSetKernelArg(kernel4, 5, sizeof(int), &K); CHECK_ERROR(err); int m1,m2,m3,m4; int m = (M/32)*32; m1 = m/4; m2 = m1*2; m3 = m1*3; m4 = m1*4 + m%4; size_t M1,M2,M3,M4; M1 = m1; M2 = m2 - m1; M3 = m3 - m2; M4 = m4 - m3; size_t TS = 32; size_t WPT = 8; // Setup global work size and local work size // size_t gws1[2] = {M/WPT, N}, lws1[2] = {TS/WPT, TS}; size_t gws1[2] = {(size_t)M1/WPT, (size_t)N}, lws1[2] = {TS/WPT, TS}; size_t gws2[2] = {(size_t)M2/WPT, (size_t)N}, lws2[2] = {TS/WPT, TS}; size_t gws3[2] = {(size_t)M3/WPT, (size_t)N}, lws3[2] = {TS/WPT, TS}; size_t gws4[2] = {(size_t)M4/WPT, (size_t)N}, lws4[2] = {TS/WPT, 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 gws1[i] = (gws1[i] + lws1[i] - 1) / lws1[i] * lws1[i]; gws2[i] = (gws2[i] + lws2[i] - 1) / lws2[i] * lws2[i]; gws3[i] = (gws3[i] + lws3[i] - 1) / lws3[i] * lws3[i]; gws4[i] = (gws4[i] + lws3[i] - 1) / lws4[i] * lws4[i]; } // Run kernel err = clEnqueueNDRangeKernel(queue[0], kernel1, 2, NULL, gws1, lws1, 0, NULL, NULL); CHECK_ERROR(err); err = clEnqueueNDRangeKernel(queue[1], kernel2, 2, NULL, gws2, lws2, 0, NULL, NULL); CHECK_ERROR(err); err = clEnqueueNDRangeKernel(queue[2], kernel3, 2, NULL, gws3, lws3, 0, NULL, NULL); CHECK_ERROR(err); err = clEnqueueNDRangeKernel(queue[3], kernel4, 2, NULL, gws4, lws4, 0, NULL, NULL); CHECK_ERROR(err); // DO NOT REMOVE; NEEDED FOR TIME MEASURE err = clFinish(queue[0]); CHECK_ERROR(err); err = clFinish(queue[1]); CHECK_ERROR(err); err = clFinish(queue[2]); CHECK_ERROR(err); err = clFinish(queue[3]); CHECK_ERROR(err); for(int i = m4; i < M; i++){ for(int k=0; k < K; k++){ float aik = A[i*K + k]; for(int j = 0; j < N; j++){ C[i*N + j] += aik * B[k*N + j]; } } } } 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)); printf("Detected OpenCL device11: %d\n", CL_DEVICE_NAME); buf = (char*)malloc(sz); CHECK_ERROR(clGetDeviceInfo(device, CL_DEVICE_NAME, sz, buf, NULL)); printf("Detected OpenCL device: %s\n", buf); printf("Detected OpenCL device22: %d\n", CL_DEVICE_NAME); 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, 4, device, NULL); CHECK_ERROR(err); print_device_info(device[0]); print_device_info(device[1]); print_device_info(device[2]); print_device_info(device[3]); // Create OpenCL context context = clCreateContext(NULL, 4, device, NULL, NULL, &err); CHECK_ERROR(err); // Create OpenCL command queue queue[0] = clCreateCommandQueue(context, device[0], 0, &err); CHECK_ERROR(err); queue[1] = clCreateCommandQueue(context, device[1], 0, &err); CHECK_ERROR(err); queue[2] = clCreateCommandQueue(context, device[2], 0, &err); CHECK_ERROR(err); queue[3] = clCreateCommandQueue(context, device[3], 0, &err); CHECK_ERROR(err); // Compile program from "kernel.cl" program1 = create_and_build_program_with_source(context, device[0], "kernel.cl"); program2 = create_and_build_program_with_source(context, device[1], "kernel.cl"); program3 = create_and_build_program_with_source(context, device[2], "kernel.cl"); program4 = create_and_build_program_with_source(context, device[3], "kernel.cl"); // Extract kernel from compiled program kernel1 = clCreateKernel(program1, "sgemm", &err); CHECK_ERROR(err); kernel2 = clCreateKernel(program2, "sgemm", &err); CHECK_ERROR(err); kernel3 = clCreateKernel(program3, "sgemm", &err); CHECK_ERROR(err); kernel4 = clCreateKernel(program4, "sgemm", &err); CHECK_ERROR(err); int m1,m2,m3,m4; int m = (M/32)*32; m1 = m/4; m2 = m1*2; m3 = m1*3; m4 = m1*4 + m%4; // Create GPU buffers a1_d = clCreateBuffer(context, CL_MEM_READ_WRITE, m1 * K * sizeof(float), NULL, &err); CHECK_ERROR(err); b1_d = clCreateBuffer(context, CL_MEM_READ_WRITE, K * N * sizeof(float), NULL, &err); CHECK_ERROR(err); c1_d = clCreateBuffer(context, CL_MEM_READ_WRITE, m1 * N * sizeof(float), NULL, &err); CHECK_ERROR(err); a2_d = clCreateBuffer(context, CL_MEM_READ_WRITE, (m2-m1) * K * sizeof(float), NULL, &err); CHECK_ERROR(err); b2_d = clCreateBuffer(context, CL_MEM_READ_WRITE, K * N * sizeof(float), NULL, &err); CHECK_ERROR(err); c2_d = clCreateBuffer(context, CL_MEM_READ_WRITE, (m2-m1) * N * sizeof(float), NULL, &err); CHECK_ERROR(err); a3_d = clCreateBuffer(context, CL_MEM_READ_WRITE, (m3-m2) * K* sizeof(float), NULL, &err); CHECK_ERROR(err); b3_d = clCreateBuffer(context, CL_MEM_READ_WRITE, K * N * sizeof(float), NULL, &err); CHECK_ERROR(err); c3_d = clCreateBuffer(context, CL_MEM_READ_WRITE, (m3-m2) * N * sizeof(float), NULL, &err); CHECK_ERROR(err); a4_d = clCreateBuffer(context, CL_MEM_READ_WRITE, (m4-m3) * K * sizeof(float), NULL, &err); CHECK_ERROR(err); b4_d = clCreateBuffer(context, CL_MEM_READ_WRITE, K * N * sizeof(float), NULL, &err); CHECK_ERROR(err); c4_d = clCreateBuffer(context, CL_MEM_READ_WRITE, (m4-m3) * N * sizeof(float), NULL, &err); CHECK_ERROR(err); // Write to GPU; A (cpu) -> a_d (gpu), B (cpu) -> b_d (gpu) err = clEnqueueWriteBuffer(queue[0], a1_d, CL_TRUE, 0, m1 * K * sizeof(float), &A[0], 0, NULL, NULL); CHECK_ERROR(err); err = clEnqueueWriteBuffer(queue[0], b1_d, CL_TRUE, 0, K * N * sizeof(float), B, 0, NULL, NULL); CHECK_ERROR(err); err = clEnqueueWriteBuffer(queue[1], a2_d, CL_TRUE, 0, (m2-m1) * K * sizeof(float), &A[m1*K], 0, NULL, NULL); CHECK_ERROR(err); err = clEnqueueWriteBuffer(queue[1], b2_d, CL_TRUE, 0, K * N * sizeof(float), B, 0, NULL, NULL); CHECK_ERROR(err); err = clEnqueueWriteBuffer(queue[2], a3_d, CL_TRUE, 0, (m3-m2) * K * sizeof(float), &A[m2*K], 0, NULL, NULL); CHECK_ERROR(err); err = clEnqueueWriteBuffer(queue[2], b3_d, CL_TRUE, 0, K * N * sizeof(float), B, 0, NULL, NULL); CHECK_ERROR(err); err = clEnqueueWriteBuffer(queue[3], a4_d, CL_TRUE, 0, (m4-m3) * K * sizeof(float), &A[m3*K], 0, NULL, NULL); CHECK_ERROR(err); err = clEnqueueWriteBuffer(queue[3], b4_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[0]); CHECK_ERROR(err); err = clFinish(queue[1]); CHECK_ERROR(err); err = clFinish(queue[2]); CHECK_ERROR(err); err = clFinish(queue[3]); CHECK_ERROR(err); } void mat_mul_final(float *A, float *B, float *C, int M, int N, int K) { int m1,m2,m3,m4; int m = (M/32)*32; m1 = m/4; m2 = m1*2; m3 = m1*3; m4 = m1*4 + m%4; // Read from GPU; c_d (gpu) -> C (cpu) err = clEnqueueReadBuffer(queue[0], c1_d, CL_TRUE, 0, m1 * N * sizeof(float), &C[0], 0, NULL, NULL); CHECK_ERROR(err); err = clEnqueueReadBuffer(queue[1], c2_d, CL_TRUE, 0, (m2-m1) * N * sizeof(float), &C[m1*N], 0, NULL, NULL); CHECK_ERROR(err); err = clEnqueueReadBuffer(queue[2], c3_d, CL_TRUE, 0, (m3-m2) * N * sizeof(float), &C[m2*N], 0, NULL, NULL); CHECK_ERROR(err); err = clEnqueueReadBuffer(queue[3], c4_d, CL_TRUE, 0, (m4-m3) * N * sizeof(float), &C[m3*N], 0, NULL, NULL); CHECK_ERROR(err); // DO NOT REMOVE; NEEDED FOR TIME MEASURE err = clFinish(queue[0]); CHECK_ERROR(err); err = clFinish(queue[1]); CHECK_ERROR(err); err = clFinish(queue[2]); CHECK_ERROR(err); err = clFinish(queue[3]); CHECK_ERROR(err); }