2 Copyright (c) 2014-2016 Intel Corporation. All Rights Reserved.
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
8 * Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13 * Neither the name of Intel Corporation nor the names of its
14 contributors may be used to endorse or promote products derived
15 from this software without specific prior written permission.
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * Include file for Offload API.
35 #ifndef OFFLOAD_H_INCLUDED
36 #define OFFLOAD_H_INCLUDED
39 #if defined(LINUX) || defined(FREEBSD)
40 #include <bits/functexcept.h>
48 // <stdint.h> is incompatible on Windows.
49 typedef unsigned long long int uint64_t;
50 typedef signed long long int int64_t;
53 #endif // TARGET_WINNT
59 #define TARGET_ATTRIBUTE __declspec(target(mic))
62 * The target architecture.
64 typedef enum TARGET_TYPE
{
65 TARGET_NONE
, /* Undefine target */
66 TARGET_HOST
, /* Host used as target */
67 TARGET_MIC
/* MIC target */
71 * The default target type.
73 #define DEFAULT_TARGET_TYPE TARGET_MIC
76 * The default target number.
78 #define DEFAULT_TARGET_NUMBER 0
85 OFFLOAD_DISABLED
, /* offload is disabled */
86 OFFLOAD_UNAVAILABLE
, /* card is not available */
87 OFFLOAD_OUT_OF_MEMORY
, /* not enough memory on device */
88 OFFLOAD_PROCESS_DIED
, /* target process has died */
89 OFFLOAD_ERROR
/* unspecified error */
93 _Offload_result result
; /* result, see above */
94 int device_number
; /* device number */
95 size_t data_sent
; /* number of bytes sent to the target */
96 size_t data_received
; /* number of bytes received by host */
99 typedef int64_t _Offload_stream
;
101 #define OFFLOAD_STATUS_INIT(x) \
102 ((x).result = OFFLOAD_DISABLED)
104 #define OFFLOAD_STATUS_INITIALIZER \
105 { OFFLOAD_DISABLED, -1, 0, 0 }
107 /* Offload runtime interfaces */
109 extern int _Offload_number_of_devices(void);
110 extern int _Offload_get_device_number(void);
111 extern int _Offload_get_physical_device_number(void);
113 /* Offload stream runtime interfaces */
115 extern _Offload_stream
_Offload_stream_create(
116 int device
, // MIC device number
117 int number_of_cpus
// Cores allocated to the stream
120 extern int _Offload_stream_destroy(
121 int device
, // MIC device number
122 _Offload_stream stream
// stream handle
125 extern int _Offload_stream_delete(
126 _Offload_stream handle
// stream handle
129 extern int _Offload_stream_completed(
130 int device
, // MIC device number
131 _Offload_stream handle
// stream handle
134 extern int _Offload_device_streams_completed(
135 int device
// MIC device number
138 extern int _Offload_stream_is_empty(
139 _Offload_stream handle
// stream handle
143 * _Offload_shared_malloc/free are only supported when offload is enabled
144 * else they are defined to malloc and free
146 #ifdef __INTEL_OFFLOAD
147 extern void* _Offload_shared_malloc(size_t size
);
148 extern void _Offload_shared_free(void *ptr
);
149 extern void* _Offload_shared_aligned_malloc(size_t size
, size_t align
);
150 extern void _Offload_shared_aligned_free(void *ptr
);
153 #define _Offload_shared_malloc(size) malloc(size)
154 #define _Offload_shared_free(ptr) free(ptr);
156 #define _Offload_shared_aligned_malloc(size, align) _aligned_malloc(size, align)
157 #define _Offload_shared_aligned_free(ptr) _aligned_free(ptr);
159 #define _Offload_shared_aligned_malloc(size, align) memalign(align, size)
160 #define _Offload_shared_aligned_free(ptr) free(ptr);
165 extern int _Offload_signaled(int index
, void *signal
);
166 extern void _Offload_report(int val
);
167 extern int _Offload_find_associated_mic_memory(
169 const void* cpu_addr
,
170 void** cpu_base_addr
,
171 uint64_t* buf_length
,
173 uint64_t* mic_buf_start_offset
,
179 extern void omp_set_default_device(int num
) __GOMP_NOTHROW
;
180 extern int omp_get_default_device(void) __GOMP_NOTHROW
;
181 extern int omp_get_num_devices(void) __GOMP_NOTHROW
;
185 /*! \fn omp_get_initial_device
186 \brief Return the device id of the initial device.
187 \return Returns the device id of the initial device.
189 extern int omp_get_initial_device(
193 /*! \fn omp_target_alloc
194 \brief Allocate memory in the device data environment.
195 \param size Number of bytes to allocate.
196 \param device_num The device number on which to allocate.
197 \return Returns a pointer to the allocated memory.
199 extern void* omp_target_alloc(
204 /*! \fn omp_target_free
205 \brief Free memory in the device data environment.
206 \param device_ptr Address of allocated device memory.
207 \param device_num The device number on which to free.
209 extern void omp_target_free(
214 /*! \fn omp_target_is_present
215 \brief Test whether a host pointer has corresponding storage on a device.
216 \param device_ptr Address of allocated device memory.
217 \param device_num The device number on which to test..
218 \return true if storage is found, false otherwise.
220 extern int omp_target_is_present(
225 /*! \fn omp_target_memcpy
226 \brief Copy memory between host/device pointers.
227 \param dst Address of destination memory.
228 \param src Address of source memory.
229 \param length Number of bytes to copy.
230 \param dst_offset Destination offset in bytes.
231 \param src_offset Source offset in bytes.
232 \param dst_device Destination device number.
233 \param src_device Source device number.
234 \return 0 on success, 1 otherwise.
236 extern int omp_target_memcpy(
246 /*! \fn omp_target_memcpy_rect
247 \brief Copy a rectangular subsection from
248 \brief one multi-dimensional array to another.
249 \param dst Address of destination array.
250 \param src Address of source array.
251 \param element_size Number of bytes in each array element.
252 \param num_dims Number of dimensions.
253 \param volume Array of element counts to copy in each dimension.
254 \param dst_offsets Destination offsets array.
255 \param src_offsets Source offsets array.
256 \param dst_dims Destination array dimensions array.
257 \param src_dims Source array dimensions array.
258 \param dst_device Destination device number.
259 \param src_device Source device number.
260 \return 0 on success, 1 otherwise.
262 extern int omp_target_memcpy_rect(
267 const size_t *volume
,
268 const size_t *dst_offsets
,
269 const size_t *src_offsets
,
270 const size_t *dst_dimensions
,
271 const size_t *src_dimensions
,
276 /*! \fn omp_target_associate_ptr
277 \brief Map a device pointer to a host pointer.
278 \param host_ptr The host pointer.
279 \param device_ptr The device pointer.
280 \param size Number of bytes to map.
281 \param device_offset Offset on device of mapped memory.
282 \param device_num Device number.
283 \return 0 on success, 1 otherwise.
285 extern int omp_target_associate_ptr(
289 size_t device_offset
,
293 /*! \fn omp_target_disassociate_ptr
294 \brief Remove a host pointer to device pointer association.
295 \param ptr The host pointer to disassociate.
296 \param device_num Device number.
297 \return 0 on success, 1 otherwise.
299 extern int omp_target_disassociate_ptr(
304 // End of OpenMP 4.5 APIs
306 /* OpenMP API wrappers */
308 /* Set num_threads on target */
309 extern void omp_set_num_threads_target(
310 TARGET_TYPE target_type
,
315 /* Get max_threads from target */
316 extern int omp_get_max_threads_target(
317 TARGET_TYPE target_type
,
321 /* Get num_procs from target */
322 extern int omp_get_num_procs_target(
323 TARGET_TYPE target_type
,
327 /* Set dynamic on target */
328 extern void omp_set_dynamic_target(
329 TARGET_TYPE target_type
,
334 /* Get dynamic from target */
335 extern int omp_get_dynamic_target(
336 TARGET_TYPE target_type
,
340 /* Set nested on target */
341 extern void omp_set_nested_target(
342 TARGET_TYPE target_type
,
347 /* Get nested from target */
348 extern int omp_get_nested_target(
349 TARGET_TYPE target_type
,
353 extern void omp_set_num_threads_target(
354 TARGET_TYPE target_type
,
359 extern int omp_get_max_threads_target(
360 TARGET_TYPE target_type
,
364 extern int omp_get_num_procs_target(
365 TARGET_TYPE target_type
,
369 extern void omp_set_dynamic_target(
370 TARGET_TYPE target_type
,
375 extern int omp_get_dynamic_target(
376 TARGET_TYPE target_type
,
380 extern void omp_set_nested_target(
381 TARGET_TYPE target_type
,
386 extern int omp_get_nested_target(
387 TARGET_TYPE target_type
,
391 extern void omp_set_schedule_target(
392 TARGET_TYPE target_type
,
398 extern void omp_get_schedule_target(
399 TARGET_TYPE target_type
,
405 /* lock API functions */
411 extern void omp_init_lock_target(
412 TARGET_TYPE target_type
,
414 omp_lock_target_t
*lock
417 extern void omp_destroy_lock_target(
418 TARGET_TYPE target_type
,
420 omp_lock_target_t
*lock
423 extern void omp_set_lock_target(
424 TARGET_TYPE target_type
,
426 omp_lock_target_t
*lock
429 extern void omp_unset_lock_target(
430 TARGET_TYPE target_type
,
432 omp_lock_target_t
*lock
435 extern int omp_test_lock_target(
436 TARGET_TYPE target_type
,
438 omp_lock_target_t
*lock
441 /* nested lock API functions */
444 omp_nest_lock_t lock
;
445 } omp_nest_lock_target_t
;
447 extern void omp_init_nest_lock_target(
448 TARGET_TYPE target_type
,
450 omp_nest_lock_target_t
*lock
453 extern void omp_destroy_nest_lock_target(
454 TARGET_TYPE target_type
,
456 omp_nest_lock_target_t
*lock
459 extern void omp_set_nest_lock_target(
460 TARGET_TYPE target_type
,
462 omp_nest_lock_target_t
*lock
465 extern void omp_unset_nest_lock_target(
466 TARGET_TYPE target_type
,
468 omp_nest_lock_target_t
*lock
471 extern int omp_test_nest_lock_target(
472 TARGET_TYPE target_type
,
474 omp_nest_lock_target_t
*lock
480 /* Namespace for the shared_allocator. */
481 namespace __offload
{
482 /* This follows the specification for std::allocator. */
483 /* Forward declaration of the class template. */
484 template <typename T
>
485 class shared_allocator
;
487 /* Specialization for shared_allocator<void>. */
489 class shared_allocator
<void> {
491 typedef void *pointer
;
492 typedef const void *const_pointer
;
493 typedef void value_type
;
494 template <class U
> struct rebind
{ typedef shared_allocator
<U
> other
; };
497 /* Definition of shared_allocator<T>. */
499 class shared_allocator
{
501 typedef size_t size_type
;
502 typedef ptrdiff_t difference_type
;
504 typedef const T
*const_pointer
;
505 typedef T
&reference
;
506 typedef const T
&const_reference
;
507 typedef T value_type
;
508 template <class U
> struct rebind
{ typedef shared_allocator
<U
> other
; };
509 shared_allocator() throw() { }
510 shared_allocator(const shared_allocator
&) throw() { }
511 template <class U
> shared_allocator(const shared_allocator
<U
>&) throw() { }
512 ~shared_allocator() throw() { }
513 pointer
address(reference x
) const { return &x
; }
514 const_pointer
address(const_reference x
) const { return &x
; }
516 size_type
, shared_allocator
<void>::const_pointer hint
= 0);
517 void deallocate(pointer p
, size_type n
);
518 size_type
max_size() const throw() {
519 return size_type(-1)/sizeof(T
);
521 void construct(pointer p
, const T
& arg
) {
524 void destroy(pointer p
) {
529 /* Definition for allocate. */
531 typename shared_allocator
<T
>::pointer
532 shared_allocator
<T
>::allocate(shared_allocator
<T
>::size_type s
,
533 shared_allocator
<void>::const_pointer
) {
534 /* Allocate from shared memory. */
535 void *ptr
= _Offload_shared_malloc(s
*sizeof(T
));
536 #if (defined(_WIN32) || defined(_WIN64)) /* Windows */
537 if (ptr
== 0) throw std::bad_alloc();
539 if (ptr
== 0) std::__throw_bad_alloc();
541 return static_cast<pointer
>(ptr
);
545 void shared_allocator
<T
>::deallocate(pointer p
,
546 shared_allocator
<T
>::size_type
) {
547 /* Free the shared memory. */
548 _Offload_shared_free(p
);
551 template <typename _T1
, typename _T2
>
552 inline bool operator==(const shared_allocator
<_T1
> &,
553 const shared_allocator
<_T2
> &) throw() {
557 template <typename _T1
, typename _T2
>
558 inline bool operator!=(const shared_allocator
<_T1
> &,
559 const shared_allocator
<_T2
> &) throw() {
563 #endif /* __cplusplus */
565 #endif /* OFFLOAD_H_INCLUDED */