junkie
junkie

Reputation: 829

opencl program not giving correct output

The following C++ and OpenCL are giving a 'result' of 3 instead of the expected 9 (3+6) and I don't know why. My intention was just to test access by using array indices and adding numbers at two different array indices to give a 'result'. Given that I'm new to C, C++ and OpenCL it's quite possible that it's something fundamental. Also, there's a reason why I'm using vectors as input - it's a trial run of something I'll need to do on a larger scale. Many thanks.

C++:

    vector<long> v1;
    vector<long> v2;
    long result;

    v1.push_back(3);
    v1.push_back(4);

    v2.push_back(5);
    v2.push_back(6);

    long* a1 = &v1[0];
    long* a2 = &v2[0];

    cl::Buffer bufA(context, CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR, v1.size()*sizeof(long), a1);
    cl::Buffer bufB(context, CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR, v2.size()*sizeof(long), a2);
    cl::Buffer bufC(context, CL_MEM_READ_WRITE, sizeof(long));

    addKernel.setArg(0, bufA);
    addKernel.setArg(1, bufB);
    addKernel.setArg(2, bufC);

    cl::CommandQueue queue(context, devices[0]);

    queue.enqueueTask(addKernel);

    queue.enqueueReadBuffer(bufC, CL_TRUE, 0, sizeof(long), &result);
    queue.flush();
    queue.finish();

    cout << "Result: " << result << endl;

OpenCL:

__kernel void useHostPtr(__global long *a, __global long *b, __global long *c) {
    *c = a[0] + b[1];
}

Upvotes: 1

Views: 425

Answers (1)

Eric Bainville
Eric Bainville

Reputation: 9886

The long type on the host side may have a different length. It will usually be 4 bytes. In OpenCL, long is defined as 64-bit integer. You should use cl_long instead of long in your host code.

Upvotes: 1

Related Questions