starpu_stdlib.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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() function.
  107. */
  108. int starpu_malloc(void **A, size_t dim);
  109. /**
  110. Free memory which has previously been allocated with
  111. starpu_malloc().
  112. */
  113. int starpu_free(void *A);
  114. /**
  115. Perform a memory allocation based on the constraints defined by the
  116. given flag.
  117. */
  118. int starpu_malloc_flags(void **A, size_t dim, int flags);
  119. /**
  120. Free memory by specifying its size. The given flags should be
  121. consistent with the ones given to starpu_malloc_flags() when
  122. allocating the memory.
  123. */
  124. int starpu_free_flags(void *A, size_t dim, int flags);
  125. typedef int (*starpu_malloc_hook)(unsigned dst_node, void **A, size_t dim, int flags);
  126. typedef int (*starpu_free_hook)(unsigned dst_node, void *A, size_t dim, int flags);
  127. /**
  128. Set allocation functions to be used by StarPU. By default, StarPU
  129. will use \c malloc() (or \c cudaHostAlloc() if CUDA GPUs are used)
  130. for all its data handle allocations. The application can specify
  131. another allocation primitive by calling this. The malloc_hook
  132. should pass the allocated pointer through the \c A parameter, and
  133. return 0 on success. On allocation failure, it should return
  134. -ENOMEM. The \c flags parameter contains ::STARPU_MALLOC_PINNED if
  135. the memory should be pinned by the hook for GPU transfer
  136. efficiency. The hook can use starpu_memory_pin() to achieve this.
  137. The \c dst_node parameter is the starpu memory node, one can
  138. convert it to an hwloc logical id with
  139. starpu_memory_nodes_numa_id_to_hwloclogid() or to an OS NUMA number
  140. with starpu_memory_nodes_numa_devid_to_id().
  141. */
  142. void starpu_malloc_set_hooks(starpu_malloc_hook malloc_hook, starpu_free_hook free_hook);
  143. /**
  144. Pin the given memory area, so that CPU-GPU transfers can be done
  145. asynchronously with DMAs. The memory must be unpinned with
  146. starpu_memory_unpin() before being freed. Return 0 on success, -1
  147. on error.
  148. */
  149. int starpu_memory_pin(void *addr, size_t size);
  150. /**
  151. Unpin the given memory area previously pinned with
  152. starpu_memory_pin(). Return 0 on success, -1 on error.
  153. */
  154. int starpu_memory_unpin(void *addr, size_t size);
  155. /**
  156. If a memory limit is defined on the given node (see Section \ref
  157. HowToLimitMemoryPerNode), return the amount of total memory on the
  158. node. Otherwise return -1.
  159. */
  160. starpu_ssize_t starpu_memory_get_total(unsigned node);
  161. /**
  162. If a memory limit is defined on the given node (see Section \ref
  163. HowToLimitMemoryPerNode), return the amount of available memory on
  164. the node. Otherwise return -1.
  165. */
  166. starpu_ssize_t starpu_memory_get_available(unsigned node);
  167. /**
  168. Return the amount of total memory on all memory nodes for whose a
  169. memory limit is defined (see Section \ref HowToLimitMemoryPerNode).
  170. */
  171. starpu_ssize_t starpu_memory_get_total_all_nodes(void);
  172. /**
  173. Return the amount of available memory on all memory nodes for whose
  174. a memory limit is defined (see Section \ref
  175. HowToLimitMemoryPerNode).
  176. */
  177. starpu_ssize_t starpu_memory_get_available_all_nodes(void);
  178. /**
  179. If a memory limit is defined on the given node (see Section \ref
  180. HowToLimitMemoryPerNode), try to allocate some of it. This does not
  181. actually allocate memory, but only accounts for it. This can be
  182. useful when the application allocates data another way, but want
  183. StarPU to be aware of the allocation size e.g. for memory
  184. reclaiming.
  185. By default, return <c>-ENOMEM</c> if there is not enough room on
  186. the given node. \p flags can be either ::STARPU_MEMORY_WAIT or
  187. ::STARPU_MEMORY_OVERFLOW to change this.
  188. */
  189. int starpu_memory_allocate(unsigned node, size_t size, int flags);
  190. /**
  191. If a memory limit is defined on the given node (see Section \ref
  192. HowToLimitMemoryPerNode), free some of it. This does not actually
  193. free memory, but only accounts for it, like
  194. starpu_memory_allocate(). The amount does not have to be exactly
  195. the same as what was passed to starpu_memory_allocate(), only the
  196. eventual amount needs to be the same, i.e. one call to
  197. starpu_memory_allocate() can be followed by several calls to
  198. starpu_memory_deallocate() to declare the deallocation piece by
  199. piece.
  200. */
  201. void starpu_memory_deallocate(unsigned node, size_t size);
  202. /**
  203. If a memory limit is defined on the given node (see Section \ref
  204. HowToLimitMemoryPerNode), this will wait for \p size bytes to
  205. become available on \p node. Of course, since another thread may be
  206. allocating memory concurrently, this does not necessarily mean that
  207. this amount will be actually available, just that it was reached.
  208. To atomically wait for some amount of memory and reserve it,
  209. starpu_memory_allocate() should be used with the
  210. ::STARPU_MEMORY_WAIT flag.
  211. */
  212. void starpu_memory_wait_available(unsigned node, size_t size);
  213. /**
  214. Sleep for the given \p nb_sec seconds.
  215. In simgrid mode, this only sleeps within virtual time.
  216. */
  217. void starpu_sleep(float nb_sec);
  218. /**
  219. Sleep for the given \p nb_micro_sec micro-seconds.
  220. In simgrid mode, this only sleeps within virtual time.
  221. */
  222. void starpu_usleep(float nb_micro_sec);
  223. /**
  224. Account for \p joules J being used.
  225. This is support in simgrid mode, to record how much energy was used, and will
  226. show up in further call to starpu_energy_used().
  227. */
  228. void starpu_energy_use(float joules);
  229. /**
  230. Return the amount of energy having been used in J.
  231. This account the amounts passed to starpu_energy_use(), but also the static
  232. energy use set by the \ref STARPU_IDLE_POWER environment variable.
  233. */
  234. double starpu_energy_used(void);
  235. /** @} */
  236. #ifdef __cplusplus
  237. }
  238. #endif
  239. #endif /* __STARPU_STDLIB_H__ */