offload.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*
  2. Copyright (c) 2014 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. /*
  27. * Include file for Offload API.
  28. */
  29. #ifndef OFFLOAD_H_INCLUDED
  30. #define OFFLOAD_H_INCLUDED
  31. #if defined(LINUX) || defined(FREEBSD)
  32. #include <bits/functexcept.h>
  33. #endif
  34. #include <stddef.h>
  35. #include <omp.h>
  36. #ifdef __cplusplus
  37. extern "C" {
  38. #endif
  39. #define TARGET_ATTRIBUTE __declspec(target(mic))
  40. /*
  41. * The target architecture.
  42. */
  43. typedef enum TARGET_TYPE {
  44. TARGET_NONE, /* Undefine target */
  45. TARGET_HOST, /* Host used as target */
  46. TARGET_MIC /* MIC target */
  47. } TARGET_TYPE;
  48. /*
  49. * The default target type.
  50. */
  51. #define DEFAULT_TARGET_TYPE TARGET_MIC
  52. /*
  53. * The default target number.
  54. */
  55. #define DEFAULT_TARGET_NUMBER 0
  56. /*
  57. * Offload status.
  58. */
  59. typedef enum {
  60. OFFLOAD_SUCCESS = 0,
  61. OFFLOAD_DISABLED, /* offload is disabled */
  62. OFFLOAD_UNAVAILABLE, /* card is not available */
  63. OFFLOAD_OUT_OF_MEMORY, /* not enough memory on device */
  64. OFFLOAD_PROCESS_DIED, /* target process has died */
  65. OFFLOAD_ERROR /* unspecified error */
  66. } _Offload_result;
  67. typedef struct {
  68. _Offload_result result; /* result, see above */
  69. int device_number; /* device number */
  70. size_t data_sent; /* number of bytes sent to the target */
  71. size_t data_received; /* number of bytes received by host */
  72. } _Offload_status;
  73. #define OFFLOAD_STATUS_INIT(x) \
  74. ((x).result = OFFLOAD_DISABLED)
  75. #define OFFLOAD_STATUS_INITIALIZER \
  76. { OFFLOAD_DISABLED, -1, 0, 0 }
  77. /* Offload runtime interfaces */
  78. extern int _Offload_number_of_devices(void);
  79. extern int _Offload_get_device_number(void);
  80. extern int _Offload_get_physical_device_number(void);
  81. extern void* _Offload_shared_malloc(size_t size);
  82. extern void _Offload_shared_free(void *ptr);
  83. extern void* _Offload_shared_aligned_malloc(size_t size, size_t align);
  84. extern void _Offload_shared_aligned_free(void *ptr);
  85. extern int _Offload_signaled(int index, void *signal);
  86. extern void _Offload_report(int val);
  87. /* OpenMP API */
  88. extern void omp_set_default_device(int num) __GOMP_NOTHROW;
  89. extern int omp_get_default_device(void) __GOMP_NOTHROW;
  90. extern int omp_get_num_devices(void) __GOMP_NOTHROW;
  91. /* OpenMP API wrappers */
  92. /* Set num_threads on target */
  93. extern void omp_set_num_threads_target(
  94. TARGET_TYPE target_type,
  95. int target_number,
  96. int num_threads
  97. );
  98. /* Get max_threads from target */
  99. extern int omp_get_max_threads_target(
  100. TARGET_TYPE target_type,
  101. int target_number
  102. );
  103. /* Get num_procs from target */
  104. extern int omp_get_num_procs_target(
  105. TARGET_TYPE target_type,
  106. int target_number
  107. );
  108. /* Set dynamic on target */
  109. extern void omp_set_dynamic_target(
  110. TARGET_TYPE target_type,
  111. int target_number,
  112. int num_threads
  113. );
  114. /* Get dynamic from target */
  115. extern int omp_get_dynamic_target(
  116. TARGET_TYPE target_type,
  117. int target_number
  118. );
  119. /* Set nested on target */
  120. extern void omp_set_nested_target(
  121. TARGET_TYPE target_type,
  122. int target_number,
  123. int nested
  124. );
  125. /* Get nested from target */
  126. extern int omp_get_nested_target(
  127. TARGET_TYPE target_type,
  128. int target_number
  129. );
  130. extern void omp_set_num_threads_target(
  131. TARGET_TYPE target_type,
  132. int target_number,
  133. int num_threads
  134. );
  135. extern int omp_get_max_threads_target(
  136. TARGET_TYPE target_type,
  137. int target_number
  138. );
  139. extern int omp_get_num_procs_target(
  140. TARGET_TYPE target_type,
  141. int target_number
  142. );
  143. extern void omp_set_dynamic_target(
  144. TARGET_TYPE target_type,
  145. int target_number,
  146. int num_threads
  147. );
  148. extern int omp_get_dynamic_target(
  149. TARGET_TYPE target_type,
  150. int target_number
  151. );
  152. extern void omp_set_nested_target(
  153. TARGET_TYPE target_type,
  154. int target_number,
  155. int num_threads
  156. );
  157. extern int omp_get_nested_target(
  158. TARGET_TYPE target_type,
  159. int target_number
  160. );
  161. extern void omp_set_schedule_target(
  162. TARGET_TYPE target_type,
  163. int target_number,
  164. omp_sched_t kind,
  165. int modifier
  166. );
  167. extern void omp_get_schedule_target(
  168. TARGET_TYPE target_type,
  169. int target_number,
  170. omp_sched_t *kind,
  171. int *modifier
  172. );
  173. /* lock API functions */
  174. typedef struct {
  175. omp_lock_t lock;
  176. } omp_lock_target_t;
  177. extern void omp_init_lock_target(
  178. TARGET_TYPE target_type,
  179. int target_number,
  180. omp_lock_target_t *lock
  181. );
  182. extern void omp_destroy_lock_target(
  183. TARGET_TYPE target_type,
  184. int target_number,
  185. omp_lock_target_t *lock
  186. );
  187. extern void omp_set_lock_target(
  188. TARGET_TYPE target_type,
  189. int target_number,
  190. omp_lock_target_t *lock
  191. );
  192. extern void omp_unset_lock_target(
  193. TARGET_TYPE target_type,
  194. int target_number,
  195. omp_lock_target_t *lock
  196. );
  197. extern int omp_test_lock_target(
  198. TARGET_TYPE target_type,
  199. int target_number,
  200. omp_lock_target_t *lock
  201. );
  202. /* nested lock API functions */
  203. typedef struct {
  204. omp_nest_lock_t lock;
  205. } omp_nest_lock_target_t;
  206. extern void omp_init_nest_lock_target(
  207. TARGET_TYPE target_type,
  208. int target_number,
  209. omp_nest_lock_target_t *lock
  210. );
  211. extern void omp_destroy_nest_lock_target(
  212. TARGET_TYPE target_type,
  213. int target_number,
  214. omp_nest_lock_target_t *lock
  215. );
  216. extern void omp_set_nest_lock_target(
  217. TARGET_TYPE target_type,
  218. int target_number,
  219. omp_nest_lock_target_t *lock
  220. );
  221. extern void omp_unset_nest_lock_target(
  222. TARGET_TYPE target_type,
  223. int target_number,
  224. omp_nest_lock_target_t *lock
  225. );
  226. extern int omp_test_nest_lock_target(
  227. TARGET_TYPE target_type,
  228. int target_number,
  229. omp_nest_lock_target_t *lock
  230. );
  231. #ifdef __cplusplus
  232. } /* extern "C" */
  233. /* Namespace for the shared_allocator. */
  234. namespace __offload {
  235. /* This follows the specification for std::allocator. */
  236. /* Forward declaration of the class template. */
  237. template <typename T>
  238. class shared_allocator;
  239. /* Specialization for shared_allocator<void>. */
  240. template <>
  241. class shared_allocator<void> {
  242. public:
  243. typedef void *pointer;
  244. typedef const void *const_pointer;
  245. typedef void value_type;
  246. template <class U> struct rebind { typedef shared_allocator<U> other; };
  247. };
  248. /* Definition of shared_allocator<T>. */
  249. template <class T>
  250. class shared_allocator {
  251. public:
  252. typedef size_t size_type;
  253. typedef ptrdiff_t difference_type;
  254. typedef T *pointer;
  255. typedef const T *const_pointer;
  256. typedef T &reference;
  257. typedef const T &const_reference;
  258. typedef T value_type;
  259. template <class U> struct rebind { typedef shared_allocator<U> other; };
  260. shared_allocator() throw() { }
  261. shared_allocator(const shared_allocator&) throw() { }
  262. template <class U> shared_allocator(const shared_allocator<U>&) throw() { }
  263. ~shared_allocator() throw() { }
  264. pointer address(reference x) const { return &x; }
  265. const_pointer address(const_reference x) const { return &x; }
  266. pointer allocate(
  267. size_type, shared_allocator<void>::const_pointer hint = 0);
  268. void deallocate(pointer p, size_type n);
  269. size_type max_size() const throw() {
  270. return size_type(-1)/sizeof(T);
  271. } /* max_size */
  272. void construct(pointer p, const T& arg) {
  273. ::new (p) T(arg);
  274. } /* construct */
  275. void destroy(pointer p) {
  276. p->~T();
  277. } /* destroy */
  278. };
  279. /* Definition for allocate. */
  280. template <class T>
  281. typename shared_allocator<T>::pointer
  282. shared_allocator<T>::allocate(shared_allocator<T>::size_type s,
  283. shared_allocator<void>::const_pointer) {
  284. /* Allocate from shared memory. */
  285. void *ptr = _Offload_shared_malloc(s*sizeof(T));
  286. if (ptr == 0) std::__throw_bad_alloc();
  287. return static_cast<pointer>(ptr);
  288. } /* allocate */
  289. template <class T>
  290. void shared_allocator<T>::deallocate(pointer p,
  291. shared_allocator<T>::size_type) {
  292. /* Free the shared memory. */
  293. _Offload_shared_free(p);
  294. } /* deallocate */
  295. template <typename _T1, typename _T2>
  296. inline bool operator==(const shared_allocator<_T1> &,
  297. const shared_allocator<_T2> &) throw() {
  298. return true;
  299. } /* operator== */
  300. template <typename _T1, typename _T2>
  301. inline bool operator!=(const shared_allocator<_T1> &,
  302. const shared_allocator<_T2> &) throw() {
  303. return false;
  304. } /* operator!= */
  305. } /* __offload */
  306. #endif /* __cplusplus */
  307. #endif /* OFFLOAD_H_INCLUDED */