cl_createcontext.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010,2011 University of Bordeaux
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include "socl.h"
  17. static void release_callback_context(void * e) {
  18. cl_context context = (cl_context)e;
  19. /* Destruct object */
  20. if (context->properties != NULL)
  21. free(context->properties);
  22. free(context->devices);
  23. }
  24. CL_API_ENTRY cl_context CL_API_CALL
  25. soclCreateContext(const cl_context_properties * properties,
  26. cl_uint num_devices,
  27. const cl_device_id * devices,
  28. void (*pfn_notify)(const char *, const void *, size_t, void *),
  29. void * user_data,
  30. cl_int * errcode_ret) CL_API_SUFFIX__VERSION_1_0
  31. {
  32. if (pfn_notify == NULL && user_data != NULL) {
  33. if (errcode_ret != NULL)
  34. *errcode_ret = CL_INVALID_VALUE;
  35. return NULL;
  36. }
  37. //Check properties
  38. if (properties != NULL) {
  39. const cl_context_properties *p = properties;
  40. int i = 0;
  41. while (p[i] != 0) {
  42. switch (p[i]) {
  43. case CL_CONTEXT_PLATFORM:
  44. i++;
  45. if (p[i] != (cl_context_properties)&socl_platform) {
  46. if (errcode_ret != NULL)
  47. *errcode_ret = CL_INVALID_PLATFORM;
  48. return NULL;
  49. }
  50. break;
  51. }
  52. i++;
  53. }
  54. }
  55. cl_context ctx;
  56. ctx = (cl_context)gc_entity_alloc(sizeof(struct _cl_context), release_callback_context);
  57. if (ctx == NULL) {
  58. if (errcode_ret != NULL)
  59. *errcode_ret = CL_OUT_OF_HOST_MEMORY;
  60. return NULL;
  61. }
  62. ctx->num_properties = 0;
  63. ctx->properties = NULL;
  64. // Cache properties
  65. if (properties != NULL) {
  66. //Count properties
  67. const cl_context_properties * p = properties;
  68. do {
  69. ctx->num_properties++;
  70. p++;
  71. } while (*p != 0);
  72. //Copy properties
  73. ctx->properties = malloc(sizeof(cl_context_properties) * ctx->num_properties);
  74. memcpy(ctx->properties, properties, sizeof(cl_context_properties) * ctx->num_properties);
  75. }
  76. ctx->pfn_notify = pfn_notify;
  77. ctx->user_data = user_data;
  78. ctx->num_devices = num_devices;
  79. #ifdef DEBUG
  80. static int id = 0;
  81. ctx->id = id++;
  82. #endif
  83. ctx->devices = malloc(sizeof(cl_device_id) * num_devices);
  84. memcpy(ctx->devices, devices, sizeof(cl_device_id)*num_devices);
  85. if (errcode_ret != NULL)
  86. *errcode_ret = CL_SUCCESS;
  87. return ctx;
  88. }