Domi
Multi-dimensional, distributed data structures
Domi_Utils.hpp
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Domi: Multi-dimensional Distributed Linear Algebra Services
5 // Copyright (2014) Sandia Corporation
6 //
7 // Under the terms of Contract DE-AC04-94AL85000 with Sandia
8 // Corporation, the U.S. Government retains certain rights in this
9 // software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact William F. Spotz (wfspotz@sandia.gov)
39 //
40 // ***********************************************************************
41 // @HEADER
42 
43 #ifndef DOMI_UTILS_HPP
44 #define DOMI_UTILS_HPP
45 
46 // Domi includes
47 #include "Domi_ConfigDefs.hpp"
48 
49 // Teuchos includes
50 #include "Teuchos_Array.hpp"
51 #include "Teuchos_ArrayView.hpp"
52 
53 #ifdef HAVE_MPI
54 // MPI include
55 #include "mpi.h"
56 #endif
57 
58 namespace Domi
59 {
60 
63 
65 
75 typedef Teuchos::Ordinal size_type;
76 
89 typedef Ordinal dim_type;
90 
94 typedef Ordinal difference_type;
95 
102 typedef Teuchos::Tuple< int, 2 > padding_type;
103 
105 
113 enum Layout
114 {
116  C_ORDER = 0,
118  FORTRAN_ORDER = 1,
120  ROW_MAJOR = 0,
122  COLUMN_MAJOR = 1,
124  LAST_INDEX_FASTEST = 0,
126  FIRST_INDEX_FASTEST = 1,
128  DEFAULT_ORDER = 1
129 };
130 
132 
134 
142 template< class T >
144 {
147  typedef T type;
148 };
149 
153 template< class T >
154 struct remove_const< const T >
155 {
159  typedef T type;
160 };
161 
163 
172 template< class SIZE_TYPE, class DIM_TYPE >
173 Teuchos::Array< SIZE_TYPE >
174 computeStrides(const Teuchos::Array< DIM_TYPE > & dimensions,
175  const Layout layout)
176 {
177  int n = dimensions.size();
178  Teuchos::Array< SIZE_TYPE > strides(n);
179  if (n == 0) return strides;
180 
181  if (layout == FIRST_INDEX_FASTEST)
182  {
183  strides[0] = 1;
184  for (int axis = 1; axis < n; ++axis)
185  strides[axis] = strides[axis-1] * dimensions[axis-1];
186  }
187  else
188  {
189  strides[n-1] = 1;
190  for (int axis = n-2; axis >= 0; --axis)
191  strides[axis] = strides[axis+1] * dimensions[axis+1];
192  }
193  return strides;
194 }
195 
197 
206 template< class SIZE_TYPE, class DIM_TYPE >
207 Teuchos::Array< SIZE_TYPE >
208 computeStrides(const Teuchos::ArrayView< DIM_TYPE > & dimensions,
209  const Layout layout)
210 {
211  // In the MDArray<T>(const MDArrayView<T> &) constructor, I try to
212  // pass the MDArrayView dimensions to computeStrides(), but they
213  // come in as ArrayView<const T> (for reasons I can't determine) and
214  // cause all sorts of const-correctness problems. So I copy them
215  // into a new Array<T> and pass its reference to the main
216  // computeStrides() function. Fortunately, the array of dimensions
217  // is small.
218  Teuchos::Array< DIM_TYPE > nonConstDims(0);
219  nonConstDims.insert(nonConstDims.begin(),
220  dimensions.begin(),
221  dimensions.end());
222  return computeStrides< SIZE_TYPE, DIM_TYPE >(nonConstDims, layout);
223 }
224 
226 
233 template< class DIM_TYPE >
234 size_type computeSize(const Teuchos::ArrayView< DIM_TYPE > & dimensions)
235 {
236  size_type result = 1;
237  for (int axis = 0; axis < dimensions.size(); ++axis)
238  result *= dimensions[axis];
239  return result;
240 }
241 
243 
250 template< class DIM_TYPE >
251 size_type computeSize(const Teuchos::Array< DIM_TYPE > & dimensions)
252 {
253  // In the MDArray<T>(const MDArrayView<T> &) constructor, I try to
254  // pass the MDArrayView dimensions to computeSize(), but they come
255  // in as ArrayView<const T> (for reasons I can't determine) and
256  // cause all sorts of const-correctness problems. So I copy them
257  // into a new Array<T> and pass its view to the main computeSize()
258  // function. Fortunately, the array of dimensions is small.
259  Teuchos::Array< DIM_TYPE > nonConstDims(0);
260  nonConstDims.insert(nonConstDims.begin(),
261  dimensions.begin(),
262  dimensions.end());
263  return computeSize(nonConstDims());
264 }
265 
267 
276 template< class SIZE_TYPE, class DIM_TYPE >
277 SIZE_TYPE computeSize(const Teuchos::ArrayView< DIM_TYPE > & dimensions,
278  const Teuchos::ArrayView< SIZE_TYPE > & strides)
279 {
280  // SIZE_TYPE might be a const type, but we need result to be non-const
281  typename remove_const< SIZE_TYPE >::type result = 1;
282  for (int axis = 0; axis < dimensions.size(); ++axis)
283  result += (dimensions[axis]-1) * strides[axis];
284  return result;
285 }
286 
288 
312 Teuchos::Array< int >
313 regularizeCommDims(int numProcs,
314  int numDims,
315  const Teuchos::ArrayView< const int > & commDims);
316 
318 
327 Teuchos::Array< int >
328 computeCommIndexes(int rank,
329  const Teuchos::ArrayView< int > & commStrides);
330 
332 
342 Teuchos::Array< int >
343 createArrayOfInts(int numDims,
344  const Teuchos::ArrayView< const int > & source);
345 
347 
353 Teuchos::Array< int >
354 splitStringOfIntsWithCommas(std::string data);
355 
357 
358 #ifdef HAVE_MPI
359 
363 template< class T > MPI_Datatype mpiType();
364 
365 #endif
366 
368 
369 #ifdef HAVE_MPI
370 
376 int mpiOrder(Layout layout);
377 
378 #endif
379 
380 } // End Domi namespace
381 
382 #endif // DOMI_MDARRAY_UTILS_HPP
Provide capability to declare a variable as non-const, even if template parameter is const...
Definition: Domi_Utils.hpp:143
T type
Typedef for the non-const version of the template parameter.
Definition: Domi_Utils.hpp:159
T type
Typedef for the template parameter.
Definition: Domi_Utils.hpp:147
Definition: Domi_ConfigDefs.hpp:97

Generated for Domi by doxygen 1.8.14