]> git.ipfire.org Git - thirdparty/gcc.git/blob - liboffloadmic/runtime/offload.h
backport: Makefile.am (liboffloadmic_host_la_DEPENDENCIES): Remove libcoi_host and...
[thirdparty/gcc.git] / liboffloadmic / runtime / offload.h
1 /*
2 Copyright (c) 2014-2015 Intel Corporation. All Rights Reserved.
3
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
6 are met:
7
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.
16
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.
28 */
29
30
31 /*
32 * Include file for Offload API.
33 */
34
35 #ifndef OFFLOAD_H_INCLUDED
36 #define OFFLOAD_H_INCLUDED
37
38 #if defined(LINUX) || defined(FREEBSD)
39 #include <bits/functexcept.h>
40 #endif
41
42 #include <stddef.h>
43 #include <omp.h>
44
45 #ifdef TARGET_WINNT
46 // <stdint.h> is not compatible with Windows
47 typedef unsigned long long int uint64_t;
48 #else
49 #include <stdint.h>
50 #endif // TARGET_WINNT
51
52 #ifdef __cplusplus
53 extern "C" {
54 #endif
55
56 #define TARGET_ATTRIBUTE __declspec(target(mic))
57
58 /*
59 * The target architecture.
60 */
61 typedef enum TARGET_TYPE {
62 TARGET_NONE, /* Undefine target */
63 TARGET_HOST, /* Host used as target */
64 TARGET_MIC /* MIC target */
65 } TARGET_TYPE;
66
67 /*
68 * The default target type.
69 */
70 #define DEFAULT_TARGET_TYPE TARGET_MIC
71
72 /*
73 * The default target number.
74 */
75 #define DEFAULT_TARGET_NUMBER 0
76
77 /*
78 * Offload status.
79 */
80 typedef enum {
81 OFFLOAD_SUCCESS = 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 */
87 } _Offload_result;
88
89 typedef struct {
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 */
94 } _Offload_status;
95
96 typedef uint64_t _Offload_stream;
97
98 #define OFFLOAD_STATUS_INIT(x) \
99 ((x).result = OFFLOAD_DISABLED)
100
101 #define OFFLOAD_STATUS_INITIALIZER \
102 { OFFLOAD_DISABLED, -1, 0, 0 }
103
104 /* Offload runtime interfaces */
105
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);
109
110 /* Offload stream runtime interfaces */
111
112 extern _Offload_stream _Offload_stream_create(
113 int device, // MIC device number
114 int number_of_cpus // Cores allocated to the stream
115 );
116
117 extern int _Offload_stream_destroy(
118 int device, // MIC device number
119 _Offload_stream stream // stream handle
120 );
121
122 extern int _Offload_stream_completed(
123 int device, // MIC device number
124 _Offload_stream handle // stream handle
125 );
126
127 /*
128 * _Offload_shared_malloc/free are only supported when offload is enabled
129 * else they are defined to malloc and free
130 */
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);
136 #else
137 #include <malloc.h>
138 #define _Offload_shared_malloc(size) malloc(size)
139 #define _Offload_shared_free(ptr) free(ptr);
140 #if defined(_WIN32)
141 #define _Offload_shared_aligned_malloc(size, align) _aligned_malloc(size, align)
142 #define _Offload_shared_aligned_free(ptr) _aligned_free(ptr);
143 #else
144 #define _Offload_shared_aligned_malloc(size, align) memalign(align, size)
145 #define _Offload_shared_aligned_free(ptr) free(ptr);
146 #endif
147 #endif
148
149
150 extern int _Offload_signaled(int index, void *signal);
151 extern void _Offload_report(int val);
152 extern int _Offload_find_associated_mic_memory(
153 int target,
154 const void* cpu_addr,
155 void** cpu_base_addr,
156 uint64_t* buf_length,
157 void** mic_addr,
158 uint64_t* mic_buf_start_offset,
159 int* is_static
160 );
161
162 /* OpenMP API */
163
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;
167
168 /* OpenMP API wrappers */
169
170 /* Set num_threads on target */
171 extern void omp_set_num_threads_target(
172 TARGET_TYPE target_type,
173 int target_number,
174 int num_threads
175 );
176
177 /* Get max_threads from target */
178 extern int omp_get_max_threads_target(
179 TARGET_TYPE target_type,
180 int target_number
181 );
182
183 /* Get num_procs from target */
184 extern int omp_get_num_procs_target(
185 TARGET_TYPE target_type,
186 int target_number
187 );
188
189 /* Set dynamic on target */
190 extern void omp_set_dynamic_target(
191 TARGET_TYPE target_type,
192 int target_number,
193 int num_threads
194 );
195
196 /* Get dynamic from target */
197 extern int omp_get_dynamic_target(
198 TARGET_TYPE target_type,
199 int target_number
200 );
201
202 /* Set nested on target */
203 extern void omp_set_nested_target(
204 TARGET_TYPE target_type,
205 int target_number,
206 int nested
207 );
208
209 /* Get nested from target */
210 extern int omp_get_nested_target(
211 TARGET_TYPE target_type,
212 int target_number
213 );
214
215 extern void omp_set_num_threads_target(
216 TARGET_TYPE target_type,
217 int target_number,
218 int num_threads
219 );
220
221 extern int omp_get_max_threads_target(
222 TARGET_TYPE target_type,
223 int target_number
224 );
225
226 extern int omp_get_num_procs_target(
227 TARGET_TYPE target_type,
228 int target_number
229 );
230
231 extern void omp_set_dynamic_target(
232 TARGET_TYPE target_type,
233 int target_number,
234 int num_threads
235 );
236
237 extern int omp_get_dynamic_target(
238 TARGET_TYPE target_type,
239 int target_number
240 );
241
242 extern void omp_set_nested_target(
243 TARGET_TYPE target_type,
244 int target_number,
245 int num_threads
246 );
247
248 extern int omp_get_nested_target(
249 TARGET_TYPE target_type,
250 int target_number
251 );
252
253 extern void omp_set_schedule_target(
254 TARGET_TYPE target_type,
255 int target_number,
256 omp_sched_t kind,
257 int modifier
258 );
259
260 extern void omp_get_schedule_target(
261 TARGET_TYPE target_type,
262 int target_number,
263 omp_sched_t *kind,
264 int *modifier
265 );
266
267 /* lock API functions */
268
269 typedef struct {
270 omp_lock_t lock;
271 } omp_lock_target_t;
272
273 extern void omp_init_lock_target(
274 TARGET_TYPE target_type,
275 int target_number,
276 omp_lock_target_t *lock
277 );
278
279 extern void omp_destroy_lock_target(
280 TARGET_TYPE target_type,
281 int target_number,
282 omp_lock_target_t *lock
283 );
284
285 extern void omp_set_lock_target(
286 TARGET_TYPE target_type,
287 int target_number,
288 omp_lock_target_t *lock
289 );
290
291 extern void omp_unset_lock_target(
292 TARGET_TYPE target_type,
293 int target_number,
294 omp_lock_target_t *lock
295 );
296
297 extern int omp_test_lock_target(
298 TARGET_TYPE target_type,
299 int target_number,
300 omp_lock_target_t *lock
301 );
302
303 /* nested lock API functions */
304
305 typedef struct {
306 omp_nest_lock_t lock;
307 } omp_nest_lock_target_t;
308
309 extern void omp_init_nest_lock_target(
310 TARGET_TYPE target_type,
311 int target_number,
312 omp_nest_lock_target_t *lock
313 );
314
315 extern void omp_destroy_nest_lock_target(
316 TARGET_TYPE target_type,
317 int target_number,
318 omp_nest_lock_target_t *lock
319 );
320
321 extern void omp_set_nest_lock_target(
322 TARGET_TYPE target_type,
323 int target_number,
324 omp_nest_lock_target_t *lock
325 );
326
327 extern void omp_unset_nest_lock_target(
328 TARGET_TYPE target_type,
329 int target_number,
330 omp_nest_lock_target_t *lock
331 );
332
333 extern int omp_test_nest_lock_target(
334 TARGET_TYPE target_type,
335 int target_number,
336 omp_nest_lock_target_t *lock
337 );
338
339 #ifdef __cplusplus
340 } /* extern "C" */
341
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;
348
349 /* Specialization for shared_allocator<void>. */
350 template <>
351 class shared_allocator<void> {
352 public:
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; };
357 };
358
359 /* Definition of shared_allocator<T>. */
360 template <class T>
361 class shared_allocator {
362 public:
363 typedef size_t size_type;
364 typedef ptrdiff_t difference_type;
365 typedef T *pointer;
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; }
377 pointer allocate(
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);
382 } /* max_size */
383 void construct(pointer p, const T& arg) {
384 ::new (p) T(arg);
385 } /* construct */
386 void destroy(pointer p) {
387 p->~T();
388 } /* destroy */
389 };
390
391 /* Definition for allocate. */
392 template <class T>
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();
400 #else
401 if (ptr == 0) std::__throw_bad_alloc();
402 #endif
403 return static_cast<pointer>(ptr);
404 } /* allocate */
405
406 template <class T>
407 void shared_allocator<T>::deallocate(pointer p,
408 shared_allocator<T>::size_type) {
409 /* Free the shared memory. */
410 _Offload_shared_free(p);
411 } /* deallocate */
412
413 template <typename _T1, typename _T2>
414 inline bool operator==(const shared_allocator<_T1> &,
415 const shared_allocator<_T2> &) throw() {
416 return true;
417 } /* operator== */
418
419 template <typename _T1, typename _T2>
420 inline bool operator!=(const shared_allocator<_T1> &,
421 const shared_allocator<_T2> &) throw() {
422 return false;
423 } /* operator!= */
424 } /* __offload */
425 #endif /* __cplusplus */
426
427 #endif /* OFFLOAD_H_INCLUDED */