Jim Cownie | 33f7b24 | 2014-04-09 15:40:23 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.txt for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | |
| 11 | /* |
| 12 | * Include file for Offload API. |
| 13 | */ |
| 14 | |
| 15 | #ifndef OFFLOAD_H_INCLUDED |
| 16 | #define OFFLOAD_H_INCLUDED |
| 17 | |
| 18 | #if defined(LINUX) || defined(FREEBSD) |
| 19 | #include <bits/functexcept.h> |
| 20 | #endif |
| 21 | |
| 22 | #include <stddef.h> |
| 23 | #include <omp.h> |
| 24 | |
| 25 | #ifdef __cplusplus |
| 26 | extern "C" { |
| 27 | #endif |
| 28 | |
| 29 | #define TARGET_ATTRIBUTE __declspec(target(mic)) |
| 30 | |
| 31 | /* |
| 32 | * The target architecture. |
| 33 | */ |
| 34 | typedef enum TARGET_TYPE { |
| 35 | TARGET_NONE, /* Undefine target */ |
| 36 | TARGET_HOST, /* Host used as target */ |
| 37 | TARGET_MIC /* MIC target */ |
| 38 | } TARGET_TYPE; |
| 39 | |
| 40 | /* |
| 41 | * The default target type. |
| 42 | */ |
| 43 | #define DEFAULT_TARGET_TYPE TARGET_MIC |
| 44 | |
| 45 | /* |
| 46 | * The default target number. |
| 47 | */ |
| 48 | #define DEFAULT_TARGET_NUMBER 0 |
| 49 | |
| 50 | /* |
| 51 | * Offload status. |
| 52 | */ |
| 53 | typedef enum { |
| 54 | OFFLOAD_SUCCESS = 0, |
| 55 | OFFLOAD_DISABLED, /* offload is disabled */ |
| 56 | OFFLOAD_UNAVAILABLE, /* card is not available */ |
| 57 | OFFLOAD_OUT_OF_MEMORY, /* not enough memory on device */ |
| 58 | OFFLOAD_PROCESS_DIED, /* target process has died */ |
| 59 | OFFLOAD_ERROR /* unspecified error */ |
| 60 | } _Offload_result; |
| 61 | |
| 62 | typedef struct { |
| 63 | _Offload_result result; /* result, see above */ |
| 64 | int device_number; /* device number */ |
| 65 | size_t data_sent; /* number of bytes sent to the target */ |
| 66 | size_t data_received; /* number of bytes received by host */ |
| 67 | } _Offload_status; |
| 68 | |
| 69 | #define OFFLOAD_STATUS_INIT(x) \ |
| 70 | ((x).result = OFFLOAD_DISABLED) |
| 71 | |
| 72 | #define OFFLOAD_STATUS_INITIALIZER \ |
| 73 | { OFFLOAD_DISABLED, -1, 0, 0 } |
| 74 | |
| 75 | /* Offload runtime interfaces */ |
| 76 | |
| 77 | extern int _Offload_number_of_devices(void); |
| 78 | extern int _Offload_get_device_number(void); |
| 79 | extern int _Offload_get_physical_device_number(void); |
| 80 | |
| 81 | extern void* _Offload_shared_malloc(size_t size); |
| 82 | extern void _Offload_shared_free(void *ptr); |
| 83 | |
| 84 | extern void* _Offload_shared_aligned_malloc(size_t size, size_t align); |
| 85 | extern void _Offload_shared_aligned_free(void *ptr); |
| 86 | |
| 87 | extern int _Offload_signaled(int index, void *signal); |
| 88 | extern void _Offload_report(int val); |
| 89 | |
| 90 | /* OpenMP API */ |
| 91 | |
| 92 | extern void omp_set_default_device(int num); |
| 93 | extern int omp_get_default_device(void); |
| 94 | extern int omp_get_num_devices(void); |
| 95 | |
| 96 | /* OpenMP API wrappers */ |
| 97 | |
| 98 | /* Set num_threads on target */ |
| 99 | extern void omp_set_num_threads_target( |
| 100 | TARGET_TYPE target_type, |
| 101 | int target_number, |
| 102 | int num_threads |
| 103 | ); |
| 104 | |
| 105 | /* Get max_threads from target */ |
| 106 | extern int omp_get_max_threads_target( |
| 107 | TARGET_TYPE target_type, |
| 108 | int target_number |
| 109 | ); |
| 110 | |
| 111 | /* Get num_procs from target */ |
| 112 | extern int omp_get_num_procs_target( |
| 113 | TARGET_TYPE target_type, |
| 114 | int target_number |
| 115 | ); |
| 116 | |
| 117 | /* Set dynamic on target */ |
| 118 | extern void omp_set_dynamic_target( |
| 119 | TARGET_TYPE target_type, |
| 120 | int target_number, |
| 121 | int num_threads |
| 122 | ); |
| 123 | |
| 124 | /* Get dynamic from target */ |
| 125 | extern int omp_get_dynamic_target( |
| 126 | TARGET_TYPE target_type, |
| 127 | int target_number |
| 128 | ); |
| 129 | |
| 130 | /* Set nested on target */ |
| 131 | extern void omp_set_nested_target( |
| 132 | TARGET_TYPE target_type, |
| 133 | int target_number, |
| 134 | int nested |
| 135 | ); |
| 136 | |
| 137 | /* Get nested from target */ |
| 138 | extern int omp_get_nested_target( |
| 139 | TARGET_TYPE target_type, |
| 140 | int target_number |
| 141 | ); |
| 142 | |
| 143 | extern void omp_set_num_threads_target( |
| 144 | TARGET_TYPE target_type, |
| 145 | int target_number, |
| 146 | int num_threads |
| 147 | ); |
| 148 | |
| 149 | extern int omp_get_max_threads_target( |
| 150 | TARGET_TYPE target_type, |
| 151 | int target_number |
| 152 | ); |
| 153 | |
| 154 | extern int omp_get_num_procs_target( |
| 155 | TARGET_TYPE target_type, |
| 156 | int target_number |
| 157 | ); |
| 158 | |
| 159 | extern void omp_set_dynamic_target( |
| 160 | TARGET_TYPE target_type, |
| 161 | int target_number, |
| 162 | int num_threads |
| 163 | ); |
| 164 | |
| 165 | extern int omp_get_dynamic_target( |
| 166 | TARGET_TYPE target_type, |
| 167 | int target_number |
| 168 | ); |
| 169 | |
| 170 | extern void omp_set_nested_target( |
| 171 | TARGET_TYPE target_type, |
| 172 | int target_number, |
| 173 | int num_threads |
| 174 | ); |
| 175 | |
| 176 | extern int omp_get_nested_target( |
| 177 | TARGET_TYPE target_type, |
| 178 | int target_number |
| 179 | ); |
| 180 | |
| 181 | extern void omp_set_schedule_target( |
| 182 | TARGET_TYPE target_type, |
| 183 | int target_number, |
| 184 | omp_sched_t kind, |
| 185 | int modifier |
| 186 | ); |
| 187 | |
| 188 | extern void omp_get_schedule_target( |
| 189 | TARGET_TYPE target_type, |
| 190 | int target_number, |
| 191 | omp_sched_t *kind, |
| 192 | int *modifier |
| 193 | ); |
| 194 | |
| 195 | /* lock API functions */ |
| 196 | |
| 197 | typedef struct { |
| 198 | omp_lock_t lock; |
| 199 | } omp_lock_target_t; |
| 200 | |
| 201 | extern void omp_init_lock_target( |
| 202 | TARGET_TYPE target_type, |
| 203 | int target_number, |
| 204 | omp_lock_target_t *lock |
| 205 | ); |
| 206 | |
| 207 | extern void omp_destroy_lock_target( |
| 208 | TARGET_TYPE target_type, |
| 209 | int target_number, |
| 210 | omp_lock_target_t *lock |
| 211 | ); |
| 212 | |
| 213 | extern void omp_set_lock_target( |
| 214 | TARGET_TYPE target_type, |
| 215 | int target_number, |
| 216 | omp_lock_target_t *lock |
| 217 | ); |
| 218 | |
| 219 | extern void omp_unset_lock_target( |
| 220 | TARGET_TYPE target_type, |
| 221 | int target_number, |
| 222 | omp_lock_target_t *lock |
| 223 | ); |
| 224 | |
| 225 | extern int omp_test_lock_target( |
| 226 | TARGET_TYPE target_type, |
| 227 | int target_number, |
| 228 | omp_lock_target_t *lock |
| 229 | ); |
| 230 | |
| 231 | /* nested lock API functions */ |
| 232 | |
| 233 | typedef struct { |
| 234 | omp_nest_lock_t lock; |
| 235 | } omp_nest_lock_target_t; |
| 236 | |
| 237 | extern void omp_init_nest_lock_target( |
| 238 | TARGET_TYPE target_type, |
| 239 | int target_number, |
| 240 | omp_nest_lock_target_t *lock |
| 241 | ); |
| 242 | |
| 243 | extern void omp_destroy_nest_lock_target( |
| 244 | TARGET_TYPE target_type, |
| 245 | int target_number, |
| 246 | omp_nest_lock_target_t *lock |
| 247 | ); |
| 248 | |
| 249 | extern void omp_set_nest_lock_target( |
| 250 | TARGET_TYPE target_type, |
| 251 | int target_number, |
| 252 | omp_nest_lock_target_t *lock |
| 253 | ); |
| 254 | |
| 255 | extern void omp_unset_nest_lock_target( |
| 256 | TARGET_TYPE target_type, |
| 257 | int target_number, |
| 258 | omp_nest_lock_target_t *lock |
| 259 | ); |
| 260 | |
| 261 | extern int omp_test_nest_lock_target( |
| 262 | TARGET_TYPE target_type, |
| 263 | int target_number, |
| 264 | omp_nest_lock_target_t *lock |
| 265 | ); |
| 266 | |
| 267 | /* kmp API functions */ |
| 268 | |
| 269 | extern void kmp_set_stacksize_target( |
| 270 | TARGET_TYPE target_type, |
| 271 | int target_number, |
| 272 | int size |
| 273 | ); |
| 274 | |
| 275 | extern int kmp_get_stacksize_target( |
| 276 | TARGET_TYPE target_type, |
| 277 | int target_number |
| 278 | ); |
| 279 | |
| 280 | extern void kmp_set_stacksize_s_target( |
| 281 | TARGET_TYPE target_type, |
| 282 | int target_number, |
| 283 | size_t size |
| 284 | ); |
| 285 | |
| 286 | extern size_t kmp_get_stacksize_s_target( |
| 287 | TARGET_TYPE target_type, |
| 288 | int target_number |
| 289 | ); |
| 290 | |
| 291 | extern void kmp_set_blocktime_target( |
| 292 | TARGET_TYPE target_type, |
| 293 | int target_number, |
| 294 | int time |
| 295 | ); |
| 296 | |
| 297 | extern int kmp_get_blocktime_target( |
| 298 | TARGET_TYPE target_type, |
| 299 | int target_number |
| 300 | ); |
| 301 | |
| 302 | extern void kmp_set_library_serial_target( |
| 303 | TARGET_TYPE target_type, |
| 304 | int target_number |
| 305 | ); |
| 306 | |
| 307 | extern void kmp_set_library_turnaround_target( |
| 308 | TARGET_TYPE target_type, |
| 309 | int target_number |
| 310 | ); |
| 311 | |
| 312 | extern void kmp_set_library_throughput_target( |
| 313 | TARGET_TYPE target_type, |
| 314 | int target_number |
| 315 | ); |
| 316 | |
| 317 | extern void kmp_set_library_target( |
| 318 | TARGET_TYPE target_type, |
| 319 | int target_number, |
| 320 | int mode |
| 321 | ); |
| 322 | |
| 323 | extern int kmp_get_library_target( |
| 324 | TARGET_TYPE target_type, |
| 325 | int target_number |
| 326 | ); |
| 327 | |
| 328 | extern void kmp_set_defaults_target( |
| 329 | TARGET_TYPE target_type, |
| 330 | int target_number, |
| 331 | char const *defaults |
| 332 | ); |
| 333 | |
| 334 | /* affinity API functions */ |
| 335 | |
| 336 | typedef struct { |
| 337 | kmp_affinity_mask_t mask; |
| 338 | } kmp_affinity_mask_target_t; |
| 339 | |
| 340 | extern void kmp_create_affinity_mask_target( |
| 341 | TARGET_TYPE target_type, |
| 342 | int target_number, |
| 343 | kmp_affinity_mask_target_t *mask |
| 344 | ); |
| 345 | |
| 346 | extern void kmp_destroy_affinity_mask_target( |
| 347 | TARGET_TYPE target_type, |
| 348 | int target_number, |
| 349 | kmp_affinity_mask_target_t *mask |
| 350 | ); |
| 351 | |
| 352 | extern int kmp_set_affinity_target( |
| 353 | TARGET_TYPE target_type, |
| 354 | int target_number, |
| 355 | kmp_affinity_mask_target_t *mask |
| 356 | ); |
| 357 | |
| 358 | extern int kmp_get_affinity_target( |
| 359 | TARGET_TYPE target_type, |
| 360 | int target_number, |
| 361 | kmp_affinity_mask_target_t *mask |
| 362 | ); |
| 363 | |
| 364 | extern int kmp_get_affinity_max_proc_target( |
| 365 | TARGET_TYPE target_type, |
| 366 | int target_number |
| 367 | ); |
| 368 | |
| 369 | extern int kmp_set_affinity_mask_proc_target( |
| 370 | TARGET_TYPE target_type, |
| 371 | int target_number, |
| 372 | int proc, |
| 373 | kmp_affinity_mask_target_t *mask |
| 374 | ); |
| 375 | |
| 376 | extern int kmp_unset_affinity_mask_proc_target( |
| 377 | TARGET_TYPE target_type, |
| 378 | int target_number, |
| 379 | int proc, |
| 380 | kmp_affinity_mask_target_t *mask |
| 381 | ); |
| 382 | |
| 383 | extern int kmp_get_affinity_mask_proc_target( |
| 384 | TARGET_TYPE target_type, |
| 385 | int target_number, |
| 386 | int proc, |
| 387 | kmp_affinity_mask_target_t *mask |
| 388 | ); |
| 389 | |
| 390 | #ifdef __cplusplus |
| 391 | } /* extern "C" */ |
| 392 | |
| 393 | /* Namespace for the shared_allocator. */ |
| 394 | namespace __offload { |
| 395 | /* This follows the specification for std::allocator. */ |
| 396 | /* Forward declaration of the class template. */ |
| 397 | template <typename T> |
| 398 | class shared_allocator; |
| 399 | |
| 400 | /* Specialization for shared_allocator<void>. */ |
| 401 | template <> |
| 402 | class shared_allocator<void> { |
| 403 | public: |
| 404 | typedef void *pointer; |
| 405 | typedef const void *const_pointer; |
| 406 | typedef void value_type; |
| 407 | template <class U> struct rebind { typedef shared_allocator<U> other; }; |
| 408 | }; |
| 409 | |
| 410 | /* Definition of shared_allocator<T>. */ |
| 411 | template <class T> |
| 412 | class shared_allocator { |
| 413 | public: |
| 414 | typedef size_t size_type; |
| 415 | typedef ptrdiff_t difference_type; |
| 416 | typedef T *pointer; |
| 417 | typedef const T *const_pointer; |
| 418 | typedef T &reference; |
| 419 | typedef const T &const_reference; |
| 420 | typedef T value_type; |
| 421 | template <class U> struct rebind { typedef shared_allocator<U> other; }; |
| 422 | shared_allocator() throw() { } |
| 423 | shared_allocator(const shared_allocator&) throw() { } |
| 424 | template <class U> shared_allocator(const shared_allocator<U>&) throw() { } |
| 425 | ~shared_allocator() throw() { } |
| 426 | pointer address(reference x) const { return &x; } |
| 427 | const_pointer address(const_reference x) const { return &x; } |
| 428 | pointer allocate( |
| 429 | size_type, shared_allocator<void>::const_pointer hint = 0); |
| 430 | void deallocate(pointer p, size_type n); |
| 431 | size_type max_size() const throw() { |
| 432 | return size_type(-1)/sizeof(T); |
| 433 | } /* max_size */ |
| 434 | void construct(pointer p, const T& arg) { |
| 435 | ::new (p) T(arg); |
| 436 | } /* construct */ |
| 437 | void destroy(pointer p) { |
| 438 | p->~T(); |
| 439 | } /* destroy */ |
| 440 | }; |
| 441 | |
| 442 | /* Definition for allocate. */ |
| 443 | template <class T> |
| 444 | typename shared_allocator<T>::pointer |
| 445 | shared_allocator<T>::allocate(shared_allocator<T>::size_type s, |
| 446 | shared_allocator<void>::const_pointer) { |
| 447 | /* Allocate from shared memory. */ |
| 448 | void *ptr = _Offload_shared_malloc(s*sizeof(T)); |
| 449 | if (ptr == 0) std::__throw_bad_alloc(); |
| 450 | return static_cast<pointer>(ptr); |
| 451 | } /* allocate */ |
| 452 | |
| 453 | template <class T> |
| 454 | void shared_allocator<T>::deallocate(pointer p, |
| 455 | shared_allocator<T>::size_type) { |
| 456 | /* Free the shared memory. */ |
| 457 | _Offload_shared_free(p); |
| 458 | } /* deallocate */ |
| 459 | |
| 460 | template <typename _T1, typename _T2> |
| 461 | inline bool operator==(const shared_allocator<_T1> &, |
| 462 | const shared_allocator<_T2> &) throw() { |
| 463 | return true; |
| 464 | } /* operator== */ |
| 465 | |
| 466 | template <typename _T1, typename _T2> |
| 467 | inline bool operator!=(const shared_allocator<_T1> &, |
| 468 | const shared_allocator<_T2> &) throw() { |
| 469 | return false; |
| 470 | } /* operator!= */ |
| 471 | } /* __offload */ |
| 472 | #endif /* __cplusplus */ |
| 473 | |
| 474 | #endif /* OFFLOAD_H_INCLUDED */ |