1 /*
2     Copyright (c) 2014-2016 Intel Corporation.  All Rights Reserved.
3 
4     Redistribution and use in source and binary forms, with or without
5     modification, are permitted provided that the following conditions
6     are met:
7 
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.
16 
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.
28 */
29 
30 
31 #ifndef CEAN_UTIL_H_INCLUDED
32 #define CEAN_UTIL_H_INCLUDED
33 
34 #include <stdint.h>
35 #include "offload_util.h"
36 
37 // CEAN expression representation
38 struct Dim_Desc {
39     int64_t size;       // Length of data type
40     int64_t lindex;     // Lower index
41     int64_t lower;      // Lower section bound
42     int64_t upper;      // Upper section bound
43     int64_t stride;     // Stride
44 };
45 
46 struct Arr_Desc {
47     int64_t base;       // Base address
48     int64_t rank;       // Rank of array
49     Dim_Desc dim[1];
50 };
51 
52 struct CeanReadDim {
53     int64_t count; // The number of elements in this dimension
54     int64_t size;  // The number of bytes between successive
55                    // elements in this dimension.
56 };
57 
58 struct CeanReadRanges {
59     Arr_Desc* arr_desc;
60     void *  ptr;
61     int64_t current_number;   // the number of ranges read
62     int64_t range_max_number; // number of contiguous ranges
63     int64_t range_size;       // size of max contiguous range
64     int     last_noncont_ind; // size of Dim array
65     int64_t init_offset;      // offset of 1-st element from array left bound
66     CeanReadDim Dim[1];
67 };
68 
69 struct IntervalDesc {
70     int64_t lower;   // Lower  index
71     int64_t size;    // Size of each element at this interval
72 };
73 
74 struct NonContigDesc {
75     int64_t base;            // Base address
76     int64_t interval_cnt;    // Number of intervals
77     struct IntervalDesc interval[1];
78 };
79 
80 // array descriptor length
81 #define __arr_desc_length(rank) \
82     (sizeof(int64_t) + sizeof(Dim_Desc) * (rank))
83 
84 // returns offset and length of the data to be transferred
85 DLL_LOCAL void __arr_data_offset_and_length(const Arr_Desc *adp,
86                                   int64_t &offset,
87                                   int64_t &length);
88 
89 // define if data array described by argument is contiguous one
90 DLL_LOCAL bool is_arr_desc_contiguous(const Arr_Desc *ap);
91 
92 // allocate element of CeanReadRanges type initialized
93 // to read consequently contiguous ranges described by "ap" argument
94 DLL_LOCAL CeanReadRanges * init_read_ranges_arr_desc(const Arr_Desc *ap);
95 
96 // check if ranges described by 1 argument could be transferred into ranges
97 // described by 2-nd one
98 DLL_LOCAL bool cean_ranges_match(
99     CeanReadRanges * read_rng1,
100     CeanReadRanges * read_rng2
101 );
102 
103 // first argument - returned value by call to init_read_ranges_arr_desc.
104 // returns true if offset and length of next range is set successfuly.
105 // returns false if the ranges is over.
106 DLL_LOCAL bool get_next_range(
107     CeanReadRanges * read_rng,
108     int64_t *offset
109 );
110 
111 // returns number of transferred bytes
112 DLL_LOCAL int64_t cean_get_transf_size(CeanReadRanges * read_rng);
113 
114 #if OFFLOAD_DEBUG > 0
115 // prints array descriptor contents to stderr
116 DLL_LOCAL void    __arr_desc_dump(
117     const char *spaces,
118     const char *name,
119     const Arr_Desc *adp,
120     bool dereference,
121     bool print_values);
122 
123 DLL_LOCAL void noncont_struct_dump(
124     const char *spaces,
125     const char *name,
126     struct NonContigDesc *desc_p);
127 
128 DLL_LOCAL int64_t get_noncont_struct_size(struct NonContigDesc *desc_p);
129 
130 #define ARRAY_DESC_DUMP(spaces, name, adp, dereference, print_values) \
131     if (console_enabled >= 2) \
132         __arr_desc_dump(spaces, name, adp, dereference, print_values);
133 #else
134 #define ARRAY_DESC_DUMP(spaces, name, adp, dereference, print_values)
135 #endif // OFFLOAD_DEBUG
136 
137 #endif // CEAN_UTIL_H_INCLUDED
138