0

i am trying a simple helloWorld openCL code, it compiles without errors but display garbage : ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠

the error function detected an error while building so a removed the "-cl-std=CL2.1" in program.build("-cl-std=CL2.1");

this is the main file:

//#define CL_USE_DEPRECATED_OPENCL_2_0_APIS
#include "stdafx.h"

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

void checkError(cl_int err, const char *operation)
{
    if (err != CL_SUCCESS)
    {
        fprintf(stderr, "Error during operation '%s': %d\n", operation, err);
        exit(1);
    }
}

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();
    auto vendor = device.getInfo<CL_DEVICE_VENDOR>();
    auto version = device.getInfo<CL_DEVICE_VERSION>();

    //*********

    cl_int err;

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

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

    cl::Context context(device,0,0,0,&err);
    checkError(err, "context");

    cl::Program program(context, sources, &err);
    checkError(err, "program");

    err = program.build();
    checkError(err, "build");

    char buf[16];
    cl::Buffer memBuf(context, CL_MEM_WRITE_ONLY | CL_MEM_HOST_READ_ONLY, sizeof(buf), &err);
    checkError(err, "memBuf");

    cl::Kernel kernel(program, "HelloWorld", &err);
    checkError(err, "kernel");

    kernel.setArg(0, memBuf);


    cl::CommandQueue queue(context, device,0, &err);
    checkError(err, "queue");

    queue.enqueueTask(kernel);
    queue.enqueueReadBuffer(memBuf, CL_TRUE, 0, sizeof(buf), buf);

    std::cout << buf;
    std::cin.get();

}

and hello_world.cl

__kernel void HelloWorld(__global char* output) {
    output[0] = 'H';
    output[1] = 'e';
    output[2] = 'l';
    output[3] = 'l';
    output[4] = 'o';
    output[5] = ' ';
    output[6] = 'W';
    output[7] = 'o';
    output[8] = 'r';
    output[9] = 'l';
    output[10] = 'd';
    output[11] = '!';
    output[12] = '\n';
}

can you help me finding the problem,

Mourad
  • 60
  • 6
  • 1
    You need to check the return code from your opencl calls, presumably `enqueueReadBuffer` is failing as `buf` is not aligned – Alan Birtles Mar 14 '19 at 11:56
  • hi @Alan, i am really a beginner i dont no how to do that , can you show me the way ? – Mourad Mar 14 '19 at 12:00
  • you're already doing it, call `checkError` after every open cl call – Alan Birtles Mar 14 '19 at 12:02
  • 1
    That symbol `╠` represents uninitialized memory on a Microsoft compiler. https://stackoverflow.com/questions/370195/when-and-why-will-an-os-initialise-memory-to-0xcd-0xdd-etc-on-malloc-free-new – drescherjm Mar 14 '19 at 12:08
  • @AlanBirtles you were right there is an error at enqueueReadBuffer ! can you tell what wrong with it ? how do you think i can fix the reading – Mourad Mar 14 '19 at 13:14
  • What is the error code? I imagine (depending on which opencl implementation you are using) that the error is that `buf` is not aligned memory, you can allocate aligned memory with [std::aligned_alloc](https://en.cppreference.com/w/cpp/memory/c/aligned_alloc) – Alan Birtles Mar 14 '19 at 13:17
  • @AlanBirtles i tried aligned_alloc but didn't work , but i changed CL_MEM_WRITE_ONLY | CL_MEM_HOST_READ_ONLY to CL_MEM_READ_WRITE and it worked fine , do you have an explanation for that ? – Mourad Mar 14 '19 at 14:13
  • Maybe your host doesn't support open cl 2.0? – Alan Birtles Mar 14 '19 at 14:40

1 Answers1

0

so the answer was to change CL_MEM_WRITE_ONLY | CL_MEM_HOST_READ_ONLY to CL_MEM_READ_WRITE when calling cl::Buffer.

Mourad
  • 60
  • 6