277 lines
8.1 KiB
C++
277 lines
8.1 KiB
C++
|
#include "mat_mul.h"
|
||
|
|
||
|
#include <stdio.h>
|
||
|
#include <CL/cl.h>
|
||
|
#include "util.h"
|
||
|
|
||
|
#define MAT_COPY (0)
|
||
|
|
||
|
#define CHECK_ERROR(err) \
|
||
|
if (err != CL_SUCCESS) { \
|
||
|
printf("[%s:%d] OpenCL error %d\n", __FILE__, __LINE__, err); \
|
||
|
exit(EXIT_FAILURE); \
|
||
|
}
|
||
|
|
||
|
#define ALIGN_UP(_X, _Y) (((_X) + (_Y) - 1) & ~((_Y) - 1))
|
||
|
#define MAX_DEV (4)
|
||
|
|
||
|
static cl_int err;
|
||
|
static cl_platform_id platform;
|
||
|
static cl_device_id device[MAX_DEV];
|
||
|
static cl_context context;
|
||
|
static cl_command_queue queue[MAX_DEV];
|
||
|
static cl_program program[MAX_DEV];
|
||
|
static cl_kernel kernel[MAX_DEV];
|
||
|
static cl_mem a_d[MAX_DEV], b_d[MAX_DEV], c_d[MAX_DEV];
|
||
|
|
||
|
static float *A, *B, *C;
|
||
|
static int M, N, K;
|
||
|
static int NON_OPTIMAL;
|
||
|
static int nDevCnt;
|
||
|
#if (MAT_COPY)
|
||
|
static float *A_backup, *B_backup, *C_backup;
|
||
|
static int M_backup, N_backup, K_backup;
|
||
|
#endif // MAT_COPY
|
||
|
#define NUM_WORK_ITEM (32)
|
||
|
#define VECTOR_WIDTH (16)
|
||
|
#define USING_NON_VECTOR (1)
|
||
|
|
||
|
#if (MAT_COPY)
|
||
|
static void mat_copy(float* __restrict pfDst, float* __restrict pfSrc, int nXDsize, int nYDsize, int nXSsize, int nYSsize, int nAddPadding)
|
||
|
{
|
||
|
if (nAddPadding)
|
||
|
{
|
||
|
#pragma omp parallel for
|
||
|
for (int i = 0; i < nXDsize; i++)
|
||
|
{
|
||
|
for (int j = 0; j < nYDsize; j++)
|
||
|
{
|
||
|
if (i >= nXSsize || j >= nYSsize)
|
||
|
{
|
||
|
*(pfDst + i * nXDsize + j) = 0.0f;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
*(pfDst + i * nXDsize + j) =*(pfSrc + i * nXSsize + j);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
#pragma omp parallel for
|
||
|
for (int i = 0; i < nXDsize; i++)
|
||
|
{
|
||
|
for (int j = 0; j < nYDsize; j++)
|
||
|
{
|
||
|
*(pfDst + i * nXSsize + j) =*(pfSrc + i * nXDsize + j);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
#endif // MAT_COPY
|
||
|
|
||
|
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;
|
||
|
|
||
|
|
||
|
if (_M % (NUM_WORK_ITEM * nDevCnt) != 0
|
||
|
|| _N % NUM_WORK_ITEM != 0
|
||
|
|| _K % NUM_WORK_ITEM != 0)
|
||
|
{
|
||
|
NON_OPTIMAL = 1;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
NON_OPTIMAL = 0;
|
||
|
}
|
||
|
|
||
|
// Setup kernel arguments
|
||
|
for (int i = 0; i < nDevCnt; i++)
|
||
|
{
|
||
|
const int nLocalM = (i == (nDevCnt - 1)) ? M - ((M / nDevCnt) * (nDevCnt - 1)) : (M / nDevCnt);
|
||
|
|
||
|
err = clSetKernelArg(kernel[i], 0, sizeof(cl_mem), &a_d[i]);
|
||
|
CHECK_ERROR(err);
|
||
|
err = clSetKernelArg(kernel[i], 1, sizeof(cl_mem), &b_d[i]);
|
||
|
CHECK_ERROR(err);
|
||
|
err = clSetKernelArg(kernel[i], 2, sizeof(cl_mem), &c_d[i]);
|
||
|
CHECK_ERROR(err);
|
||
|
err = clSetKernelArg(kernel[i], 3, sizeof(int), &nLocalM);
|
||
|
CHECK_ERROR(err);
|
||
|
err = clSetKernelArg(kernel[i], 4, sizeof(int), &N);
|
||
|
CHECK_ERROR(err);
|
||
|
err = clSetKernelArg(kernel[i], 5, sizeof(int), &K);
|
||
|
CHECK_ERROR(err);
|
||
|
err = clSetKernelArg(kernel[i], 6, sizeof(int), &NON_OPTIMAL);
|
||
|
CHECK_ERROR(err);
|
||
|
|
||
|
|
||
|
// Setup global work size and local work size
|
||
|
size_t gws[2] , lws[2];
|
||
|
#if (!USING_NON_VECTOR)
|
||
|
if (NON_OPTIMAL == 0)
|
||
|
{
|
||
|
gws[0] = (size_t)nLocalM;
|
||
|
gws[1] = (size_t)N / VECTOR_WIDTH;
|
||
|
lws[0] = NUM_WORK_ITEM;
|
||
|
lws[1] = NUM_WORK_ITEM / VECTOR_WIDTH;
|
||
|
}
|
||
|
else
|
||
|
#endif
|
||
|
{
|
||
|
gws[0] = (size_t)ALIGN_UP(nLocalM, VECTOR_WIDTH) / VECTOR_WIDTH;
|
||
|
gws[1] = (size_t)ALIGN_UP(N, NUM_WORK_ITEM);
|
||
|
lws[0] = NUM_WORK_ITEM / VECTOR_WIDTH;
|
||
|
lws[1] = NUM_WORK_ITEM;
|
||
|
}
|
||
|
|
||
|
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",gws[0],gws[1],lws[0],lws[1]);
|
||
|
// Run kernel
|
||
|
err = clEnqueueNDRangeKernel(queue[i], kernel[i], 2, NULL, gws, lws, 0, NULL, NULL);
|
||
|
CHECK_ERROR(err);
|
||
|
}
|
||
|
|
||
|
// DO NOT REMOVE; NEEDED FOR TIME MEASURE
|
||
|
for (int i = 0; i < nDevCnt; 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, (cl_uint*)&nDevCnt);
|
||
|
err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, (cl_uint)nDevCnt, device, NULL);
|
||
|
CHECK_ERROR(err);
|
||
|
// for (int i = 0; i < nDevCnt; i++)
|
||
|
{
|
||
|
print_device_info(device[0]);
|
||
|
}
|
||
|
|
||
|
// Create OpenCL context
|
||
|
context = clCreateContext(NULL, (cl_uint)nDevCnt, device, NULL, NULL, &err);
|
||
|
CHECK_ERROR(err);
|
||
|
|
||
|
// Create OpenCL command queue
|
||
|
for (int i = 0; i < nDevCnt; i++)
|
||
|
{
|
||
|
int nLocalM = (i == (nDevCnt - 1)) ? M - ((M / nDevCnt) * (nDevCnt - 1)) : (M / nDevCnt);
|
||
|
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");
|
||
|
|
||
|
// Extract kernel from compiled program
|
||
|
kernel[i] = clCreateKernel(program[i], "sgemm", &err);
|
||
|
CHECK_ERROR(err);
|
||
|
|
||
|
// Create GPU buffers
|
||
|
a_d[i] = clCreateBuffer(context, CL_MEM_READ_WRITE, nLocalM * K * sizeof(float), NULL, &err);
|
||
|
CHECK_ERROR(err);
|
||
|
b_d[i] = clCreateBuffer(context, CL_MEM_READ_WRITE, K * N * sizeof(float), NULL, &err);
|
||
|
CHECK_ERROR(err);
|
||
|
c_d[i] = clCreateBuffer(context, CL_MEM_READ_WRITE, nLocalM * N * 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_TRUE, 0, nLocalM * K * sizeof(float), &A[i * (M / nDevCnt) * K], 0, NULL, NULL);
|
||
|
CHECK_ERROR(err);
|
||
|
err = clEnqueueWriteBuffer(queue[i], b_d[i], CL_TRUE, 0, K * N * sizeof(float), B, 0, NULL, NULL);
|
||
|
CHECK_ERROR(err);
|
||
|
}
|
||
|
|
||
|
// DO NOT REMOVE; NEEDED FOR TIME MEASURE
|
||
|
for (int i = 0; i < nDevCnt; i++)
|
||
|
{
|
||
|
err = clFinish(queue[i]);
|
||
|
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)
|
||
|
for (int i = 0; i < nDevCnt; i++)
|
||
|
{
|
||
|
int nLocalM = (i == (nDevCnt - 1)) ? M - ((M / nDevCnt) * (nDevCnt - 1)) : (M / nDevCnt);
|
||
|
err = clEnqueueReadBuffer(queue[i], c_d[i], CL_TRUE, 0, nLocalM * N * sizeof(float), &C[i * (M / nDevCnt) * N], 0, NULL, NULL);
|
||
|
CHECK_ERROR(err);
|
||
|
}
|
||
|
|
||
|
for (int i = 0; i < nDevCnt; i++)
|
||
|
{
|
||
|
// DO NOT REMOVE; NEEDED FOR TIME MEASURE
|
||
|
err = clFinish(queue[i]);
|
||
|
CHECK_ERROR(err);
|
||
|
}
|
||
|
}
|