/cp
[official-gcc.git] / liboffloadmic / runtime / dv_util.cpp
blobf79cf1f8699a1db188217fe7bfcdc248781514cd
1 /*
2 Copyright (c) 2014-2016 Intel Corporation. All Rights Reserved.
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
6 are met:
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.
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.
31 #include "offload_common.h"
33 bool __dv_is_contiguous(const ArrDesc *dvp)
35 if (dvp->Flags & ArrDescFlagsContiguous) {
36 return true;
39 if (dvp->Rank != 0) {
40 if (dvp->Dim[0].Mult != dvp->Len) {
41 return false;
43 for (int i = 1; i < dvp->Rank; i++) {
44 if (dvp->Dim[i].Mult !=
45 dvp->Dim[i-1].Extent * dvp->Dim[i-1].Mult) {
46 return false;
50 return true;
53 bool __dv_is_allocated(const ArrDesc *dvp)
55 return (dvp->Flags & ArrDescFlagsDefined);
58 uint64_t __dv_data_length(const ArrDesc *dvp)
60 uint64_t size;
62 if (dvp->Rank == 0) {
63 size = dvp->Len;
64 return size;
67 size = dvp->Len;
68 for (int i = 0; i < dvp->Rank; ++i) {
69 size += (dvp->Dim[i].Extent-1) * dvp->Dim[i].Mult;
71 return size;
74 uint64_t __dv_data_length(const ArrDesc *dvp, int64_t count)
76 if (dvp->Rank == 0) {
77 return count;
80 return count * dvp->Dim[0].Mult;
83 // Create CeanReadRanges data for reading contiguous ranges of
84 // noncontiguous array defined by the argument
85 CeanReadRanges * init_read_ranges_dv(const ArrDesc *dvp)
87 int64_t len;
88 int count;
89 int rank = dvp->Rank;
90 CeanReadRanges *res = NULL;
92 if (rank != 0) {
93 int i = 0;
94 len = dvp->Len;
95 if (dvp->Dim[0].Mult == len) {
96 for (i = 1; i < rank; i++) {
97 len *= dvp->Dim[i-1].Extent;
98 if (dvp->Dim[i].Mult != len) {
99 break;
103 res = (CeanReadRanges *)malloc(
104 sizeof(CeanReadRanges) + (rank - i) * sizeof(CeanReadDim));
105 if (res == NULL)
106 LIBOFFLOAD_ERROR(c_malloc);
107 res -> last_noncont_ind = rank - i - 1;
108 count = 1;
109 for (; i < rank; i++) {
110 res->Dim[rank - i - 1].count = count;
111 res->Dim[rank - i - 1].size = dvp->Dim[i].Mult;
112 count *= dvp->Dim[i].Extent;
114 res -> range_max_number = count;
115 res -> range_size = len;
116 res -> ptr = (void*)dvp->Base;
117 res -> current_number = 0;
118 res -> init_offset = 0;
120 return res;
123 #if OFFLOAD_DEBUG > 0
124 void __dv_desc_dump(const char *name, const ArrDesc *dvp)
126 OFFLOAD_TRACE(3, "%s DV %p\n", name, dvp);
128 if (dvp != 0) {
129 OFFLOAD_TRACE(3,
130 " dv->Base = 0x%lx\n"
131 " dv->Len = 0x%lx\n"
132 " dv->Offset = 0x%lx\n"
133 " dv->Flags = 0x%lx\n"
134 " dv->Rank = 0x%lx\n"
135 " dv->Resrvd = 0x%lx\n",
136 dvp->Base,
137 dvp->Len,
138 dvp->Offset,
139 dvp->Flags,
140 dvp->Rank,
141 dvp->Reserved);
143 for (int i = 0 ; i < dvp->Rank; i++) {
144 OFFLOAD_TRACE(3,
145 " (%d) Extent=%ld, Multiplier=%ld, LowerBound=%ld\n",
147 dvp->Dim[i].Extent,
148 dvp->Dim[i].Mult,
149 dvp->Dim[i].LowerBound);
153 #endif // OFFLOAD_DEBUG > 0