Domi
Multi-dimensional, distributed data structures
Loading...
Searching...
No Matches
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#include "Teuchos_ParameterList.hpp"
53
54#ifdef HAVE_MPI
55// MPI include
56#include <mpi.h>
57#endif
58
59namespace Domi
60{
61
64
66
76typedef Teuchos::Ordinal size_type;
77
90typedef Ordinal dim_type;
91
95typedef Ordinal difference_type;
96
103typedef Teuchos::Tuple< int, 2 > padding_type;
104
106
114enum Layout
115{
117 C_ORDER = 0,
119 FORTRAN_ORDER = 1,
121 ROW_MAJOR = 0,
123 COLUMN_MAJOR = 1,
125 LAST_INDEX_FASTEST = 0,
127 FIRST_INDEX_FASTEST = 1,
129 DEFAULT_ORDER = 1
130};
131
133
135
143template< class T >
145{
148 typedef T type;
149};
150
154template< class T >
156{
160 typedef T type;
161};
162
164
173template< class SIZE_TYPE, class DIM_TYPE >
174Teuchos::Array< SIZE_TYPE >
175computeStrides(const Teuchos::Array< DIM_TYPE > & dimensions,
176 const Layout layout)
177{
178 int n = dimensions.size();
179 Teuchos::Array< SIZE_TYPE > strides(n);
180 if (n == 0) return strides;
181
182 if (layout == FIRST_INDEX_FASTEST)
183 {
184 strides[0] = 1;
185 for (int axis = 1; axis < n; ++axis)
186 strides[axis] = strides[axis-1] * dimensions[axis-1];
187 }
188 else
189 {
190 strides[n-1] = 1;
191 for (int axis = n-2; axis >= 0; --axis)
192 strides[axis] = strides[axis+1] * dimensions[axis+1];
193 }
194 return strides;
195}
196
198
207template< class SIZE_TYPE, class DIM_TYPE >
208Teuchos::Array< SIZE_TYPE >
209computeStrides(const Teuchos::ArrayView< DIM_TYPE > & dimensions,
210 const Layout layout)
211{
212 // In the MDArray<T>(const MDArrayView<T> &) constructor, I try to
213 // pass the MDArrayView dimensions to computeStrides(), but they
214 // come in as ArrayView<const T> (for reasons I can't determine) and
215 // cause all sorts of const-correctness problems. So I copy them
216 // into a new Array<T> and pass its reference to the main
217 // computeStrides() function. Fortunately, the array of dimensions
218 // is small.
219 Teuchos::Array< DIM_TYPE > nonConstDims(0);
220 nonConstDims.insert(nonConstDims.begin(),
221 dimensions.begin(),
222 dimensions.end());
223 return computeStrides< SIZE_TYPE, DIM_TYPE >(nonConstDims, layout);
224}
225
227
234template< class DIM_TYPE >
235size_type computeSize(const Teuchos::ArrayView< DIM_TYPE > & dimensions)
236{
237 size_type result = 1;
238 for (int axis = 0; axis < dimensions.size(); ++axis)
239 result *= dimensions[axis];
240 return result;
241}
242
244
251template< class DIM_TYPE >
252size_type computeSize(const Teuchos::Array< DIM_TYPE > & dimensions)
253{
254 // In the MDArray<T>(const MDArrayView<T> &) constructor, I try to
255 // pass the MDArrayView dimensions to computeSize(), but they come
256 // in as ArrayView<const T> (for reasons I can't determine) and
257 // cause all sorts of const-correctness problems. So I copy them
258 // into a new Array<T> and pass its view to the main computeSize()
259 // function. Fortunately, the array of dimensions is small.
260 Teuchos::Array< DIM_TYPE > nonConstDims(0);
261 nonConstDims.insert(nonConstDims.begin(),
262 dimensions.begin(),
263 dimensions.end());
264 return computeSize(nonConstDims());
265}
266
268
277template< class SIZE_TYPE, class DIM_TYPE >
278SIZE_TYPE computeSize(const Teuchos::ArrayView< DIM_TYPE > & dimensions,
279 const Teuchos::ArrayView< SIZE_TYPE > & strides)
280{
281 // SIZE_TYPE might be a const type, but we need result to be non-const
282 typename remove_const< SIZE_TYPE >::type result = 1;
283 for (int axis = 0; axis < dimensions.size(); ++axis)
284 result += (dimensions[axis]-1) * strides[axis];
285 return result;
286}
287
289
295Teuchos::Array< int >
296factor(int n);
297
299
305int indexOfMax(const Teuchos::ArrayView< const float > & seq);
306
308
323Teuchos::Array< int >
324decomposeProcs(int nprocs,
325 const Teuchos::ArrayView< dim_type > & dimensions);
326
328
352Teuchos::Array< int >
353regularizeCommDims(int numProcs,
354 int numDims,
355 const Teuchos::ArrayView< const int > & commDims);
356
358
377Teuchos::Array< int >
378regularizeCommDims(int numProcs,
379 Teuchos::ParameterList & plist);
380
382
391Teuchos::Array< int >
392computeCommIndexes(int rank,
393 const Teuchos::ArrayView< int > & commStrides);
394
396
406Teuchos::Array< int >
407createArrayOfInts(int numDims,
408 const Teuchos::ArrayView< const int > & source);
409
411
417Teuchos::Array< int >
418splitStringOfIntsWithCommas(std::string data);
419
421
422#ifdef HAVE_MPI
423
427template< class T > MPI_Datatype mpiType();
428
429#endif
430
432
433#ifdef HAVE_MPI
434
440int mpiOrder(Layout layout);
441
442#endif
443
444} // End Domi namespace
445
446#endif // DOMI_MDARRAY_UTILS_HPP
Memory-safe templated multi-dimensional array class.
Definition Domi_MDArray.hpp:287
size_type size() const
Return the total size of the MDArray
Definition Domi_MDArray.hpp:1037
T type
Typedef for the non-const version of the template parameter.
Definition Domi_Utils.hpp:160
Provide capability to declare a variable as non-const, even if template parameter is const.
Definition Domi_Utils.hpp:145
T type
Typedef for the template parameter.
Definition Domi_Utils.hpp:148

Generated for Domi by doxygen 1.10.0