cl_createcontext.c 3.9 KB

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