cl_createbuffer.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2020 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_memobject(void * e)
  18. {
  19. cl_mem mem = (cl_mem)e;
  20. /* Release references */
  21. gc_entity_unstore(&mem->context);
  22. //Delete this mem_object from the mem_object list
  23. mem_object_release(mem);
  24. /* Destruct object */
  25. starpu_data_unregister_submit(mem->handle);
  26. if (!(mem->flags & CL_MEM_USE_HOST_PTR))
  27. free(mem->ptr);
  28. }
  29. /**
  30. * \brief Create a buffer
  31. *
  32. * A buffer has always an allocated region in host memory. If CL_MEM_USE_HOST_PTR
  33. * is set, we use memory pointed by host_ptr, otherwise some host memory is
  34. * allocated.
  35. *
  36. * If CL_MEM_USE_HOST_PTR or CL_MEM_ALLOC_HOST_PTR are set, memory pointed by host_ptr
  37. * is not coherent. To enforce coherency, you have to map the buffer (clEnqueueMapBuffer).
  38. *
  39. * If CL_MEM_COPY_HOST_PTR is set, the buffer will be duplicated in host memory. You
  40. * should avoid it.
  41. *
  42. */
  43. CL_API_SUFFIX__VERSION_1_0
  44. CL_API_ENTRY cl_mem CL_API_CALL
  45. soclCreateBuffer(cl_context context,
  46. cl_mem_flags flags,
  47. size_t size,
  48. void * host_ptr,
  49. cl_int * errcode_ret)
  50. {
  51. cl_mem mem;
  52. if (errcode_ret != NULL)
  53. *errcode_ret = CL_SUCCESS;
  54. //Check flags
  55. if (((flags & CL_MEM_READ_ONLY) && (flags & CL_MEM_WRITE_ONLY))
  56. || ((flags & CL_MEM_READ_WRITE) && (flags & CL_MEM_READ_ONLY))
  57. || ((flags & CL_MEM_READ_WRITE) && (flags & CL_MEM_WRITE_ONLY))
  58. || ((flags & CL_MEM_USE_HOST_PTR) && (flags & CL_MEM_ALLOC_HOST_PTR))
  59. || ((flags & CL_MEM_USE_HOST_PTR) && (flags & CL_MEM_COPY_HOST_PTR)))
  60. {
  61. if (errcode_ret != NULL)
  62. *errcode_ret = CL_INVALID_VALUE;
  63. return NULL;
  64. }
  65. if (size == 0)
  66. {
  67. if (errcode_ret != NULL)
  68. *errcode_ret = CL_INVALID_BUFFER_SIZE;
  69. return NULL;
  70. }
  71. if ((host_ptr == NULL && (flags & (CL_MEM_USE_HOST_PTR | CL_MEM_COPY_HOST_PTR)))
  72. || (host_ptr != NULL && !(flags & (CL_MEM_USE_HOST_PTR | CL_MEM_COPY_HOST_PTR))))
  73. {
  74. if (errcode_ret != NULL)
  75. *errcode_ret = CL_INVALID_HOST_PTR;
  76. return NULL;
  77. }
  78. //Alloc cl_mem structure
  79. mem = (cl_mem)gc_entity_alloc(sizeof(struct _cl_mem), release_callback_memobject, "buffer");
  80. if (mem == NULL)
  81. {
  82. if (errcode_ret != NULL)
  83. *errcode_ret = CL_OUT_OF_HOST_MEMORY;
  84. return NULL;
  85. }
  86. mem->ptr = NULL;
  87. mem->map_count = 0;
  88. gc_entity_store(&mem->context, context);
  89. mem->flags = flags;
  90. mem->size = size;
  91. mem->host_ptr = host_ptr;
  92. #ifdef DEBUG
  93. static int id = 0;
  94. mem->id = id++;
  95. #endif
  96. mem_object_store(mem);
  97. //TODO: we shouldn't allocate the buffer ourselves. StarPU allocates it if a NULL pointer is given
  98. // If not MEM_USE_HOST_PTR, we need to alloc the buffer ourselves
  99. if (!(flags & CL_MEM_USE_HOST_PTR))
  100. {
  101. mem->ptr = valloc(size);
  102. if (mem->ptr == NULL)
  103. {
  104. if (errcode_ret != NULL)
  105. *errcode_ret = CL_MEM_OBJECT_ALLOCATION_FAILURE;
  106. free(mem);
  107. return NULL;
  108. }
  109. //The buffer doesn't contain meaningful data
  110. mem->scratch = 1;
  111. }
  112. else
  113. {
  114. //The buffer may contain meaningful data
  115. mem->scratch = 0;
  116. mem->ptr = host_ptr;
  117. }
  118. // Access mode
  119. mem->mode = (flags & CL_MEM_READ_ONLY) ? CL_MEM_READ_ONLY :
  120. (flags & CL_MEM_WRITE_ONLY) ? CL_MEM_WRITE_ONLY : CL_MEM_READ_WRITE;
  121. // Perform data copy if necessary
  122. if (flags & CL_MEM_COPY_HOST_PTR)
  123. memcpy(mem->ptr, host_ptr, size);
  124. // Create StarPU buffer (on home node? what's this?)
  125. starpu_variable_data_register(&mem->handle, STARPU_MAIN_RAM, (uintptr_t)mem->ptr, size);
  126. DEBUG_MSG("[Buffer %d] Initialized (cl_mem %p handle %p)\n", mem->id, mem, mem->handle);
  127. return mem;
  128. }