orsl-lite.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. Copyright (c) 2014-2016 Intel Corporation. All Rights Reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions
  5. are met:
  6. * Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. * Neither the name of Intel Corporation nor the names of its
  12. contributors may be used to endorse or promote products derived
  13. from this software without specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  16. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  18. HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  20. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #ifndef _ORSL_LITE_H_
  27. #define _ORSL_LITE_H_
  28. #ifndef TARGET_WINNT
  29. #include <sched.h>
  30. #else
  31. #define cpu_set_t int
  32. #endif
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. /** Type of a ORSLBusySet */
  37. typedef enum ORSLBusySetType {
  38. BUSY_SET_EMPTY = 0, /**< Empty set */
  39. BUSY_SET_PARTIAL = 1, /**< Non-empty set that omits some threads */
  40. BUSY_SET_FULL = 2 /**< A set that includes all threads on the card */
  41. } BusySetType;
  42. /** ORSLBusySet encapsulation */
  43. typedef struct ORSLBusySet {
  44. BusySetType type; /**< Set type */
  45. #ifdef __linux__
  46. cpu_set_t cpu_set; /**< CPU mask (unused for BUSY_SET_EMPTY and
  47. BUSY_SET_PARTIAL sets) represented by the standard
  48. Linux CPU set type -- cpu_set_t. Threads are numbered
  49. starting from 0. The maximal possible thread number
  50. is system-specific. See CPU_SET(3) family of macros
  51. for more details. Unused in ORSL Lite. */
  52. #endif
  53. } ORSLBusySet;
  54. /** Client tag */
  55. typedef char* ORSLTag;
  56. /** Maximal length of tag in characters */
  57. #define ORSL_MAX_TAG_LEN 128
  58. /** Maximal number of cards that can be managed by ORSL */
  59. #define ORSL_MAX_CARDS 32
  60. /** Reserves computational resources on a set of cards. Blocks.
  61. *
  62. * If any of the resources cannot be reserved, this function will block until
  63. * they become available. Reservation can be recursive if performed by the
  64. * same tag. A recursively reserved resource must be released the same number
  65. * of times it was reserved.
  66. *
  67. * @see ORSLTryReserve
  68. *
  69. * @param[in] n Number of cards to reserve resources on. Cannot be < 0
  70. * or > ORSL_MAX_CARDS.
  71. *
  72. * @param[in] inds Indices of the cards: an integer array with n elements.
  73. * Cannot be NULL if n > 0. Valid card indices are from 0
  74. * to ORSL_MAX_CARDS-1. Cannot contain duplicate elements.
  75. *
  76. * @param[in] bsets Requested resources on each of the card. Cannot be NULL
  77. * if n > 0.
  78. *
  79. * @param[in] tag ORSLTag of the calling client. Cannot be NULL. Length
  80. * must not exeed ORSL_MAX_TAG_LEN.
  81. *
  82. * @returns 0 if the resources were successfully reserved
  83. *
  84. * @returns EINVAL if any of the arguments is invalid
  85. *
  86. * @returns EAGAIN limit of recursive reservations reached
  87. * (not in ORSL Lite)
  88. *
  89. * @returns ENOSYS (in ORSL Lite) if type of any of the busy sets is
  90. * equal to BUSY_SET_PARTIAL
  91. */
  92. int ORSLReserve(const int n, const int *__restrict inds,
  93. const ORSLBusySet *__restrict bsets,
  94. const ORSLTag __restrict tag);
  95. /** Reserves computational resources on a set of cards. Does not block.
  96. *
  97. * If any of the resources cannot be reserved, this function will return
  98. * immediately. Reservation can be recursive if performed by the same tag.
  99. * A recursively reserved resource must be released the same number of times
  100. * it was reserved.
  101. *
  102. * @see ORSLReserve
  103. *
  104. * @param[in] n Number of cards to reserve resources on. Cannot be < 0
  105. * or > ORSL_MAX_CARDS.
  106. *
  107. * @param[in] inds Indices of the cards: an integer array with n elements.
  108. * Cannot be NULL if n > 0. Valid card indices are from 0
  109. * to ORSL_MAX_CARDS-1. Cannot contain duplicate elements.
  110. *
  111. * @param[inout] bsets Requested resources on each of the card. Cannot be
  112. * NULL if n > 0.
  113. *
  114. * @param[in] tag ORSLTag of the calling client. Cannot be NULL. Length
  115. * must not exceed ORSL_MAX_TAG_LEN.
  116. *
  117. * @returns 0 if the resources were successfully reserved
  118. *
  119. * @returns EBUSY if some of the requested resources are busy
  120. *
  121. * @returns EINVAL if any of the arguments is invalid
  122. *
  123. * @returns EAGAIN limit of recursive reservations reached
  124. * (not in ORSL Lite)
  125. *
  126. * @returns ENOSYS (in ORSL Lite) if type of any of the busy sets is
  127. * equal to BUSY_SET_PARTIAL
  128. */
  129. int ORSLTryReserve(const int n, const int *__restrict inds,
  130. const ORSLBusySet *__restrict bsets,
  131. const ORSLTag __restrict tag);
  132. /** Granularify of partial reservation */
  133. typedef enum ORSLPartialGranularity {
  134. GRAN_CARD = 0, /**< Card granularity */
  135. GRAN_THREAD = 1 /**< Thread granularity */
  136. } ORSLPartialGranularity;
  137. /** Requests reservation of some of computational resources on a set of cards.
  138. * Does not block. Updates user-provided bsets to indicate which resources
  139. * were reserved.
  140. *
  141. * If any of the resources cannot be reserved, this function will update busy
  142. * sets provided by the caller to reflect what resources were actually
  143. * reserved. This function supports two granularity modes: 'card' and
  144. * 'thread'. When granularity is set to 'card', a failure to reserve a thread
  145. * on the card will imply that reservation has failed for the whole card. When
  146. * granularity is set to 'thread', reservation on a card will be considered
  147. * successful as long as at least one thread on the card was successfully
  148. * reserved. Reservation can be recursive if performed by the same tag. A
  149. * recursively reserved resource must be released the same number of times it
  150. * was reserved.
  151. *
  152. * @param[in] gran Reservation granularity
  153. *
  154. * @param[in] n Number of cards to reserve resources on. Cannot be < 0
  155. * or > ORSL_MAX_CARDS.
  156. *
  157. * @param[in] inds Indices of the cards: an integer array with n elements.
  158. * Cannot be NULL if n > 0. Valid card indices are from 0
  159. * to ORSL_MAX_CARDS-1. Cannot contain duplicate elements.
  160. *
  161. * @param[in] bsets Requested resources on each of the card. Cannot be NULL
  162. * if n > 0.
  163. *
  164. * @param[in] tag ORSLTag of the calling client. Cannot be NULL. Length
  165. * must not exceed ORSL_MAX_TAG_LEN.
  166. *
  167. * @returns 0 if at least some of the resources were successfully
  168. * reserved
  169. *
  170. * @returns EBUSY if all of the requested resources are busy
  171. *
  172. * @returns EINVAL if any of the arguments is invalid
  173. *
  174. * @returns EAGAIN limit of recursive reservations reached
  175. * (not in ORSL Lite)
  176. *
  177. * @returns ENOSYS (in ORSL Lite) if type of any of the busy sets is
  178. * equal to BUSY_SET_PARTIAL
  179. */
  180. int ORSLReservePartial(const ORSLPartialGranularity gran, const int n,
  181. const int *__restrict inds,
  182. ORSLBusySet *__restrict bsets,
  183. const ORSLTag __restrict tag);
  184. /** Releases previously reserved computational resources on a set of cards.
  185. *
  186. * This function will fail if any of the resources to be released were not
  187. * reserved by the calling client.
  188. *
  189. * @see ORSLReserve
  190. * @see ORSLTryReserve
  191. * @see ORSLReservePartial
  192. *
  193. * @param[in] n Number of cards to reserve resources on. Cannot be < 0
  194. * or > ORSL_MAX_CARDS.
  195. *
  196. * @param[in] inds Indices of the cards: an integer array with n elements.
  197. * Cannot be NULL if n > 0. Valid card indices are from 0
  198. * to ORSL_MAX_CARDS-1. Cannot contain duplicate elements.
  199. *
  200. * @param[in] bsets Requested resources on each of the card. Cannot be NULL
  201. * if n > 0.
  202. *
  203. * @param[in] tag ORSLTag of the calling client. Cannot be NULL. Length
  204. * must not exceed ORSL_MAX_TAG_LEN.
  205. *
  206. * @returns 0 if the resources were successfully released
  207. *
  208. * @returns EINVAL if any of the arguments is invalid
  209. *
  210. * @returns EPERM the calling client did not reserve some of the
  211. * resources it is trying to release.
  212. *
  213. * @returns ENOSYS (in ORSL Lite) if type of any of the busy sets is
  214. * equal to BUSY_SET_PARTIAL
  215. */
  216. int ORSLRelease(const int n, const int *__restrict inds,
  217. const ORSLBusySet *__restrict bsets,
  218. const ORSLTag __restrict tag);
  219. #ifdef __cplusplus
  220. }
  221. #endif
  222. #endif