16#ifndef __INTREPID2_REALSPACETOOLS_DEF_HPP__
17#define __INTREPID2_REALSPACETOOLS_DEF_HPP__
29 template<
typename DeviceType>
30 template<
typename inVecValueType,
class ...inVecProperties>
31 KOKKOS_INLINE_FUNCTION
34 vectorNorm(
const Kokkos::DynRankView<inVecValueType,inVecProperties...> inVec,
35 const ENorm normType ) {
36#ifdef HAVE_INTREPID2_DEBUG
39 INTREPID2_TEST_FOR_DEBUG_ABORT( !(inVec.rank() >= 1 && inVec.rank() <= 5), dbgInfo,
40 ">>> ERROR (RealSpaceTools::vectorNorm): Vector argument must have rank 1!" );
41#ifdef INTREPID2_TEST_FOR_DEBUG_ABORT_OVERRIDE_TO_CONTINUE
42 if (dbgInfo)
return inVecValueType(0);
46 typedef inVecValueType value_type;
50 const ordinal_type iend = inVec.extent(0);
51 const ordinal_type jend = inVec.extent(1);
52 const ordinal_type kend = inVec.extent(2);
53 const ordinal_type lend = inVec.extent(3);
54 const ordinal_type mend = inVec.extent(4);
57 for (ordinal_type i=0;i<iend;++i)
58 for (ordinal_type j=0;j<jend;++j)
59 for (ordinal_type k=0;k<kend;++k)
60 for (ordinal_type l=0;l<lend;++l)
61 for (ordinal_type m=0;m<mend;++m)
62 norm += inVec.access(i,j,k,l,m)*inVec.access(i,j,k,l,m);
67 for (ordinal_type i=0;i<iend;++i)
68 for (ordinal_type j=0;j<jend;++j)
69 for (ordinal_type k=0;k<kend;++k)
70 for (ordinal_type l=0;l<lend;++l)
71 for (ordinal_type m=0;m<mend;++m) {
73 norm = (norm < current ? current : norm);
78 for (ordinal_type i=0;i<iend;++i)
79 for (ordinal_type j=0;j<jend;++j)
80 for (ordinal_type k=0;k<kend;++k)
81 for (ordinal_type l=0;l<lend;++l)
82 for (ordinal_type m=0;m<mend;++m)
87 INTREPID2_TEST_FOR_ABORT( ( (normType != NORM_TWO) &&
88 (normType != NORM_INF) &&
89 (normType != NORM_ONE) ),
90 ">>> ERROR (RealSpaceTools::vectorNorm): Invalid argument normType.");
98 template<
typename DeviceType>
99 template<ordinal_type D,
class MatrixViewType>
100 KOKKOS_INLINE_FUNCTION
101 typename MatrixViewType::value_type
103 det(
const MatrixViewType inMat )
105 typedef typename decltype(inMat)::non_const_value_type value_type;
106#ifdef HAVE_INTREPID2_DEBUG
108 bool dbgInfo =
false;
109 INTREPID2_TEST_FOR_DEBUG_ABORT(
getFunctorRank( inMat ) != 2, dbgInfo,
110 ">>> ERROR (RealSpaceTools::det): Rank of matrix argument must be 2!");
111 INTREPID2_TEST_FOR_DEBUG_ABORT( inMat.extent(0) != inMat.extent(1), dbgInfo,
112 ">>> ERROR (RealSpaceTools::det): Matrix is not square!");
113 INTREPID2_TEST_FOR_DEBUG_ABORT( inMat.extent(0) < 1 || inMat.extent(0) > 3, dbgInfo,
114 ">>> ERROR (RealSpaceTools::det): Spatial dimension must be 1, 2, or 3!");
115#ifdef INTREPID2_TEST_FOR_DEBUG_ABORT_OVERRIDE_TO_CONTINUE
116 if (dbgInfo)
return value_type(0);
122 value_type r_val = ( inMat(0,0) );
125 else if constexpr (D==2)
127 value_type r_val = ( inMat(0,0) * inMat(1,1) -
128 inMat(0,1) * inMat(1,0) );
131 else if constexpr (D==3)
133 value_type r_val = ( inMat(0,0) * inMat(1,1) * inMat(2,2) +
134 inMat(1,0) * inMat(2,1) * inMat(0,2) +
135 inMat(2,0) * inMat(0,1) * inMat(1,2) -
136 inMat(2,0) * inMat(1,1) * inMat(0,2) -
137 inMat(0,0) * inMat(2,1) * inMat(1,2) -
138 inMat(1,0) * inMat(0,1) * inMat(2,2) );
141 static_assert((D >= 1) && (D <= 3));
144 template<
typename DeviceType>
145 template<
class MatrixViewType>
146 KOKKOS_INLINE_FUNCTION
147 typename MatrixViewType::value_type
149 det(
const MatrixViewType inMat ) {
150 typename decltype(inMat)::non_const_value_type val(0);
151#ifdef HAVE_INTREPID2_DEBUG
153 bool dbgInfo =
false;
154 INTREPID2_TEST_FOR_DEBUG_ABORT(
getFunctorRank( inMat ) != 2, dbgInfo,
155 ">>> ERROR (RealSpaceTools::det): Rank of matrix argument must be 2!");
156 INTREPID2_TEST_FOR_DEBUG_ABORT( inMat.extent(0) != inMat.extent(1), dbgInfo,
157 ">>> ERROR (RealSpaceTools::det): Matrix is not square!");
158 INTREPID2_TEST_FOR_DEBUG_ABORT( inMat.extent(0) < 1 || inMat.extent(0) > 3, dbgInfo,
159 ">>> ERROR (RealSpaceTools::det): Spatial dimension must be 1, 2, or 3!");
160#ifdef INTREPID2_TEST_FOR_DEBUG_ABORT_OVERRIDE_TO_CONTINUE
161 if (dbgInfo)
return val;
165 const auto dim = inMat.extent(0);
167 case 3: val = det<3, MatrixViewType>(inMat);
break;
168 case 2: val = det<2, MatrixViewType>(inMat);
break;
169 case 1: val = det<1, MatrixViewType>(inMat);
break;
179 template<
typename DeviceType>
180 template<
typename inVec1ValueType,
class ...inVec1Properties,
181 typename inVec2ValueType,
class ...inVec2Properties>
182 KOKKOS_INLINE_FUNCTION
185 dot(
const Kokkos::DynRankView<inVec1ValueType,inVec1Properties...> inVec1,
186 const Kokkos::DynRankView<inVec2ValueType,inVec2Properties...> inVec2 ) {
188#ifdef HAVE_INTREPID2_DEBUG
190 bool dbgInfo =
false;
191 INTREPID2_TEST_FOR_DEBUG_ABORT( inVec1.rank() != 1 || inVec2.rank() != 1, dbgInfo,
192 ">>> ERROR (RealSpaceTools::dot): Vector arguments must have rank 1!");
193 INTREPID2_TEST_FOR_DEBUG_ABORT( inVec1.extent(0) != inVec2.extent(0), dbgInfo,
194 ">>> ERROR (RealSpaceTools::dot): Dimensions of vector arguments must agree!");
195#ifdef INTREPID2_TEST_FOR_EXCEPTION_OVERRIDE_TO_CONTINUE
196 if (dbgInfo)
return inVec1ValueType(0);
200 typedef inVec1ValueType value_type;
205 const ordinal_type iend = inVec1.extent(0);
206 for (ordinal_type i=0;i<iend;++i)
207 r_val += inVec1(i)*inVec2(i);
220 namespace FunctorRealSpaceTools {
225 template<
typename OutputViewType,
226 typename inputViewType>
228 OutputViewType _output;
229 inputViewType _input;
231 KOKKOS_INLINE_FUNCTION
233 inputViewType input_ )
234 : _output(output_), _input(input_) {}
236 KOKKOS_INLINE_FUNCTION
237 void operator()(
const ordinal_type i)
const {
238 const ordinal_type jend = _input.extent_int(1);
239 const ordinal_type kend = _input.extent_int(2);
240 const ordinal_type lend = _input.extent_int(3);
241 const ordinal_type mend = _input.extent_int(4);
243 for (ordinal_type j=0;j<jend;++j)
244 for (ordinal_type k=0;k<kend;++k)
245 for (ordinal_type l=0;l<lend;++l)
246 for (ordinal_type m=0;m<mend;++m)
252 template<
typename DeviceType>
253 template<
typename outputValueType,
class ...outputProperties,
254 typename inputValueType,
class ...inputProperties>
258 const Kokkos::DynRankView<inputValueType, inputProperties...> input ) {
260 using MemSpaceType =
typename DeviceType::memory_space;
261 constexpr bool are_accessible =
262 Kokkos::Impl::MemorySpaceAccess<MemSpaceType,
263 typename decltype(output)::memory_space>::accessible &&
264 Kokkos::Impl::MemorySpaceAccess<MemSpaceType,
265 typename decltype(input)::memory_space>::accessible;
266 static_assert(are_accessible,
"RealSpaceTools<DeviceType>::extractScalarValues(..): input/output views' memory spaces are not compatible with DeviceType");
270 const auto loopSize = input.extent(0);
271 using ExecSpaceType =
typename DeviceType::execution_space;
272 Kokkos::RangePolicy<ExecSpaceType,Kokkos::Schedule<Kokkos::Static> > policy(0, loopSize);
273 Kokkos::parallel_for( policy, FunctorType(output, input) );
276 namespace FunctorRealSpaceTools {
280 template<
typename OutputViewType,
281 typename inputViewType>
283 OutputViewType _output;
284 inputViewType _input;
286 KOKKOS_INLINE_FUNCTION
287 F_clone( OutputViewType output_,
288 inputViewType input_ )
289 : _output(output_), _input(input_) {}
291 KOKKOS_INLINE_FUNCTION
292 void operator()(
const ordinal_type iter)
const {
293 ordinal_type k0(0), k1(0), k2(0);
294 const auto rankDiff = _output.rank() - _input.rank();
297 unrollIndex( k0, k1, k2,
314 auto out = (rankDiff == 3 ? Kokkos::subview(_output, k0, k1, k2, Kokkos::ALL(), Kokkos::ALL(), Kokkos::ALL()) :
315 rankDiff == 2 ? Kokkos::subview(_output, k0, k1, Kokkos::ALL(), Kokkos::ALL(), Kokkos::ALL()) :
316 rankDiff == 1 ? Kokkos::subview(_output, k0, Kokkos::ALL(), Kokkos::ALL(), Kokkos::ALL()) :
317 Kokkos::subview(_output, Kokkos::ALL(), Kokkos::ALL(), Kokkos::ALL()));
318 const ordinal_type iend = _input.extent(0);
319 const ordinal_type jend = _input.extent(1);
320 const ordinal_type kend = _input.extent(2);
321 for (ordinal_type i=0;i<iend;++i)
322 for (ordinal_type j=0;j<jend;++j)
323 for (ordinal_type k=0;k<kend;++k)
324 out.access(i,j,k) = _input.access(i,j,k);
329 template<
typename DeviceType>
330 template<
typename outputValueType,
class ...outputProperties,
331 typename inputValueType,
class ...inputProperties>
334 clone( Kokkos::DynRankView<outputValueType,outputProperties...> output,
335 const Kokkos::DynRankView<inputValueType,inputProperties...> input ) {
336#ifdef HAVE_INTREPID2_DEBUG
339 INTREPID2_TEST_FOR_EXCEPTION( input.rank() > 3, std::invalid_argument,
340 ">>> ERROR (RealSpaceTools::clone): Input container has rank larger than 3.");
343 INTREPID2_TEST_FOR_EXCEPTION( input.rank() > output.rank(), std::invalid_argument,
344 ">>> ERROR (RealSpaceTools::clone): Input container rank should be smaller than ouput rank.");
346 const size_type rankDiff = output.rank() - input.rank();
349 INTREPID2_TEST_FOR_EXCEPTION( rankDiff > 3, std::invalid_argument,
350 ">>> ERROR (RealSpaceTools::clone): Difference between output container rank and input container rank is larger than 3.");
353 for (size_type i=0;i<input.rank();++i) {
354 INTREPID2_TEST_FOR_EXCEPTION( input.extent(i) != output.extent(rankDiff + i), std::invalid_argument,
355 ">>> ERROR (RealSpaceTools::clone): Dimensions of array arguments do not agree!");
359 using MemSpaceType =
typename DeviceType::memory_space;
360 constexpr bool are_accessible =
361 Kokkos::Impl::MemorySpaceAccess<MemSpaceType,
362 typename decltype(output)::memory_space>::accessible &&
363 Kokkos::Impl::MemorySpaceAccess<MemSpaceType,
364 typename decltype(input)::memory_space>::accessible;
365 static_assert(are_accessible,
"RealSpaceTools<DeviceType>::clone(..): input/output views' memory spaces are not compatible with DeviceType");
369 size_type loopSize = 1;
370 const ordinal_type out_rank = output.rank();
371 const ordinal_type in_rank = input.rank();
372 const ordinal_type rankDiff = out_rank - in_rank;
373 for (ordinal_type i=0;i<rankDiff;++i)
374 loopSize *= output.extent(i);
376 using ExecSpaceType =
typename DeviceType::execution_space;
377 Kokkos::RangePolicy<ExecSpaceType,Kokkos::Schedule<Kokkos::Static> > policy(0, loopSize);
378 Kokkos::parallel_for( policy, FunctorType(output, input) );
381 namespace FunctorRealSpaceTools {
385 template<
typename absArrayViewType,
386 typename inArrayViewType>
388 absArrayViewType _absArray;
389 inArrayViewType _inArray;
391 KOKKOS_INLINE_FUNCTION
392 F_absval( absArrayViewType absArray_,
393 inArrayViewType inArray_ )
394 : _absArray(absArray_), _inArray(inArray_) {}
396 KOKKOS_INLINE_FUNCTION
397 void operator()(
const ordinal_type i)
const {
398 const ordinal_type jend = _inArray.extent(1);
399 const ordinal_type kend = _inArray.extent(2);
400 const ordinal_type lend = _inArray.extent(3);
401 const ordinal_type mend = _inArray.extent(4);
403 typedef typename inArrayViewType::non_const_value_type value_type;
405 for (ordinal_type j=0;j<jend;++j)
406 for (ordinal_type k=0;k<kend;++k)
407 for (ordinal_type l=0;l<lend;++l)
408 for (ordinal_type m=0;m<mend;++m)
414 template<
typename DeviceType>
415 template<
typename absArrayValueType,
class ...absArrayProperties,
416 typename inArrayValueType,
class ...inArrayProperties>
419 absval( Kokkos::DynRankView<absArrayValueType,absArrayProperties...> absArray,
420 const Kokkos::DynRankView<inArrayValueType, inArrayProperties...> inArray ) {
421#ifdef HAVE_INTREPID2_DEBUG
423 INTREPID2_TEST_FOR_EXCEPTION( inArray.rank() > 5, std::invalid_argument,
424 ">>> ERROR (RealSpaceTools::absval): Input array container has rank larger than 5.");
426 INTREPID2_TEST_FOR_EXCEPTION( inArray.rank() != absArray.rank(), std::invalid_argument,
427 ">>> ERROR (RealSpaceTools::absval): Array arguments must have identical ranks!");
428 for (size_type i=0;i<inArray.rank();++i) {
429 INTREPID2_TEST_FOR_EXCEPTION( inArray.extent(i) != absArray.extent(i), std::invalid_argument,
430 ">>> ERROR (RealSpaceTools::absval): Dimensions of array arguments do not agree!");
434 using MemSpaceType =
typename DeviceType::memory_space;
435 constexpr bool are_accessible =
436 Kokkos::Impl::MemorySpaceAccess<MemSpaceType,
437 typename decltype(absArray)::memory_space>::accessible &&
438 Kokkos::Impl::MemorySpaceAccess<MemSpaceType,
439 typename decltype(inArray)::memory_space>::accessible;
440 static_assert(are_accessible,
"RealSpaceTools<DeviceType>::absval(..): input/output views' memory spaces are not compatible with DeviceType");
444 const auto loopSize = inArray.extent(0);
445 using ExecSpaceType =
typename DeviceType::execution_space;
446 Kokkos::RangePolicy<ExecSpaceType,Kokkos::Schedule<Kokkos::Static> > policy(0, loopSize);
447 Kokkos::parallel_for( policy, FunctorType(absArray, inArray) );
452 template<
typename DeviceType>
453 template<
typename inoutArrayValueType,
class ...inoutAbsArrayProperties>
456 absval( Kokkos::DynRankView<inoutArrayValueType,inoutAbsArrayProperties...> inoutAbsArray ) {
462 namespace FunctorRealSpaceTools {
466 template<
typename normArrayViewType,
467 typename inVecViewType>
469 normArrayViewType _normArray;
470 inVecViewType _inVecs;
471 const ENorm _normType;
473 KOKKOS_INLINE_FUNCTION
475 inVecViewType inVecs_,
476 const ENorm normType_ )
477 : _normArray(normArray_), _inVecs(inVecs_), _normType(normType_) {}
479 KOKKOS_INLINE_FUNCTION
480 void operator()(
const ordinal_type iter)
const {
483 _normArray.extent(0),
484 _normArray.extent(1),
487 auto vec = ( _inVecs.rank() == 2 ? Kokkos::subview(_inVecs, i, Kokkos::ALL()) :
488 Kokkos::subview(_inVecs, i, j, Kokkos::ALL()) );
495 template<
typename DeviceType>
496 template<
typename normArrayValueType,
class ...normArrayProperties,
497 typename inVecValueType,
class ...inVecProperties>
500 vectorNorm( Kokkos::DynRankView<normArrayValueType,normArrayProperties...> normArray,
501 const Kokkos::DynRankView<inVecValueType, inVecProperties...> inVecs,
502 const ENorm normType ) {
503#ifdef HAVE_INTREPID2_DEBUG
505 INTREPID2_TEST_FOR_EXCEPTION( inVecs.rank() != (normArray.rank()+1), std::invalid_argument,
506 ">>> ERROR (RealSpaceTools::vectorNorm): Ranks of norm and vector array arguments are incompatible!");
507 INTREPID2_TEST_FOR_EXCEPTION( inVecs.rank() < 2 || inVecs.rank() > 3, std::invalid_argument,
508 ">>> ERROR (RealSpaceTools::vectorNorm): Rank of vector array must be 2 or 3!");
509 for (size_type i=0;i<inVecs.rank()-1;++i) {
510 INTREPID2_TEST_FOR_EXCEPTION( inVecs.extent(i) != normArray.extent(i), std::invalid_argument,
511 ">>> ERROR (RealSpaceTools::vectorNorm): Dimensions of norm and vector arguments do not agree!");
516 using MemSpaceType =
typename DeviceType::memory_space;
517 constexpr bool are_accessible =
518 Kokkos::Impl::MemorySpaceAccess<MemSpaceType,
519 typename decltype(normArray)::memory_space>::accessible &&
520 Kokkos::Impl::MemorySpaceAccess<MemSpaceType,
521 typename decltype(inVecs)::memory_space>::accessible;
522 static_assert(are_accessible,
"RealSpaceTools<DeviceType>::vectorNorm(..): input/output views' memory spaces are not compatible with DeviceType");
526 const auto loopSize = normArray.extent(0)*normArray.extent(1);
527 using ExecSpaceType =
typename DeviceType::execution_space;
528 Kokkos::RangePolicy<ExecSpaceType,Kokkos::Schedule<Kokkos::Static> > policy(0, loopSize);
529 Kokkos::parallel_for( policy, FunctorType(normArray, inVecs, normType) );
534 namespace FunctorRealSpaceTools {
538 template<
typename transposeMatViewType,
539 typename inMatViewType>
541 transposeMatViewType _transposeMats;
542 inMatViewType _inMats;
544 KOKKOS_INLINE_FUNCTION
546 inMatViewType inMats_)
547 : _transposeMats(transposeMats_), _inMats(inMats_) {}
549 KOKKOS_INLINE_FUNCTION
550 void operator()(
const size_type iter)
const {
552 const auto r = _transposeMats.rank();
553 ordinal_type _i = iter, _j = 0;
557 _transposeMats.extent(0),
558 _transposeMats.extent(1),
561 auto dst = ( r == 2 ? Kokkos::subview(_transposeMats, Kokkos::ALL(), Kokkos::ALL()) :
562 r == 3 ? Kokkos::subview(_transposeMats, _i, Kokkos::ALL(), Kokkos::ALL()) :
563 Kokkos::subview(_transposeMats, _i, _j, Kokkos::ALL(), Kokkos::ALL()) );
565 auto src = ( r == 2 ? Kokkos::subview(_inMats, Kokkos::ALL(), Kokkos::ALL()) :
566 r == 3 ? Kokkos::subview(_inMats, _i, Kokkos::ALL(), Kokkos::ALL()) :
567 Kokkos::subview(_inMats, _i, _j, Kokkos::ALL(), Kokkos::ALL()) );
569 for (size_type i=0;i<src.extent(0);++i) {
570 dst(i, i) = src(i, i);
571 for (size_type j=i+1;j<src.extent(1);++j) {
572 dst(i, j) = src(j, i);
573 dst(j, i) = src(i, j);
580 template<
typename DeviceType>
581 template<
typename transposeMatValueType,
class ...transposeMatProperties,
582 typename inMatValueType,
class ...inMatProperties>
585 transpose( Kokkos::DynRankView<transposeMatValueType,transposeMatProperties...> transposeMats,
586 const Kokkos::DynRankView<inMatValueType, inMatProperties...> inMats ) {
588#ifdef HAVE_INTREPID2_DEBUG
591 INTREPID2_TEST_FOR_EXCEPTION( inMats.rank() != transposeMats.rank(), std::invalid_argument,
592 ">>> ERROR (RealSpaceTools::transpose): Matrix array arguments do not have identical ranks!");
593 INTREPID2_TEST_FOR_EXCEPTION( inMats.rank() < 2 || inMats.rank() > 4, std::invalid_argument,
594 ">>> ERROR (RealSpaceTools::transpose): Rank of matrix array must be 2, 3, or 4!");
595 for (size_type i=0;i<inMats.rank();++i) {
596 INTREPID2_TEST_FOR_EXCEPTION( inMats.extent(i) != transposeMats.extent(i), std::invalid_argument,
597 ">>> ERROR (RealSpaceTools::transpose): Dimensions of matrix arguments do not agree!");
599 INTREPID2_TEST_FOR_EXCEPTION( inMats.extent(inMats.rank()-2) != inMats.extent(inMats.rank()-1), std::invalid_argument,
600 ">>> ERROR (RealSpaceTools::transpose): Matrices are not square!");
604 using MemSpaceType =
typename DeviceType::memory_space;
605 constexpr bool are_accessible =
606 Kokkos::Impl::MemorySpaceAccess<MemSpaceType,
607 typename decltype(transposeMats)::memory_space>::accessible &&
608 Kokkos::Impl::MemorySpaceAccess<MemSpaceType,
609 typename decltype(inMats)::memory_space>::accessible;
610 static_assert(are_accessible,
"RealSpaceTools<DeviceType>::transpose(..): input/output views' memory spaces are not compatible with DeviceType");
613 const auto r = transposeMats.rank();
614 const auto loopSize = ( r == 2 ? 1 :
615 r == 3 ? transposeMats.extent(0) :
616 transposeMats.extent(0)*transposeMats.extent(1) );
618 using ExecSpaceType =
typename DeviceType::execution_space;
619 Kokkos::RangePolicy<ExecSpaceType,Kokkos::Schedule<Kokkos::Static> > policy(0, loopSize);
620 Kokkos::parallel_for( policy, FunctorType(transposeMats, inMats) );
625 namespace FunctorRealSpaceTools {
629 template<ordinal_type D,
630 typename inverseMatViewType,
631 typename inMatViewType>
633 typedef typename inMatViewType::non_const_value_type value_type;
634 inverseMatViewType _inverseMats;
635 inMatViewType _inMats;
637 KOKKOS_INLINE_FUNCTION
638 F_inverse( inverseMatViewType inverseMats_,
639 inMatViewType inMats_ )
640 : _inverseMats(inverseMats_), _inMats(inMats_) {}
642 template<
typename matViewType,
643 typename invViewType>
644 KOKKOS_FORCEINLINE_FUNCTION
646 apply_inverse( invViewType inv,
647 const matViewType mat ) {
651#ifdef HAVE_INTREPID2_DEBUG
653#ifdef HAVE_INTREPID2_DEBUG_INF_CHECK
654 bool dbgInfo =
false;
655 INTREPID2_TEST_FOR_DEBUG_ABORT( val == 0, dbgInfo,
656 ">>> ERROR (Matrix): Inverse of a singular matrix is undefined!");
657#ifdef INTREPID2_TEST_FOR_DEBUG_ABORT_OVERRIDE_TO_CONTINUE
665 inv(0,0) = value_type(1)/mat(0,0);
667 else if constexpr (D==2)
669 inv(0,0) = mat(1,1)/val;
670 inv(1,1) = mat(0,0)/val;
672 inv(1,0) = - mat(1,0)/val;
673 inv(0,1) = - mat(0,1)/val;
675 else if constexpr (D==3)
677 value_type val0, val1, val2;
679 val0 = mat(1,1)*mat(2,2) - mat(2,1)*mat(1,2);
680 val1 = - mat(1,0)*mat(2,2) + mat(2,0)*mat(1,2);
681 val2 = mat(1,0)*mat(2,1) - mat(2,0)*mat(1,1);
687 val0 = mat(2,1)*mat(0,2) - mat(0,1)*mat(2,2);
688 val1 = mat(0,0)*mat(2,2) - mat(2,0)*mat(0,2);
689 val2 = - mat(0,0)*mat(2,1) + mat(2,0)*mat(0,1);
695 val0 = mat(0,1)*mat(1,2) - mat(1,1)*mat(0,2);
696 val1 = - mat(0,0)*mat(1,2) + mat(1,0)*mat(0,2);
697 val2 = mat(0,0)*mat(1,1) - mat(1,0)*mat(0,1);
703 static_assert((D >= 1) && (D <= 3));
706 template<
bool B,
class T =
void >
707 using enable_if_t =
typename std::enable_if<B,T>::type;
710 KOKKOS_INLINE_FUNCTION
711 enable_if_t<M==0 && supports_rank_4<inMatViewType>::value >
712 operator()(
const ordinal_type cl,
713 const ordinal_type pt)
const {
714 const auto mat = Kokkos::subview(_inMats, cl, pt, Kokkos::ALL(), Kokkos::ALL());
715 auto inv = Kokkos::subview(_inverseMats, cl, pt, Kokkos::ALL(), Kokkos::ALL());
717 apply_inverse( inv, mat );
721 KOKKOS_INLINE_FUNCTION
722 enable_if_t<M==0 && !supports_rank_4<inMatViewType>::value >
723 operator()(
const ordinal_type cl,
const ordinal_type pt)
const {
728 KOKKOS_INLINE_FUNCTION
729 enable_if_t<M==0 && supports_rank_3<inMatViewType>::value >
730 operator()(
const ordinal_type pt)
const {
731 const auto mat = Kokkos::subview(_inMats, pt, Kokkos::ALL(), Kokkos::ALL());
732 auto inv = Kokkos::subview(_inverseMats, pt, Kokkos::ALL(), Kokkos::ALL());
734 apply_inverse( inv, mat );
738 KOKKOS_INLINE_FUNCTION
739 enable_if_t<M==0 && !supports_rank_3<inMatViewType>::value >
740 operator()(
const ordinal_type pt)
const {
746 template<
typename DeviceType>
747 template<ordinal_type D,
class InverseMatrixViewType,
class MatrixViewType>
750 inverse( InverseMatrixViewType inverseMats, MatrixViewType inMats ) {
752#ifdef HAVE_INTREPID2_DEBUG
754 INTREPID2_TEST_FOR_EXCEPTION( rank !=
getFunctorRank(inverseMats), std::invalid_argument,
755 ">>> ERROR (RealSpaceTools::inverse): Matrix array arguments do not have identical ranks!");
756 INTREPID2_TEST_FOR_EXCEPTION( rank < 3 || rank > 4, std::invalid_argument,
757 ">>> ERROR (RealSpaceTools::inverse): Rank of matrix array must be 3, or 4!");
758 for (size_type i=0;i<rank;++i) {
759 INTREPID2_TEST_FOR_EXCEPTION( inMats.extent(i) != inverseMats.extent(i), std::invalid_argument,
760 ">>> ERROR (RealSpaceTools::inverse): Dimensions of matrix arguments do not agree!");
762 INTREPID2_TEST_FOR_EXCEPTION( inMats.extent(rank-2) != inMats.extent(rank-1), std::invalid_argument,
763 ">>> ERROR (RealSpaceTools::inverse): Matrices are not square!");
764 INTREPID2_TEST_FOR_EXCEPTION( inMats.extent(rank-2) < 1 || inMats.extent(rank-2) > 3, std::invalid_argument,
765 ">>> ERROR (RealSpaceTools::inverse): Spatial dimension must be 1, 2, or 3!");
769 using ExecSpaceType =
typename DeviceType::execution_space;
774 using range_policy_type = Kokkos::RangePolicy<ExecSpaceType,Kokkos::Schedule<Kokkos::Static> >;
775 range_policy_type policy(0, inverseMats.extent(0));
776 Kokkos::parallel_for( policy, FunctorType(inverseMats, inMats) );
780 using range_policy_type = Kokkos::MDRangePolicy
781 < ExecSpaceType, Kokkos::Rank<2>, Kokkos::IndexType<ordinal_type> >;
782 range_policy_type policy( { 0, 0 },
783 { inverseMats.extent(0), inverseMats.extent(1) } );
784 Kokkos::parallel_for( policy, FunctorType(inverseMats, inMats) );
788 INTREPID2_TEST_FOR_EXCEPTION(
true, std::invalid_argument,
789 ">>> ERROR (RealSpaceTools::inverse): Rank of matrix array must be 2, 3, or 4!");
794 template<
typename DeviceType>
795 template<
class InverseMatrixViewType,
class MatrixViewType>
798 inverse( InverseMatrixViewType inverseMats, MatrixViewType inMats ) {
802 const auto dim = inMats.extent(2);
805 case 1: inverse<1,InverseMatrixViewType,MatrixViewType>(inverseMats,inMats);
break;
806 case 2: inverse<2,InverseMatrixViewType,MatrixViewType>(inverseMats,inMats);
break;
807 case 3: inverse<3,InverseMatrixViewType,MatrixViewType>(inverseMats,inMats);
break;
808 default: INTREPID2_TEST_FOR_EXCEPTION((dim < 1) || (dim > 3), std::invalid_argument,
">>> ERROR (RealSpaceTools::inverse): Spatial dimension must be 1, 2, or 3!");
814 namespace FunctorRealSpaceTools {
818 template<
typename detArrayViewType,
819 typename inMatViewType,
int rank>
821 detArrayViewType _detArray;
822 inMatViewType _inMats;
824 KOKKOS_INLINE_FUNCTION
825 F_det( detArrayViewType detArray_,
826 inMatViewType inMats_ )
827 : _detArray(detArray_), _inMats(inMats_) {}
829 template<
bool B,
class T =
void >
830 using enable_if_t =
typename std::enable_if<B,T>::type;
833 KOKKOS_INLINE_FUNCTION
834 enable_if_t<M==1 && supports_rank_3<inMatViewType>::value >
835 operator()(
const ordinal_type pt)
const {
836 const auto mat = Kokkos::subview(_inMats, pt, Kokkos::ALL(), Kokkos::ALL());
841 KOKKOS_INLINE_FUNCTION
842 enable_if_t<M==1 && !supports_rank_3<inMatViewType>::value >
843 operator()(
const ordinal_type pt)
const {
848 KOKKOS_INLINE_FUNCTION
849 enable_if_t<M==2 && supports_rank_4<inMatViewType>::value >
850 operator()(
const ordinal_type cl,
851 const ordinal_type pt)
const {
852 const auto mat = Kokkos::subview(_inMats, cl, pt, Kokkos::ALL(), Kokkos::ALL());
857 KOKKOS_INLINE_FUNCTION
858 enable_if_t<M==2 && !supports_rank_4<inMatViewType>::value >
859 operator()(
const ordinal_type cl,
860 const ordinal_type pt)
const {
866template<
class DeterminantArrayViewType,
class MatrixViewType>
868det( DeterminantArrayViewType detArray,
const MatrixViewType inMats );
870 template<
typename DeviceType>
871 template<
class DeterminantArrayViewType,
class MatrixViewType>
874 det( DeterminantArrayViewType detArray,
const MatrixViewType inMats ) {
876#ifdef HAVE_INTREPID2_DEBUG
880 INTREPID2_TEST_FOR_EXCEPTION( matrixRank != detRank+2, std::invalid_argument,
881 ">>> ERROR (RealSpaceTools::det): Determinant and matrix array arguments do not have compatible ranks!");
882 INTREPID2_TEST_FOR_EXCEPTION( matrixRank < 3 || matrixRank > 4, std::invalid_argument,
883 ">>> ERROR (RealSpaceTools::det): Rank of matrix array must be 3 or 4!");
884 for (size_type i=0;i<matrixRank-2;++i) {
885 INTREPID2_TEST_FOR_EXCEPTION( inMats.extent(i) != detArray.extent(i), std::invalid_argument,
886 ">>> ERROR (RealSpaceTools::det): Dimensions of determinant and matrix array arguments do not agree!");
888 INTREPID2_TEST_FOR_EXCEPTION( inMats.extent(matrixRank-2) != inMats.extent(matrixRank-1), std::invalid_argument,
889 ">>> ERROR (RealSpaceTools::det): Matrices are not square!");
890 INTREPID2_TEST_FOR_EXCEPTION( inMats.extent(matrixRank-2) < 1 || inMats.extent(matrixRank-2) > 3, std::invalid_argument,
891 ">>> ERROR (RealSpaceTools::det): Spatial dimension must be 1, 2, or 3!");
895 typedef typename DeviceType::execution_space ExecSpaceType;
899 switch (detArrayRank) {
902 using range_policy_type = Kokkos::RangePolicy<ExecSpaceType,Kokkos::Schedule<Kokkos::Static> >;
903 range_policy_type policy(0, detArray.extent(0));
904 Kokkos::parallel_for( policy, FunctorType(detArray, inMats) );
909 using range_policy_type = Kokkos::MDRangePolicy
910 < ExecSpaceType, Kokkos::Rank<2>, Kokkos::IndexType<ordinal_type> >;
911 range_policy_type policy( { 0, 0 },
912 { detArray.extent(0), detArray.extent(1) } );
913 Kokkos::parallel_for( policy, FunctorType(detArray, inMats) );
917 INTREPID2_TEST_FOR_EXCEPTION(
true, std::invalid_argument,
918 ">>> ERROR (RealSpaceTools::det): Rank of detArray must be 1 or 2!");
925 namespace FunctorRealSpaceTools {
929 template<
typename sumArrayViewType,
930 typename inArray1Viewtype,
931 typename inArray2ViewType>
933 sumArrayViewType _sumArray;
934 inArray1Viewtype _inArray1;
935 inArray2ViewType _inArray2;
937 KOKKOS_INLINE_FUNCTION
938 F_add( sumArrayViewType sumArray_,
939 inArray1Viewtype inArray1_,
940 inArray2ViewType inArray2_ )
941 : _sumArray(sumArray_), _inArray1(inArray1_), _inArray2(inArray2_) {}
943 KOKKOS_INLINE_FUNCTION
944 void operator()(
const ordinal_type i)
const {
945 const ordinal_type jend = _sumArray.extent(1);
946 const ordinal_type kend = _sumArray.extent(2);
947 const ordinal_type lend = _sumArray.extent(3);
948 const ordinal_type mend = _sumArray.extent(4);
950 for (ordinal_type j=0;j<jend;++j)
951 for (ordinal_type k=0;k<kend;++k)
952 for (ordinal_type l=0;l<lend;++l)
953 for (ordinal_type m=0;m<mend;++m)
954 _sumArray.access(i,j,k,l,m) = _inArray1.access(i,j,k,l,m) + _inArray2.access(i,j,k,l,m);
959 template<
typename DeviceType>
960 template<
typename sumArrayValueType,
class ...sumArrayProperties,
961 typename inArray1ValueType,
class ...inArray1Properties,
962 typename inArray2ValueType,
class ...inArray2Properties>
965 add( Kokkos::DynRankView<sumArrayValueType,sumArrayProperties...> sumArray,
966 const Kokkos::DynRankView<inArray1ValueType,inArray1Properties...> inArray1,
967 const Kokkos::DynRankView<inArray2ValueType,inArray2Properties...> inArray2 ) {
969#ifdef HAVE_INTREPID2_DEBUG
971 INTREPID2_TEST_FOR_EXCEPTION( inArray1.rank() != inArray2.rank() ||
972 inArray1.rank() != sumArray.rank(), std::invalid_argument,
973 ">>> ERROR (RealSpaceTools::add): Array arguments must have identical ranks!");
974 for (size_type i=0;i<inArray1.rank();++i) {
975 INTREPID2_TEST_FOR_EXCEPTION( inArray1.extent(i) != inArray2.extent(i) ||
976 inArray1.extent(i) != sumArray.extent(i), std::invalid_argument,
977 ">>> ERROR (RealSpaceTools::add): Dimensions of array arguments do not agree!");
981 using MemSpaceType =
typename DeviceType::memory_space;
982 constexpr bool are_accessible =
983 Kokkos::Impl::MemorySpaceAccess<MemSpaceType,
984 typename decltype(sumArray)::memory_space>::accessible &&
985 Kokkos::Impl::MemorySpaceAccess<MemSpaceType,
986 typename decltype(inArray1)::memory_space>::accessible &&
987 Kokkos::Impl::MemorySpaceAccess<MemSpaceType,
988 typename decltype(inArray2)::memory_space>::accessible;
989 static_assert(are_accessible,
"RealSpaceTools<DeviceType>::add(..): input/output views' memory spaces are not compatible with DeviceType");
993 const auto loopSize = sumArray.extent(0);
994 using ExecSpaceType =
typename DeviceType::execution_space;
995 Kokkos::RangePolicy<ExecSpaceType,Kokkos::Schedule<Kokkos::Static> > policy(0, loopSize);
996 Kokkos::parallel_for( policy, FunctorType(sumArray, inArray1, inArray2) );
1001 template<
typename DeviceType>
1002 template<
typename inoutSumArrayValueType,
class ...inoutSumArrayProperties,
1003 typename inArrayValueType,
class ...inArrayProperties>
1006 add( Kokkos::DynRankView<inoutSumArrayValueType,inoutSumArrayProperties...> inoutSumArray,
1007 const Kokkos::DynRankView<inArrayValueType, inArrayProperties...> inArray ) {
1009#ifdef HAVE_INTREPID2_DEBUG
1011 INTREPID2_TEST_FOR_EXCEPTION( inArray.rank() != inoutSumArray.rank(), std::invalid_argument,
1012 ">>> ERROR (RealSpaceTools::sum): Array arguments must have identical ranks!");
1013 for (size_type i=0;i<inArray.rank();++i) {
1014 INTREPID2_TEST_FOR_EXCEPTION( inArray.extent(i) != inoutSumArray.extent(i), std::invalid_argument,
1015 ">>> ERROR (RealSpaceTools::sum): Dimensions of array arguments do not agree!");
1024 namespace FunctorRealSpaceTools {
1028 template<
typename diffArrayViewType,
1029 typename inArray1ViewType,
1030 typename inArray2ViewType>
1032 diffArrayViewType _diffArray;
1033 const inArray1ViewType _inArray1;
1034 const inArray2ViewType _inArray2;
1036 KOKKOS_INLINE_FUNCTION
1038 inArray1ViewType inArray1_,
1039 inArray2ViewType inArray2_ )
1040 : _diffArray(diffArray_), _inArray1(inArray1_), _inArray2(inArray2_) {}
1042 KOKKOS_INLINE_FUNCTION
1043 void operator()(
const ordinal_type i)
const {
1044 const ordinal_type jend = _diffArray.extent(1);
1045 const ordinal_type kend = _diffArray.extent(2);
1046 const ordinal_type lend = _diffArray.extent(3);
1047 const ordinal_type mend = _diffArray.extent(4);
1049 for (ordinal_type j=0;j<jend;++j)
1050 for (ordinal_type k=0;k<kend;++k)
1051 for (ordinal_type l=0;l<lend;++l)
1052 for (ordinal_type m=0;m<mend;++m)
1053 _diffArray.access(i,j,k,l,m) = _inArray1.access(i,j,k,l,m) - _inArray2.access(i,j,k,l,m);
1058 template<
typename DeviceType>
1059 template<
typename diffArrayValueType,
class ...diffArrayProperties,
1060 typename inArray1ValueType,
class ...inArray1Properties,
1061 typename inArray2ValueType,
class ...inArray2Properties>
1064 subtract( Kokkos::DynRankView<diffArrayValueType,diffArrayProperties...> diffArray,
1065 const Kokkos::DynRankView<inArray1ValueType, inArray1Properties...> inArray1,
1066 const Kokkos::DynRankView<inArray2ValueType, inArray2Properties...> inArray2 ) {
1068#ifdef HAVE_INTREPID2_DEBUG
1070 INTREPID2_TEST_FOR_EXCEPTION( inArray1.rank() != inArray2.rank() ||
1071 inArray1.rank() != diffArray.rank(), std::invalid_argument,
1072 ">>> ERROR (RealSpaceTools::subtract): Array arguments must have identical ranks!");
1073 for (size_type i=0;i<inArray1.rank();++i) {
1074 INTREPID2_TEST_FOR_EXCEPTION( inArray1.extent(i) != inArray2.extent(i) ||
1075 inArray1.extent(i) != diffArray.extent(i), std::invalid_argument,
1076 ">>> ERROR (RealSpaceTools::subtract): Dimensions of array arguments do not agree!");
1080 using MemSpaceType =
typename DeviceType::memory_space;
1081 constexpr bool are_accessible =
1082 Kokkos::Impl::MemorySpaceAccess<MemSpaceType,
1083 typename decltype(diffArray)::memory_space>::accessible &&
1084 Kokkos::Impl::MemorySpaceAccess<MemSpaceType,
1085 typename decltype(inArray1)::memory_space>::accessible &&
1086 Kokkos::Impl::MemorySpaceAccess<MemSpaceType,
1087 typename decltype(inArray2)::memory_space>::accessible;
1088 static_assert(are_accessible,
"RealSpaceTools<DeviceType>::subtract(..): input/output views' memory spaces are not compatible with DeviceType");
1092 const size_type loopSize = diffArray.extent(0);
1093 using ExecSpaceType =
typename DeviceType::execution_space;
1094 Kokkos::RangePolicy<ExecSpaceType,Kokkos::Schedule<Kokkos::Static> > policy(0, loopSize);
1095 Kokkos::parallel_for( policy, FunctorType(diffArray, inArray1, inArray2) );
1100 template<
typename DeviceType>
1101 template<
typename inoutDiffArrayValueType,
class ...inoutDiffArrayProperties,
1102 typename inArrayValueType,
class ...inArrayProperties>
1105 subtract( Kokkos::DynRankView<inoutDiffArrayValueType,inoutDiffArrayProperties...> inoutDiffArray,
1106 const Kokkos::DynRankView<inArrayValueType, inArrayProperties...> inArray ) {
1107#ifdef HAVE_INTREPID2_DEBUG
1109 INTREPID2_TEST_FOR_EXCEPTION( inArray.rank() != inoutDiffArray.rank(), std::invalid_argument,
1110 ">>> ERROR (RealSpaceTools::subtract): Array arguments must have identical ranks!");
1111 for (size_type i=0;i<inArray.rank();++i) {
1112 INTREPID2_TEST_FOR_EXCEPTION( inArray.extent(i) != inoutDiffArray.extent(i), std::invalid_argument,
1113 ">>> ERROR (RealSpaceTools::subtract): Dimensions of array arguments do not agree!");
1122 namespace FunctorRealSpaceTools {
1126 template<
typename ValueType,
1127 typename scaledArrayViewType,
1128 typename inArrayViewType>
1130 scaledArrayViewType _scaledArray;
1131 const inArrayViewType _inArray;
1132 const ValueType _alpha;
1134 KOKKOS_INLINE_FUNCTION
1135 F_scale( scaledArrayViewType scaledArray_,
1136 inArrayViewType inArray_,
1137 const ValueType alpha_ )
1138 : _scaledArray(scaledArray_), _inArray(inArray_), _alpha(alpha_) {}
1140 KOKKOS_INLINE_FUNCTION
1141 void operator()(
const ordinal_type i)
const {
1142 const ordinal_type jend = _inArray.extent(1);
1143 const ordinal_type kend = _inArray.extent(2);
1144 const ordinal_type lend = _inArray.extent(3);
1145 const ordinal_type mend = _inArray.extent(4);
1147 for (ordinal_type j=0;j<jend;++j)
1148 for (ordinal_type k=0;k<kend;++k)
1149 for (ordinal_type l=0;l<lend;++l)
1150 for (ordinal_type m=0;m<mend;++m)
1151 _scaledArray.access(i,j,k,l,m) = _alpha*_inArray.access(i,j,k,l,m);
1157 template<
typename DeviceType>
1158 template<
typename ValueType,
1159 typename scaledArrayValueType,
class ...scaledArrayProperties,
1160 typename inArrayValueType,
class ...inArrayProperties>
1163 scale( Kokkos::DynRankView<scaledArrayValueType,scaledArrayProperties...> scaledArray,
1164 const Kokkos::DynRankView<inArrayValueType, inArrayProperties...> inArray,
1165 const ValueType alpha ) {
1167#ifdef HAVE_INTREPID2_DEBUG
1169 INTREPID2_TEST_FOR_EXCEPTION( inArray.rank() > 5, std::invalid_argument,
1170 ">>> ERROR (RealSpaceTools::scale): Input array container has rank larger than 5.");
1171 INTREPID2_TEST_FOR_EXCEPTION( inArray.rank() != scaledArray.rank(), std::invalid_argument,
1172 ">>> ERROR (RealSpaceTools::scale): Array arguments must have identical ranks!");
1173 for (size_type i=0;i<inArray.rank();++i) {
1174 INTREPID2_TEST_FOR_EXCEPTION( inArray.extent(i) != scaledArray.extent(i), std::invalid_argument,
1175 ">>> ERROR (RealSpaceTools::scale): Dimensions of array arguments do not agree!");
1180 using MemSpaceType =
typename DeviceType::memory_space;
1181 constexpr bool are_accessible =
1182 Kokkos::Impl::MemorySpaceAccess<MemSpaceType,
1183 typename decltype(scaledArray)::memory_space>::accessible &&
1184 Kokkos::Impl::MemorySpaceAccess<MemSpaceType,
1185 typename decltype(inArray)::memory_space>::accessible;
1186 static_assert(are_accessible,
"RealSpaceTools<DeviceType>::scale(..): input/output views' memory spaces are not compatible with DeviceType");
1190 const auto loopSize = scaledArray.extent(0);
1191 using ExecSpaceType =
typename DeviceType::execution_space;
1192 Kokkos::RangePolicy<ExecSpaceType,Kokkos::Schedule<Kokkos::Static> > policy(0, loopSize);
1193 Kokkos::parallel_for( policy, FunctorType(scaledArray, inArray, alpha) );
1198 template<
typename DeviceType>
1199 template<
typename ValueType,
1200 typename inoutScaledArrayValueType,
class ...inoutScaledArrayProperties>
1203 scale( Kokkos::DynRankView<inoutScaledArrayValueType,inoutScaledArrayProperties...> inoutScaledArray,
1204 const ValueType alpha ) {
1211 namespace FunctorRealSpaceTools {
1215 template<
typename dotArrayViewType,
1216 typename inVec1ViewType,
1217 typename inVec2ViewType>
1219 dotArrayViewType _dotArray;
1220 const inVec1ViewType _inVecs1;
1221 const inVec2ViewType _inVecs2;
1223 KOKKOS_INLINE_FUNCTION
1224 F_dot( dotArrayViewType dotArray_,
1225 inVec1ViewType inVecs1_,
1226 inVec2ViewType inVecs2_ )
1227 : _dotArray(dotArray_), _inVecs1(inVecs1_), _inVecs2(inVecs2_) {}
1229 KOKKOS_INLINE_FUNCTION
1230 void operator()(
const ordinal_type iter)
const {
1234 _dotArray.extent(0),
1235 _dotArray.extent(1),
1238 const auto r = _inVecs1.rank();
1239 auto vec1 = ( r == 2 ? Kokkos::subview(_inVecs1, i, Kokkos::ALL()) :
1240 Kokkos::subview(_inVecs1, i, j, Kokkos::ALL()) );
1241 auto vec2 = ( r == 2 ? Kokkos::subview(_inVecs2, i, Kokkos::ALL()) :
1242 Kokkos::subview(_inVecs2, i, j, Kokkos::ALL()) );
1249 template<
typename DeviceType>
1250 template<
typename dotArrayValueType,
class ...dotArrayProperties,
1251 typename inVec1ValueType,
class ...inVec1Properties,
1252 typename inVec2ValueType,
class ...inVec2Properties>
1255 dot( Kokkos::DynRankView<dotArrayValueType,dotArrayProperties...> dotArray,
1256 const Kokkos::DynRankView<inVec1ValueType, inVec1Properties...> inVecs1,
1257 const Kokkos::DynRankView<inVec2ValueType, inVec2Properties...> inVecs2 ) {
1259#ifdef HAVE_INTREPID2_DEBUG
1261 INTREPID2_TEST_FOR_EXCEPTION( inVecs1.rank() != (dotArray.rank()+1), std::invalid_argument,
1262 ">>> ERROR (RealSpaceTools::dot): Ranks of norm and vector array arguments are incompatible!");
1263 INTREPID2_TEST_FOR_EXCEPTION( inVecs1.rank() != inVecs2.rank(), std::invalid_argument,
1264 ">>> ERROR (RealSpaceTools::dot): Ranks of input vector arguments must be identical!");
1265 INTREPID2_TEST_FOR_EXCEPTION( inVecs1.rank() < 2 || inVecs1.rank() > 3, std::invalid_argument,
1266 ">>> ERROR (RealSpaceTools::dot): Rank of input vector arguments must be 2 or 3!");
1267 for (size_type i=0;i<inVecs1.rank();++i) {
1268 INTREPID2_TEST_FOR_EXCEPTION( inVecs1.extent(i) != inVecs2.extent(i), std::invalid_argument,
1269 ">>> ERROR (RealSpaceTools::dot): Dimensions of input vector arguments do not agree!");
1271 for (size_type i=0;i<dotArray.rank();++i) {
1272 INTREPID2_TEST_FOR_EXCEPTION( inVecs1.extent(i) != dotArray.extent(i), std::invalid_argument,
1273 ">>> ERROR (RealSpaceTools::dot): Dimensions of dot-product and vector arrays do not agree!");
1277 using MemSpaceType =
typename DeviceType::memory_space;
1278 constexpr bool are_accessible =
1279 Kokkos::Impl::MemorySpaceAccess<MemSpaceType,
1280 typename decltype(dotArray)::memory_space>::accessible &&
1281 Kokkos::Impl::MemorySpaceAccess<MemSpaceType,
1282 typename decltype(inVecs1)::memory_space>::accessible &&
1283 Kokkos::Impl::MemorySpaceAccess<MemSpaceType,
1284 typename decltype(inVecs2)::memory_space>::accessible;
1285 static_assert(are_accessible,
"RealSpaceTools<DeviceType>::dot(..): input/output views' memory spaces are not compatible with DeviceType");
1289 const auto loopSize = dotArray.extent(0)*dotArray.extent(1);
1290 using ExecSpaceType =
typename DeviceType::execution_space;
1291 Kokkos::RangePolicy<ExecSpaceType,Kokkos::Schedule<Kokkos::Static> > policy(0, loopSize);
1292 Kokkos::parallel_for( policy, FunctorType(dotArray, inVecs1, inVecs2) );
1297 namespace FunctorRealSpaceTools {
1301 template<
typename matVecViewType,
1302 typename inMatViewType,
1303 typename inVecViewType>
1305 matVecViewType _matVecs;
1306 const inMatViewType _inMats;
1307 const inVecViewType _inVecs;
1309 KOKKOS_INLINE_FUNCTION
1311 inMatViewType inMats_,
1312 inVecViewType inVecs_ )
1313 : _matVecs(matVecs_), _inMats(inMats_), _inVecs(inVecs_) {}
1315 KOKKOS_INLINE_FUNCTION
1316 void operator()(
const ordinal_type iter)
const {
1317 const ordinal_type rm = _inMats.rank(), rv = _inVecs.rank(), rr = _matVecs.rank();
1318 ordinal_type _i = iter, _j = 0;
1321 unrollIndex( _i, _j,
1326 auto mat = ( rm == 2 ? Kokkos::subview(_inMats, Kokkos::ALL(), Kokkos::ALL()) :
1327 rm == 3 ? Kokkos::subview(_inMats, _i, Kokkos::ALL(), Kokkos::ALL()) :
1328 Kokkos::subview(_inMats, _i, _j, Kokkos::ALL(), Kokkos::ALL()) );
1330 auto vec = ( rv == 1 ? Kokkos::subview(_inVecs, Kokkos::ALL()) :
1331 rv == 2 ? Kokkos::subview(_inVecs, _i, Kokkos::ALL()) :
1332 Kokkos::subview(_inVecs, _i, _j, Kokkos::ALL()) );
1334 auto result = ( rr == 1 ? Kokkos::subview(_matVecs, Kokkos::ALL()) :
1335 rr == 2 ? Kokkos::subview(_matVecs, _i, Kokkos::ALL()) :
1336 Kokkos::subview(_matVecs, _i, _j, Kokkos::ALL()) );
1338 const ordinal_type iend = result.extent(0);
1339 const ordinal_type jend = vec.extent(0);
1341 for (ordinal_type i=0;i<iend;++i) {
1343 for (ordinal_type j=0;j<jend;++j)
1344 result(i) += mat(i, j)*vec(j);
1353 template<
typename outMatViewType,
1354 typename inMatViewType>
1356 outMatViewType _outMats;
1357 const inMatViewType _inMats;
1359 KOKKOS_INLINE_FUNCTION
1360 F_AtA( outMatViewType outMats_,
1361 inMatViewType inMats_)
1362 : _outMats(outMats_), _inMats(inMats_) {}
1364 KOKKOS_INLINE_FUNCTION
1365 void operator()(
const ordinal_type iter)
const {
1366 const ordinal_type rIM = _inMats.rank(), rOM = _outMats.rank();
1367 ordinal_type _i = iter, _j = 0;
1370 unrollIndex( _i, _j,
1375 auto inMat = ( rIM == 2 ? Kokkos::subview(_inMats, Kokkos::ALL(), Kokkos::ALL()) :
1376 rIM == 3 ? Kokkos::subview(_inMats, _i, Kokkos::ALL(), Kokkos::ALL()) :
1377 Kokkos::subview(_inMats, _i, _j, Kokkos::ALL(), Kokkos::ALL()) );
1379 auto outMat = ( rOM == 2 ? Kokkos::subview(_outMats, Kokkos::ALL(), Kokkos::ALL()) :
1380 rOM == 3 ? Kokkos::subview(_outMats, _i, Kokkos::ALL(), Kokkos::ALL()) :
1381 Kokkos::subview(_outMats, _i, _j, Kokkos::ALL(), Kokkos::ALL()) );
1383 const ordinal_type iend = outMat.extent(0);
1384 const ordinal_type jend = outMat.extent(1);
1385 const ordinal_type kend = inMat.extent(0);
1387 for (ordinal_type i=0;i<iend;++i) {
1388 for (ordinal_type j=i;j<jend;++j) {
1390 for (ordinal_type k=0;k<kend;++k)
1391 outMat(i,j) += inMat(k, i)*inMat(k,j);
1393 for (ordinal_type j=0;j<i;++j)
1394 outMat(i,j) = outMat(j,i);
1401 template<
typename DeviceType>
1402 template<
typename matVecValueType,
class ...matVecProperties,
1403 typename inMatValueType,
class ...inMatProperties,
1404 typename inVecValueType,
class ...inVecProperties>
1407 matvec( Kokkos::DynRankView<matVecValueType,matVecProperties...> matVecs,
1408 const Kokkos::DynRankView<inMatValueType, inMatProperties...> inMats,
1409 const Kokkos::DynRankView<inVecValueType, inVecProperties...> inVecs ) {
1411#ifdef HAVE_INTREPID2_DEBUG
1412 INTREPID2_TEST_FOR_EXCEPTION( inMats.rank() < 2 || inMats.rank() > 4, std::invalid_argument,
1413 ">>> ERROR (RealSpaceTools::matvec): Rank of matrix array must be 2, 3 or 4!");
1414 INTREPID2_TEST_FOR_EXCEPTION( matVecs.rank() < 1 || matVecs.rank() > 3, std::invalid_argument,
1415 ">>> ERROR (RealSpaceTools::matvec): Rank of matVecs array must be 1, 2 or 3!");
1416 INTREPID2_TEST_FOR_EXCEPTION( inVecs.rank() < 1 || inVecs.rank() > 3, std::invalid_argument,
1417 ">>> ERROR (RealSpaceTools::matvec): Rank of inVecs array must be 1, 2 or 3!");
1418 if (inMats.rank() == 2) {
1420 INTREPID2_TEST_FOR_EXCEPTION( matVecs.rank() != inVecs.rank(), std::invalid_argument,
1421 ">>> ERROR (RealSpaceTools::matvec): Vector arrays must be have the same rank!");
1423 for (ordinal_type i=0;i< (static_cast<ordinal_type>(inVecs.rank())-1);++i) {
1424 INTREPID2_TEST_FOR_EXCEPTION( matVecs.extent(i) != inVecs.extent(i), std::invalid_argument,
1425 ">>> ERROR (RealSpaceTools::matvec): Dimensions of vector array arguments do not agree!");
1428 INTREPID2_TEST_FOR_EXCEPTION( inMats.extent(0) != matVecs.extent(matVecs.rank()-1) ||
1429 inMats.extent(1) != inVecs.extent(inVecs.rank()-1), std::invalid_argument,
1430 ">>> ERROR (RealSpaceTools::matvec): Matvec dimensions are not compatible each other.");
1431 }
else if (inVecs.rank() < matVecs.rank()) {
1433 INTREPID2_TEST_FOR_EXCEPTION( inMats.rank() != (matVecs.rank()+1), std::invalid_argument,
1434 ">>> ERROR (RealSpaceTools::matvec): The result vector and matrix array arguments do not have compatible ranks!");
1435 for (ordinal_type i=0;i<(static_cast<ordinal_type>(inMats.rank())-2);++i) {
1436 INTREPID2_TEST_FOR_EXCEPTION( inMats.extent(i) != matVecs.extent(i), std::invalid_argument,
1437 ">>> ERROR (RealSpaceTools::matvec): Dimensions of vector and matrix array arguments do not agree!");
1440 INTREPID2_TEST_FOR_EXCEPTION( inMats.extent(inMats.rank()-2) != matVecs.extent(matVecs.rank()-1) ||
1441 inMats.extent(inMats.rank()-1) != inVecs.extent(inVecs.rank()-1), std::invalid_argument,
1442 ">>> ERROR (RealSpaceTools::matvec): Matvec dimensions are not compatible each other.");
1445 INTREPID2_TEST_FOR_EXCEPTION( inMats.rank() != (matVecs.rank()+1), std::invalid_argument,
1446 ">>> ERROR (RealSpaceTools::matvec): The result vector and matrix array arguments do not have compatible ranks!");
1447 INTREPID2_TEST_FOR_EXCEPTION( matVecs.rank() != inVecs.rank(), std::invalid_argument,
1448 ">>> ERROR (RealSpaceTools::matvec): Vector arrays must be have the same rank!");
1449 for (ordinal_type i=0;i<(static_cast<ordinal_type>(inMats.rank())-2);++i) {
1450 INTREPID2_TEST_FOR_EXCEPTION( inMats.extent(i) != matVecs.extent(i), std::invalid_argument,
1451 ">>> ERROR (RealSpaceTools::matvec): Dimensions of vector and matrix array arguments do not agree!");
1453 for (size_type i=0;i<inVecs.rank();++i) {
1454 INTREPID2_TEST_FOR_EXCEPTION( matVecs.extent(i) != inVecs.extent(i), std::invalid_argument,
1455 ">>> ERROR (RealSpaceTools::matvec): Dimensions of vector array arguments do not agree!");
1457 INTREPID2_TEST_FOR_EXCEPTION( inMats.extent(inMats.rank()-1) != inVecs.extent(inVecs.rank()-1), std::invalid_argument,
1458 ">>> ERROR (RealSpaceTools::matvec): Matrix column dimension does not match to the length of a vector!");
1461 using MemSpaceType =
typename DeviceType::memory_space;
1462 constexpr bool are_accessible =
1463 Kokkos::Impl::MemorySpaceAccess<MemSpaceType,
1464 typename decltype(matVecs)::memory_space>::accessible &&
1465 Kokkos::Impl::MemorySpaceAccess<MemSpaceType,
1466 typename decltype(inMats)::memory_space>::accessible &&
1467 Kokkos::Impl::MemorySpaceAccess<MemSpaceType,
1468 typename decltype(inVecs)::memory_space>::accessible;
1469 static_assert(are_accessible,
"RealSpaceTools<DeviceType>::matvec(..): input/output views' memory spaces are not compatible with DeviceType");
1473 size_type loopSize = 1;
1474 const ordinal_type r = matVecs.rank() - 1;
1475 for (ordinal_type i=0;i<r;++i)
1476 loopSize *= matVecs.extent(i);
1478 using ExecSpaceType =
typename DeviceType::execution_space;
1479 Kokkos::RangePolicy<ExecSpaceType,Kokkos::Schedule<Kokkos::Static> > policy(0, loopSize);
1480 Kokkos::parallel_for( policy, FunctorType(matVecs, inMats, inVecs) );
1484 template<
typename DeviceType>
1485 template<
typename outMatValueType,
class ...outMatProperties,
1486 typename inMatValueType,
class ...inMatProperties>
1489 AtA( Kokkos::DynRankView<outMatValueType,outMatProperties...> outMats,
1490 const Kokkos::DynRankView<inMatValueType, inMatProperties...> inMats) {
1492#ifdef HAVE_INTREPID2_DEBUG
1493 INTREPID2_TEST_FOR_EXCEPTION( inMats.rank() < 2 || inMats.rank() > 4, std::invalid_argument,
1494 ">>> ERROR (RealSpaceTools::AtA): Rank of input matrix array must be 2, 3 or 4!");
1496 INTREPID2_TEST_FOR_EXCEPTION( inMats.rank() != outMats.rank(), std::invalid_argument,
1497 ">>> ERROR (RealSpaceTools::AtA): The matrices do not have the same ranks!");
1498 for (ordinal_type i=0;i<(static_cast<ordinal_type>(inMats.rank())-2);++i) {
1499 INTREPID2_TEST_FOR_EXCEPTION( inMats.extent(i) != outMats.extent(i), std::invalid_argument,
1500 ">>> ERROR (RealSpaceTools::AtA): Dimensions of matrices arrays do not agree!");
1503 INTREPID2_TEST_FOR_EXCEPTION( inMats.extent(inMats.rank()-1) != outMats.extent(outMats.rank()-1) ||
1504 outMats.extent(outMats.rank()-1) != outMats.extent(outMats.rank()-2), std::invalid_argument,
1505 ">>> ERROR (RealSpaceTools::AtA): Matrices dimensions are not compatible.");
1508 using MemSpaceType =
typename DeviceType::memory_space;
1509 constexpr bool are_accessible =
1510 Kokkos::Impl::MemorySpaceAccess<MemSpaceType,
1511 typename decltype(outMats)::memory_space>::accessible &&
1512 Kokkos::Impl::MemorySpaceAccess<MemSpaceType,
1513 typename decltype(inMats)::memory_space>::accessible;
1514 static_assert(are_accessible,
"RealSpaceTools<DeviceType>::AtA(..): input/output views' memory spaces are not compatible with DeviceType");
1518 size_type loopSize = 1;
1519 const ordinal_type r = outMats.rank() - 2;
1520 for (ordinal_type i=0;i<r;++i)
1521 loopSize *= outMats.extent(i);
1523 using ExecSpaceType =
typename DeviceType::execution_space;
1524 Kokkos::RangePolicy<ExecSpaceType,Kokkos::Schedule<Kokkos::Static> > policy(0, loopSize);
1525 Kokkos::parallel_for( policy, FunctorType(outMats, inMats) );
1531 namespace FunctorRealSpaceTools {
1535 template<
typename vecProdViewType,
1536 typename inLeftViewType,
1537 typename inRightViewType>
1539 vecProdViewType _vecProd;
1540 const inLeftViewType _inLeft;
1541 const inRightViewType _inRight;
1542 const bool _is_vecprod_3d;
1544 KOKKOS_INLINE_FUNCTION
1546 inLeftViewType inLeft_,
1547 inRightViewType inRight_,
1548 const bool is_vecprod_3d_ )
1549 : _vecProd(vecProd_), _inLeft(inLeft_), _inRight(inRight_), _is_vecprod_3d(is_vecprod_3d_) {}
1551 KOKKOS_INLINE_FUNCTION
1552 void operator()(
const size_type iter)
const {
1559 const auto r = _inLeft.rank();
1563 if (_is_vecprod_3d) {
1564 _vecProd(0) = _inLeft(1)*_inRight(2) - _inLeft(2)*_inRight(1);
1565 _vecProd(1) = _inLeft(2)*_inRight(0) - _inLeft(0)*_inRight(2);
1566 _vecProd(2) = _inLeft(0)*_inRight(1) - _inLeft(1)*_inRight(0);
1568 _vecProd(0) = _inLeft(0)*_inRight(1) - _inLeft(1)*_inRight(0);
1571 if (_is_vecprod_3d) {
1572 _vecProd(i, 0) = _inLeft(i, 1)*_inRight(i, 2) - _inLeft(i, 2)*_inRight(i, 1);
1573 _vecProd(i, 1) = _inLeft(i, 2)*_inRight(i, 0) - _inLeft(i, 0)*_inRight(i, 2);
1574 _vecProd(i, 2) = _inLeft(i, 0)*_inRight(i, 1) - _inLeft(i, 1)*_inRight(i, 0);
1576 _vecProd(i, 0) = _inLeft(i, 0)*_inRight(i, 1) - _inLeft(i, 1)*_inRight(i, 0);
1579 if (_is_vecprod_3d) {
1580 _vecProd(i, j, 0) = _inLeft(i, j, 1)*_inRight(i, j, 2) - _inLeft(i, j, 2)*_inRight(i, j, 1);
1581 _vecProd(i, j, 1) = _inLeft(i, j, 2)*_inRight(i, j, 0) - _inLeft(i, j, 0)*_inRight(i, j, 2);
1582 _vecProd(i, j, 2) = _inLeft(i, j, 0)*_inRight(i, j, 1) - _inLeft(i, j, 1)*_inRight(i, j, 0);
1584 _vecProd(i, j, 0) = _inLeft(i, j, 0)*_inRight(i, j, 1) - _inLeft(i, j, 1)*_inRight(i, j, 0);
1591 template<
typename DeviceType>
1592 template<
typename vecProdValueType,
class ...vecProdProperties,
1593 typename inLeftValueType,
class ...inLeftProperties,
1594 typename inRightValueType,
class ...inRightProperties>
1597 vecprod( Kokkos::DynRankView<vecProdValueType,vecProdProperties...> vecProd,
1598 const Kokkos::DynRankView<inLeftValueType, inLeftProperties...> inLeft,
1599 const Kokkos::DynRankView<inRightValueType,inRightProperties...> inRight ) {
1601#ifdef HAVE_INTREPID2_DEBUG
1609 INTREPID2_TEST_FOR_EXCEPTION( inLeft.rank() < 1 || inLeft.rank() > 3, std::invalid_argument,
1610 ">>> ERROR (RealSpaceTools::vecprod): Rank of matrix array must be 1, 2, or 3!");
1611 INTREPID2_TEST_FOR_EXCEPTION( inLeft.rank() != inRight.rank(), std::invalid_argument,
1612 ">>> ERROR (RealSpaceTools::vecprod): Right and left arrays must be have the same rank!");
1613 INTREPID2_TEST_FOR_EXCEPTION( inLeft.rank() != vecProd.rank(), std::invalid_argument,
1614 ">>> ERROR (RealSpaceTools::vecprod): Left and vecProd arrays must be have the same rank!");
1615 for (size_type i=0;i<inLeft.rank();++i) {
1616 INTREPID2_TEST_FOR_EXCEPTION( inLeft.extent(i) != inRight.extent(i), std::invalid_argument,
1617 ">>> ERROR (RealSpaceTools::vecprod): Dimensions of matrix arguments do not agree!");
1618 INTREPID2_TEST_FOR_EXCEPTION( inLeft.extent(i) != vecProd.extent(i), std::invalid_argument,
1619 ">>> ERROR (RealSpaceTools::vecprod): Dimensions of matrix arguments do not agree!");
1624 INTREPID2_TEST_FOR_EXCEPTION( inLeft.extent(inLeft.rank()-1) < 2 ||
1625 inLeft.extent(inLeft.rank()-1) > 3, std::invalid_argument,
1626 ">>> ERROR (RealSpaceTools::vecprod): Dimensions of arrays (rank-1) must be 2 or 3!");
1629 using MemSpaceType =
typename DeviceType::memory_space;
1630 constexpr bool are_accessible =
1631 Kokkos::Impl::MemorySpaceAccess<MemSpaceType,
1632 typename decltype(vecProd)::memory_space>::accessible &&
1633 Kokkos::Impl::MemorySpaceAccess<MemSpaceType,
1634 typename decltype(inLeft)::memory_space>::accessible &&
1635 Kokkos::Impl::MemorySpaceAccess<MemSpaceType,
1636 typename decltype(inRight)::memory_space>::accessible;
1637 static_assert(are_accessible,
"RealSpaceTools<DeviceType>::vecprod(..): input/output views' memory spaces are not compatible with DeviceType");
1641 const auto r = inLeft.rank();
1642 const auto loopSize = ( r == 1 ? 1 :
1643 r == 2 ? inLeft.extent(0) :
1644 inLeft.extent(0)*inLeft.extent(1) );
1645 const bool is_vecprod_3d = (inLeft.extent(inLeft.rank() - 1) == 3);
1646 using ExecSpaceType =
typename DeviceType::execution_space;
1647 Kokkos::RangePolicy<ExecSpaceType,Kokkos::Schedule<Kokkos::Static> > policy(0, loopSize);
1648 Kokkos::parallel_for( policy, FunctorType(vecProd, inLeft, inRight, is_vecprod_3d) );
enable_if_t< has_rank_method< Functor >::value, unsigned > KOKKOS_INLINE_FUNCTION getFunctorRank(const Functor &functor)
KOKKOS_FORCEINLINE_FUNCTION constexpr std::enable_if<!(std::is_standard_layout< T >::value &&std::is_trivial< T >::value), typenameScalarTraits< T >::scalar_type >::type get_scalar_value(const T &obj)
functions returning the scalar value. for pod types, they return the input object itself....
#define INTREPID2_TEST_FOR_EXCEPTION_DEVICE_SAFE(test, x, msg)