Exemplo n.º 1
0
void Histogram::initializeOpenCL() {
    int err;

	ExampleBase::initializeOpenCL();

	if (!this->supportsBaseAtomics())
        this->exitWith("Error: No support for base atomic operations for " + this->getName() + "!", 0);
	
	const char
		*algo2 = this->algorithm2(),
		*algo3 = this->algorithm3();
	this->program2 = clCreateProgramWithSource(this->context, 1, (const char **) &algo2, NULL, &err);
	this->program3 = clCreateProgramWithSource(this->context, 1, (const char **) &algo3, NULL, &err);
	if (!this->program2 || !this->program3)
        this->exitWith("Error: Failed to create compute program for " + this->getName() + "!", err);

    err  = clBuildProgram(this->program2, 0, NULL, NULL, NULL, NULL);
    err |= clBuildProgram(this->program3, 0, NULL, NULL, NULL, NULL);
    if (err != CL_SUCCESS)     {
        size_t len;
        char buffer[2048];

        clGetProgramBuildInfo(this->program2, this->device_ids[0], CL_PROGRAM_BUILD_LOG, sizeof(buffer), buffer, &len);
        printf("%s\n", buffer);
        clGetProgramBuildInfo(this->program3, this->device_ids[0], CL_PROGRAM_BUILD_LOG, sizeof(buffer), buffer, &len);
        printf("%s\n", buffer);
        this->exitWith("Error: Failed to build program executable for " + this->getName() + "!", err);
    }

	createKernels(this->kernel2, this->program2);
	createKernels(this->kernel3, this->program3);
}
Exemplo n.º 2
0
std::vector<Kernel> Kernel::createKernelsFromFile(const std::string& fileName, 
                                                  const std::vector<std::string>& kernelNames,
                                                  const std::string& options)
{
    std::ifstream file(fileName.c_str(), std::ios::in);
    if (!file.is_open())
    {
        throw std::runtime_error("Could not open source file.");
    }
    std::ostringstream source;
    source << file.rdbuf();
    return createKernels(source.str(), kernelNames, options);
}
Exemplo n.º 3
0
Lbm::Lbm(cl::Platform platform, cl::Device device, std::vector<std::string> kernels)
{
  cl_context_properties cProps[] = {
    CL_CONTEXT_PLATFORM, (cl_context_properties)(platform)(),
    0};

  try
  {
    context = cl::Context(CL_DEVICE_TYPE_ALL, cProps, errorCallbackCL);
    queue = cl::CommandQueue(context, device);
  }
  catch(cl::Error e)
  {
    cluErr("Sim", e);
  }

  std::vector<std::string> sourceStr;
  for(auto it = kernels.begin(); it != kernels.end(); ++it)
  {
    std::ifstream file(it->c_str());
    if(!file.good())
     std::cerr<<"Sim: Kernel: file not good"<<std::endl;
    sourceStr.push_back(std::string((std::istreambuf_iterator<char>(file)),
     std::istreambuf_iterator<char>()));
  }

  try
  {
    cl::Program::Sources source = cl::Program::Sources();

    for(auto it = sourceStr.begin(); it != sourceStr.end(); ++it)
      source.push_back(std::make_pair(it->c_str(), it->size()));

    program = cl::Program(context, source);
  }
  catch(cl::Error e)
  {
    cluErr("Sim: program", e);
  }

  createKernels(device, kernels);
  initData();
}
Exemplo n.º 4
0
cl_int
clCreateKernelsInProgram(cl_program     d_program,
                         cl_uint        num_kernels,
                         cl_kernel *    kernels,
                         cl_uint *      num_kernels_ret)
{
    cl_int rs = CL_SUCCESS;
    auto program = pobj(d_program);

    if (!program->isA(Coal::Object::T_Program))
        return CL_INVALID_PROGRAM;

    if (program->state() != Coal::Program::Built)
        return CL_INVALID_PROGRAM_EXECUTABLE;

    std::vector<Coal::Kernel *> ks = program->createKernels(&rs);

    if (rs != CL_SUCCESS)
    {
        while (ks.size())
        {
            delete ks.back();
            ks.pop_back();
        }

        return rs;
    }

    // Check that the kernels will fit in the array, if needed
    if (num_kernels_ret)
        *num_kernels_ret = ks.size();

    if (kernels && num_kernels < ks.size())
    {
        while (ks.size())
        {
            delete ks.back();
            ks.pop_back();
        }

        return CL_INVALID_VALUE;
    }

    if (!kernels)
    {
        // We don't need the kernels in fact
        /* while (ks.size())
        {
            delete ks.back();
            ks.pop_back();
        } */
    }
    else
    {
        // Copy the kernels
        for (size_t i=0; i<ks.size(); ++i)
        {
            kernels[i] = desc(ks[i]);
        }
    }

    return CL_SUCCESS;
}