This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

OpenCL runtime compiler not thread safe

The OpenCL runtime compiler for the EVMK2H device is not thread safe (it should be --- see Appendix A.2 of the OpenCL 1.1 specification).  The following program sometimes crashes randomly with error messages like:

bug: /opt/llvm/llvm33-src/include/llvm/Support/Mutex.h:117: bool llvm::sys::SmartMutex<mt_only>::release() [with bool mt_only = true]: Assertion `((recursive && acquired) || (acquired == 1)) && "Lock not acquired before release!"' failed.

compile with -fopenmp

Thanks,  John Romein

#define __CL_ENABLE_EXCEPTIONS
#include <CL/cl.hpp>
#include <iostream>
#include <string>
#include <vector>


const char *kernelSource = "kernel void foo() { }";


void build(cl::Context &context, std::vector<cl::Device> &devices)
{
  try { // new thread --> new try/catch block
    cl::Program::Sources    source(1, std::make_pair(kernelSource, strlen(kernelSource)));
    cl::Program             program(context, source);
    program.build(devices);
  } catch (cl::Error &err) {
    std::cerr <<"cl::Error(): " << err.what() << '(' << err.err() << ')' << std::endl;
  }
}


int main()
{
  try
  {
    cl::Context             context(CL_DEVICE_TYPE_ACCELERATOR);
    std::vector<cl::Device> devices(context.getInfo<CL_CONTEXT_DEVICES>());

#pragma omp parallel for num_threads(3)
    for (int i = 0; i < 20; i ++)
      build(context, devices);
  } catch (cl::Error &err) {
    std::cerr <<"cl::Error(): " << err.what() << '(' << err.err() << ')' << std::endl;
  }

  return 0;
}