1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
|
// @HEADER
// ************************************************************************
//
// Intrepid2 Package
// Copyright (2007) Sandia Corporation
//
// Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
// license for use of this work by or on behalf of the U.S. Government.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the Corporation nor the names of the
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Questions? Contact Kyungjoo Kim (kyukim@sandia.gov),
// Mauro Perego (mperego@sandia.gov), or
// Nate Roberts (nvrober@sandia.gov)
//
// ************************************************************************
// @HEADER
/** \file TensorViewFunctorTests.cpp
\brief Tests to verify the TensorViewFunctor used in TensorBasis.
\author Created by N.V. Roberts.
*/
#include "Teuchos_UnitTestHarness.hpp"
#include "Intrepid2_TensorBasis.hpp"
#include "Intrepid2_Types.hpp"
#include "Intrepid2_TestUtils.hpp"
#include "Kokkos_Core.hpp"
namespace
{
using namespace Intrepid2;
template<typename ScalarViewType>
void runTensorViewFunctorTest(ScalarViewType tensor_expected, ScalarViewType view1, ScalarViewType view2, double weight, bool tensorPoints,
Teuchos::FancyOStream &out, bool &success)
{
double tol = 1e-15;
using namespace Intrepid2;
using ExecutionSpace = typename ScalarViewType::execution_space;
using Scalar = typename ScalarViewType::value_type;
const bool hasADType = false;
const int vectorSize = hasADType ? FAD_VECTOR_SIZE : VECTOR_SIZE;
auto policy = Kokkos::TeamPolicy<ExecutionSpace>(view1.extent_int(0),Kokkos::AUTO(),vectorSize);
using FunctorType = TensorViewFunctor<ExecutionSpace, Scalar, ScalarViewType>;
ScalarViewType tensor_actual;
// TODO: figure out a better way to initialize tensor_actual to be the same size/shape as tensor_expected
if (tensor_expected.rank() == 1)
{
tensor_actual = ScalarViewType("tensor actual", tensor_expected.extent_int(0));
}
else if (tensor_expected.rank() == 2)
{
tensor_actual = ScalarViewType("tensor actual", tensor_expected.extent_int(0),tensor_expected.extent_int(1));
}
else if (tensor_expected.rank() == 3)
{
tensor_actual = ScalarViewType("tensor actual", tensor_expected.extent_int(0),tensor_expected.extent_int(1), tensor_expected.extent_int(2));
}
else if (tensor_expected.rank() == 4)
{
tensor_actual = ScalarViewType("tensor actual", tensor_expected.extent_int(0),tensor_expected.extent_int(1), tensor_expected.extent_int(2), tensor_expected.extent_int(2));
}
else
{
TEUCHOS_TEST_FOR_EXCEPTION(true, std::invalid_argument, "Test does not yet support this output rank");
}
FunctorType functor(tensor_actual, view1, view2, tensorPoints, weight);
Kokkos::parallel_for( policy , functor, "TensorViewFunctor");
switch (tensor_expected.rank())
{
case 1: testFloatingEquality1(tensor_actual,tensor_expected,tol,tol,out,success); break;
case 2: testFloatingEquality2(tensor_actual,tensor_expected,tol,tol,out,success); break;
case 3: testFloatingEquality3(tensor_actual,tensor_expected,tol,tol,out,success); break;
case 4: testFloatingEquality4(tensor_actual,tensor_expected,tol,tol,out,success); break;
default:
TEUCHOS_TEST_FOR_EXCEPTION(true, std::invalid_argument, "Test does not yet support this output rank");
}
}
template<typename Scalar>
void runTensorViewFunctorTests(Teuchos::FancyOStream &out, bool &success)
{
using DeviceType = DefaultTestDeviceType;
using ScalarViewType = ViewType<Scalar,DeviceType>;
// TEST 1: simple contraction
// we'll use trivial fields so as to factor out problems in the tensor product logic
out << "TEST 1: simple contraction.\n";
int num_fields1 = 1;
int num_fields2 = 1;
int num_fields = num_fields1 * num_fields2;
int num_points = 1;
int space_dim = 3;
ScalarViewType tensor_expected("expected_tensor",num_fields,num_points);
ScalarViewType view1("view1",num_fields,num_points,space_dim);
ScalarViewType view2("view2",num_fields,num_points,space_dim);
auto tensor_expected_host = getHostCopy(tensor_expected);
auto view1_host = getHostCopy(view1);
auto view2_host = getHostCopy(view2);
view1_host(0,0,0) = 3.0;
view1_host(0,0,1) = 2.0;
view1_host(0,0,2) = 0.0;
view2_host(0,0,0) = 1.0;
view2_host(0,0,1) = 0.5;
view2_host(0,0,2) = 3.0;
tensor_expected_host(0,0,0) = 0;
for (int d=0; d<space_dim; d++)
{
tensor_expected_host(0,0,0) += view1_host(0,0,d) * view2_host(0,0,d);
}
Kokkos::deep_copy(view1, view1_host);
Kokkos::deep_copy(view2, view2_host);
Kokkos::deep_copy(tensor_expected, tensor_expected_host);
double weight = 1.0;
bool tensor_points = false; // does not matter for the single-point case
runTensorViewFunctorTest(tensor_expected, view1, view2, weight, tensor_points, out, success);
// TEST 2: tensor product ordering
out << "TEST 2: tensor product ordering.\n";
num_fields1 = 2;
num_fields2 = 2;
num_fields = num_fields1 * num_fields2;
num_points = 1;
Kokkos::resize(tensor_expected,num_fields,num_points);
Kokkos::resize(view1, num_fields1,num_points);
Kokkos::resize(view2, num_fields2,num_points);
Kokkos::resize(tensor_expected_host,num_fields,num_points);
Kokkos::resize(view1_host, num_fields1,num_points);
Kokkos::resize(view2_host, num_fields2,num_points);
view1_host(0,0) = 3.0;
view1_host(1,0) = 2.0;
view2_host(0,0) = 1.0;
view2_host(1,0) = 0.5;
// view1 is the fastest-moving: tensor entry 1 corresponds to view1(1,0) and view2(0,0)
tensor_expected_host(0,0) = view1_host(0,0) * view2_host(0,0);
tensor_expected_host(1,0) = view1_host(1,0) * view2_host(0,0);
tensor_expected_host(2,0) = view1_host(0,0) * view2_host(1,0);
tensor_expected_host(3,0) = view1_host(1,0) * view2_host(1,0);
Kokkos::deep_copy(view1, view1_host);
Kokkos::deep_copy(view2, view2_host);
Kokkos::deep_copy(tensor_expected, tensor_expected_host);
weight = 1.0;
tensor_points = false; // does not matter for the single-point case
runTensorViewFunctorTest(tensor_expected, view1, view2, weight, tensor_points, out, success);
// TEST 3: like TEST 2, but include non-trivial weight
out << "TEST 3: like TEST 2, but include non-trivial weight.\n";
weight = 2.0;
for (int i=0; i<num_fields; i++)
{
tensor_expected_host(i,0) *= weight;
}
Kokkos::deep_copy(tensor_expected, tensor_expected_host);
runTensorViewFunctorTest(tensor_expected, view1, view2, weight, tensor_points, out, success);
// TEST 4: scalar times vector
out << "TEST 4: scalar times vector.\n";
num_fields1 = 2;
num_fields2 = 2;
num_fields = num_fields1 * num_fields2;
num_points = 1;
Kokkos::resize(tensor_expected,num_fields, num_points, space_dim);
Kokkos::resize(view1, num_fields1, num_points);
Kokkos::resize(view2, num_fields2, num_points, space_dim);
Kokkos::resize(tensor_expected_host, num_fields, num_points,space_dim);
Kokkos::resize(view1_host, num_fields1, num_points);
Kokkos::resize(view2_host, num_fields2, num_points,space_dim);
view1_host(0,0) = 3.0;
view1_host(1,0) = 2.0;
view2_host(0,0,0) = 1.0;
view2_host(1,0,0) = 0.5;
view2_host(0,0,1) = 2.0;
view2_host(1,0,1) = 1.0;
view2_host(0,0,2) = 4.0;
view2_host(1,0,2) = 2.0;
for (int d=0; d<space_dim; d++)
{
// view1 is the fastest-moving: tensor entry 1 corresponds to view1(1,…) and view2(0,…)
tensor_expected_host(0,0,d) = view1_host(0,0) * view2_host(0,0,d);
tensor_expected_host(1,0,d) = view1_host(1,0) * view2_host(0,0,d);
tensor_expected_host(2,0,d) = view1_host(0,0) * view2_host(1,0,d);
tensor_expected_host(3,0,d) = view1_host(1,0) * view2_host(1,0,d);
}
Kokkos::deep_copy(view1, view1_host);
Kokkos::deep_copy(view2, view2_host);
Kokkos::deep_copy(tensor_expected, tensor_expected_host);
weight = 1.0;
tensor_points = false; // does not matter for the single-point case
runTensorViewFunctorTest(tensor_expected, view1, view2, weight, tensor_points, out, success);
// TEST 5: scalar times vector, nontrivial points, but still matching in point dimension
out << "TEST 5: scalar times vector, nontrivial points, but still matching in point dimension.\n";
num_fields1 = 2;
num_fields2 = 2;
num_fields = num_fields1 * num_fields2;
num_points = 2;
Kokkos::resize(tensor_expected, num_fields, num_points, space_dim);
Kokkos::resize(view1, num_fields1, num_points);
Kokkos::resize(view2, num_fields2, num_points, space_dim);
Kokkos::resize(tensor_expected_host, num_fields, num_points, space_dim);
Kokkos::resize(view1_host, num_fields1, num_points);
Kokkos::resize(view2_host, num_fields2, num_points, space_dim);
view1_host(0,0) = 3.0;
view1_host(1,0) = 2.0;
view2_host(0,0,0) = 1.0;
view2_host(1,0,0) = 0.5;
view2_host(0,0,1) = 2.0;
view2_host(1,0,1) = 1.0;
view2_host(0,0,2) = 4.0;
view2_host(1,0,2) = 2.0;
view1_host(0,1) = 1.0;
view1_host(1,1) = 1.0;
view2_host(0,1,0) = 1.0;
view2_host(1,1,0) = 2.0;
view2_host(0,1,1) = 3.0;
view2_host(1,1,1) = 4.0;
view2_host(0,1,2) = 5.0;
view2_host(1,1,2) = 6.0;
for (int point_ordinal=0; point_ordinal<num_points; point_ordinal++)
{
for (int d=0; d<space_dim; d++)
{
// view1 is the fastest-moving: tensor entry 1 corresponds to view1(1,…) and view2(0,…)
tensor_expected_host(0,point_ordinal,d) = view1_host(0,point_ordinal) * view2_host(0,point_ordinal,d);
tensor_expected_host(1,point_ordinal,d) = view1_host(1,point_ordinal) * view2_host(0,point_ordinal,d);
tensor_expected_host(2,point_ordinal,d) = view1_host(0,point_ordinal) * view2_host(1,point_ordinal,d);
tensor_expected_host(3,point_ordinal,d) = view1_host(1,point_ordinal) * view2_host(1,point_ordinal,d);
}
}
Kokkos::deep_copy(view1, view1_host);
Kokkos::deep_copy(view2, view2_host);
Kokkos::deep_copy(tensor_expected, tensor_expected_host);
weight = 1.0;
tensor_points = false; // does not matter for the single-point case
runTensorViewFunctorTest(tensor_expected, view1, view2, weight, tensor_points, out, success);
// TEST 6: like TEST 2 above, but with different field counts
out << "TEST 6: like TEST 2 above, but with different field counts.\n";
num_fields1 = 2;
num_fields2 = 3;
num_fields = num_fields1 * num_fields2;
num_points = 1;
Kokkos::resize(tensor_expected,num_fields,num_points);
Kokkos::resize(view1, num_fields1,num_points);
Kokkos::resize(view2, num_fields2,num_points);
Kokkos::resize(tensor_expected_host, num_fields, num_points);
Kokkos::resize(view1_host, num_fields1, num_points);
Kokkos::resize(view2_host, num_fields2, num_points);
view1_host(0,0) = 3.0;
view1_host(1,0) = 2.0;
view2_host(0,0) = 1.0;
view2_host(1,0) = 0.5;
view2_host(2,0) = 1.5;
// view1 is the fastest-moving: tensor entry 1 corresponds to view1(1,0) and view2(0,0)
tensor_expected_host(0,0) = view1_host(0,0) * view2_host(0,0);
tensor_expected_host(1,0) = view1_host(1,0) * view2_host(0,0);
tensor_expected_host(2,0) = view1_host(0,0) * view2_host(1,0);
tensor_expected_host(3,0) = view1_host(1,0) * view2_host(1,0);
tensor_expected_host(4,0) = view1_host(0,0) * view2_host(2,0);
tensor_expected_host(5,0) = view1_host(1,0) * view2_host(2,0);
Kokkos::deep_copy(view1, view1_host);
Kokkos::deep_copy(view2, view2_host);
Kokkos::deep_copy(tensor_expected, tensor_expected_host);
weight = 1.0;
tensor_points = false; // does not matter for the single-point case
runTensorViewFunctorTest(tensor_expected, view1, view2, weight, tensor_points, out, success);
}
TEUCHOS_UNIT_TEST( TensorViewFunctor, MultipleTests )
{
using Scalar = double;
runTensorViewFunctorTests<Scalar>(out, success);
}
} // namespace
|