cl_createcontext.c 4.0 KB

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