Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. //
  2. // Copyright 2012 Francisco Jerez
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a
  5. // copy of this software and associated documentation files (the "Software"),
  6. // to deal in the Software without restriction, including without limitation
  7. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. // and/or sell copies of the Software, and to permit persons to whom the
  9. // Software is furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  17. // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. // OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. // OTHER DEALINGS IN THE SOFTWARE.
  21. //
  22.  
  23. #include "api/util.hpp"
  24. #include "core/context.hpp"
  25.  
  26. using namespace clover;
  27.  
  28. PUBLIC cl_context
  29. clCreateContext(const cl_context_properties *props, cl_uint num_devs,
  30.                 const cl_device_id *devs,
  31.                 void (CL_CALLBACK *pfn_notify)(const char *, const void *,
  32.                                                size_t, void *),
  33.                 void *user_data, cl_int *errcode_ret) try {
  34.    auto mprops = property_map(props);
  35.  
  36.    if (!devs || !num_devs ||
  37.        (!pfn_notify && user_data))
  38.       throw error(CL_INVALID_VALUE);
  39.  
  40.    if (any_of(is_zero<cl_device_id>, devs, devs + num_devs))
  41.       throw error(CL_INVALID_DEVICE);
  42.  
  43.    for (auto p : mprops) {
  44.       if (p.first != CL_CONTEXT_PLATFORM)
  45.          throw error(CL_INVALID_PROPERTY);
  46.    }
  47.  
  48.    ret_error(errcode_ret, CL_SUCCESS);
  49.    return new context(
  50.       property_vector(mprops),
  51.       std::vector<cl_device_id>(devs, devs + num_devs));
  52.  
  53. } catch(error &e) {
  54.    ret_error(errcode_ret, e);
  55.    return NULL;
  56. }
  57.  
  58. PUBLIC cl_context
  59. clCreateContextFromType(const cl_context_properties *props,
  60.                         cl_device_type type,
  61.                         void (CL_CALLBACK *pfn_notify)(
  62.                            const char *, const void *, size_t, void *),
  63.                         void *user_data, cl_int *errcode_ret) try {
  64.    cl_platform_id platform;
  65.    cl_uint num_platforms;
  66.    cl_device_id dev;
  67.    cl_int ret;
  68.  
  69.    ret = clGetPlatformIDs(1, &platform, &num_platforms);
  70.    if (ret || !num_platforms)
  71.       throw error(CL_INVALID_PLATFORM);
  72.  
  73.    ret = clGetDeviceIDs(platform, type, 1, &dev, 0);
  74.    if (ret)
  75.       throw error(CL_DEVICE_NOT_FOUND);
  76.  
  77.    return clCreateContext(props, 1, &dev, pfn_notify, user_data, errcode_ret);
  78.  
  79. } catch(error &e) {
  80.    ret_error(errcode_ret, e);
  81.    return NULL;
  82. }
  83.  
  84. PUBLIC cl_int
  85. clRetainContext(cl_context ctx) {
  86.    if (!ctx)
  87.       return CL_INVALID_CONTEXT;
  88.  
  89.    ctx->retain();
  90.    return CL_SUCCESS;
  91. }
  92.  
  93. PUBLIC cl_int
  94. clReleaseContext(cl_context ctx) {
  95.    if (!ctx)
  96.       return CL_INVALID_CONTEXT;
  97.  
  98.    if (ctx->release())
  99.       delete ctx;
  100.  
  101.    return CL_SUCCESS;
  102. }
  103.  
  104. PUBLIC cl_int
  105. clGetContextInfo(cl_context ctx, cl_context_info param,
  106.                  size_t size, void *buf, size_t *size_ret) {
  107.    if (!ctx)
  108.       return CL_INVALID_CONTEXT;
  109.  
  110.    switch (param) {
  111.    case CL_CONTEXT_REFERENCE_COUNT:
  112.       return scalar_property<cl_uint>(buf, size, size_ret, ctx->ref_count());
  113.  
  114.    case CL_CONTEXT_NUM_DEVICES:
  115.       return scalar_property<cl_uint>(buf, size, size_ret, ctx->devs.size());
  116.  
  117.    case CL_CONTEXT_DEVICES:
  118.       return vector_property<cl_device_id>(buf, size, size_ret, ctx->devs);
  119.  
  120.    case CL_CONTEXT_PROPERTIES:
  121.       return vector_property<cl_context_properties>(buf, size, size_ret,
  122.                                                     ctx->props());
  123.  
  124.    default:
  125.       return CL_INVALID_VALUE;
  126.    }
  127. }
  128.