offload_orsl.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. Copyright (c) 2014-2016 Intel Corporation. All Rights Reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions
  5. are met:
  6. * Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. * Neither the name of Intel Corporation nor the names of its
  12. contributors may be used to endorse or promote products derived
  13. from this software without specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  16. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  18. HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  20. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "offload_orsl.h"
  27. #include <stdlib.h>
  28. #include "offload_host.h"
  29. #include "orsl-lite/include/orsl-lite.h"
  30. namespace ORSL {
  31. static bool is_enabled = false;
  32. static const ORSLTag my_tag = (const ORSLTag) "Offload";
  33. void init()
  34. {
  35. const char *env_var = getenv("OFFLOAD_ENABLE_ORSL");
  36. if (env_var != 0 && *env_var != '\0') {
  37. int64_t new_val;
  38. if (__offload_parse_int_string(env_var, new_val)) {
  39. is_enabled = new_val;
  40. }
  41. else {
  42. LIBOFFLOAD_ERROR(c_invalid_env_var_int_value,
  43. "OFFLOAD_ENABLE_ORSL");
  44. }
  45. }
  46. if (is_enabled) {
  47. OFFLOAD_DEBUG_TRACE(2, "ORSL is enabled\n");
  48. }
  49. else {
  50. OFFLOAD_DEBUG_TRACE(2, "ORSL is disabled\n");
  51. }
  52. }
  53. bool reserve(int device)
  54. {
  55. if (is_enabled) {
  56. int pnum = mic_engines[device].get_physical_index();
  57. ORSLBusySet bset;
  58. bset.type = BUSY_SET_FULL;
  59. if (ORSLReserve(1, &pnum, &bset, my_tag) != 0) {
  60. return false;
  61. }
  62. }
  63. return true;
  64. }
  65. bool try_reserve(int device)
  66. {
  67. if (is_enabled) {
  68. int pnum = mic_engines[device].get_physical_index();
  69. ORSLBusySet bset;
  70. bset.type = BUSY_SET_FULL;
  71. if (ORSLTryReserve(1, &pnum, &bset, my_tag) != 0) {
  72. return false;
  73. }
  74. }
  75. return true;
  76. }
  77. void release(int device)
  78. {
  79. if (is_enabled) {
  80. int pnum = mic_engines[device].get_physical_index();
  81. ORSLBusySet bset;
  82. bset.type = BUSY_SET_FULL;
  83. if (ORSLRelease(1, &pnum, &bset, my_tag) != 0) {
  84. // should never get here
  85. }
  86. }
  87. }
  88. } // namespace ORSL