dv_util.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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_common.h"
  27. bool __dv_is_contiguous(const ArrDesc *dvp)
  28. {
  29. if (dvp->Flags & ArrDescFlagsContiguous) {
  30. return true;
  31. }
  32. if (dvp->Rank != 0) {
  33. if (dvp->Dim[0].Mult != dvp->Len) {
  34. return false;
  35. }
  36. for (int i = 1; i < dvp->Rank; i++) {
  37. if (dvp->Dim[i].Mult !=
  38. dvp->Dim[i-1].Extent * dvp->Dim[i-1].Mult) {
  39. return false;
  40. }
  41. }
  42. }
  43. return true;
  44. }
  45. bool __dv_is_allocated(const ArrDesc *dvp)
  46. {
  47. return (dvp->Flags & ArrDescFlagsDefined);
  48. }
  49. uint64_t __dv_data_length(const ArrDesc *dvp)
  50. {
  51. uint64_t size;
  52. if (dvp->Rank == 0) {
  53. size = dvp->Len;
  54. return size;
  55. }
  56. size = dvp->Len;
  57. for (int i = 0; i < dvp->Rank; ++i) {
  58. size += (dvp->Dim[i].Extent-1) * dvp->Dim[i].Mult;
  59. }
  60. return size;
  61. }
  62. uint64_t __dv_data_length(const ArrDesc *dvp, int64_t count)
  63. {
  64. if (dvp->Rank == 0) {
  65. return count;
  66. }
  67. return count * dvp->Dim[0].Mult;
  68. }
  69. // Create CeanReadRanges data for reading contiguous ranges of
  70. // noncontiguous array defined by the argument
  71. CeanReadRanges * init_read_ranges_dv(const ArrDesc *dvp)
  72. {
  73. int64_t len;
  74. int count;
  75. int rank = dvp->Rank;
  76. CeanReadRanges *res = NULL;
  77. if (rank != 0) {
  78. int i = 0;
  79. len = dvp->Len;
  80. if (dvp->Dim[0].Mult == len) {
  81. for (i = 1; i < rank; i++) {
  82. len *= dvp->Dim[i-1].Extent;
  83. if (dvp->Dim[i].Mult != len) {
  84. break;
  85. }
  86. }
  87. }
  88. res = (CeanReadRanges *)malloc(
  89. sizeof(CeanReadRanges) + (rank - i) * sizeof(CeanReadDim));
  90. if (res == NULL)
  91. LIBOFFLOAD_ERROR(c_malloc);
  92. res -> last_noncont_ind = rank - i - 1;
  93. count = 1;
  94. for (; i < rank; i++) {
  95. res->Dim[rank - i - 1].count = count;
  96. res->Dim[rank - i - 1].size = dvp->Dim[i].Mult;
  97. count *= dvp->Dim[i].Extent;
  98. }
  99. res -> range_max_number = count;
  100. res -> range_size = len;
  101. res -> ptr = (void*)dvp->Base;
  102. res -> current_number = 0;
  103. res -> init_offset = 0;
  104. }
  105. return res;
  106. }
  107. #if OFFLOAD_DEBUG > 0
  108. void __dv_desc_dump(const char *name, const ArrDesc *dvp)
  109. {
  110. OFFLOAD_TRACE(3, "%s DV %p\n", name, dvp);
  111. if (dvp != 0) {
  112. OFFLOAD_TRACE(3,
  113. " dv->Base = 0x%lx\n"
  114. " dv->Len = 0x%lx\n"
  115. " dv->Offset = 0x%lx\n"
  116. " dv->Flags = 0x%lx\n"
  117. " dv->Rank = 0x%lx\n"
  118. " dv->Resrvd = 0x%lx\n",
  119. dvp->Base,
  120. dvp->Len,
  121. dvp->Offset,
  122. dvp->Flags,
  123. dvp->Rank,
  124. dvp->Reserved);
  125. for (int i = 0 ; i < dvp->Rank; i++) {
  126. OFFLOAD_TRACE(3,
  127. " (%d) Extent=%ld, Multiplier=%ld, LowerBound=%ld\n",
  128. i,
  129. dvp->Dim[i].Extent,
  130. dvp->Dim[i].Mult,
  131. dvp->Dim[i].LowerBound);
  132. }
  133. }
  134. }
  135. #endif // OFFLOAD_DEBUG > 0