cl_createcontext.c 3.7 KB

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