cl_createcontext.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011 Inria
  4. * Copyright (C) 2012-2013,2017 CNRS
  5. * Copyright (C) 2010-2013 Université de Bordeaux
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include "socl.h"
  19. static void release_callback_context(void * e)
  20. {
  21. cl_context context = (cl_context)e;
  22. /* Destruct object */
  23. if (context->properties != NULL)
  24. free(context->properties);
  25. //FIXME: should we free StarPU contexts?
  26. //starpu_sched_ctx_finished_submit(context->sched_ctx);
  27. free(context->devices);
  28. }
  29. static char * defaultScheduler = "dmda";
  30. static char * defaultName = "default";
  31. CL_API_ENTRY cl_context CL_API_CALL
  32. soclCreateContext(const cl_context_properties * properties,
  33. cl_uint num_devices,
  34. const cl_device_id * devices,
  35. void (*pfn_notify)(const char *, const void *, size_t, void *),
  36. void * user_data,
  37. cl_int * errcode_ret) CL_API_SUFFIX__VERSION_1_0
  38. {
  39. if (pfn_notify == NULL && user_data != NULL)
  40. {
  41. if (errcode_ret != NULL)
  42. *errcode_ret = CL_INVALID_VALUE;
  43. return NULL;
  44. }
  45. //Check properties
  46. if (properties != NULL)
  47. {
  48. const cl_context_properties *p = properties;
  49. int i = 0;
  50. while (p[i] != 0)
  51. {
  52. switch (p[i])
  53. {
  54. case CL_CONTEXT_PLATFORM:
  55. i++;
  56. if (p[i] != (cl_context_properties)&socl_platform)
  57. {
  58. if (errcode_ret != NULL)
  59. *errcode_ret = CL_INVALID_PLATFORM;
  60. return NULL;
  61. }
  62. break;
  63. case CL_CONTEXT_SCHEDULER_SOCL:
  64. case CL_CONTEXT_NAME_SOCL:
  65. i++;
  66. if (p[i] == 0)
  67. {
  68. if (errcode_ret != NULL)
  69. *errcode_ret = CL_INVALID_PROPERTY;
  70. return NULL;
  71. }
  72. break;
  73. }
  74. i++;
  75. }
  76. }
  77. cl_context ctx;
  78. ctx = (cl_context)gc_entity_alloc(sizeof(struct _cl_context), release_callback_context, "context");
  79. if (ctx == NULL)
  80. {
  81. if (errcode_ret != NULL)
  82. *errcode_ret = CL_OUT_OF_HOST_MEMORY;
  83. return NULL;
  84. }
  85. ctx->num_properties = 0;
  86. ctx->properties = NULL;
  87. char * sched = getenv("STARPU_SCHED");
  88. char * scheduler = sched == NULL ? defaultScheduler : sched;
  89. char * name = defaultName;
  90. // Properties
  91. if (properties != NULL)
  92. {
  93. //Count properties
  94. const cl_context_properties * p = properties;
  95. do
  96. {
  97. ctx->num_properties++;
  98. p++;
  99. } while (*p != 0);
  100. //Copy properties
  101. ctx->properties = malloc(sizeof(cl_context_properties) * ctx->num_properties);
  102. memcpy(ctx->properties, properties, sizeof(cl_context_properties) * ctx->num_properties);
  103. //Selected scheduler
  104. cl_uint i = 0;
  105. for (i=0; i<ctx->num_properties; i++)
  106. {
  107. if (p[i] == CL_CONTEXT_SCHEDULER_SOCL)
  108. {
  109. i++;
  110. scheduler = (char*)p[i];
  111. }
  112. if (p[i] == CL_CONTEXT_NAME_SOCL)
  113. {
  114. i++;
  115. name = (char*)p[i];
  116. }
  117. }
  118. }
  119. ctx->pfn_notify = pfn_notify;
  120. ctx->user_data = user_data;
  121. ctx->num_devices = num_devices;
  122. #ifdef DEBUG
  123. static int id = 0;
  124. ctx->id = id++;
  125. #endif
  126. ctx->devices = malloc(sizeof(cl_device_id) * num_devices);
  127. memcpy(ctx->devices, devices, sizeof(cl_device_id)*num_devices);
  128. // Create context
  129. int workers[num_devices];
  130. unsigned int i;
  131. for (i=0; i<num_devices; i++)
  132. {
  133. workers[i] = ctx->devices[i]->worker_id;
  134. }
  135. ctx->sched_ctx = starpu_sched_ctx_create(workers, num_devices, name, STARPU_SCHED_CTX_POLICY_NAME, scheduler, 0);
  136. if (errcode_ret != NULL)
  137. *errcode_ret = CL_SUCCESS;
  138. return ctx;
  139. }