2 Copyright (c) 2014 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>
49 #define TARGET_ATTRIBUTE __declspec(target(mic))
52 * The target architecture.
54 typedef enum TARGET_TYPE
{
55 TARGET_NONE
, /* Undefine target */
56 TARGET_HOST
, /* Host used as target */
57 TARGET_MIC
/* MIC target */
61 * The default target type.
63 #define DEFAULT_TARGET_TYPE TARGET_MIC
66 * The default target number.
68 #define DEFAULT_TARGET_NUMBER 0
75 OFFLOAD_DISABLED
, /* offload is disabled */
76 OFFLOAD_UNAVAILABLE
, /* card is not available */
77 OFFLOAD_OUT_OF_MEMORY
, /* not enough memory on device */
78 OFFLOAD_PROCESS_DIED
, /* target process has died */
79 OFFLOAD_ERROR
/* unspecified error */
83 _Offload_result result
; /* result, see above */
84 int device_number
; /* device number */
85 size_t data_sent
; /* number of bytes sent to the target */
86 size_t data_received
; /* number of bytes received by host */
89 #define OFFLOAD_STATUS_INIT(x) \
90 ((x).result = OFFLOAD_DISABLED)
92 #define OFFLOAD_STATUS_INITIALIZER \
93 { OFFLOAD_DISABLED, -1, 0, 0 }
95 /* Offload runtime interfaces */
97 extern int _Offload_number_of_devices(void);
98 extern int _Offload_get_device_number(void);
99 extern int _Offload_get_physical_device_number(void);
101 extern void* _Offload_shared_malloc(size_t size
);
102 extern void _Offload_shared_free(void *ptr
);
104 extern void* _Offload_shared_aligned_malloc(size_t size
, size_t align
);
105 extern void _Offload_shared_aligned_free(void *ptr
);
107 extern int _Offload_signaled(int index
, void *signal
);
108 extern void _Offload_report(int val
);
112 extern void omp_set_default_device(int num
) __GOMP_NOTHROW
;
113 extern int omp_get_default_device(void) __GOMP_NOTHROW
;
114 extern int omp_get_num_devices(void) __GOMP_NOTHROW
;
116 /* OpenMP API wrappers */
118 /* Set num_threads on target */
119 extern void omp_set_num_threads_target(
120 TARGET_TYPE target_type
,
125 /* Get max_threads from target */
126 extern int omp_get_max_threads_target(
127 TARGET_TYPE target_type
,
131 /* Get num_procs from target */
132 extern int omp_get_num_procs_target(
133 TARGET_TYPE target_type
,
137 /* Set dynamic on target */
138 extern void omp_set_dynamic_target(
139 TARGET_TYPE target_type
,
144 /* Get dynamic from target */
145 extern int omp_get_dynamic_target(
146 TARGET_TYPE target_type
,
150 /* Set nested on target */
151 extern void omp_set_nested_target(
152 TARGET_TYPE target_type
,
157 /* Get nested from target */
158 extern int omp_get_nested_target(
159 TARGET_TYPE target_type
,
163 extern void omp_set_num_threads_target(
164 TARGET_TYPE target_type
,
169 extern int omp_get_max_threads_target(
170 TARGET_TYPE target_type
,
174 extern int omp_get_num_procs_target(
175 TARGET_TYPE target_type
,
179 extern void omp_set_dynamic_target(
180 TARGET_TYPE target_type
,
185 extern int omp_get_dynamic_target(
186 TARGET_TYPE target_type
,
190 extern void omp_set_nested_target(
191 TARGET_TYPE target_type
,
196 extern int omp_get_nested_target(
197 TARGET_TYPE target_type
,
201 extern void omp_set_schedule_target(
202 TARGET_TYPE target_type
,
208 extern void omp_get_schedule_target(
209 TARGET_TYPE target_type
,
215 /* lock API functions */
221 extern void omp_init_lock_target(
222 TARGET_TYPE target_type
,
224 omp_lock_target_t
*lock
227 extern void omp_destroy_lock_target(
228 TARGET_TYPE target_type
,
230 omp_lock_target_t
*lock
233 extern void omp_set_lock_target(
234 TARGET_TYPE target_type
,
236 omp_lock_target_t
*lock
239 extern void omp_unset_lock_target(
240 TARGET_TYPE target_type
,
242 omp_lock_target_t
*lock
245 extern int omp_test_lock_target(
246 TARGET_TYPE target_type
,
248 omp_lock_target_t
*lock
251 /* nested lock API functions */
254 omp_nest_lock_t lock
;
255 } omp_nest_lock_target_t
;
257 extern void omp_init_nest_lock_target(
258 TARGET_TYPE target_type
,
260 omp_nest_lock_target_t
*lock
263 extern void omp_destroy_nest_lock_target(
264 TARGET_TYPE target_type
,
266 omp_nest_lock_target_t
*lock
269 extern void omp_set_nest_lock_target(
270 TARGET_TYPE target_type
,
272 omp_nest_lock_target_t
*lock
275 extern void omp_unset_nest_lock_target(
276 TARGET_TYPE target_type
,
278 omp_nest_lock_target_t
*lock
281 extern int omp_test_nest_lock_target(
282 TARGET_TYPE target_type
,
284 omp_nest_lock_target_t
*lock
290 /* Namespace for the shared_allocator. */
291 namespace __offload
{
292 /* This follows the specification for std::allocator. */
293 /* Forward declaration of the class template. */
294 template <typename T
>
295 class shared_allocator
;
297 /* Specialization for shared_allocator<void>. */
299 class shared_allocator
<void> {
301 typedef void *pointer
;
302 typedef const void *const_pointer
;
303 typedef void value_type
;
304 template <class U
> struct rebind
{ typedef shared_allocator
<U
> other
; };
307 /* Definition of shared_allocator<T>. */
309 class shared_allocator
{
311 typedef size_t size_type
;
312 typedef ptrdiff_t difference_type
;
314 typedef const T
*const_pointer
;
315 typedef T
&reference
;
316 typedef const T
&const_reference
;
317 typedef T value_type
;
318 template <class U
> struct rebind
{ typedef shared_allocator
<U
> other
; };
319 shared_allocator() throw() { }
320 shared_allocator(const shared_allocator
&) throw() { }
321 template <class U
> shared_allocator(const shared_allocator
<U
>&) throw() { }
322 ~shared_allocator() throw() { }
323 pointer
address(reference x
) const { return &x
; }
324 const_pointer
address(const_reference x
) const { return &x
; }
326 size_type
, shared_allocator
<void>::const_pointer hint
= 0);
327 void deallocate(pointer p
, size_type n
);
328 size_type
max_size() const throw() {
329 return size_type(-1)/sizeof(T
);
331 void construct(pointer p
, const T
& arg
) {
334 void destroy(pointer p
) {
339 /* Definition for allocate. */
341 typename shared_allocator
<T
>::pointer
342 shared_allocator
<T
>::allocate(shared_allocator
<T
>::size_type s
,
343 shared_allocator
<void>::const_pointer
) {
344 /* Allocate from shared memory. */
345 void *ptr
= _Offload_shared_malloc(s
*sizeof(T
));
346 if (ptr
== 0) std::__throw_bad_alloc();
347 return static_cast<pointer
>(ptr
);
351 void shared_allocator
<T
>::deallocate(pointer p
,
352 shared_allocator
<T
>::size_type
) {
353 /* Free the shared memory. */
354 _Offload_shared_free(p
);
357 template <typename _T1
, typename _T2
>
358 inline bool operator==(const shared_allocator
<_T1
> &,
359 const shared_allocator
<_T2
> &) throw() {
363 template <typename _T1
, typename _T2
>
364 inline bool operator!=(const shared_allocator
<_T1
> &,
365 const shared_allocator
<_T2
> &) throw() {
369 #endif /* __cplusplus */
371 #endif /* OFFLOAD_H_INCLUDED */