当我试图增加矩阵大小时,在AMD openCL/C中实现矩阵向量乘法会导致系统冻结



对于大学的一个项目,我正在使用AMD OpenCL实现矩阵向量乘法。我使用的机器是一台运行Ubuntu 12.04的全新台式机,配有Radeon HD 7970和AMD FX-4100四核处理器。I AMD APP 1.2和Radeon的最新ATI Catalyst驱动程序。这是我尝试使用的内核。

__kernel void mvKernel(__global float* a, const __global float* x, __global float* y, int m, int n)
{
float sum = 0.0f;
__global float* A;
int i;
int j = 0;
int indx = get_global_id(0);
__local float xs[2048];
for(i = get_local_id(0); i < n; i+= get_local_size(0)) {
xs[i] = x[i];
} 
mem_fence(CLK_LOCAL_MEM_FENCE|CLK_GLOBAL_MEM_FENCE);
A = &a[indx];
for(i = 0; i < n; i++) {
sum += xs[i] * A[j];
j += m;
}
y[indx] = sum;
}

当在矩阵大小为256 x 256的GPU上运行时,生成的结果是正确的,不会出现任何问题。然而,当我试图增加矩阵大小时,作为命令行参数,系统将挂起,需要重新启动。然而,当我使用AMD的CodeXL调试器/探查器运行代码时,代码大部分时间都会运行,没有错误。这是我运行的主机代码

