starpu_stdlib.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2013,2015-2017,2019 CNRS
  4. * Copyright (C) 2017 Inria
  5. * Copyright (C) 2010-2016,2019 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. #ifndef __STARPU_STDLIB_H__
  19. #define __STARPU_STDLIB_H__
  20. #include <starpu.h>
  21. #ifdef __cplusplus
  22. extern "C"
  23. {
  24. #endif
  25. /**
  26. @defgroup API_Standard_Memory_Library Standard Memory Library
  27. @{
  28. */
  29. /**
  30. Value passed to the function starpu_malloc_flags() to indicate the
  31. memory allocation should be pinned.
  32. */
  33. #define STARPU_MALLOC_PINNED ((1ULL)<<1)
  34. /**
  35. Value passed to the function starpu_malloc_flags() to indicate the
  36. memory allocation should be in the limit defined by the environment
  37. variables \ref STARPU_LIMIT_CUDA_devid_MEM, \ref
  38. STARPU_LIMIT_CUDA_MEM, \ref STARPU_LIMIT_OPENCL_devid_MEM, \ref
  39. STARPU_LIMIT_OPENCL_MEM and \ref STARPU_LIMIT_CPU_MEM (see Section
  40. \ref HowToLimitMemoryPerNode).
  41. If no memory is available, it tries to reclaim memory from StarPU.
  42. Memory allocated this way needs to be freed by calling the function
  43. starpu_free_flags() with the same flag.
  44. */
  45. #define STARPU_MALLOC_COUNT ((1ULL)<<2)
  46. /**
  47. Value passed to the function starpu_malloc_flags() along
  48. ::STARPU_MALLOC_COUNT to indicate that while the memory allocation
  49. should be kept in the limits defined for ::STARPU_MALLOC_COUNT, no
  50. reclaiming should be performed by starpu_malloc_flags() itself,
  51. thus potentially overflowing the memory node a bit. StarPU will
  52. reclaim memory after next task termination, according to the \ref
  53. STARPU_MINIMUM_AVAILABLE_MEM, \ref STARPU_TARGET_AVAILABLE_MEM,
  54. \ref STARPU_MINIMUM_CLEAN_BUFFERS, and \ref
  55. STARPU_TARGET_CLEAN_BUFFERS environment variables. If
  56. ::STARPU_MEMORY_WAIT is set, no overflowing will happen,
  57. starpu_malloc_flags() will wait for other eviction mechanisms to
  58. release enough memory.
  59. */
  60. #define STARPU_MALLOC_NORECLAIM ((1ULL)<<3)
  61. /**
  62. Value passed to starpu_memory_allocate() to specify that the
  63. function should wait for the requested amount of memory to become
  64. available, and atomically allocate it.
  65. */
  66. #define STARPU_MEMORY_WAIT ((1ULL)<<4)
  67. /**
  68. Value passed to starpu_memory_allocate() to specify that the
  69. function should allocate the amount of memory, even if that means
  70. overflowing the total size of the memory node.
  71. */
  72. #define STARPU_MEMORY_OVERFLOW ((1ULL)<<5)
  73. /**
  74. Value passed to the function starpu_malloc_flags() to indicate that
  75. when StarPU is using simgrid, the allocation can be "folded", i.e.
  76. a memory area is allocated, but its content is actually a replicate
  77. of the same memory area, to avoid having to actually allocate that
  78. much memory . This thus allows to have a memory area that does not
  79. actually consumes memory, to which one can read from and write to
  80. normally, but get bogus values.
  81. */
  82. #define STARPU_MALLOC_SIMULATION_FOLDED ((1ULL)<<6)
  83. /**
  84. @deprecated
  85. Equivalent to starpu_malloc(). This macro is provided to avoid
  86. breaking old codes.
  87. */
  88. #define starpu_data_malloc_pinned_if_possible starpu_malloc
  89. /**
  90. @deprecated
  91. Equivalent to starpu_free(). This macro is provided to avoid
  92. breaking old codes.
  93. */
  94. #define starpu_data_free_pinned_if_possible starpu_free
  95. /**
  96. Set an alignment constraints for starpu_malloc() allocations. \p
  97. align must be a power of two. This is for instance called
  98. automatically by the OpenCL driver to specify its own alignment
  99. constraints.
  100. */
  101. void starpu_malloc_set_align(size_t align);
  102. /**
  103. Allocate data of the given size \p dim in main memory, and return
  104. the pointer to the allocated data through \p A. It will also try to
  105. pin it in CUDA or OpenCL, so that data transfers from this buffer
  106. can be asynchronous, and thus permit data transfer and computation
  107. overlapping. The allocated buffer must be freed thanks to the
  108. starpu_free() function.
  109. */
  110. int starpu_malloc(void **A, size_t dim);
  111. /**
  112. Free memory which has previously been allocated with
  113. starpu_malloc().
  114. */
  115. int starpu_free(void *A);
  116. /**
  117. Perform a memory allocation based on the constraints defined by the
  118. given flag.
  119. */
  120. int starpu_malloc_flags(void **A, size_t dim, int flags);
  121. /**
  122. Free memory by specifying its size. The given flags should be
  123. consistent with the ones given to starpu_malloc_flags() when
  124. allocating the memory.
  125. */
  126. int starpu_free_flags(void *A, size_t dim, int flags);
  127. typedef int (*starpu_malloc_hook)(unsigned dst_node, void **A, size_t dim, int flags);
  128. typedef int (*starpu_free_hook)(unsigned dst_node, void *A, size_t dim, int flags);
  129. /**
  130. Set allocation functions to be used by StarPU. By default, StarPU
  131. will use \c malloc() (or \c cudaHostAlloc() if CUDA GPUs are used)
  132. for all its data handle allocations. The application can specify
  133. another allocation primitive by calling this. The malloc_hook
  134. should pass the allocated pointer through the \c A parameter, and
  135. return 0 on success. On allocation failure, it should return
  136. -ENOMEM. The \c flags parameter contains ::STARPU_MALLOC_PINNED if
  137. the memory should be pinned by the hook for GPU transfer
  138. efficiency. The hook can use starpu_memory_pin() to achieve this.
  139. The \c dst_node parameter is the starpu memory node, one can
  140. convert it to an hwloc logical id with
  141. starpu_memory_nodes_numa_id_to_hwloclogid() or to an OS NUMA number
  142. with starpu_memory_nodes_numa_devid_to_id().
  143. */
  144. void starpu_malloc_set_hooks(starpu_malloc_hook malloc_hook, starpu_free_hook free_hook);
  145. /**
  146. Pin the given memory area, so that CPU-GPU transfers can be done
  147. asynchronously with DMAs. The memory must be unpinned with
  148. starpu_memory_unpin() before being freed. Return 0 on success, -1
  149. on error.
  150. */
  151. int starpu_memory_pin(void *addr, size_t size);
  152. /**
  153. Unpin the given memory area previously pinned with
  154. starpu_memory_pin(). Return 0 on success, -1 on error.
  155. */
  156. int starpu_memory_unpin(void *addr, size_t size);
  157. /**
  158. If a memory limit is defined on the given node (see Section \ref
  159. HowToLimitMemoryPerNode), return the amount of total memory on the
  160. node. Otherwise return -1.
  161. */
  162. starpu_ssize_t starpu_memory_get_total(unsigned node);
  163. /**
  164. If a memory limit is defined on the given node (see Section \ref
  165. HowToLimitMemoryPerNode), return the amount of available memory on
  166. the node. Otherwise return -1.
  167. */
  168. starpu_ssize_t starpu_memory_get_available(unsigned node);
  169. /**
  170. Return the amount of total memory on all memory nodes for whose a
  171. memory limit is defined (see Section \ref HowToLimitMemoryPerNode).
  172. */
  173. starpu_ssize_t starpu_memory_get_total_all_nodes();
  174. /**
  175. Return the amount of available memory on all memory nodes for whose
  176. a memory limit is defined (see Section \ref
  177. HowToLimitMemoryPerNode).
  178. */
  179. starpu_ssize_t starpu_memory_get_available_all_nodes();
  180. /**
  181. If a memory limit is defined on the given node (see Section \ref
  182. HowToLimitMemoryPerNode), try to allocate some of it. This does not
  183. actually allocate memory, but only accounts for it. This can be
  184. useful when the application allocates data another way, but want
  185. StarPU to be aware of the allocation size e.g. for memory
  186. reclaiming.
  187. By default, return <c>-ENOMEM</c> if there is not enough room on
  188. the given node. \p flags can be either ::STARPU_MEMORY_WAIT or
  189. ::STARPU_MEMORY_OVERFLOW to change this.
  190. */
  191. int starpu_memory_allocate(unsigned node, size_t size, int flags);
  192. /**
  193. If a memory limit is defined on the given node (see Section \ref
  194. HowToLimitMemoryPerNode), free some of it. This does not actually
  195. free memory, but only accounts for it, like
  196. starpu_memory_allocate(). The amount does not have to be exactly
  197. the same as what was passed to starpu_memory_allocate(), only the
  198. eventual amount needs to be the same, i.e. one call to
  199. starpu_memory_allocate() can be followed by several calls to
  200. starpu_memory_deallocate() to declare the deallocation piece by
  201. piece.
  202. */
  203. void starpu_memory_deallocate(unsigned node, size_t size);
  204. /**
  205. If a memory limit is defined on the given node (see Section \ref
  206. HowToLimitMemoryPerNode), this will wait for \p size bytes to
  207. become available on \p node. Of course, since another thread may be
  208. allocating memory concurrently, this does not necessarily mean that
  209. this amount will be actually available, just that it was reached.
  210. To atomically wait for some amount of memory and reserve it,
  211. starpu_memory_allocate() should be used with the
  212. ::STARPU_MEMORY_WAIT flag.
  213. */
  214. void starpu_memory_wait_available(unsigned node, size_t size);
  215. void starpu_sleep(float nb_sec);
  216. /** @} */
  217. #ifdef __cplusplus
  218. }
  219. #endif
  220. #endif /* __STARPU_STDLIB_H__ */