user1235183
user1235183

Reputation: 3072

OpenCL causes CL_INVALID_COMMAND_QUEUE if loaded from binary

I'm struggling saving/loading binary-kernels: If i build from source, the program runs fine. But if i write and load the kernel to binary. I get CL_INVALID_COMMAND_QUEUE calling m_prog.getCommandQueues()[iDevice].enqueueNDRangeKernel. My Platform is in both cases (from binary, from source) Intel and my device is in both cases my CPU.

Maybe helpful additional information:

  1. No exceptions are thrown before CL_INVALID_COMMAND_QUEUE ( with __CL_ENABLE_EXCEPTIONS defined)

  2. program ref count 1

  3. program num devs 1

  4. program num devs 1

  5. program build status for device CL_BUILD_SUCCES

  6. program build log

    • Device build started

    • Device build done

    • Reload Program Binary Object.

.

/// write binaries to file
std::ofstream bfile(filenameGenerator(m_platform, fileName), std::ios::binary);
if (!bfile) return IT_CL_FILE_NOT_FOUND;

std::vector<size_t> sizes = m_prog.getInfo<CL_PROGRAM_BINARY_SIZES>();
std::vector<char*>  binaries = m_prog.getInfo<CL_PROGRAM_BINARIES>();

bfile.write((char*)&sizes[0], sizeof(size_t));
bfile.write(binaries[0], sizes[0]);
delete[] binaries[0];

/// ... somewhere else ...

///read binaries from file
ifstream bfile(filenameGenerator(platform, fileName), std::ios::binary);
size_t n;
std::vector<char> buf;

bfile.read((char*)&n, sizeof(size_t));
buf.resize(n);
bfile.read(buf.data(), n);

m_prog = cl::Program(context, devs, cl::Program::Binaries(
    1, std::make_pair(static_cast<const void*>(buf.data()), n)));
m_prog.build(devs);

/// later ...
m_prog.getCommandQueues()[iDevice].enqueueNDRangeKernel ///throws

EDIT: added the last two lines of code, to clearify the calling order

Upvotes: 0

Views: 227

Answers (2)

user1235183
user1235183

Reputation: 3072

I found the solution myself, i was able to succed in not initializing the vector<cl::CommandQueue> and only resizes the vector.

Upvotes: 0

DarkZeros
DarkZeros

Reputation: 8410

A program should only have an associated command queue when it has been built for a specific device.

A program loaded from a binary data has not been built, and it is unclear if it has a command queue.

Calling m_prog.getCommandQueues()[device] is not recommended, since you cannot be sure a queue will be returned. You should create your own queue outside, and queue the program there.

Upvotes: 1

Related Questions