blob: fe1890b718bee6e0a8fde3e4113fa8ae3645f365 [file] [log] [blame]
Jim Cownie33f7b242014-04-09 15:40:23 +00001//===----------------------------------------------------------------------===//
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 "cean_util.h"
12#include "offload_common.h"
13
14// 1. allocate element of CeanReadRanges type
15// 2. initialized it for reading consequently contiguous ranges
16// described by "ap" argument
17CeanReadRanges * init_read_ranges_arr_desc(const arr_desc *ap)
18{
19 CeanReadRanges * res;
20
21 // find the max contiguous range
22 int64_t rank = ap->rank - 1;
23 int64_t length = ap->dim[rank].size;
24 for (; rank >= 0; rank--) {
25 if (ap->dim[rank].stride == 1) {
26 length *= (ap->dim[rank].upper - ap->dim[rank].lower + 1);
27 if (rank > 0 && length != ap->dim[rank - 1].size) {
28 break;
29 }
30 }
31 else {
32 break;
33 }
34 }
35
36 res =(CeanReadRanges *)malloc(sizeof(CeanReadRanges) +
37 (ap->rank - rank) * sizeof(CeanReadDim));
38 res->current_number = 0;
39 res->range_size = length;
40 res->last_noncont_ind = rank;
41
42 // calculate number of contiguous ranges inside noncontiguous dimensions
43 int count = 1;
44 bool prev_is_cont = true;
45 int64_t offset = 0;
46
47 for (; rank >= 0; rank--) {
48 res->Dim[rank].count = count;
49 res->Dim[rank].size = ap->dim[rank].stride * ap->dim[rank].size;
50 count *= (prev_is_cont && ap->dim[rank].stride == 1? 1 :
51 (ap->dim[rank].upper - ap->dim[rank].lower +
52 ap->dim[rank].stride) / ap->dim[rank].stride);
53 prev_is_cont = false;
54 offset +=(ap->dim[rank].lower - ap->dim[rank].lindex) *
55 ap->dim[rank].size;
56 }
57 res->range_max_number = count;
58 res -> ptr = (void*)ap->base;
59 res -> init_offset = offset;
60 return res;
61}
62
Alp Tokerc2d5e612014-06-01 18:28:36 +000063// check if ranges described by 1 argument could be transferred into ranges
Jim Cownie33f7b242014-04-09 15:40:23 +000064// described by 2-nd one
65bool cean_ranges_match(
66 CeanReadRanges * read_rng1,
67 CeanReadRanges * read_rng2
68)
69{
70 return ( read_rng1 == NULL || read_rng2 == NULL ||
71 (read_rng1->range_size % read_rng2->range_size == 0 ||
72 read_rng2->range_size % read_rng1->range_size == 0));
73}
74
75// Set next offset and length and returns true for next range.
76// Returns false if the ranges are over.
77bool get_next_range(
78 CeanReadRanges * read_rng,
79 int64_t *offset
80)
81{
82 if (++read_rng->current_number > read_rng->range_max_number) {
83 read_rng->current_number = 0;
84 return false;
85 }
86 int rank = 0;
87 int num = read_rng->current_number - 1;
88 int64_t cur_offset = 0;
89 int num_loc;
90 for (; rank <= read_rng->last_noncont_ind; rank++) {
91 num_loc = num / read_rng->Dim[rank].count;
92 cur_offset += num_loc * read_rng->Dim[rank].size;
93 num = num % read_rng->Dim[rank].count;
94 }
95 *offset = cur_offset + read_rng->init_offset;
96 return true;
97}
98
99bool is_arr_desc_contiguous(const arr_desc *ap)
100{
101 int64_t rank = ap->rank - 1;
102 int64_t length = ap->dim[rank].size;
103 for (; rank >= 0; rank--) {
104 if (ap->dim[rank].stride > 1 &&
105 ap->dim[rank].upper - ap->dim[rank].lower != 0) {
106 return false;
107 }
108 else if (length != ap->dim[rank].size) {
109 for (; rank >= 0; rank--) {
110 if (ap->dim[rank].upper - ap->dim[rank].lower != 0) {
111 return false;
112 }
113 }
114 return true;
115 }
116 length *= (ap->dim[rank].upper - ap->dim[rank].lower + 1);
117 }
118 return true;
119}
120
121int64_t cean_get_transf_size(CeanReadRanges * read_rng)
122{
123 return(read_rng->range_max_number * read_rng->range_size);
124}
125
126static uint64_t last_left, last_right;
127typedef void (*fpp)(const char *spaces, uint64_t low, uint64_t high, int esize);
128
129static void generate_one_range(
130 const char *spaces,
131 uint64_t lrange,
132 uint64_t rrange,
133 fpp fp,
134 int esize
135)
136{
137 OFFLOAD_TRACE(3,
138 "%s generate_one_range(lrange=%p, rrange=%p, esize=%d)\n",
139 spaces, (void*)lrange, (void*)rrange, esize);
140 if (last_left == -1) {
141 // First range
142 last_left = lrange;
143 }
144 else {
145 if (lrange == last_right+1) {
146 // Extend previous range, don't print
147 }
148 else {
149 (*fp)(spaces, last_left, last_right, esize);
150 last_left = lrange;
151 }
152 }
153 last_right = rrange;
154}
155
156static void generate_mem_ranges_one_rank(
157 const char *spaces,
158 uint64_t base,
159 uint64_t rank,
160 const struct dim_desc *ddp,
161 fpp fp,
162 int esize
163)
164{
165 uint64_t lindex = ddp->lindex;
166 uint64_t lower = ddp->lower;
167 uint64_t upper = ddp->upper;
168 uint64_t stride = ddp->stride;
169 uint64_t size = ddp->size;
170 OFFLOAD_TRACE(3,
171 "%s "
172 "generate_mem_ranges_one_rank(base=%p, rank=%lld, lindex=%lld, "
173 "lower=%lld, upper=%lld, stride=%lld, size=%lld, esize=%d)\n",
174 spaces, (void*)base, rank, lindex, lower, upper, stride, size, esize);
175 if (rank == 1) {
176 uint64_t lrange, rrange;
177 if (stride == 1) {
178 lrange = base + (lower-lindex)*size;
179 rrange = lrange + (upper-lower+1)*size - 1;
180 generate_one_range(spaces, lrange, rrange, fp, esize);
181 }
182 else {
183 for (int i=lower-lindex; i<=upper-lindex; i+=stride) {
184 lrange = base + i*size;
185 rrange = lrange + size - 1;
186 generate_one_range(spaces, lrange, rrange, fp, esize);
187 }
188 }
189 }
190 else {
191 for (int i=lower-lindex; i<=upper-lindex; i+=stride) {
192 generate_mem_ranges_one_rank(
193 spaces, base+i*size, rank-1, ddp+1, fp, esize);
194
195 }
196 }
197}
198
199static void generate_mem_ranges(
200 const char *spaces,
201 const arr_desc *adp,
202 bool deref,
203 fpp fp
204)
205{
206 uint64_t esize;
207
208 OFFLOAD_TRACE(3,
209 "%s "
210 "generate_mem_ranges(adp=%p, deref=%d, fp)\n",
211 spaces, adp, deref);
212 last_left = -1;
213 last_right = -2;
214
215 // Element size is derived from last dimension
216 esize = adp->dim[adp->rank-1].size;
217
218 generate_mem_ranges_one_rank(
219 // For c_cean_var the base addr is the address of the data
220 // For c_cean_var_ptr the base addr is dereferenced to get to the data
221 spaces, deref ? *((uint64_t*)(adp->base)) : adp->base,
222 adp->rank, &adp->dim[0], fp, esize);
223 (*fp)(spaces, last_left, last_right, esize);
224}
225
226// returns offset and length of the data to be transferred
227void __arr_data_offset_and_length(
228 const arr_desc *adp,
229 int64_t &offset,
230 int64_t &length
231)
232{
233 int64_t rank = adp->rank - 1;
234 int64_t size = adp->dim[rank].size;
235 int64_t r_off = 0; // offset from right boundary
236
237 // find the rightmost dimension which takes just part of its
238 // range. We define it if the size of left rank is not equal
239 // the range's length between upper and lower boungaries
240 while (rank > 0) {
241 size *= (adp->dim[rank].upper - adp->dim[rank].lower + 1);
242 if (size != adp->dim[rank - 1].size) {
243 break;
244 }
245 rank--;
246 }
247
248 offset = (adp->dim[rank].lower - adp->dim[rank].lindex) *
249 adp->dim[rank].size;
250
251 // find gaps both from the left - offset and from the right - r_off
252 for (rank--; rank >= 0; rank--) {
253 offset += (adp->dim[rank].lower - adp->dim[rank].lindex) *
254 adp->dim[rank].size;
255 r_off += adp->dim[rank].size -
256 (adp->dim[rank + 1].upper - adp->dim[rank + 1].lindex + 1) *
257 adp->dim[rank + 1].size;
258 }
259 length = (adp->dim[0].upper - adp->dim[0].lindex + 1) *
260 adp->dim[0].size - offset - r_off;
261}
262
263#if OFFLOAD_DEBUG > 0
264
265void print_range(
266 const char *spaces,
267 uint64_t low,
268 uint64_t high,
269 int esize
270)
271{
272 char buffer[1024];
273 char number[32];
274
275 OFFLOAD_TRACE(3, "%s print_range(low=%p, high=%p, esize=%d)\n",
276 spaces, (void*)low, (void*)high, esize);
277
278 if (console_enabled < 4) {
279 return;
280 }
281 OFFLOAD_TRACE(4, "%s values:\n", spaces);
282 int count = 0;
283 buffer[0] = '\0';
284 while (low <= high)
285 {
286 switch (esize)
287 {
288 case 1:
289 sprintf(number, "%d ", *((char *)low));
290 low += 1;
291 break;
292 case 2:
293 sprintf(number, "%d ", *((short *)low));
294 low += 2;
295 break;
296 case 4:
297 sprintf(number, "%d ", *((int *)low));
298 low += 4;
299 break;
300 default:
301 sprintf(number, "0x%016x ", *((uint64_t *)low));
302 low += 8;
303 break;
304 }
305 strcat(buffer, number);
306 count++;
307 if (count == 10) {
308 OFFLOAD_TRACE(4, "%s %s\n", spaces, buffer);
309 count = 0;
310 buffer[0] = '\0';
311 }
312 }
313 if (count != 0) {
314 OFFLOAD_TRACE(4, "%s %s\n", spaces, buffer);
315 }
316}
317
318void __arr_desc_dump(
319 const char *spaces,
320 const char *name,
321 const arr_desc *adp,
322 bool deref
323)
324{
325 OFFLOAD_TRACE(2, "%s%s CEAN expression %p\n", spaces, name, adp);
326
327 if (adp != 0) {
328 OFFLOAD_TRACE(2, "%s base=%llx, rank=%lld\n",
329 spaces, adp->base, adp->rank);
330
331 for (int i = 0; i < adp->rank; i++) {
332 OFFLOAD_TRACE(2,
333 "%s dimension %d: size=%lld, lindex=%lld, "
334 "lower=%lld, upper=%lld, stride=%lld\n",
335 spaces, i, adp->dim[i].size, adp->dim[i].lindex,
336 adp->dim[i].lower, adp->dim[i].upper,
337 adp->dim[i].stride);
338 }
339 // For c_cean_var the base addr is the address of the data
340 // For c_cean_var_ptr the base addr is dereferenced to get to the data
341 generate_mem_ranges(spaces, adp, deref, &print_range);
342 }
343}
344#endif // OFFLOAD_DEBUG