FasTensor  1.0.0
Transform Supercomputing for AI
ft_array_view_access.h
Go to the documentation of this file.
1 /*
2 ****************************
3 
4 FasTensor (FT) Copyright (c) 2021, The Regents of the University of
5 California, through Lawrence Berkeley National Laboratory (subject to
6 receipt of any required approvals from the U.S. Dept. of Energy).
7 All rights reserved.
8 
9 If you have questions about your rights to use or distribute this software,
10 please contact Berkeley Lab's Intellectual Property Office at
12 
13 NOTICE. This Software was developed under funding from the U.S. Department
14 of Energy and the U.S. Government consequently retains certain rights. As
15 such, the U.S. Government has been granted for itself and others acting on
16 its behalf a paid-up, nonexclusive, irrevocable, worldwide license in the
17 Software to reproduce, distribute copies to the public, prepare derivative
18 works, and perform publicly and display publicly, and to permit others to do so.
19 
20 
21 ****************************
22 
23 
24 *** License Agreement ***
25 
26 FasTensor (FT) Copyright (c) 2021, The Regents of the University of
27 California, through Lawrence Berkeley National Laboratory (subject to
28 receipt of any required approvals from the U.S. Dept. of Energy).
29 All rights reserved.
30 
31 Redistribution and use in source and binary forms, with or without
32 modification, are permitted provided that the following conditions are met:
33 
34 (1) Redistributions of source code must retain the above copyright notice,
35 this list of conditions and the following disclaimer.
36 
37 (2) Redistributions in binary form must reproduce the above copyright
38 notice, this list of conditions and the following disclaimer in the
39 documentation and/or other materials provided with the distribution.
40 
41 (3) Neither the name of the University of California, Lawrence Berkeley
42 National Laboratory, U.S. Dept. of Energy nor the names of its contributors
43 may be used to endorse or promote products derived from this software
44 without specific prior written permission.
45 
46 
47 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
48 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
51 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
52 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
53 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
54 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
55 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
56 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
57 POSSIBILITY OF SUCH DAMAGE.
58 
59 You are under no obligation whatsoever to provide any bug fixes, patches,
60 or upgrades to the features, functionality or performance of the source
61 code ("Enhancements") to anyone; however, if you choose to make your
62 Enhancements available either publicly, or directly to Lawrence Berkeley
63 National Laboratory, without imposing a separate written license agreement
64 for such Enhancements, then you hereby grant the following license: a
65 non-exclusive, royalty-free perpetual license to install, use, modify,
66 prepare derivative works, incorporate into other computer software,
67 distribute, and sublicense such enhancements or derivative works thereof,
68 in binary and source code form.
69 */
70 
80 #ifndef AU_ARRAY_VIEW_ACCESS_H
81 #define AU_ARRAY_VIEW_ACCESS_H
82 
83 #include <vector>
84 #include <cstdint>
85 #include <cstring>
86 #include "ft_array_iterator.h"
87 #include "ft_utility_macro.h"
88 
89 #define ARRAY_VIEW_READ 0
90 #define ARRAY_VIEW_WRITE 1
91 
102 #define VIEW_ACCESS_HELP_V(v_bu, v_st, a_bu, a_st, count_n, rw) \
103  { \
104  if (rw == ARRAY_VIEW_READ) \
105  { \
106  std::memcpy(); \
107  std::copy(a_bu.begin() + a_st, a_bu.begin() + a_st + count_n, v_bu.begin() + v_st); \
108  } \
109  else \
110  { \
111  std::copy(v_bu.begin() + v_st, v_bu.begin() + v_st + count_n, a_bu.begin() + a_st); \
112  } \
113  }
126 template <class T>
127 inline int ArrayViewAccessV(std::vector<T> &view_v, std::vector<T> &array_v, std::vector<unsigned long long> &array_size, std::vector<unsigned long long> &start, std::vector<unsigned long long> &end, int read_write_code)
128 {
129  std::vector<unsigned long long> view_size;
130  unsigned long long element_count = 1;
131  view_size.resize(array_size.size());
132  for (int i = 0; i < array_size.size(); i++)
133  {
134  view_size[i] = end[i] - start[i] + 1;
135  element_count = element_count * view_size[i];
136  }
137 
138  view_v.resize(element_count);
139  if (view_size == array_size)
140  {
141  VIEW_ACCESS_HELP(view_v, 0, array_v, 0, element_count, read_write_code);
142  return 0;
143  }
144 
145  if (view_size.size() == 1)
146  {
147  VIEW_ACCESS_HELP(view_v, 0, array_v, start[0], element_count, read_write_code);
148  return 0;
149  }
150 
151  unsigned long long array_buffer_offset = 0, view_buffer_offset = 0;
152 
153  if (view_size.size() == 2)
154  {
155  for (int i = 0; i < view_size[0]; i++)
156  {
157  array_buffer_offset = array_size[1] * (start[0] + i) + start[1];
158  view_buffer_offset = view_size[1] * i;
159  //Copy each row
160  VIEW_ACCESS_HELP(view_v, view_buffer_offset, array_v, array_buffer_offset, view_size[1], read_write_code);
161  }
162  return 0;
163  }
164 
165  //Generic version
166  array_buffer_offset = 0, view_buffer_offset = 0;
167  std::vector<unsigned long long> ord(start.begin(), start.end());
168  for (unsigned long long i = 0; i < element_count; i++)
169  {
170  ROW_MAJOR_ORDER_MACRO(array_size, array_size.size(), ord, array_buffer_offset);
171  MEMCPY_ACCESS_HELP(view_v, view_buffer_offset, array_v, array_buffer_offset, 1, read_write_code);
172  view_buffer_offset++;
173  ITERATOR_MACRO(ord, start, end); //update ord by increasing "1", row-major order
174  }
175 
176  return 0;
177 }
178 
179 //void* memcpy( void* dest, const void* src, std::size_t count );
180 //std::memcpy(data, local_mirror_buffer, element_count * sizeof(T));
181 
182 #define VIEW_ACCESS_HELP_P(v_bu, v_st, a_bu, a_st, count_n, rw, type_size) \
183  { \
184  if (rw == ARRAY_VIEW_READ) \
185  { \
186  std::memcpy(v_bu + v_st, a_bu + a_st, count_n * type_size); \
187  } \
188  else \
189  { \
190  std::memcpy(a_bu + a_st, v_bu + v_st, count_n * type_size); \
191  } \
192  }
193 
194 template <class T>
195 inline int ArrayViewAccessP(T *view_v, T *array_v, std::vector<unsigned long long> array_size, std::vector<unsigned long long> start, std::vector<unsigned long long> end, int read_write_code)
196 {
197  std::vector<unsigned long long> view_size;
198  unsigned long long element_count = 1;
199  view_size.resize(array_size.size());
200  for (int i = 0; i < array_size.size(); i++)
201  {
202  view_size[i] = end[i] - start[i] + 1;
203  element_count = element_count * view_size[i];
204  }
205 
206  //view_v.resize(element_count);
207  if (view_size == array_size)
208  {
209  VIEW_ACCESS_HELP_P(view_v, 0, array_v, 0, element_count, read_write_code, sizeof(T));
210  return 0;
211  }
212 
213  if (view_size.size() == 1)
214  {
215  VIEW_ACCESS_HELP_P(view_v, 0, array_v, start[0], element_count, read_write_code, sizeof(T));
216  return 0;
217  }
218 
219  unsigned long long array_buffer_offset = 0, view_buffer_offset = 0;
220  if (view_size.size() == 2)
221  {
222  //PrintVector("view_size =", view_size);
223  //PrintVector("array_size =", array_size);
224 
225  for (int i = 0; i < view_size[0]; i++)
226  {
227  // PrintScalar("array_buffer_offset = ", array_buffer_offset);
228  // PrintScalar("view_buffer_offset = ", view_buffer_offset);
229  array_buffer_offset = array_size[1] * (start[0] + i) + start[1];
230  view_buffer_offset = view_size[1] * i;
231  //Copy each row
232  VIEW_ACCESS_HELP_P(view_v, view_buffer_offset, array_v, array_buffer_offset, view_size[1], read_write_code, sizeof(T));
233  //VIEW_ACCESS_HELP(view_v, view_size[1] * i, array_v, array_size[1] * (start[0] + i) + start[1], view_size[1], read_write_code);
234  }
235  return 0;
236  }
237 
238  //Generic version
239  array_buffer_offset = 0, view_buffer_offset = 0;
240  std::vector<unsigned long long> ord(start.begin(), start.end());
241  for (unsigned long long i = 0; i < element_count; i++)
242  {
243  ROW_MAJOR_ORDER_MACRO(array_size, array_size.size(), ord, array_buffer_offset);
244  VIEW_ACCESS_HELP_P(view_v, view_buffer_offset, array_v, array_buffer_offset, 1, read_write_code, sizeof(T));
245  view_buffer_offset++;
246  ITERATOR_MACRO(ord, start, end); //update ord by increasing "1", row-major order
247  }
248 
249  return 0;
250 }
251 #endif
#define ITERATOR_MACRO(ordinates_p, start_p, end_p)
Definition: ft_array_iterator.h:97
#define VIEW_ACCESS_HELP_P(v_bu, v_st, a_bu, a_st, count_n, rw, type_size)
Definition: ft_array_view_access.h:182
int ArrayViewAccessP(T *view_v, T *array_v, std::vector< unsigned long long > array_size, std::vector< unsigned long long > start, std::vector< unsigned long long > end, int read_write_code)
Definition: ft_array_view_access.h:195
int ArrayViewAccessV(std::vector< T > &view_v, std::vector< T > &array_v, std::vector< unsigned long long > &array_size, std::vector< unsigned long long > &start, std::vector< unsigned long long > &end, int read_write_code)
Access an view (subset) of an array.
Definition: ft_array_view_access.h:127
#define ROW_MAJOR_ORDER_MACRO(dsize, dsize_len, coordinate, offset)
macro version of above two functions for speed
Definition: ft_utility_macro.h:124