0

I want to read binary file to OpenCL Memory Object using Zero Copy method. But, when my code execute readFile, my program has stopped working.

I have read other Q&A,

But, I haven't solved my problem yet.

This is my code

//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;
}

Then, while read binary file, my program has stopped working.

Community
  • 1
  • 1
Khalif21
  • 97
  • 2
  • 11
  • OpenCL doesn't set the errno that perror check. You should yourself check the errorcode from the err variable. – Jovasa Jan 19 '17 at 07:24
  • 1
    after clCreateBuffer you check if(err == CL_INVALID_MEM_OBJECT), it could return other error for example size does not fit. What your debugger says there? – Tiger Hwang Jan 19 '17 at 09:19
  • @Jovasa I using it to check Error Code: http://stackoverflow.com/questions/24326432/convenient-way-to-show-opencl-error-codes – Khalif21 Jan 20 '17 at 03:33
  • @Tiger Hwang I get "CL_INVALID_BUFFER_SIZE" after clCreateBuffer and before clEnqueueMapBuffer. – Khalif21 Jan 20 '17 at 03:33
  • I have changed BUFFER_SIZE to less than 512 MB. Then, after clEnqueueMapBuffer my program stopped working. – Khalif21 Jan 20 '17 at 06:05
  • Can not see any problem now. Suggest you begin with a small buffer, maybe there are some limitation in buffer size on your device. – Tiger Hwang Jan 20 '17 at 10:28
  • @Tiger Hwang CL_DEVICE_MAX_MEM_ALLOC_SIZE: 512 MByte CL_DEVICE_GLOBAL_MEM_SIZE: 2048 MByte. I haven't understand yet, I have GPU with 2 GB memory and CPU with 8 GB memory, but it failed when I allocated it more than 512 MB. – Khalif21 Jan 23 '17 at 04:28
  • That's not suprise, your display driver, system or whatever else will use many memory on graphcs card. You can check how many left with tools comes with your graphics driver vendor. – Tiger Hwang Jan 23 '17 at 09:25

0 Answers0