1

Good afternoon! I need to run a program written in C++ that uses OpenCL. Before that I enabled OpenCL headers, installed CUDA (this is optional) and reinstalled Visual Studio and MinGW. I have an NVIDIA GeFOrce 1080.

Well, I have an array<int, 3> loc and as a result of the execution loc should change to 15, 15, 15 according to kernelFile2.cl(found is loc, check main code):

kernel void kernelFile(global int *faces, global int *facecount, global int *found, global int *xlength, global int *zlength, global int *ymin)
{

            found[0] = 15;
            found[1] = 15;
            found[2] = 15;
}

I'm sorry, but I don't understand much about OpenCL, so I'll show you the whole program.

file.cpp:

#define CL_USE_DEPRECATED_OPENCL_1_2_APIS

#include <iostream>
#include <fstream>
#include <CL/cl.hpp>
#include <array>


int main()
{
    std::vector<cl::Platform> platforms;
    cl::Platform::get(&platforms);

    _ASSERT(platforms.size() > 0);

    auto platform = platforms.front();
    std::vector<cl::Device> devices;

    platform.getDevices(CL_DEVICE_TYPE_GPU, &devices);

    _ASSERT(devices.size() > 0);

    auto device = devices.front();

    std::cout << "Device: " << device.getInfo<CL_DEVICE_NAME>() << std::endl;

    std::string fileName;

    std::ifstream kernelFile("kernelFile2.cl");
    std::string src(std::istreambuf_iterator<char>(kernelFile), (std::istreambuf_iterator<char>()));

    cl::Program::Sources sources(1, std::make_pair(src.c_str(), src.length() + 1));


    
    cl::Context context(device);
    cl::Program program(context, sources);

    auto err = program.build("-cl-std=CL1.2");

    std::cout << std::to_string(err) << " error" <<std::endl;//print error code

    int facecount = 1;
    const int arraysize = 5;//5 times facecount
    std::array<int, arraysize> formation = {{0, 0, 0, 1, 3,}};
    
    std::array<int, 3> loc =  {0, 0, 0} ;
    int xlength = 10000;
    int zlength = 10000;
    int yrangesize = 1;
    int ymin = 59;

    cl::Buffer facesbuf(context, CL_MEM_READ_WRITE | CL_MEM_HOST_READ_ONLY | CL_MEM_COPY_HOST_PTR, sizeof(int) * arraysize, formation.data());
    cl::Buffer facecountbuf(context, CL_MEM_READ_WRITE | CL_MEM_HOST_READ_ONLY | CL_MEM_COPY_HOST_PTR, sizeof(int), &facecount);
    cl::Buffer resultbuf(context, CL_MEM_READ_WRITE | CL_MEM_HOST_READ_ONLY | CL_MEM_COPY_HOST_PTR, sizeof(int) * 3, loc.data());
    cl::Buffer xlengthbuf(context, CL_MEM_READ_WRITE | CL_MEM_HOST_READ_ONLY | CL_MEM_COPY_HOST_PTR, sizeof(int), &xlength);
    cl::Buffer zlengthbuf(context, CL_MEM_READ_WRITE | CL_MEM_HOST_READ_ONLY | CL_MEM_COPY_HOST_PTR, sizeof(int), &zlength);
    cl::Buffer yminbuf(context, CL_MEM_READ_WRITE | CL_MEM_HOST_READ_ONLY | CL_MEM_COPY_HOST_PTR, sizeof(int), &ymin);


    cl::Kernel kernel(program, "kernelFile");
    kernel.setArg(0, facesbuf);
    kernel.setArg(1, facecountbuf);
    kernel.setArg(2, resultbuf);
    kernel.setArg(3, xlengthbuf);
    kernel.setArg(4, zlengthbuf);
    kernel.setArg(5, yminbuf);

    cl::CommandQueue queue(context, device);
    std::cout << std::to_string(queue.enqueueNDRangeKernel(kernel, cl::NDRange(NULL), cl::NDRange(xlength, yrangesize, zlength))) << std::endl;

    queue.enqueueReadBuffer(resultbuf, CL_TRUE, 0, sizeof(int) * 3, loc.data());
    
    queue.finish();

    std::cout << "X: " << loc[0] << " Y: " << loc[1] << " Z: " << loc[2];


}

But, unfortunately, this program does not change the variables.

It looked different in the beginning, but I changed it and left only what would help understand the problem.

Thank you so much if someone helps!

talonmies
  • 70,661
  • 34
  • 192
  • 269
maxet24
  • 111
  • 1
  • 10
  • Huh, strange, for me it works without issues. I only removed the arguments from `auto err = program.build();`. Is the `.cl` file loaded from the correct file path? And maybe set the Buffer flags to only `CL_MEM_READ_WRITE`. – ProjectPhysX Jan 30 '22 at 09:55
  • 1
    @ProjectPhysX Thanks for the answer, for some reason it doesn't work for me. I set `CL_MEM_READ_WRITE` and `program.build()`, but still the program writes: `Device: NVIDIA GeForce GTX 1080` `0 error` `0` `X: 0 Y: 0 Z: 0` The .cl is exactly in the same folder. I am using VS 2022 and both files are in the "Source Files" folder. Also if I click on "look in explorer" I see that the files are in the same folder. – maxet24 Jan 31 '22 at 15:57
  • 1
    Oh, it FINALLY WORKED! I needed to create the files in VS and copy the code there instead of just drag and drop from explorer to VS. Thanks! If you post an answer, I'll mark it as correct. – maxet24 Jan 31 '22 at 18:11

1 Answers1

1

I tested the code and for me it works without issues. Must be a problem with the .cl file not loaded from the correct filepath or files not loaded into the VS project.

ProjectPhysX
  • 4,535
  • 2
  • 14
  • 34