Intrepid2
Intrepid2_ArrayToolsDefCloneScale.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ************************************************************************
3 //
4 // Intrepid Package
5 // Copyright (2007) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Kyungjoo Kim (kyukim@sandia.gov), or
38 // Mauro Perego (mperego@sandia.gov)
39 //
40 // ************************************************************************
41 // @HEADER
42 
49 #ifndef __INTREPID2_ARRAYTOOLS_DEF_CLONESCALE_HPP__
50 #define __INTREPID2_ARRAYTOOLS_DEF_CLONESCALE_HPP__
51 
52 namespace Intrepid2 {
53 
54  namespace FunctorArrayTools {
55 
59  template<typename OutputViewType,
60  typename inputViewType,
61  ordinal_type valRank>
62  struct F_clone {
63  OutputViewType _output;
64  const inputViewType _input;
65 
66  KOKKOS_INLINE_FUNCTION
67  F_clone(OutputViewType output_,
68  inputViewType input_)
69  : _output(output_),
70  _input(input_) {}
71 
72  // clone data
73  KOKKOS_INLINE_FUNCTION
74  void
75  operator()(const ordinal_type cl,
76  const ordinal_type pt) const {
77  switch (valRank) {
78  case 0: {
79  _output.access(cl, pt) = _input.access(pt);
80  break;
81  }
82  case 1: {
83  const ordinal_type iend = _output.extent(2);
84  for (ordinal_type i=0;i<iend;++i)
85  _output.access(cl, pt, i) = _input.access(pt, i);
86  break;
87  }
88  case 2: {
89  const ordinal_type
90  iend = _output.extent(2),
91  jend = _output.extent(3);
92  for (ordinal_type i=0;i<iend;++i)
93  for (ordinal_type j=0;j<jend;++j)
94  _output.access(cl, pt, i, j) = _input.access(pt, i, j);
95  break;
96  }
97  }
98  }
99 
100  // clone fields
101  KOKKOS_INLINE_FUNCTION
102  void
103  operator()(const ordinal_type cl,
104  const ordinal_type bf,
105  const ordinal_type pt) const {
106  switch (valRank) {
107  case 0: {
108  _output.access(cl, bf, pt) = _input.access(bf, pt);
109  break;
110  }
111  case 1: {
112  const ordinal_type iend = _output.extent(3);
113  for (ordinal_type i=0;i<iend;++i)
114  _output.access(cl, bf, pt, i) = _input.access(bf, pt, i);
115  break;
116  }
117  case 2: {
118  const ordinal_type
119  iend = _output.extent(3),
120  jend = _output.extent(4);
121  for (ordinal_type i=0;i<iend;++i)
122  for (ordinal_type j=0;j<jend;++j)
123  _output.access(cl, bf, pt, i, j) = _input.access(bf, pt, i, j);
124  break;
125  }
126  }
127  }
128  };
129  }
130 
131  template<typename SpT>
132  template<typename outputValueType, class ...outputProperties,
133  typename inputValueType, class ...inputProperties>
134  void ArrayTools<SpT>::
135  cloneFields( Kokkos::DynRankView<outputValueType,outputProperties...> output,
136  const Kokkos::DynRankView<inputValueType, inputProperties...> input ) {
137 #ifdef HAVE_INTREPID2_DEBUG
138  {
139  INTREPID2_TEST_FOR_EXCEPTION( ( input.rank() < 2 || input.rank() > 4 ), std::invalid_argument,
140  ">>> ERROR (ArrayTools::clone): Input fields container must have rank 2, 3, or 4.");
141  INTREPID2_TEST_FOR_EXCEPTION( ( output.rank() != (input.rank()+1) ), std::invalid_argument,
142  ">>> ERROR (ArrayTools::clone): The rank of the input fields container must be one less than the rank of the output fields container.");
143  for (size_type i=0;i< input.rank();++i) {
144  INTREPID2_TEST_FOR_EXCEPTION( (input.extent(i) != output.extent(i+1)), std::invalid_argument,
145  ">>> ERROR (ArrayTools::clone): Dimensions of input and output fields containers do not match.");
146  }
147  }
148 #endif
149 
150  typedef Kokkos::DynRankView<outputValueType,outputProperties...> OutputViewType;
151  typedef Kokkos::DynRankView<inputValueType, inputProperties...> inputViewType;
152  typedef typename ExecSpace< typename inputViewType::execution_space , SpT >::ExecSpaceType ExecSpaceType;
153 
154  using range_policy_type = Kokkos::Experimental::MDRangePolicy
155  < ExecSpaceType, Kokkos::Experimental::Rank<3>, Kokkos::IndexType<ordinal_type> >;
156 
157  range_policy_type policy( { 0, 0, 0 },
158  { /*C*/ output.extent(0), /*F*/ output.extent(1), /*P*/ output.extent(2) } );
159  const ordinal_type valRank = output.rank() - 3;
160  switch (valRank) {
161  case 0: {
162  typedef FunctorArrayTools::F_clone<OutputViewType,inputViewType,0> FunctorType;
163  Kokkos::parallel_for( policy, FunctorType(output, input) );
164  break;
165  }
166  case 1: {
167  typedef FunctorArrayTools::F_clone<OutputViewType,inputViewType,1> FunctorType;
168  Kokkos::parallel_for( policy, FunctorType(output, input) );
169  break;
170  }
171  case 2: {
172  typedef FunctorArrayTools::F_clone<OutputViewType,inputViewType,2> FunctorType;
173  Kokkos::parallel_for( policy, FunctorType(output, input) );
174  break;
175  }
176  }
177  }
178 
179  template<typename SpT>
180  template<typename outputValueType, class ...outputProperties,
181  typename inputValueType, class ...inputProperties>
182  void ArrayTools<SpT>::
183  cloneData( Kokkos::DynRankView<outputValueType,outputProperties...> output,
184  const Kokkos::DynRankView<inputValueType, inputProperties...> input ) {
185 #ifdef HAVE_INTREPID2_DEBUG
186  {
187  INTREPID2_TEST_FOR_EXCEPTION( ( input.rank() < 1 || input.rank() > 3 ), std::invalid_argument,
188  ">>> ERROR (ArrayTools::clone): Input fields container must have rank 1, 2, or 3.");
189  INTREPID2_TEST_FOR_EXCEPTION( ( output.rank() != (input.rank()+1) ), std::invalid_argument,
190  ">>> ERROR (ArrayTools::clone): The rank of the input fields container must be one less than the rank of the output fields container.");
191  for (ordinal_type i=0;i<input.rank();++i) {
192  INTREPID2_TEST_FOR_EXCEPTION( (input.extent(i) != output.extent(i+1)), std::invalid_argument,
193  ">>> ERROR (ArrayTools::clone): Dimensions of input and output fields containers do not match.");
194  }
195  }
196 #endif
197 
198  typedef Kokkos::DynRankView<outputValueType,outputProperties...> OutputViewType;
199  typedef Kokkos::DynRankView<inputValueType, inputProperties...> inputViewType;
200  typedef typename ExecSpace< typename inputViewType::execution_space , SpT >::ExecSpaceType ExecSpaceType;
201 
202  using range_policy_type = Kokkos::Experimental::MDRangePolicy
203  < ExecSpaceType, Kokkos::Experimental::Rank<2>, Kokkos::IndexType<ordinal_type> >;
204 
205  range_policy_type policy( { 0, 0 },
206  { /*C*/ output.extent(0), /*P*/ output.extent(1) } );
207  const ordinal_type valRank = output.rank() - 2;
208  switch (valRank) {
209  case 0: {
210  typedef FunctorArrayTools::F_clone<OutputViewType,inputViewType,0> FunctorType;
211  Kokkos::parallel_for( policy, FunctorType(output, input) );
212  break;
213  }
214  case 1: {
215  typedef FunctorArrayTools::F_clone<OutputViewType,inputViewType,1> FunctorType;
216  Kokkos::parallel_for( policy, FunctorType(output, input) );
217  break;
218  }
219  case 2: {
220  typedef FunctorArrayTools::F_clone<OutputViewType,inputViewType,2> FunctorType;
221  Kokkos::parallel_for( policy, FunctorType(output, input) );
222  break;
223  }
224  }
225  }
226 
227 } // end namespace Intrepid2
228 
229 #endif
Functor for clone see Intrepid2::ArrayTools for more.
static void cloneFields(Kokkos::DynRankView< outputFieldValueType, outputFieldProperties...> outputFields, const Kokkos::DynRankView< inputFieldValueType, inputFieldProperties...> inputFields)
Replicates a rank-2, 3, or 4 container with dimensions (F,P), (F,P,D1) or (F,P,D1,D2), representing the values of a scalar, vector or a tensor field, into an output value container of size (C,F,P), (C,F,P,D1) or (C,F,P,D1,D2).
static void cloneData(Kokkos::DynRankView< outputDataValueType, outputDataProperties...> outputData, const Kokkos::DynRankView< inputDataValueType, inputDataProperties...> inputData)
Replicates a rank-2, 3, or 4 container with dimensions (F,P), (F,P,D1) or (F,P,D1,D2), representing the values of a scalar, vector or a tensor field, into an output value container of size (C,F,P), (C,F,P,D1) or (C,F,P,D1,D2).