cl_int
clRetainContext(cl_context context)
{
    if (!context->isA(Coal::Object::T_Context))
        return CL_INVALID_CONTEXT;

    context->reference();

    return CL_SUCCESS;
}
Exemple #2
0
	context_resource::context_resource(cl_context context)
		: context(context)
	{
		if (context)
		{
			context->lock();
			context->resources.insert(this);
			context->retain();
			context->unlock();
		}
	}
cl_int
clReleaseContext(cl_context context)
{
    if (!context->isA(Coal::Object::T_Context))
        return CL_INVALID_CONTEXT;

    if (context->dereference())
        delete context;

    return CL_SUCCESS;
}
Exemple #4
0
// Memory Object APIs
cl_mem
clCreateBuffer(cl_context   context,
               cl_mem_flags flags,
               size_t       size,
               void *       host_ptr,
               cl_int *     errcode_ret)
{
    cl_int dummy_errcode;

    if (!errcode_ret)
        errcode_ret = &dummy_errcode;

    if (!context->isA(Coal::Object::T_Context))
    {
        *errcode_ret = CL_INVALID_CONTEXT;
        return 0;
    }

    *errcode_ret = CL_SUCCESS;

    Coal::Buffer *buf = new Coal::Buffer(context, size, host_ptr, flags,
                                         errcode_ret);

    if (*errcode_ret != CL_SUCCESS || (*errcode_ret = buf->init()) != CL_SUCCESS)
    {
        delete buf;
        return 0;
    }

    return (cl_mem)buf;
}
Exemple #5
0
cl_int
clGetSupportedImageFormats(cl_context           context,
                           cl_mem_flags         flags,
                           cl_mem_object_type   image_type,
                           cl_uint              num_entries,
                           cl_image_format *    image_formats,
                           cl_uint *            num_image_formats)
{
    if (!context->isA(Coal::Object::T_Context))
        return CL_INVALID_CONTEXT;

    (void) flags;
    (void) image_type;

    if (!num_entries && image_formats)
        return CL_INVALID_VALUE;

    if (image_formats)
    {
        std::memcpy(image_formats, supported_formats,
                    MIN(num_entries * sizeof(cl_image_format),
                        sizeof(supported_formats)));
    }

    if (num_image_formats)
        *num_image_formats = sizeof(supported_formats) / sizeof(cl_image_format);

    return CL_SUCCESS;
}
Exemple #6
0
cl_mem
clCreateImage2D(cl_context              context,
                cl_mem_flags            flags,
                const cl_image_format * image_format,
                size_t                  image_width,
                size_t                  image_height,
                size_t                  image_row_pitch,
                void *                  host_ptr,
                cl_int *                errcode_ret)
{
    cl_int dummy_errcode;

    if (!errcode_ret)
        errcode_ret = &dummy_errcode;

    if (!context->isA(Coal::Object::T_Context))
    {
        *errcode_ret = CL_INVALID_CONTEXT;
        return 0;
    }

    *errcode_ret = CL_SUCCESS;

    Coal::Image2D *image = new Coal::Image2D(context, image_width, image_height,
                                             image_row_pitch, image_format,
                                             host_ptr, flags, errcode_ret);

    if (*errcode_ret != CL_SUCCESS || (*errcode_ret = image->init()) != CL_SUCCESS)
    {
        delete image;
        return 0;
    }

    return (cl_mem)image;
}
cl_int
clGetContextInfo(cl_context         context,
                 cl_context_info    param_name,
                 size_t             param_value_size,
                 void *             param_value,
                 size_t *           param_value_size_ret)
{
#ifdef DBG_API
  std::cerr << "clGetContextInfo\n";
#endif
    if (!context->isA(Coal::Object::T_Context))
        return CL_INVALID_CONTEXT;

    return context->info(param_name, param_value_size, param_value,
                         param_value_size_ret);
}
PUBLIC cl_program
clCreateProgramWithBinary(cl_context ctx, cl_uint count,
                          const cl_device_id *devs, const size_t *lengths,
                          const unsigned char **binaries, cl_int *status_ret,
                          cl_int *errcode_ret) try {
   if (!ctx)
      throw error(CL_INVALID_CONTEXT);

   if (!count || !devs || !lengths || !binaries)
      throw error(CL_INVALID_VALUE);

   if (any_of([&](const cl_device_id dev) {
            return !ctx->has_device(dev);
         }, devs, devs + count))
      throw error(CL_INVALID_DEVICE);

   // Deserialize the provided binaries,
   auto modules = map(
      [](const unsigned char *p, size_t l) -> std::pair<cl_int, module> {
         if (!p || !l)
            return { CL_INVALID_VALUE, {} };

         try {
            compat::istream::buffer_t bin(p, l);
            compat::istream s(bin);

            return { CL_SUCCESS, module::deserialize(s) };

         } catch (compat::istream::error &e) {
            return { CL_INVALID_BINARY, {} };
         }
      },
      binaries, binaries + count, lengths);

   // update the status array,
   if (status_ret)
      std::transform(modules.begin(), modules.end(), status_ret,
                     keys<cl_int, module>);

   if (any_of(key_equals<cl_int, module>(CL_INVALID_VALUE),
              modules.begin(), modules.end()))
      throw error(CL_INVALID_VALUE);

   if (any_of(key_equals<cl_int, module>(CL_INVALID_BINARY),
              modules.begin(), modules.end()))
      throw error(CL_INVALID_BINARY);

   // initialize a program object with them.
   ret_error(errcode_ret, CL_SUCCESS);
   return new program(*ctx, { devs, devs + count },
                      map(values<cl_int, module>,
                          modules.begin(), modules.end()));

} catch (error &e) {
   ret_error(errcode_ret, e);
   return NULL;
}
Exemple #9
0
PUBLIC cl_command_queue
clCreateCommandQueue(cl_context ctx, cl_device_id dev,
                     cl_command_queue_properties props,
                     cl_int *errcode_ret) try {
   if (!ctx)
      throw error(CL_INVALID_CONTEXT);

   if (!ctx->has_device(dev))
      throw error(CL_INVALID_DEVICE);

   if (props & ~(CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE |
                 CL_QUEUE_PROFILING_ENABLE))
      throw error(CL_INVALID_VALUE);

   ret_error(errcode_ret, CL_SUCCESS);
   return new command_queue(*ctx, *dev, props);

} catch (error &e) {
   ret_error(errcode_ret, e);
   return NULL;
}
// Program Object APIs
cl_program
clCreateProgramWithSource(cl_context        context,
                          cl_uint           count,
                          const char **     strings,
                          const size_t *    lengths,
                          cl_int *          errcode_ret)
{
#ifdef DBG_API
  std::cerr << "clCreateProgramWithSource\n";
#endif
    cl_int dummy_errcode;

    if (!errcode_ret)
        errcode_ret = &dummy_errcode;

    if (!context->isA(Coal::Object::T_Context))
    {
        *errcode_ret = CL_INVALID_CONTEXT;
        return 0;
    }

    if (!count || !strings)
    {
        *errcode_ret = CL_INVALID_VALUE;
        return 0;
    }

    Coal::Program *program = new Coal::Program(context);

    *errcode_ret = CL_SUCCESS;
    *errcode_ret = program->loadSources(count, strings, lengths);

    if (*errcode_ret != CL_SUCCESS)
    {
        delete program;
        return 0;
    }

    return (cl_program)program;
}
Exemple #11
0
// Command Queue APIs
cl_command_queue
clCreateCommandQueue(cl_context                     context,
                     cl_device_id                   device,
                     cl_command_queue_properties    properties,
                     cl_int *                       errcode_ret)
{
    cl_int default_errcode_ret;

    // No errcode_ret ?
    if (!errcode_ret)
        errcode_ret = &default_errcode_ret;

    if (!device->isA(Coal::Object::T_Device))
    {
        *errcode_ret = CL_INVALID_DEVICE;
        return 0;
    }

    if (!context->isA(Coal::Object::T_Context))
    {
        *errcode_ret = CL_INVALID_CONTEXT;
        return 0;
    }

    *errcode_ret = CL_SUCCESS;
    Coal::CommandQueue *queue = new Coal::CommandQueue(
                                        (Coal::Context *)context,
                                        (Coal::DeviceInterface *)device,
                                        properties,
                                        errcode_ret);

    if (*errcode_ret != CL_SUCCESS)
    {
        // Initialization failed, destroy context
        delete queue;
        return 0;
    }

    return (_cl_command_queue *)queue;
}
// Command Queue APIs
cl_command_queue
clCreateCommandQueue(cl_context                     context,
                     cl_device_id                   device,
                     cl_command_queue_properties    properties,
                     cl_int *                       errcode_ret)
{
#ifdef DBG_API
  std::cerr << "Entering clCreateCommandQueue\n";
#endif
    cl_int default_errcode_ret;

    // No errcode_ret ?
    if (!errcode_ret)
        errcode_ret = &default_errcode_ret;



#ifdef DBG_API
    std::cerr << "Check if the device is an object\n";
#endif
    if (!device->isA(Coal::Object::T_Device))
    {
      std::cerr << "INVALID_DEVICE\n";
        *errcode_ret = CL_INVALID_DEVICE;
        return 0;
    }

    if (!context->isA(Coal::Object::T_Context))
    {
#ifdef DBG_OUTPUT
      std::cout << "!!! ERROR: INVALID_CONTEXT" << std::endl;
#endif
      *errcode_ret = CL_INVALID_CONTEXT;
      return 0;
    }
#ifdef DBG_API
    std::cerr << "Attempt to initialise device\n";
#endif
    if (!device->init()) {
#ifdef DBG_OUTPUT
      std::cout << "!!!ERROR: Device initialisation failed!\n";
#endif
      *errcode_ret = CL_DEVICE_NOT_AVAILABLE;
      return 0;
    }

    *errcode_ret = CL_SUCCESS;
    Coal::CommandQueue *queue = new Coal::CommandQueue(
                                        (Coal::Context *)context,
                                        (Coal::DeviceInterface *)device,
                                        properties,
                                        errcode_ret);

    if (*errcode_ret != CL_SUCCESS)
    {
#ifdef DBG_OUTPUT
      std::cout << "!!! ERROR: CommandQueue create failed" << std::endl;
#endif
        // Initialization failed, destroy context
        delete queue;
        return 0;
    }

    return (_cl_command_queue *)queue;
}
cl_program
clCreateProgramWithBinary(cl_context            context,
                          cl_uint               num_devices,
                          const cl_device_id *  device_list,
                          const size_t *        lengths,
                          const unsigned char **binaries,
                          cl_int *              binary_status,
                          cl_int *              errcode_ret)
{
#ifdef DBG_API
  std::cerr << "clCreateProgramWithBinary\n";
#endif
    cl_int dummy_errcode;

    if (!errcode_ret)
        errcode_ret = &dummy_errcode;

    if (!context->isA(Coal::Object::T_Context))
    {
        *errcode_ret = CL_INVALID_CONTEXT;
        return 0;
    }

    if (!num_devices || !device_list || !lengths || !binaries)
    {
        *errcode_ret = CL_INVALID_VALUE;
        return 0;
    }

    // Check the devices for compliance
    cl_uint context_num_devices = 0;
    cl_device_id *context_devices;

    *errcode_ret = context->info(CL_CONTEXT_NUM_DEVICES, sizeof(cl_uint),
                                 &context_num_devices, 0);

    if (*errcode_ret != CL_SUCCESS)
        return 0;

    context_devices =
        (cl_device_id *)std::malloc(context_num_devices * sizeof(cl_device_id));

    *errcode_ret = context->info(CL_CONTEXT_DEVICES,
                                 context_num_devices * sizeof(cl_device_id),
                                 context_devices, 0);

    if (*errcode_ret != CL_SUCCESS)
        return 0;

    for (cl_uint i=0; i<num_devices; ++i)
    {
        bool found = false;

        if (!lengths[i] || !binaries[i])
        {
            if (binary_status)
                binary_status[i] = CL_INVALID_VALUE;

            *errcode_ret = CL_INVALID_VALUE;
            return 0;
        }

        for (cl_uint j=0; j<context_num_devices; ++j)
        {
            if (device_list[i] == context_devices[j])
            {
                found = true;
                break;
            }
        }

        if (!found)
        {
            *errcode_ret = CL_INVALID_DEVICE;
            return 0;
        }
    }

    // Create a program
    Coal::Program *program = new Coal::Program(context);
    *errcode_ret = CL_SUCCESS;

    // Init program
    *errcode_ret = program->loadBinaries(binaries,
                                         lengths, binary_status, num_devices,
                                         (Coal::DeviceInterface * const*)device_list);

    if (*errcode_ret != CL_SUCCESS)
    {
        delete program;
        return 0;
    }

    return (cl_program)program;
}