cl_createbuffer.c 4.2 KB

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