offload.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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. /*
  27. * Include file for Offload API.
  28. */
  29. #ifndef OFFLOAD_H_INCLUDED
  30. #define OFFLOAD_H_INCLUDED
  31. #ifdef __cplusplus
  32. #if defined(LINUX) || defined(FREEBSD)
  33. #include <bits/functexcept.h>
  34. #endif
  35. #endif
  36. #include <stddef.h>
  37. #include <omp.h>
  38. #ifdef TARGET_WINNT
  39. // <stdint.h> is incompatible on Windows.
  40. typedef unsigned long long int uint64_t;
  41. typedef signed long long int int64_t;
  42. #else
  43. #include <stdint.h>
  44. #endif // TARGET_WINNT
  45. #ifdef __cplusplus
  46. extern "C" {
  47. #endif
  48. #define TARGET_ATTRIBUTE __declspec(target(mic))
  49. /*
  50. * The target architecture.
  51. */
  52. typedef enum TARGET_TYPE {
  53. TARGET_NONE, /* Undefine target */
  54. TARGET_HOST, /* Host used as target */
  55. TARGET_MIC /* MIC target */
  56. } TARGET_TYPE;
  57. /*
  58. * The default target type.
  59. */
  60. #define DEFAULT_TARGET_TYPE TARGET_MIC
  61. /*
  62. * The default target number.
  63. */
  64. #define DEFAULT_TARGET_NUMBER 0
  65. /*
  66. * Offload status.
  67. */
  68. typedef enum {
  69. OFFLOAD_SUCCESS = 0,
  70. OFFLOAD_DISABLED, /* offload is disabled */
  71. OFFLOAD_UNAVAILABLE, /* card is not available */
  72. OFFLOAD_OUT_OF_MEMORY, /* not enough memory on device */
  73. OFFLOAD_PROCESS_DIED, /* target process has died */
  74. OFFLOAD_ERROR /* unspecified error */
  75. } _Offload_result;
  76. typedef struct {
  77. _Offload_result result; /* result, see above */
  78. int device_number; /* device number */
  79. size_t data_sent; /* number of bytes sent to the target */
  80. size_t data_received; /* number of bytes received by host */
  81. } _Offload_status;
  82. typedef int64_t _Offload_stream;
  83. #define OFFLOAD_STATUS_INIT(x) \
  84. ((x).result = OFFLOAD_DISABLED)
  85. #define OFFLOAD_STATUS_INITIALIZER \
  86. { OFFLOAD_DISABLED, -1, 0, 0 }
  87. /* Offload runtime interfaces */
  88. extern int _Offload_number_of_devices(void);
  89. extern int _Offload_get_device_number(void);
  90. extern int _Offload_get_physical_device_number(void);
  91. /* Offload stream runtime interfaces */
  92. extern _Offload_stream _Offload_stream_create(
  93. int device, // MIC device number
  94. int number_of_cpus // Cores allocated to the stream
  95. );
  96. extern int _Offload_stream_destroy(
  97. int device, // MIC device number
  98. _Offload_stream stream // stream handle
  99. );
  100. extern int _Offload_stream_delete(
  101. _Offload_stream handle // stream handle
  102. );
  103. extern int _Offload_stream_completed(
  104. int device, // MIC device number
  105. _Offload_stream handle // stream handle
  106. );
  107. extern int _Offload_device_streams_completed(
  108. int device // MIC device number
  109. );
  110. extern int _Offload_stream_is_empty(
  111. _Offload_stream handle // stream handle
  112. );
  113. /*
  114. * _Offload_shared_malloc/free are only supported when offload is enabled
  115. * else they are defined to malloc and free
  116. */
  117. #ifdef __INTEL_OFFLOAD
  118. extern void* _Offload_shared_malloc(size_t size);
  119. extern void _Offload_shared_free(void *ptr);
  120. extern void* _Offload_shared_aligned_malloc(size_t size, size_t align);
  121. extern void _Offload_shared_aligned_free(void *ptr);
  122. #else
  123. #include <malloc.h>
  124. #define _Offload_shared_malloc(size) malloc(size)
  125. #define _Offload_shared_free(ptr) free(ptr);
  126. #if defined(_WIN32)
  127. #define _Offload_shared_aligned_malloc(size, align) _aligned_malloc(size, align)
  128. #define _Offload_shared_aligned_free(ptr) _aligned_free(ptr);
  129. #else
  130. #define _Offload_shared_aligned_malloc(size, align) memalign(align, size)
  131. #define _Offload_shared_aligned_free(ptr) free(ptr);
  132. #endif
  133. #endif
  134. extern int _Offload_signaled(int index, void *signal);
  135. extern void _Offload_report(int val);
  136. extern int _Offload_find_associated_mic_memory(
  137. int target,
  138. const void* cpu_addr,
  139. void** cpu_base_addr,
  140. uint64_t* buf_length,
  141. void** mic_addr,
  142. uint64_t* mic_buf_start_offset,
  143. int* is_static
  144. );
  145. /* OpenMP API */
  146. extern void omp_set_default_device(int num) __GOMP_NOTHROW;
  147. extern int omp_get_default_device(void) __GOMP_NOTHROW;
  148. extern int omp_get_num_devices(void) __GOMP_NOTHROW;
  149. // OpenMP 4.5 APIs
  150. /*! \fn omp_get_initial_device
  151. \brief Return the device id of the initial device.
  152. \return Returns the device id of the initial device.
  153. */
  154. extern int omp_get_initial_device(
  155. void
  156. ) __GOMP_NOTHROW;
  157. /*! \fn omp_target_alloc
  158. \brief Allocate memory in the device data environment.
  159. \param size Number of bytes to allocate.
  160. \param device_num The device number on which to allocate.
  161. \return Returns a pointer to the allocated memory.
  162. */
  163. extern void* omp_target_alloc(
  164. size_t size,
  165. int device_num
  166. ) __GOMP_NOTHROW;
  167. /*! \fn omp_target_free
  168. \brief Free memory in the device data environment.
  169. \param device_ptr Address of allocated device memory.
  170. \param device_num The device number on which to free.
  171. */
  172. extern void omp_target_free(
  173. void *device_ptr,
  174. int device_num
  175. ) __GOMP_NOTHROW;
  176. /*! \fn omp_target_is_present
  177. \brief Test whether a host pointer has corresponding storage on a device.
  178. \param device_ptr Address of allocated device memory.
  179. \param device_num The device number on which to test..
  180. \return true if storage is found, false otherwise.
  181. */
  182. extern int omp_target_is_present(
  183. const void *ptr,
  184. int device_num
  185. ) __GOMP_NOTHROW;
  186. /*! \fn omp_target_memcpy
  187. \brief Copy memory between host/device pointers.
  188. \param dst Address of destination memory.
  189. \param src Address of source memory.
  190. \param length Number of bytes to copy.
  191. \param dst_offset Destination offset in bytes.
  192. \param src_offset Source offset in bytes.
  193. \param dst_device Destination device number.
  194. \param src_device Source device number.
  195. \return 0 on success, 1 otherwise.
  196. */
  197. extern int omp_target_memcpy(
  198. void *dst,
  199. const void *src,
  200. size_t length,
  201. size_t dst_offset,
  202. size_t src_offset,
  203. int dst_device,
  204. int src_device
  205. ) __GOMP_NOTHROW;
  206. /*! \fn omp_target_memcpy_rect
  207. \brief Copy a rectangular subsection from
  208. \brief one multi-dimensional array to another.
  209. \param dst Address of destination array.
  210. \param src Address of source array.
  211. \param element_size Number of bytes in each array element.
  212. \param num_dims Number of dimensions.
  213. \param volume Array of element counts to copy in each dimension.
  214. \param dst_offsets Destination offsets array.
  215. \param src_offsets Source offsets array.
  216. \param dst_dims Destination array dimensions array.
  217. \param src_dims Source array dimensions array.
  218. \param dst_device Destination device number.
  219. \param src_device Source device number.
  220. \return 0 on success, 1 otherwise.
  221. */
  222. extern int omp_target_memcpy_rect(
  223. void *dst,
  224. const void *src,
  225. size_t element_size,
  226. int num_dims,
  227. const size_t *volume,
  228. const size_t *dst_offsets,
  229. const size_t *src_offsets,
  230. const size_t *dst_dimensions,
  231. const size_t *src_dimensions,
  232. int dst_device,
  233. int src_device
  234. ) __GOMP_NOTHROW;
  235. /*! \fn omp_target_associate_ptr
  236. \brief Map a device pointer to a host pointer.
  237. \param host_ptr The host pointer.
  238. \param device_ptr The device pointer.
  239. \param size Number of bytes to map.
  240. \param device_offset Offset on device of mapped memory.
  241. \param device_num Device number.
  242. \return 0 on success, 1 otherwise.
  243. */
  244. extern int omp_target_associate_ptr(
  245. const void *host_ptr,
  246. const void *device_ptr,
  247. size_t size,
  248. size_t device_offset,
  249. int device_num
  250. ) __GOMP_NOTHROW;
  251. /*! \fn omp_target_disassociate_ptr
  252. \brief Remove a host pointer to device pointer association.
  253. \param ptr The host pointer to disassociate.
  254. \param device_num Device number.
  255. \return 0 on success, 1 otherwise.
  256. */
  257. extern int omp_target_disassociate_ptr(
  258. const void *host_ptr,
  259. int device_num
  260. ) __GOMP_NOTHROW;
  261. // End of OpenMP 4.5 APIs
  262. /* OpenMP API wrappers */
  263. /* Set num_threads on target */
  264. extern void omp_set_num_threads_target(
  265. TARGET_TYPE target_type,
  266. int target_number,
  267. int num_threads
  268. );
  269. /* Get max_threads from target */
  270. extern int omp_get_max_threads_target(
  271. TARGET_TYPE target_type,
  272. int target_number
  273. );
  274. /* Get num_procs from target */
  275. extern int omp_get_num_procs_target(
  276. TARGET_TYPE target_type,
  277. int target_number
  278. );
  279. /* Set dynamic on target */
  280. extern void omp_set_dynamic_target(
  281. TARGET_TYPE target_type,
  282. int target_number,
  283. int num_threads
  284. );
  285. /* Get dynamic from target */
  286. extern int omp_get_dynamic_target(
  287. TARGET_TYPE target_type,
  288. int target_number
  289. );
  290. /* Set nested on target */
  291. extern void omp_set_nested_target(
  292. TARGET_TYPE target_type,
  293. int target_number,
  294. int nested
  295. );
  296. /* Get nested from target */
  297. extern int omp_get_nested_target(
  298. TARGET_TYPE target_type,
  299. int target_number
  300. );
  301. extern void omp_set_num_threads_target(
  302. TARGET_TYPE target_type,
  303. int target_number,
  304. int num_threads
  305. );
  306. extern int omp_get_max_threads_target(
  307. TARGET_TYPE target_type,
  308. int target_number
  309. );
  310. extern int omp_get_num_procs_target(
  311. TARGET_TYPE target_type,
  312. int target_number
  313. );
  314. extern void omp_set_dynamic_target(
  315. TARGET_TYPE target_type,
  316. int target_number,
  317. int num_threads
  318. );
  319. extern int omp_get_dynamic_target(
  320. TARGET_TYPE target_type,
  321. int target_number
  322. );
  323. extern void omp_set_nested_target(
  324. TARGET_TYPE target_type,
  325. int target_number,
  326. int num_threads
  327. );
  328. extern int omp_get_nested_target(
  329. TARGET_TYPE target_type,
  330. int target_number
  331. );
  332. extern void omp_set_schedule_target(
  333. TARGET_TYPE target_type,
  334. int target_number,
  335. omp_sched_t kind,
  336. int modifier
  337. );
  338. extern void omp_get_schedule_target(
  339. TARGET_TYPE target_type,
  340. int target_number,
  341. omp_sched_t *kind,
  342. int *modifier
  343. );
  344. /* lock API functions */
  345. typedef struct {
  346. omp_lock_t lock;
  347. } omp_lock_target_t;
  348. extern void omp_init_lock_target(
  349. TARGET_TYPE target_type,
  350. int target_number,
  351. omp_lock_target_t *lock
  352. );
  353. extern void omp_destroy_lock_target(
  354. TARGET_TYPE target_type,
  355. int target_number,
  356. omp_lock_target_t *lock
  357. );
  358. extern void omp_set_lock_target(
  359. TARGET_TYPE target_type,
  360. int target_number,
  361. omp_lock_target_t *lock
  362. );
  363. extern void omp_unset_lock_target(
  364. TARGET_TYPE target_type,
  365. int target_number,
  366. omp_lock_target_t *lock
  367. );
  368. extern int omp_test_lock_target(
  369. TARGET_TYPE target_type,
  370. int target_number,
  371. omp_lock_target_t *lock
  372. );
  373. /* nested lock API functions */
  374. typedef struct {
  375. omp_nest_lock_t lock;
  376. } omp_nest_lock_target_t;
  377. extern void omp_init_nest_lock_target(
  378. TARGET_TYPE target_type,
  379. int target_number,
  380. omp_nest_lock_target_t *lock
  381. );
  382. extern void omp_destroy_nest_lock_target(
  383. TARGET_TYPE target_type,
  384. int target_number,
  385. omp_nest_lock_target_t *lock
  386. );
  387. extern void omp_set_nest_lock_target(
  388. TARGET_TYPE target_type,
  389. int target_number,
  390. omp_nest_lock_target_t *lock
  391. );
  392. extern void omp_unset_nest_lock_target(
  393. TARGET_TYPE target_type,
  394. int target_number,
  395. omp_nest_lock_target_t *lock
  396. );
  397. extern int omp_test_nest_lock_target(
  398. TARGET_TYPE target_type,
  399. int target_number,
  400. omp_nest_lock_target_t *lock
  401. );
  402. #ifdef __cplusplus
  403. } /* extern "C" */
  404. /* Namespace for the shared_allocator. */
  405. namespace __offload {
  406. /* This follows the specification for std::allocator. */
  407. /* Forward declaration of the class template. */
  408. template <typename T>
  409. class shared_allocator;
  410. /* Specialization for shared_allocator<void>. */
  411. template <>
  412. class shared_allocator<void> {
  413. public:
  414. typedef void *pointer;
  415. typedef const void *const_pointer;
  416. typedef void value_type;
  417. template <class U> struct rebind { typedef shared_allocator<U> other; };
  418. };
  419. /* Definition of shared_allocator<T>. */
  420. template <class T>
  421. class shared_allocator {
  422. public:
  423. typedef size_t size_type;
  424. typedef ptrdiff_t difference_type;
  425. typedef T *pointer;
  426. typedef const T *const_pointer;
  427. typedef T &reference;
  428. typedef const T &const_reference;
  429. typedef T value_type;
  430. template <class U> struct rebind { typedef shared_allocator<U> other; };
  431. shared_allocator() throw() { }
  432. shared_allocator(const shared_allocator&) throw() { }
  433. template <class U> shared_allocator(const shared_allocator<U>&) throw() { }
  434. ~shared_allocator() throw() { }
  435. pointer address(reference x) const { return &x; }
  436. const_pointer address(const_reference x) const { return &x; }
  437. pointer allocate(
  438. size_type, shared_allocator<void>::const_pointer hint = 0);
  439. void deallocate(pointer p, size_type n);
  440. size_type max_size() const throw() {
  441. return size_type(-1)/sizeof(T);
  442. } /* max_size */
  443. void construct(pointer p, const T& arg) {
  444. ::new (p) T(arg);
  445. } /* construct */
  446. void destroy(pointer p) {
  447. p->~T();
  448. } /* destroy */
  449. };
  450. /* Definition for allocate. */
  451. template <class T>
  452. typename shared_allocator<T>::pointer
  453. shared_allocator<T>::allocate(shared_allocator<T>::size_type s,
  454. shared_allocator<void>::const_pointer) {
  455. /* Allocate from shared memory. */
  456. void *ptr = _Offload_shared_malloc(s*sizeof(T));
  457. #if (defined(_WIN32) || defined(_WIN64)) /* Windows */
  458. if (ptr == 0) throw std::bad_alloc();
  459. #else
  460. if (ptr == 0) std::__throw_bad_alloc();
  461. #endif
  462. return static_cast<pointer>(ptr);
  463. } /* allocate */
  464. template <class T>
  465. void shared_allocator<T>::deallocate(pointer p,
  466. shared_allocator<T>::size_type) {
  467. /* Free the shared memory. */
  468. _Offload_shared_free(p);
  469. } /* deallocate */
  470. template <typename _T1, typename _T2>
  471. inline bool operator==(const shared_allocator<_T1> &,
  472. const shared_allocator<_T2> &) throw() {
  473. return true;
  474. } /* operator== */
  475. template <typename _T1, typename _T2>
  476. inline bool operator!=(const shared_allocator<_T1> &,
  477. const shared_allocator<_T2> &) throw() {
  478. return false;
  479. } /* operator!= */
  480. } /* __offload */
  481. #endif /* __cplusplus */
  482. #endif /* OFFLOAD_H_INCLUDED */