#include <stdio.h>
#include <stdlib.h>
#include <CL/cl.h>
#include <math.h>
#include <string.h>
char* readSource(const char* sourceFilename);
void randomInit(float* data, int size)
{
int i =0;
for(i; i < size; i++)
data[i] = (rand()/(float)RAND_MAX) * 10;
}
void cpuMV (float* y, float* A, float* X, int M, int N)
{
for(int i = 0; i< M; i++) {
double sum = 0;
y[i] = 0;
for(int k = 0; k < N; k++) {
double a = A[i + k* M];
double x = X[k];
sum += a * x;
}
y[i] = (float) sum;
}
}
int main( int argc, char ** argv) {
int M = atoi(argv[1]);//1024;
int N = atoi(argv[2]);//1024;
float *A, *x;
float *y;
A = (float *)malloc(sizeof(float) * M * N);
x = (float *)malloc(sizeof(float) * N);
y = (float *)malloc(sizeof(float) * M);
randomInit(A, M * N);
randomInit(x, N);
int wrong;
wrong = 0;  
cl_int err;
cl_uint numPlatforms;
cl_platform_id *platforms;
err = clGetPlatformIDs(0, NULL, &numPlatforms);
if (err != CL_SUCCESS) {
printf("clGetPlatformIDs failedn");
exit(-1);
}
if(numPlatforms == 0) {
printf("No platforms detected.n");
exit(-1);   
}
platforms = (cl_platform_id*)malloc(numPlatforms*sizeof(cl_platform_id));
clGetPlatformIDs(numPlatforms, platforms, NULL);
printf("%u platforms foundn", numPlatforms);
for(int i =0; i < numPlatforms; i++) {
char buff[100];
printf("Platform %u:n", i);
err = clGetPlatformInfo(platforms[i], CL_PLATFORM_VENDOR, sizeof(buff), buff, NULL);
printf("tVendor: %sn", buff);
err = clGetPlatformInfo(platforms[i], CL_PLATFORM_NAME, sizeof(buff), buff, NULL);
printf("tName: %sn", buff);
if (err != CL_SUCCESS) {
printf("clGetPlatformInfo failedn");
exit(-1);
}
}
printf("n");
cl_uint numDevices = 0;
cl_device_id *devices;
err = clGetDeviceIDs(platforms[0], CL_DEVICE_TYPE_GPU, 0, NULL, &numDevices);
if(err != CL_SUCCESS) {
printf("clGetDeviceIDs failedn");
exit(-1);
}
if (numDevices == 0){
printf("No devices foundn");
exit(-1);
}
devices = (cl_device_id*)malloc(numDevices*sizeof(cl_device_id));
err = clGetDeviceIDs(platforms[0], CL_DEVICE_TYPE_GPU, numDevices, devices, NULL);
printf("%u devices foundn", numDevices);
for(int i =0; i < numDevices; i++) {
char buff[100];
printf("Device %u:n", i);
err = clGetDeviceInfo(devices[i], CL_DEVICE_VENDOR, sizeof(buff), buff, NULL);
printf("tVendor: %sn", buff);
err = clGetDeviceInfo(devices[i], CL_DEVICE_NAME, sizeof(buff), buff, NULL);
printf("tName: %sn", buff);
if (err != CL_SUCCESS) {
printf("clGetDeviceInfo failedn");
exit(-1);
}
}
cl_context context;
context = clCreateContext(NULL, numDevices,devices, NULL, NULL, &err);
if(err != CL_SUCCESS){
printf("clCreateContext failedn");
exit(-1);
}
cl_command_queue cmdQueue;
cmdQueue = clCreateCommandQueue(context, devices[0], 0, &err);
if(err != CL_SUCCESS) { 
printf("clCreateCommandQueue failedn");
exit(-1);
}
cl_mem d_A, d_x;
cl_mem d_y;
d_A = clCreateBuffer(context, CL_MEM_READ_ONLY|CL_MEM_COPY_HOST_PTR, M * N * sizeof(float), A, &err);
if (err != CL_SUCCESS) {
printf("clCreateBuffer for A failedn");
exit(-1);
}
d_x = clCreateBuffer(context, CL_MEM_READ_ONLY|CL_MEM_COPY_HOST_PTR,  N * sizeof(float), x, &err);
if (err != CL_SUCCESS) {
printf("clCreateBuffer for x failedn");
exit(-1);
}
d_y = clCreateBuffer(context, CL_MEM_READ_WRITE, M * sizeof(float), NULL, &err);
if (err != CL_SUCCESS) {
printf("clCreateBuffer for y failedn");
exit(-1);
}
cl_program program;
char* source;
const char *sourceFile = "MVM_Kernel2.cl";
source = readSource(sourceFile);
program = clCreateProgramWithSource(context, 1, (const char**) &source, NULL, &err);
if (err != CL_SUCCESS) {
printf("clCreateProgramFailed");
exit(-1);
}
cl_int buildErr;
buildErr = clBuildProgram(program, numDevices, devices, NULL, NULL, NULL);
if (buildErr != CL_SUCCESS) {
printf("Program failed to build,n");
cl_build_status buildStatus;
for(int i = 0; i < numDevices; i++) {
clGetProgramBuildInfo(program, devices[i], CL_PROGRAM_BUILD_STATUS, sizeof(cl_build_status), &buildStatus, NULL);
if(buildStatus == CL_SUCCESS) {
continue;
}
char *buildLog;
size_t buildLogSize;
clGetProgramBuildInfo(program, devices[i], CL_PROGRAM_BUILD_LOG, 0, NULL, &buildLogSize);
buildLog = (char *)malloc(buildLogSize);
clGetProgramBuildInfo(program, devices[i], CL_PROGRAM_BUILD_LOG,buildLogSize, buildLog, NULL);
buildLog[buildLogSize -1] = '';
printf("Device %u Build Log:n%sn", i, buildLog);
free(buildLog);
}
exit(0);
}
else {
printf("No build errorsn");
}
cl_kernel kernel;
kernel = clCreateKernel(program, "mvKernel", &err);
if(err != CL_SUCCESS) {
printf("clCreateKernel failedn");
exit(-1);
}
err = clSetKernelArg(kernel, 0, sizeof(cl_mem), &d_A);
err |= clSetKernelArg(kernel, 1, sizeof(cl_mem), &d_x);
err |= clSetKernelArg(kernel, 2, sizeof(cl_mem), &d_y);
err |= clSetKernelArg(kernel, 3, sizeof(int), &M);
err |= clSetKernelArg(kernel, 4, sizeof(int), &N);
size_t globalWorkSize[1];
globalWorkSize[0] = M * N;
size_t localWorkSize[1];
localWorkSize[0] = 256;
err = clEnqueueNDRangeKernel(cmdQueue, kernel, 1, NULL, globalWorkSize, localWorkSize, 0, NULL, NULL);
clEnqueueReadBuffer(cmdQueue, d_y, CL_TRUE, 0, M * sizeof(float), y, 0, NULL, NULL);
clFlush(cmdQueue);
err = clFinish(cmdQueue);
if(err != CL_SUCCESS) {
printf("ERROR!!");
exit(-1);
}
clReleaseKernel(kernel);
clReleaseProgram(program);
clReleaseCommandQueue(cmdQueue);
clReleaseMemObject(d_A);
clReleaseMemObject(d_x);
clReleaseMemObject(d_y);
clReleaseContext(context);
for(int i=0; i < (M <10 ? M : 10); i++)
printf("vector y = %fn", y[i]);
float* refY;
refY = (float*)malloc(M*sizeof(float));
cpuMV(refY, A, x, M, N);
for (int i = 0; i < M; ++i) {
float diff = refY[i] - y[i];
if (fabsf(diff)/ refY[i] > 1e-4)
wrong++;
}
printf("There were %d errors!!n", wrong);
free(A);
free(y);
free(x);
free(source);
free(platforms);    
free(devices);
}
char* readSource(const char *sourceFilename) {
FILE *fp;
int errs;
int size;
char *source;
fp = fopen(sourceFilename, "rb");
errs = fseek(fp, 0, SEEK_END);
if(errs != 0) {
printf("Error seeking to end of file");
exit(-1);
}
size = ftell(fp);
if(size<0) {
printf("Errror getting file position");
exit(-1);
}
errs = fseek(fp, 0, SEEK_SET);
if(errs != 0){
printf("Error seeking to start of filen");
exit(-1);
}
source = (char*)malloc(size +1);
errs = fread(source, 1, size, fp);
if(errs != size) {
printf("only read %d bytesn", errs);
exit(0);
}
source[size]= '';
return source;
}

最终,这需要对约10000阶的矩阵进行运算EDIT我也在我的笔记本电脑上尝试过同样的代码,它有一台Nvidia GT525m,该程序对352*352的矩阵运行良好,如果矩阵更大,答案只是零,但它不会崩溃。

问题是globalWorkSize太大(M*N),而它本应只有M。这一定是GPU过载并导致系统冻结。我现在有代码在英伟达和AMD GPU以及AMD CPU 上可靠运行

最新更新