cl_createcontext.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. //FIXME: should we free StarPU contexts?
  23. //starpu_sched_ctx_finished_submit(context->sched_ctx);
  24. free(context->devices);
  25. }
  26. static char * defaultScheduler = "dmda";
  27. static char * defaultName = "default";
  28. CL_API_ENTRY cl_context CL_API_CALL
  29. soclCreateContext(const cl_context_properties * properties,
  30. cl_uint num_devices,
  31. const cl_device_id * devices,
  32. void (*pfn_notify)(const char *, const void *, size_t, void *),
  33. void * user_data,
  34. cl_int * errcode_ret) CL_API_SUFFIX__VERSION_1_0
  35. {
  36. if (pfn_notify == NULL && user_data != NULL) {
  37. if (errcode_ret != NULL)
  38. *errcode_ret = CL_INVALID_VALUE;
  39. return NULL;
  40. }
  41. //Check properties
  42. if (properties != NULL) {
  43. const cl_context_properties *p = properties;
  44. int i = 0;
  45. while (p[i] != 0) {
  46. switch (p[i]) {
  47. case CL_CONTEXT_PLATFORM:
  48. i++;
  49. if (p[i] != (cl_context_properties)&socl_platform) {
  50. if (errcode_ret != NULL)
  51. *errcode_ret = CL_INVALID_PLATFORM;
  52. return NULL;
  53. }
  54. break;
  55. case CL_CONTEXT_SCHEDULER_SOCL:
  56. case CL_CONTEXT_NAME_SOCL:
  57. i++;
  58. if (p[i] == 0) {
  59. if (errcode_ret != NULL)
  60. *errcode_ret = CL_INVALID_PROPERTY;
  61. return NULL;
  62. }
  63. break;
  64. }
  65. i++;
  66. }
  67. }
  68. cl_context ctx;
  69. ctx = (cl_context)gc_entity_alloc(sizeof(struct _cl_context), release_callback_context, "context");
  70. if (ctx == NULL) {
  71. if (errcode_ret != NULL)
  72. *errcode_ret = CL_OUT_OF_HOST_MEMORY;
  73. return NULL;
  74. }
  75. ctx->num_properties = 0;
  76. ctx->properties = NULL;
  77. char * sched = getenv("STARPU_SCHED");
  78. char * scheduler = sched == NULL ? defaultScheduler : sched;
  79. char * name = defaultName;
  80. // Properties
  81. if (properties != NULL) {
  82. //Count properties
  83. const cl_context_properties * p = properties;
  84. do {
  85. ctx->num_properties++;
  86. p++;
  87. } while (*p != 0);
  88. //Copy properties
  89. ctx->properties = malloc(sizeof(cl_context_properties) * ctx->num_properties);
  90. memcpy(ctx->properties, properties, sizeof(cl_context_properties) * ctx->num_properties);
  91. //Selected scheduler
  92. cl_uint i = 0;
  93. for (i=0; i<ctx->num_properties; i++) {
  94. if (p[i] == CL_CONTEXT_SCHEDULER_SOCL) {
  95. i++;
  96. scheduler = (char*)p[i];
  97. }
  98. if (p[i] == CL_CONTEXT_NAME_SOCL) {
  99. i++;
  100. name = (char*)p[i];
  101. }
  102. }
  103. }
  104. ctx->pfn_notify = pfn_notify;
  105. ctx->user_data = user_data;
  106. ctx->num_devices = num_devices;
  107. #ifdef DEBUG
  108. static int id = 0;
  109. ctx->id = id++;
  110. #endif
  111. ctx->devices = malloc(sizeof(cl_device_id) * num_devices);
  112. memcpy(ctx->devices, devices, sizeof(cl_device_id)*num_devices);
  113. // Create context
  114. int workers[num_devices];
  115. unsigned int i;
  116. for (i=0; i<num_devices; i++) {
  117. workers[i] = ctx->devices[i]->worker_id;
  118. }
  119. ctx->sched_ctx = starpu_sched_ctx_create(workers, num_devices, name, STARPU_SCHED_CTX_POLICY_NAME, scheduler, 0);
  120. if (errcode_ret != NULL)
  121. *errcode_ret = CL_SUCCESS;
  122. return ctx;
  123. }