C++ OpenCL内核因特定参数而崩溃
我的OpenCL内核有问题:C++ OpenCL内核因特定参数而崩溃,c++,opencl,nvidia,C++,Opencl,Nvidia,我的OpenCL内核有问题: #pragma OPENCL EXTENSION cl_khr_fp64 : enable struct complex { double im; double re; double r; double phi; }; struct complex createComplexFromPolar(double _r, double _phi){ struct complex t; t.r = _r; t.p
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
struct complex {
double im;
double re;
double r;
double phi;
};
struct complex createComplexFromPolar(double _r, double _phi){
struct complex t;
t.r = _r;
t.phi = _phi;
t.re = cos(t.phi);
t.im = sin(t.phi);
return t;
}
struct complex createComplexFromKarthes(double real, double imag){
struct complex t;
t.re = real;
t.im = imag;
t.phi = atan(imag / real);
t.r = sqrt(pow(real, 2) + pow(imag, 2));
return t;
}
struct complex recreateComplexFromKarthes(struct complex t){
return t = createComplexFromKarthes(t.re, t.im);
}
struct complex recreateComplexFromPolar(struct complex t){
return t = createComplexFromPolar(t.r, t.phi);
}
struct complex addComplex(const struct complex z, const struct complex c){
struct complex t;
t.re = c.re + z.re;
t.im = c.im + z.re;
return recreateComplexFromKarthes(t);
}
struct complex subComplex(const struct complex z, const struct complex c){
struct complex t;
t.re = z.re - c.re;
t.im = z.im - c.im;
return recreateComplexFromKarthes(t);
}
struct complex addComplexScalar(const struct complex z, const double n){
struct complex t;
t.re = z.re + n;
return recreateComplexFromKarthes(t);
}
struct complex subComplexScalar(const struct complex z, const double n){
struct complex t;
t.re = z.re - n;
return recreateComplexFromKarthes(t);
}
struct complex multComplexScalar(const struct complex z, const double n) {
struct complex t;
t.re = z.re * n;
t.im = z.im * n;
return recreateComplexFromKarthes(t);
}
struct complex multComplex(const struct complex z, const struct complex c) {
struct complex t;
t.re = z.re*c.re - z.im*c.re;
t.im = z.re*c.im + z.im*c.re;
return recreateComplexFromKarthes(t);
}
struct complex divComplex(const struct complex z, const struct complex c) {
return createComplexFromPolar(z.r / c.r, z.phi - c.phi);
}
__kernel void newtonFraktal(__global const int* res, __global const double* param, __global int* result){
const int x = get_global_id(0);
const int y = get_global_id(1);
const int xRes = res[0];
const int yRes = res[1];
struct complex z = createComplexFromKarthes(x - (xRes / 2), y - (yRes / 2));
struct complex c = createComplexFromKarthes(param[0], param[1]);
int i = 0;
while (z.r < 500){
if (i >= 10000)
break;
z = subComplex(z, divComplex(addComplex(addComplex(multComplex(multComplex(z,z),c),multComplex(z,c)),c),addComplex(multComplexScalar(multComplex(z,c),2),c)));
//z-(c*z*z + c*z + c) / ((c*z) * 2 + c);
i++;
}
result[x + res[0]*y] = i;
}
好的-现在我真的明白了(我希望:D):
我的内核是在图形卡上执行的,并连接了一个监视器。。执行时间比5秒多一点。->TDR(具有Windows驱动程序安全性的东西)重置驱动程序->内核执行被终止。。所以现在我将注册表中的TDR设置为一个更高的值,将来可能会使用另一个图形卡:D 这对我来说有点烦人。。但最后我解决了。。所以感谢所有这些帮助我回答其他问题的人 如果有人再次遇到此问题,请在此描述如何增加TDR显示(不应用于生产系统;-):
打开注册表编辑器(例如,通过
Win-Key
+R
并在窗口中写入regedit
,然后执行)浏览HKEY\U LOCAL\U MACHINE\SYSTEM\CurrentControlSet\Control\GraphicsDrivers
,并使用您可以选择的值创建一个名为TdreLay
的新条目(它间接地表示内核的最大执行时间)秒(我已将其设置为10秒)。现在重新启动系统并享受未阻塞的OpenCL/CUDA内核:D
NewtonFraktalCLGeneration::NewtonFraktalCLGeneration(cl_double* param){
FILE* f;
if (fopen_s(&f, "newton.cl", "r") != 0){
return;
}
char* buf = (char*)malloc(100 * sizeof(char));
char* temp = buf;
int recv_size = 0, total_recv = 0;
int i = 1;
while ((recv_size = fread_s(temp, sizeof(char) * 100, sizeof(char), 100, f)) > 0){
total_recv += recv_size;
buf = (char*)realloc(buf, total_recv + 100 * sizeof(char));
temp = buf + total_recv;
}
buf[total_recv] = '\0';
err = CL_SUCCESS;
try {
cl::vector<cl::Platform> platforms;
cl::Platform::get(&platforms);
cl_context_properties properties[] =
{ CL_CONTEXT_PLATFORM, (cl_context_properties)(platforms[0])(), 0 };
cl::Context context(CL_DEVICE_TYPE_GPU, properties);
cl::vector<cl::Device> devices = context.getInfo<CL_CONTEXT_DEVICES>();
cl::Program::Sources source(1, std::make_pair(buf,strlen(buf)));
cl::Program program_ = cl::Program(context, source);
program_.build(devices);
cl::Kernel kernel(program_, "newtonFraktal", &err);
int *res = new int[2];
res[0] = Services()->getCore()->getXRes(), res[1] = Services()->getCore()->getYRes();
cl::Buffer resBuf(context, CL_MEM_READ_ONLY, 2 * sizeof(int));
cl::Buffer paramBuf(context, CL_MEM_READ_ONLY, 2 * sizeof(cl_double));
result = (cl_int*)calloc(res[0] * res[1], sizeof(cl_int) + 1);
cl::Buffer outBuf(context, CL_MEM_WRITE_ONLY | CL_MEM_USE_HOST_PTR, res[0] * res[1] * sizeof(cl_int) + 1, result);
cl::CommandQueue queue(context, devices[0], 0, &err);
cl::Event event;
queue.enqueueWriteBuffer(resBuf, CL_TRUE, 0, 2 * sizeof(int), res);
queue.enqueueWriteBuffer(paramBuf, CL_TRUE, 0, 2 * sizeof(double), param);
kernel.setArg(0, resBuf);
kernel.setArg(1, paramBuf);
kernel.setArg(2, outBuf);
queue.enqueueNDRangeKernel(
kernel,
cl::NullRange,
cl::NDRange(res[0], res[1]),
cl::NullRange,
NULL,
&event);
queue.finish();
queue.enqueueReadBuffer(outBuf, CL_TRUE, 0, res[0] * res[1] * sizeof(cl_int) + 1, result);
}
catch (cl::Error& err) {
std::cerr
<< "ERROR: "
<< err.what()
<< "("
<< err.err()
<< ")"
<< std::endl;
this->err = err.err();
}
}