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 | #include "offload_orsl.h" |
| 12 | #include <stdlib.h> |
| 13 | #include "offload_host.h" |
| 14 | #include "orsl-lite/include/orsl-lite.h" |
| 15 | |
| 16 | namespace ORSL { |
| 17 | |
| 18 | static bool is_enabled = false; |
| 19 | static const ORSLTag my_tag = "Offload"; |
| 20 | |
| 21 | void init() |
| 22 | { |
| 23 | const char *env_var = getenv("OFFLOAD_ENABLE_ORSL"); |
| 24 | if (env_var != 0 && *env_var != '\0') { |
| 25 | int64_t new_val; |
| 26 | if (__offload_parse_int_string(env_var, new_val)) { |
| 27 | is_enabled = new_val; |
| 28 | } |
| 29 | else { |
| 30 | LIBOFFLOAD_ERROR(c_invalid_env_var_int_value, |
| 31 | "OFFLOAD_ENABLE_ORSL"); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | if (is_enabled) { |
| 36 | OFFLOAD_DEBUG_TRACE(2, "ORSL is enabled\n"); |
| 37 | } |
| 38 | else { |
| 39 | OFFLOAD_DEBUG_TRACE(2, "ORSL is disabled\n"); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | bool reserve(int device) |
| 44 | { |
| 45 | if (is_enabled) { |
| 46 | int pnum = mic_engines[device].get_physical_index(); |
| 47 | ORSLBusySet bset; |
| 48 | |
| 49 | bset.type = BUSY_SET_FULL; |
| 50 | if (ORSLReserve(1, &pnum, &bset, my_tag) != 0) { |
| 51 | return false; |
| 52 | } |
| 53 | } |
| 54 | return true; |
| 55 | } |
| 56 | |
| 57 | bool try_reserve(int device) |
| 58 | { |
| 59 | if (is_enabled) { |
| 60 | int pnum = mic_engines[device].get_physical_index(); |
| 61 | ORSLBusySet bset; |
| 62 | |
| 63 | bset.type = BUSY_SET_FULL; |
| 64 | if (ORSLTryReserve(1, &pnum, &bset, my_tag) != 0) { |
| 65 | return false; |
| 66 | } |
| 67 | } |
| 68 | return true; |
| 69 | } |
| 70 | |
| 71 | void release(int device) |
| 72 | { |
| 73 | if (is_enabled) { |
| 74 | int pnum = mic_engines[device].get_physical_index(); |
| 75 | ORSLBusySet bset; |
| 76 | |
| 77 | bset.type = BUSY_SET_FULL; |
| 78 | if (ORSLRelease(1, &pnum, &bset, my_tag) != 0) { |
| 79 | // should never get here |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | } // namespace ORSL |