2 Copyright (c) 2014-2015 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
38 #if defined(LINUX) || defined(FREEBSD)
39 #include <bits/functexcept.h>
46 // <stdint.h> is not compatible with Windows
47 typedef unsigned long long int uint64_t;
50 #endif // TARGET_WINNT
56 #define TARGET_ATTRIBUTE __declspec(target(mic))
59 * The target architecture.
61 typedef enum TARGET_TYPE
{
62 TARGET_NONE
, /* Undefine target */
63 TARGET_HOST
, /* Host used as target */
64 TARGET_MIC
/* MIC target */
68 * The default target type.
70 #define DEFAULT_TARGET_TYPE TARGET_MIC
73 * The default target number.
75 #define DEFAULT_TARGET_NUMBER 0
82 OFFLOAD_DISABLED
, /* offload is disabled */
83 OFFLOAD_UNAVAILABLE
, /* card is not available */
84 OFFLOAD_OUT_OF_MEMORY
, /* not enough memory on device */
85 OFFLOAD_PROCESS_DIED
, /* target process has died */
86 OFFLOAD_ERROR
/* unspecified error */
90 _Offload_result result
; /* result, see above */
91 int device_number
; /* device number */
92 size_t data_sent
; /* number of bytes sent to the target */
93 size_t data_received
; /* number of bytes received by host */
96 typedef uint64_t _Offload_stream
;
98 #define OFFLOAD_STATUS_INIT(x) \
99 ((x).result = OFFLOAD_DISABLED)
101 #define OFFLOAD_STATUS_INITIALIZER \
102 { OFFLOAD_DISABLED, -1, 0, 0 }
104 /* Offload runtime interfaces */
106 extern int _Offload_number_of_devices(void);
107 extern int _Offload_get_device_number(void);
108 extern int _Offload_get_physical_device_number(void);
110 /* Offload stream runtime interfaces */
112 extern _Offload_stream
_Offload_stream_create(
113 int device
, // MIC device number
114 int number_of_cpus
// Cores allocated to the stream
117 extern int _Offload_stream_destroy(
118 int device
, // MIC device number
119 _Offload_stream stream
// stream handle
122 extern int _Offload_stream_completed(
123 int device
, // MIC device number
124 _Offload_stream handle
// stream handle
128 * _Offload_shared_malloc/free are only supported when offload is enabled
129 * else they are defined to malloc and free
131 #ifdef __INTEL_OFFLOAD
132 extern void* _Offload_shared_malloc(size_t size
);
133 extern void _Offload_shared_free(void *ptr
);
134 extern void* _Offload_shared_aligned_malloc(size_t size
, size_t align
);
135 extern void _Offload_shared_aligned_free(void *ptr
);
138 #define _Offload_shared_malloc(size) malloc(size)
139 #define _Offload_shared_free(ptr) free(ptr);
141 #define _Offload_shared_aligned_malloc(size, align) _aligned_malloc(size, align)
142 #define _Offload_shared_aligned_free(ptr) _aligned_free(ptr);
144 #define _Offload_shared_aligned_malloc(size, align) memalign(align, size)
145 #define _Offload_shared_aligned_free(ptr) free(ptr);
150 extern int _Offload_signaled(int index
, void *signal
);
151 extern void _Offload_report(int val
);
152 extern int _Offload_find_associated_mic_memory(
154 const void* cpu_addr
,
155 void** cpu_base_addr
,
156 uint64_t* buf_length
,
158 uint64_t* mic_buf_start_offset
,
164 extern void omp_set_default_device(int num
) __GOMP_NOTHROW
;
165 extern int omp_get_default_device(void) __GOMP_NOTHROW
;
166 extern int omp_get_num_devices(void) __GOMP_NOTHROW
;
168 /* OpenMP API wrappers */
170 /* Set num_threads on target */
171 extern void omp_set_num_threads_target(
172 TARGET_TYPE target_type
,
177 /* Get max_threads from target */
178 extern int omp_get_max_threads_target(
179 TARGET_TYPE target_type
,
183 /* Get num_procs from target */
184 extern int omp_get_num_procs_target(
185 TARGET_TYPE target_type
,
189 /* Set dynamic on target */
190 extern void omp_set_dynamic_target(
191 TARGET_TYPE target_type
,
196 /* Get dynamic from target */
197 extern int omp_get_dynamic_target(
198 TARGET_TYPE target_type
,
202 /* Set nested on target */
203 extern void omp_set_nested_target(
204 TARGET_TYPE target_type
,
209 /* Get nested from target */
210 extern int omp_get_nested_target(
211 TARGET_TYPE target_type
,
215 extern void omp_set_num_threads_target(
216 TARGET_TYPE target_type
,
221 extern int omp_get_max_threads_target(
222 TARGET_TYPE target_type
,
226 extern int omp_get_num_procs_target(
227 TARGET_TYPE target_type
,
231 extern void omp_set_dynamic_target(
232 TARGET_TYPE target_type
,
237 extern int omp_get_dynamic_target(
238 TARGET_TYPE target_type
,
242 extern void omp_set_nested_target(
243 TARGET_TYPE target_type
,
248 extern int omp_get_nested_target(
249 TARGET_TYPE target_type
,
253 extern void omp_set_schedule_target(
254 TARGET_TYPE target_type
,
260 extern void omp_get_schedule_target(
261 TARGET_TYPE target_type
,
267 /* lock API functions */
273 extern void omp_init_lock_target(
274 TARGET_TYPE target_type
,
276 omp_lock_target_t
*lock
279 extern void omp_destroy_lock_target(
280 TARGET_TYPE target_type
,
282 omp_lock_target_t
*lock
285 extern void omp_set_lock_target(
286 TARGET_TYPE target_type
,
288 omp_lock_target_t
*lock
291 extern void omp_unset_lock_target(
292 TARGET_TYPE target_type
,
294 omp_lock_target_t
*lock
297 extern int omp_test_lock_target(
298 TARGET_TYPE target_type
,
300 omp_lock_target_t
*lock
303 /* nested lock API functions */
306 omp_nest_lock_t lock
;
307 } omp_nest_lock_target_t
;
309 extern void omp_init_nest_lock_target(
310 TARGET_TYPE target_type
,
312 omp_nest_lock_target_t
*lock
315 extern void omp_destroy_nest_lock_target(
316 TARGET_TYPE target_type
,
318 omp_nest_lock_target_t
*lock
321 extern void omp_set_nest_lock_target(
322 TARGET_TYPE target_type
,
324 omp_nest_lock_target_t
*lock
327 extern void omp_unset_nest_lock_target(
328 TARGET_TYPE target_type
,
330 omp_nest_lock_target_t
*lock
333 extern int omp_test_nest_lock_target(
334 TARGET_TYPE target_type
,
336 omp_nest_lock_target_t
*lock
342 /* Namespace for the shared_allocator. */
343 namespace __offload
{
344 /* This follows the specification for std::allocator. */
345 /* Forward declaration of the class template. */
346 template <typename T
>
347 class shared_allocator
;
349 /* Specialization for shared_allocator<void>. */
351 class shared_allocator
<void> {
353 typedef void *pointer
;
354 typedef const void *const_pointer
;
355 typedef void value_type
;
356 template <class U
> struct rebind
{ typedef shared_allocator
<U
> other
; };
359 /* Definition of shared_allocator<T>. */
361 class shared_allocator
{
363 typedef size_t size_type
;
364 typedef ptrdiff_t difference_type
;
366 typedef const T
*const_pointer
;
367 typedef T
&reference
;
368 typedef const T
&const_reference
;
369 typedef T value_type
;
370 template <class U
> struct rebind
{ typedef shared_allocator
<U
> other
; };
371 shared_allocator() throw() { }
372 shared_allocator(const shared_allocator
&) throw() { }
373 template <class U
> shared_allocator(const shared_allocator
<U
>&) throw() { }
374 ~shared_allocator() throw() { }
375 pointer
address(reference x
) const { return &x
; }
376 const_pointer
address(const_reference x
) const { return &x
; }
378 size_type
, shared_allocator
<void>::const_pointer hint
= 0);
379 void deallocate(pointer p
, size_type n
);
380 size_type
max_size() const throw() {
381 return size_type(-1)/sizeof(T
);
383 void construct(pointer p
, const T
& arg
) {
386 void destroy(pointer p
) {
391 /* Definition for allocate. */
393 typename shared_allocator
<T
>::pointer
394 shared_allocator
<T
>::allocate(shared_allocator
<T
>::size_type s
,
395 shared_allocator
<void>::const_pointer
) {
396 /* Allocate from shared memory. */
397 void *ptr
= _Offload_shared_malloc(s
*sizeof(T
));
398 #if (defined(_WIN32) || defined(_WIN64)) /* Windows */
399 if (ptr
== 0) throw std::bad_alloc();
401 if (ptr
== 0) std::__throw_bad_alloc();
403 return static_cast<pointer
>(ptr
);
407 void shared_allocator
<T
>::deallocate(pointer p
,
408 shared_allocator
<T
>::size_type
) {
409 /* Free the shared memory. */
410 _Offload_shared_free(p
);
413 template <typename _T1
, typename _T2
>
414 inline bool operator==(const shared_allocator
<_T1
> &,
415 const shared_allocator
<_T2
> &) throw() {
419 template <typename _T1
, typename _T2
>
420 inline bool operator!=(const shared_allocator
<_T1
> &,
421 const shared_allocator
<_T2
> &) throw() {
425 #endif /* __cplusplus */
427 #endif /* OFFLOAD_H_INCLUDED */