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