#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; #define DEVICE_NUM 4 static cl_device_id device[DEVICE_NUM]; static cl_context context; static cl_command_queue queue[DEVICE_NUM]; static cl_program program; static cl_kernel kernel; static cl_mem a_d[DEVICE_NUM], b_d[DEVICE_NUM], c_d[DEVICE_NUM]; static int M_start[DEVICE_NUM]; static int M_size[DEVICE_NUM]; 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(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); // Setup global work size and local work size // 1st method //size_t gws[2] = {(size_t)M, (size_t)N}, lws[2] = {32,32}; // 72 GFlops //size_t gws[2] = {(size_t)M, (size_t)N}, lws[2] = {16,16}; // 156 //size_t gws[2] = {(size_t)M, (size_t)N}, lws[2] = {8,8}; // 457 //size_t gws[2] = {(size_t)M, (size_t)N}, lws[2] = {4,4}; // 121 // // 2nd method //size_t gws[2] = {(size_t)M, (size_t)N}, lws[2] = {32,32}; // 680 // // 3rd method //#define TS 32 //#define WPT 8 //size_t gws[2] = {(size_t)M, (size_t)N/WPT}, lws[2] = {TS,TS/WPT}; // 1433 // // 4th method //#define TS 32 //#define WIDTH 4 //size_t gws[2] = {(size_t)M, (size_t)N/WIDTH}, lws[2] = {TS,TS/WIDTH}; // 2675 // // 5th method //#define TS 32 //#define WIDTH 8 //size_t gws[2] = {(size_t)M, (size_t)N/WIDTH}, lws[2] = {TS,TS/WIDTH}; // 3638 // // 6th method - Tile-unaligned //#define TS 32 //#define WIDTH 8 //size_t gws[2] = {(size_t)M, (size_t)(N+WIDTH-1)/WIDTH}, lws[2] = {TS,TS/WIDTH}; // ... // // 7th method - 3rd + unaligned //#define TS 32 //#define WPT 8 // printf("\n"); // size_t gws[2] = {(size_t)(M+WPT-1)/WPT, (size_t)N}, lws[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 // gws[i] = (gws[i] + lws[i] - 1) / lws[i] * lws[i]; // printf("gws%d:%d, lws%d:%d\n", i, gws[i], i, lws[i]); //} for(int d=0; d 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); // DO NOT REMOVE; NEEDED FOR TIME MEASURE err = clFinish(queue); CHECK_ERROR(err); } 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); // DO NOT REMOVE; NEEDED FOR TIME MEASURE err = clFinish(queue); CHECK_ERROR(err); }*/ 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 4 OpenCL devices err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, DEVICE_NUM, device, NULL); CHECK_ERROR(err); print_device_info(device); // Create OpenCL context context = clCreateContext(NULL, DEVICE_NUM, device, NULL, NULL, &err); CHECK_ERROR(err); // Create OpenCL command queue for(int i=0; i a_d (gpu), B (cpu) -> b_d (gpu) for(int d=0; d C (cpu) err = clEnqueueReadBuffer(queue[d], c_d[d], CL_TRUE, 0, M_size[d] * N * sizeof(float), C + M_start[d] * N, 0, NULL, NULL); CHECK_ERROR(err); } for(int d=0; d