FasTensor  1.0.0
Transform Supercomputing for AI
ft_array_iterator.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_ITERATOR_H
81 #define AU_ARRAY_ITERATOR_H
82 
83 #include <array>
84 #include <iostream>
85 #include <vector>
86 
94 inline void
95 iterate(std::vector<int> &ordinates_p, const std::vector<int> start_p, const std::vector<int> end_p);
96 
97 #define ITERATOR_MACRO(ordinates_p, start_p, end_p) \
98  { \
99  int dimensions = start_p.size(); \
100  for (int dimension_index = dimensions - 1; dimension_index >= 0; dimension_index--) \
101  { \
102  if (ordinates_p[dimension_index] < end_p[dimension_index]) \
103  { \
104  ordinates_p[dimension_index]++; \
105  break; \
106  } \
107  ordinates_p[dimension_index] = start_p[dimension_index]; \
108  } \
109  }
110 
111 #endif
112 
113 /*
114 //Example file of usage
115 #include <vector>
116 #include <iostream>
117 
118 inline void
119 iterate(std::vector<int> &ordinates, const std::vector<int> minimums, const std::vector<int> maximums)
120 {
121  int dimensions = minimums.size();
122  // iterate over dimensions in reverse...
123  for (int dimension = dimensions - 1; dimension >= 0; dimension--)
124  {
125 
126  if (ordinates[dimension] < maximums[dimension])
127  {
128  // If this dimension can handle another increment... then done.
129  ordinates[dimension]++;
130  break;
131  }
132 
133  // Otherwise, reset this dimension and bubble up to the next dimension to take a look
134  ordinates[dimension] = minimums[dimension];
135  }
136 }
137 
138 int main()
139 {
140  int N = 3;
141  std::vector<int> min_c, max_c, ord_c;
142  min_c.resize(N);
143  max_c.resize(N);
144  ord_c.resize(N);
145 
146  min_c[0] = 1;
147  min_c[1] = 2;
148  min_c[2] = 1;
149 
150  max_c[0] = 4;
151  max_c[1] = 5;
152  max_c[2] = 6;
153 
154  ord_c[0] = min_c[0];
155  ord_c[1] = min_c[1];
156  ord_c[2] = min_c[2];
157 
158  int maxIterations = 1;
159  for (int i = 0; i < N; i++)
160  {
161  maxIterations = maxIterations * (max_c[i] - min_c[i] + 1);
162  }
163 
164  for (int iteration = 0; iteration < maxIterations; iteration++)
165  {
166  for (int i = 0; i < N; i++)
167  {
168  std::cout << ord_c[i] << " , ";
169  }
170  std::cout << "\n";
171  //iterate(n, ord, min, max)
172 
173  iterate(ord_c, min_c, max_c);
174  }
175 }
176 
177 */
void iterate(std::vector< int > &ordinates_p, const std::vector< int > start_p, const std::vector< int > end_p)
get the next ordinates_p from current ordinates_p call
Definition: au_array_iterator.cpp:89