C++ OpenCL-如何将二进制文件读取到OpenCL内存对象(零拷贝)
我想使用零拷贝方法将二进制文件读取到OpenCL内存对象。但是,当我的代码执行readFile时,我的程序已经停止工作 我读过其他问答C++ OpenCL-如何将二进制文件读取到OpenCL内存对象(零拷贝),c++,c,memory,opencl,C++,C,Memory,Opencl,我想使用零拷贝方法将二进制文件读取到OpenCL内存对象。但是,当我的代码执行readFile时,我的程序已经停止工作 我读过其他问答 但是,我还没有解决我的问题 这是我的密码 //OPENCL Include #include <CL/cl.h> typedef float2 cplx; int readFile(char *filen, cplx* data); int main(int argc, char* argv[]) { char *filename = (c
//OPENCL Include
#include <CL/cl.h>
typedef float2 cplx;
int readFile(char *filen, cplx* data);
int main(int argc, char* argv[])
{
char *filename = (char*)malloc(100);
sprintf(filename,"%s",argv[1]);
//OpenCL Structures
cl_device_id device;
cl_context context;
cl_command_queue commandQueue;
int err;
int filesize = 1024;
device = create_device();
context = clCreateContext(NULL, 1, &device, NULL, NULL, &err);
if(err < 0)
{
perror("Couldn't create a context");
exit(1);
}
commandQueue = clCreateCommandQueue(context, device, 0, &err);
if(err < 0)
{
perror("Couldn't create Command Queue");
exit(1);
}
cl_mem memObj_data[1] = {0};
size_t buffer_size = 1024 * sizeof(cplx);
printf("Create Buffer\n");
memObj_data[0] = clCreateBuffer(context, CL_MEM_READ_WRITE | CL_MEM_ALLOC_HOST_PTR, buffer_size, NULL, &err);
if(err != CL_SUCCESS)
{
cerr << getErrorString(err) << endl;
exit(1);
}
// cplx *data = (cplx*)malloc(sizeof(cplx)*sizeFile);
// data = (cplx*)clEnqueueMapBuffer(commandQueue, memObj_data[0], CL_TRUE, CL_MAP_WRITE, 0, buffer_size, 0, NULL, NULL, &err);
printf("Enqueue Map Buffer\n");
cplx* data = (cplx*)clEnqueueMapBuffer(commandQueue, memObj_data[0], CL_TRUE, CL_MAP_WRITE, 0, buffer_size, 0, NULL, NULL, &err);
if(err != CL_SUCCESS)
{
cerr << getErrorString(err) << endl;
exit(1);
}
int ret = readFile(filename, data);
if(ret == 1)
{
cout << "Error on Reading File" << endl;
return 1;
printf("Enequeue Unmap Memory Object\n");
err = clEnqueueUnmapMemObject(commandQueue, memObj_data[0], data, 0, NULL, NULL);
if(err != CL_SUCCESS)
{
cerr << getErrorString(err) << endl;
exit(1);
}
//Deallocate resource
clReleaseMemObject(memObj_data[0]);
clReleaseCommandQueue(commandQueue);
clReleaseContext(context);
clReleaseDevice(device);
//FREE MEMORY
delete[] filename;
return 0;
}
int readFile(char *filen, cplx* data)
{
cout << "Read File" << endl;
//Get size contains of file
// ifstream file("../testfile", ios::in | ios::binary | ios::ate);
streampos size;
char path[20];
sprintf(path,"%s",filen);
ifstream file(path, ios::in | ios::binary | ios::ate);
if(file.is_open())
{
size = file.tellg();
// *data = (cplx*)realloc(*data, sizeof(cplx)*size);
if(size)
{
// data = data;
cout << "Read CFL file Success" << endl;
}
else
{
cout << "Error Allocating Memory" << endl;
return 1;
}
cout << "Contains Size : "<< std::to_string(size) << endl;
}
else
{
cout << "Unable to open file";
return 1;
}
if(file.is_open())
{
file.seekg(0, ios::beg);
file.read((char*)data, size);
file.close();
}
// int start = 230;
// int finish = 250;
// cout << "ON Functions" << endl;
// for(int i = start; i < finish; i++)
// {
// cout << i << " "<< std::to_string((*data)[i].x) << " + " << std::to_string((*data)[i].y) << endl;
// }
// data = memblock;
// free(memblock);
return 0;
}
//OPENCL包含
#包括
typedef-float2cplx;
int readFile(char*filen,cplx*data);
int main(int argc,char*argv[])
{
char*filename=(char*)malloc(100);
sprintf(文件名为“%s”,argv[1]);
//OpenCL结构
cl_设备\u id设备;
语境;
命令队列命令队列;
INTERR;
int filesize=1024;
设备=创建设备();
context=clCreateContext(NULL,1,&device,NULL,NULL,&err);
如果(误差<0)
{
perror(“无法创建上下文”);
出口(1);
}
commandQueue=clCreateCommandQueue(上下文、设备、0和错误);
如果(误差<0)
{
perror(“无法创建命令队列”);
出口(1);
}
cl_mem memObj_data[1]={0};
大小缓冲区大小=1024*sizeof(cplx);
printf(“创建缓冲区\n”);
memObj_data[0]=clCreateBuffer(上下文、CL_MEM_READ_WRITE、CL_MEM_ALLOC_HOST_PTR、缓冲区大小、空值和错误);
如果(错误!=CL_成功)
{
cerr OpenCL没有设置perror检查的错误号。您应该自己检查errr变量中的错误代码。在clCreateBuffer检查(err==CL\u INVALID\u MEM\u OBJECT)后,它可能会返回其他错误,例如大小不合适。调试器在那里说了什么?@Jovasa I使用它检查错误代码:@Tiger Hwang I getclCreateBuffer之后和ClenqueueEmapBuffer之前的“CL_无效缓冲区大小”。我已将缓冲区大小更改为小于512 MB。然后,在ClenqueEmapBuffer之后,我的程序停止工作。