starpu_stdlib.h 9.9 KB

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