Debian Codegraph @

packages/trilinoscouplings/examples/fenl/fenl_functors_pce.hpp

raw
1 //@HEADER
2 // ************************************************************************
3 //
4 // Kokkos v. 4.0
5 // Copyright (2022) National Technology & Engineering
6 // Solutions of Sandia, LLC (NTESS).
7 //
8 // Under the terms of Contract DE-NA0003525 with NTESS,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions.
12 // See https://kokkos.org/LICENSE for license information.
13 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
14 //
15 //@HEADER
16
17 #ifndef KOKKOS_EXAMPLE_FENLFUNCTORS_PCE_HPP
18 #define KOKKOS_EXAMPLE_FENLFUNCTORS_PCE_HPP
19
20 #include <fenl_functors.hpp>
21 #include "Kokkos_Parallel_MP_Vector.hpp"
22 #if defined( KOKKOS_ENABLE_CUDA )
23 #include "Stokhos_Cuda_WarpShuffle.hpp"
24 #endif
25
26 //----------------------------------------------------------------------------
27 //----------------------------------------------------------------------------
28
29 namespace Kokkos {
30 namespace Example {
31 namespace FENL {
32
33 // Specialization of ResponseComputation for PCE scalar types. We currently
34 // have to do this because parallel_reduce doesn't support non-POD types
35 template< typename FixtureType ,
36 typename S , typename ... P , typename ... Q >
37 class ResponseComputation< FixtureType,
38 Kokkos::View<Sacado::UQ::PCE<S>*,P...>,
39 Kokkos::View<Sacado::UQ::PCE<S>**,Q...>
40 >
41 {
42 public:
43
44 typedef FixtureType fixture_type ;
45 typedef Kokkos::View<Sacado::UQ::PCE<S>*,P...> vector_type ;
46 typedef Kokkos::View<Sacado::UQ::PCE<S>**,Q...> multi_vector_type ;
47 typedef typename vector_type::execution_space execution_space ;
48 typedef typename vector_type::value_type scalar_type ;
49
50 // Hack to get parallel_reduce to work
51 typedef typename Kokkos::IntrinsicScalarType<vector_type>::type reduce_scalar_type;
52 typedef reduce_scalar_type value_type[] ;
53 typedef typename Kokkos::CijkType<vector_type>::type cijk_type ;
54
55 typedef Kokkos::Example::HexElement_Data< fixture_type::ElemNode > element_data_type ;
56 static const unsigned SpatialDim = element_data_type::spatial_dimension ;
57 static const unsigned TensorDim = SpatialDim * SpatialDim ;
58 static const unsigned ElemNodeCount = element_data_type::element_node_count ;
59 static const unsigned IntegrationCount = element_data_type::integration_count ;
60
61 //------------------------------------
62 // Computational data:
63
64 const element_data_type elem_data ;
65 const fixture_type fixture ;
66 const vector_type solution ;
67 const multi_vector_type solution_dp ;
68 const unsigned pce_size ;
69 const unsigned num_param ;
70 const unsigned value_count ;
71 const cijk_type cijk ;
72
73 ResponseComputation( const ResponseComputation & rhs )
74 : elem_data()
75 , fixture( rhs.fixture )
76 , solution( rhs.solution )
77 , solution_dp( rhs.solution_dp )
78 , pce_size( rhs.pce_size )
79 , num_param( rhs.num_param )
80 , value_count( rhs.value_count )
81 , cijk( rhs.cijk )
82 {}
83
84 ResponseComputation( const fixture_type& arg_fixture ,
85 const vector_type & arg_solution ,
86 const multi_vector_type& arg_solution_dp =
87 multi_vector_type() )
88 : elem_data()
89 , fixture( arg_fixture )
90 , solution( arg_solution )
91 , solution_dp( arg_solution_dp )
92 , pce_size( Kokkos::dimension_scalar(solution) )
93 , num_param( solution_dp.extent(1) )
94 , value_count( pce_size*(num_param+1) )
95 , cijk( Kokkos::cijk(solution) )
96 {
97 TEUCHOS_TEST_FOR_EXCEPTION(
98 num_param > 0, std::logic_error, "PCE gradients not implemented!");
99 }
100
101 //------------------------------------
102
103 scalar_type apply() const
104 {
105 scalar_type response(cijk, value_count);
106 //Kokkos::parallel_reduce( fixture.elem_count() , *this , response.coeff() );
107 Kokkos::View<reduce_scalar_type*, Kokkos::HostSpace> resp_view(response.coeff(), value_count);
108 Kokkos::parallel_reduce( solution.extent(0) , *this , resp_view );
109 return response;
110 }
111
112 Teuchos::Array<scalar_type> apply_gradient() const
113 {
114 TEUCHOS_TEST_FOR_EXCEPTION(
115 true, std::logic_error, "PCE gradients not implemented!");
116 }
117
118 //------------------------------------
119
120 /*
121 KOKKOS_INLINE_FUNCTION
122 double compute_detJ(
123 const double grad[][ ElemNodeCount ] , // Gradient of bases master element
124 const double x[] ,
125 const double y[] ,
126 const double z[] ) const
127 {
128 enum { j11 = 0 , j12 = 1 , j13 = 2 ,
129 j21 = 3 , j22 = 4 , j23 = 5 ,
130 j31 = 6 , j32 = 7 , j33 = 8 };
131
132 // Jacobian accumulation:
133
134 double J[ TensorDim ] = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
135
136 for( unsigned i = 0; i < ElemNodeCount ; ++i ) {
137 const double x1 = x[i] ;
138 const double x2 = y[i] ;
139 const double x3 = z[i] ;
140
141 const double g1 = grad[0][i] ;
142 const double g2 = grad[1][i] ;
143 const double g3 = grad[2][i] ;
144
145 J[j11] += g1 * x1 ;
146 J[j12] += g1 * x2 ;
147 J[j13] += g1 * x3 ;
148
149 J[j21] += g2 * x1 ;
150 J[j22] += g2 * x2 ;
151 J[j23] += g2 * x3 ;
152
153 J[j31] += g3 * x1 ;
154 J[j32] += g3 * x2 ;
155 J[j33] += g3 * x3 ;
156 }
157
158 // Inverse jacobian:
159
160 double invJ[ TensorDim ] = {
161 static_cast<double>( J[j22] * J[j33] - J[j23] * J[j32] ) ,
162 static_cast<double>( J[j13] * J[j32] - J[j12] * J[j33] ) ,
163 static_cast<double>( J[j12] * J[j23] - J[j13] * J[j22] ) ,
164
165 static_cast<double>( J[j23] * J[j31] - J[j21] * J[j33] ) ,
166 static_cast<double>( J[j11] * J[j33] - J[j13] * J[j31] ) ,
167 static_cast<double>( J[j13] * J[j21] - J[j11] * J[j23] ) ,
168
169 static_cast<double>( J[j21] * J[j32] - J[j22] * J[j31] ) ,
170 static_cast<double>( J[j12] * J[j31] - J[j11] * J[j32] ) ,
171 static_cast<double>( J[j11] * J[j22] - J[j12] * J[j21] ) };
172
173 const double detJ = J[j11] * invJ[j11] +
174 J[j21] * invJ[j12] +
175 J[j31] * invJ[j13] ;
176
177 return detJ ;
178 }
179
180 KOKKOS_INLINE_FUNCTION
181 scalar_type contributeResponse(
182 const scalar_type dof_values[] ,
183 const double detJ ,
184 const double integ_weight ,
185 const double bases_vals[] ) const
186 {
187 // $$ g_i = \int_{\Omega} T^2 d \Omega $$
188
189 scalar_type value_at_pt = 0 ;
190 for ( unsigned m = 0 ; m < ElemNodeCount ; m++ ) {
191 value_at_pt += dof_values[m] * bases_vals[m] ;
192 }
193
194 scalar_type elem_response =
195 value_at_pt * value_at_pt * detJ * integ_weight ;
196
197 return elem_response;
198 }
199
200 KOKKOS_INLINE_FUNCTION
201 void operator()( const unsigned ielem , value_type response ) const
202 {
203 // Gather nodal coordinates and solution vector:
204
205 double x[ ElemNodeCount ] ;
206 double y[ ElemNodeCount ] ;
207 double z[ ElemNodeCount ] ;
208 scalar_type val[ ElemNodeCount ] ;
209
210 for ( unsigned i = 0 ; i < ElemNodeCount ; ++i ) {
211 const unsigned ni = fixture.elem_node( ielem , i );
212
213 x[i] = fixture.node_coord( ni , 0 );
214 y[i] = fixture.node_coord( ni , 1 );
215 z[i] = fixture.node_coord( ni , 2 );
216
217 val[i] = solution( ni );
218 }
219
220 scalar_type response_pce( cijk, value_count, response, false );
221
222 for ( unsigned i = 0 ; i < IntegrationCount ; ++i ) {
223
224 const double detJ = compute_detJ( elem_data.gradients[i] , x , y , z );
225
226 response_pce += contributeResponse( val , detJ , elem_data.weights[i] ,
227 elem_data.values[i] );
228 }
229 }
230 */
231
232 KOKKOS_INLINE_FUNCTION
233 void operator()( const unsigned i , value_type response ) const
234 {
235 const scalar_type& u = solution(i);
236 scalar_type response_pce( cijk, value_count, response, false );
237 response_pce += (u * u) / fixture.node_count_global();
238 }
239
240 KOKKOS_INLINE_FUNCTION
241 void init( value_type response ) const
242 {
243 for (unsigned i=0; i<value_count; ++i)
244 response[i] = 0 ;
245 }
246
247 KOKKOS_INLINE_FUNCTION
248 void join( value_type response ,
249 const value_type input ) const
250 {
251 for (unsigned i=0; i<value_count; ++i)
252 response[i] += input[i] ;
253 }
254
255 }; /* ResponseComputation */
256
257 //------------------------------------
258
259 template < typename pce_view_type,
260 typename scalar_view_type,
261 typename quad_values_type,
262 typename ensemble_scalar_type,
263 int EnsembleSize,
264 typename Device = typename pce_view_type::execution_space >
265 struct EvaluatePCE {
266 typedef Device execution_space;
267 typedef typename pce_view_type::array_type pce_array_type;
268
269 const pce_array_type pce_view;
270 const scalar_view_type scalar_view;
271 const quad_values_type quad_values;
272 unsigned qp;
273 const unsigned num_pce;
274
275 EvaluatePCE( const pce_view_type& arg_pce_view,
276 const scalar_view_type& arg_scalar_view,
277 const quad_values_type& arg_quad_values) :
278 pce_view( arg_pce_view ),
279 scalar_view( arg_scalar_view ),
280 quad_values( arg_quad_values ),
281 qp( 0 ),
282 num_pce( Kokkos::dimension_scalar(arg_pce_view) ) {}
283
284 void apply(const unsigned arg_qp) {
285 qp = arg_qp;
286 Kokkos::parallel_for( pce_view.extent(1), *this );
287 }
288
289 KOKKOS_INLINE_FUNCTION
290 void operator() ( const unsigned row ) const {
291 ensemble_scalar_type s = 0.0;
292 for (unsigned pce=0; pce<num_pce; ++pce)
293 for (int i=0; i<EnsembleSize; ++i)
294 s.fastAccessCoeff(i) += pce_view(pce,row)*quad_values(qp+i,pce);
295 scalar_view(row) = s;
296 }
297 };
298
299 template < typename pce_view_type,
300 typename scalar_view_type,
301 typename quad_values_type,
302 typename quad_weights_type,
303 int EnsembleSize,
304 typename Device = typename pce_view_type::execution_space >
305 struct AssemblePCE {
306 typedef Device execution_space;
307 typedef typename pce_view_type::array_type pce_array_type;
308
309 const pce_array_type pce_view;
310 const scalar_view_type scalar_view;
311 const quad_values_type quad_values;
312 const quad_weights_type quad_weights;
313 unsigned qp;
314 const unsigned num_pce;
315
316 AssemblePCE( const pce_view_type& arg_pce_view,
317 const scalar_view_type& arg_scalar_view,
318 const quad_values_type& arg_quad_values,
319 const quad_weights_type& arg_quad_weights ) :
320 pce_view( arg_pce_view ),
321 scalar_view( arg_scalar_view ),
322 quad_values( arg_quad_values ),
323 quad_weights( arg_quad_weights ),
324 qp( 0 ),
325 num_pce( Kokkos::dimension_scalar(arg_pce_view) ) {}
326
327 void apply( const unsigned arg_qp ) {
328 qp = arg_qp;
329 Kokkos::parallel_for( pce_view.extent(1), *this );
330 }
331
332 KOKKOS_INLINE_FUNCTION
333 void operator() ( const unsigned row ) const {
334 for (unsigned pce=0; pce<num_pce; ++pce) {
335 for (int i=0; i<EnsembleSize; ++i)
336 pce_view(pce,row) +=
337 quad_weights(qp+i)*scalar_view(row).fastAccessCoeff(i)*quad_values(qp+i,pce);
338 }
339 }
340 };
341
342 template < typename pce_view_type,
343 typename scalar_view_type,
344 typename quad_values_type,
345 typename quad_weights_type,
346 int EnsembleSize,
347 typename Device = typename pce_view_type::execution_space >
348 struct AssembleRightPCE {
349 typedef Device execution_space;
350 typedef typename pce_view_type::array_type pce_array_type;
351
352 const pce_array_type pce_view;
353 const scalar_view_type scalar_view;
354 const quad_values_type quad_values;
355 const quad_weights_type quad_weights;
356 unsigned qp;
357 const unsigned num_pce;
358
359 AssembleRightPCE( const pce_view_type& arg_pce_view,
360 const scalar_view_type& arg_scalar_view,
361 const quad_values_type& arg_quad_values,
362 const quad_weights_type& arg_quad_weights ) :
363 pce_view( arg_pce_view ),
364 scalar_view( arg_scalar_view ),
365 quad_values( arg_quad_values ),
366 quad_weights( arg_quad_weights ),
367 qp( 0 ),
368 num_pce( Kokkos::dimension_scalar(arg_pce_view) ) {}
369
370 void apply( const unsigned arg_qp ) {
371 qp = arg_qp;
372 Kokkos::parallel_for( pce_view.extent(0), *this );
373 }
374
375 KOKKOS_INLINE_FUNCTION
376 void operator() ( const unsigned row ) const {
377 for (unsigned pce=0; pce<num_pce; ++pce) {
378 for (int i=0; i<EnsembleSize; ++i)
379 pce_view(row,pce) +=
380 quad_weights(qp+i)*scalar_view(row).fastAccessCoeff(i)*quad_values(qp+i,pce);
381 }
382 }
383 };
384
385 #if defined( KOKKOS_ENABLE_CUDA ) && defined( __CUDACC__ )
386 template < typename pce_view_type,
387 typename scalar_view_type,
388 typename quad_values_type,
389 typename ensemble_scalar_type,
390 int EnsembleSize >
391 struct EvaluatePCE< pce_view_type,
392 scalar_view_type,
393 quad_values_type,
394 ensemble_scalar_type,
395 EnsembleSize,
396 Kokkos::Cuda > {
397 typedef Kokkos::Cuda execution_space;
398 typedef typename pce_view_type::array_type pce_array_type;
399 typedef typename pce_array_type::value_type scalar_type;
400 typedef typename quad_values_type::value_type quad_scalar_type;
401
402 const pce_array_type pce_view;
403 const scalar_view_type scalar_view;
404 const quad_values_type quad_values;
405 unsigned qp;
406 const unsigned num_pce;
407 const unsigned row_count;
408
409 EvaluatePCE( const pce_view_type& arg_pce_view,
410 const scalar_view_type& arg_scalar_view,
411 const quad_values_type& arg_quad_values) :
412 pce_view( arg_pce_view ),
413 scalar_view( arg_scalar_view ),
414 quad_values( arg_quad_values ),
415 qp( 0 ),
416 num_pce( Kokkos::dimension_scalar(arg_pce_view) ),
417 row_count( pce_view.extent(1) ) {}
418
419 void apply(const unsigned arg_qp) {
420 qp = arg_qp;
421 Kokkos::parallel_for( Kokkos::MPVectorWorkConfig<execution_space>( row_count,
422 EnsembleSize ),
423 *this );
424 }
425
426 KOKKOS_INLINE_FUNCTION
427 void operator() ( const unsigned row, const unsigned tid ) const {
428 scalar_type s = 0.0;
429 for (unsigned pce=0; pce<num_pce; ++pce) {
430 s += pce_view(pce,row)*quad_values(qp+tid,pce);
431 }
432 scalar_view(row).fastAccessCoeff(tid) = s;
433 }
434
435 /*
436 void apply(const unsigned arg_qp) {
437 qp = arg_qp;
438 const unsigned threads_per_vector = EnsembleSize;
439 const unsigned rows_per_block = 6;
440 const unsigned num_blocks = (row_count+rows_per_block-1)/rows_per_block;
441 const unsigned shared = EnsembleSize*EnsembleSize*sizeof(quad_scalar_type);
442 const dim3 block( threads_per_vector, rows_per_block, 1 );
443 const dim3 grid( num_blocks, 1 );
444
445 Kokkos::Impl::cuda_parallel_launch_local_memory<<< grid, block, shared >>>
446 ( *this );
447 Kokkos::parallel_for( Kokkos::MPVectorWorkConfig<execution_space>( row_count,
448 EnsembleSize ),
449 *this );
450 }
451
452 __device__ inline
453 void operator() () const {
454 __shared__ volatile quad_scalar_type sh_qv[EnsembleSize][EnsembleSize];
455
456 const unsigned row = blockIdx.x * blockDim.y + threadIdx.y;
457 if ( row < row_count ) {
458 scalar_type s = 0.0;
459 for (unsigned pce_block=0; pce_block<num_pce; pce_block+=EnsembleSize) {
460 const unsigned block_size = pce_block+EnsembleSize<num_pce ?
461 EnsembleSize : num_pce-pce_block;
462 for (unsigned j=threadIdx.y; j<block_size; j+=blockDim.y) {
463 sh_qv[j][threadIdx.x] = quad_values(qp+threadIdx.x,pce_block+j);
464 }
465 __syncthreads();
466 for (unsigned j=0; j<block_size; ++j) {
467 s += pce_view(pce_block+j,row)*sh_qv[j][threadIdx.x];
468 }
469 }
470 scalar_view(row).fastAccessCoeff(threadIdx.x) = s;
471 }
472 }
473 */
474 };
475
476 template < typename pce_view_type,
477 typename scalar_view_type,
478 typename quad_values_type,
479 typename quad_weights_type,
480 int EnsembleSize >
481 struct AssemblePCE< pce_view_type,
482 scalar_view_type,
483 quad_values_type,
484 quad_weights_type,
485 EnsembleSize,
486 Kokkos::Cuda > {
487 typedef Kokkos::Cuda execution_space;
488 typedef typename pce_view_type::array_type pce_array_type;
489 typedef typename pce_array_type::value_type scalar_type;
490 typedef typename quad_weights_type::value_type weights_scalar_type;
491
492 const pce_array_type pce_view;
493 const scalar_view_type scalar_view;
494 const quad_values_type quad_values;
495 const quad_weights_type quad_weights;
496 unsigned qp;
497 const unsigned num_pce;
498
499 AssemblePCE( const pce_view_type& arg_pce_view,
500 const scalar_view_type& arg_scalar_view,
501 const quad_values_type& arg_quad_values,
502 const quad_weights_type& arg_quad_weights ) :
503 pce_view( arg_pce_view ),
504 scalar_view( arg_scalar_view ),
505 quad_values( arg_quad_values ),
506 quad_weights( arg_quad_weights ),
507 qp( 0 ),
508 num_pce( Kokkos::dimension_scalar(arg_pce_view) ) {}
509
510 void apply(const unsigned arg_qp) {
511 qp = arg_qp;
512 Kokkos::parallel_for( Kokkos::MPVectorWorkConfig<execution_space>( pce_view.extent(1),
513 EnsembleSize ),
514 *this );
515 }
516
517 __device__ inline
518 void operator() ( const unsigned row, const unsigned tid ) const {
519 const weights_scalar_type w = quad_weights(qp+tid);
520 const scalar_type v = w*scalar_view(row).fastAccessCoeff(tid);
521 for (unsigned pce=0; pce<num_pce; ++pce) {
522 scalar_type s = v*quad_values(qp+tid,pce);
523 if (EnsembleSize >= 2) s += Stokhos::shfl_down(s, 1, EnsembleSize);
524 if (EnsembleSize >= 4) s += Stokhos::shfl_down(s, 2, EnsembleSize);
525 if (EnsembleSize >= 8) s += Stokhos::shfl_down(s, 4, EnsembleSize);
526 if (EnsembleSize >= 16) s += Stokhos::shfl_down(s, 8, EnsembleSize);
527 if (EnsembleSize >= 32) s += Stokhos::shfl_down(s, 16, EnsembleSize);
528 if (tid == 0) pce_view(pce,row) += s;
529 }
530 }
531 };
532
533 template < typename pce_view_type,
534 typename scalar_view_type,
535 typename quad_values_type,
536 typename quad_weights_type,
537 int EnsembleSize >
538 struct AssembleRightPCE< pce_view_type,
539 scalar_view_type,
540 quad_values_type,
541 quad_weights_type,
542 EnsembleSize,
543 Kokkos::Cuda > {
544 typedef Kokkos::Cuda execution_space;
545 typedef typename pce_view_type::array_type pce_array_type;
546 typedef typename pce_array_type::value_type scalar_type;
547 typedef typename quad_weights_type::value_type weights_scalar_type;
548 typedef typename quad_values_type::value_type quad_scalar_type;
549
550 static const unsigned threads_per_vector = EnsembleSize;
551 static const unsigned rows_per_block = 256 / EnsembleSize;
552
553 const pce_array_type pce_view;
554 const scalar_view_type scalar_view;
555 const quad_values_type quad_values;
556 const quad_weights_type quad_weights;
557 unsigned qp;
558 const unsigned num_pce;
559 const unsigned row_count;
560
561 AssembleRightPCE( const pce_view_type& arg_pce_view,
562 const scalar_view_type& arg_scalar_view,
563 const quad_values_type& arg_quad_values,
564 const quad_weights_type& arg_quad_weights ) :
565 pce_view( arg_pce_view ),
566 scalar_view( arg_scalar_view ),
567 quad_values( arg_quad_values ),
568 quad_weights( arg_quad_weights ),
569 qp( 0 ),
570 num_pce( Kokkos::dimension_scalar(arg_pce_view) ),
571 row_count( pce_view.extent(0) ) {}
572
573 void apply(const unsigned arg_qp) {
574 qp = arg_qp;
575 Kokkos::parallel_for( Kokkos::MPVectorWorkConfig<execution_space>( row_count,
576 EnsembleSize ),
577 *this );
578 }
579
580 __device__ inline
581 void operator() ( const unsigned row, const unsigned tid ) const {
582 const weights_scalar_type w = quad_weights(qp+tid);
583 const scalar_type v = w*scalar_view(row).fastAccessCoeff(tid);
584 for (unsigned pce=0; pce<num_pce; ++pce) {
585 scalar_type s = v*quad_values(qp+tid,pce);
586 if (EnsembleSize >= 2) s += Stokhos::shfl_down(s, 1, EnsembleSize);
587 if (EnsembleSize >= 4) s += Stokhos::shfl_down(s, 2, EnsembleSize);
588 if (EnsembleSize >= 8) s += Stokhos::shfl_down(s, 4, EnsembleSize);
589 if (EnsembleSize >= 16) s += Stokhos::shfl_down(s, 8, EnsembleSize);
590 if (EnsembleSize >= 32) s += Stokhos::shfl_down(s, 16, EnsembleSize);
591 if (tid == 0) pce_view(row,pce) += s;
592 }
593 }
594
595 /*
596 void apply(const unsigned arg_qp) {
597 qp = arg_qp;
598 const unsigned num_blocks = (row_count+rows_per_block-1)/rows_per_block;
599 const unsigned shared = 0;
600 const dim3 block( threads_per_vector, rows_per_block, 1 );
601 const dim3 grid( num_blocks, 1 );
602
603 Kokkos::Impl::cuda_parallel_launch_local_memory<<< grid, block, shared >>>
604 ( *this );
605 }
606
607 __device__ inline
608 void operator() () const {
609 __shared__ volatile quad_scalar_type sh_qv[rows_per_block][EnsembleSize];
610
611 const unsigned row = blockIdx.x * blockDim.y + threadIdx.y;
612 if ( row < row_count ) {
613 const weights_scalar_type w = quad_weights(qp+threadIdx.x);
614 const scalar_type v = w*scalar_view(row).fastAccessCoeff(threadIdx.x);
615 for (unsigned pce_block=0; pce_block<num_pce; pce_block+=rows_per_block) {
616 const unsigned block_size = pce_block+rows_per_block<num_pce ?
617 rows_per_block : num_pce-pce_block;
618 if (threadIdx.y < block_size)
619 sh_qv[threadIdx.y][threadIdx.x] =
620 quad_values(qp+threadIdx.x,pce_block+threadIdx.y);
621 __syncthreads();
622 for (unsigned j=0; j<block_size; ++j) {
623 scalar_type s = v*sh_qv[j][threadIdx.x];
624 if (EnsembleSize >= 2) s += Stokhos::shfl_down(s, 1, EnsembleSize);
625 if (EnsembleSize >= 4) s += Stokhos::shfl_down(s, 2, EnsembleSize);
626 if (EnsembleSize >= 8) s += Stokhos::shfl_down(s, 4, EnsembleSize);
627 if (EnsembleSize >= 16) s += Stokhos::shfl_down(s, 8, EnsembleSize);
628 if (EnsembleSize >= 32) s += Stokhos::shfl_down(s, 16, EnsembleSize);
629 if (threadIdx.x == 0) pce_view(row,pce_block+j) += s;
630 }
631 }
632 }
633 }
634 */
635 };
636 #endif
637
638 template< typename ExecutionSpace ,
639 BoxElemPart::ElemOrder Order ,
640 typename CoordinateMap ,
641 typename StorageType ,
642 typename OrdinalType ,
643 typename MemoryTraits ,
644 typename SizeType ,
645 class CoeffFunctionType>
646 class ElementComputation<
647 Kokkos::Example::BoxElemFixture< ExecutionSpace , Order , CoordinateMap >,
648 KokkosSparse::CrsMatrix< Sacado::UQ::PCE<StorageType> , OrdinalType , Kokkos::Device<ExecutionSpace, typename ExecutionSpace::memory_space> , MemoryTraits , SizeType >,
649 CoeffFunctionType >
650 {
651 public:
652
653 typedef Kokkos::Example::BoxElemFixture< ExecutionSpace, Order, CoordinateMap > mesh_type ;
654 typedef Kokkos::Example::HexElement_Data< mesh_type::ElemNode > element_data_type ;
655 typedef Sacado::UQ::PCE<StorageType> ScalarType;
656
657 //------------------------------------
658
659 typedef ExecutionSpace execution_space ;
660 typedef Kokkos::Device<execution_space, typename execution_space::memory_space> DeviceType;
661 typedef ScalarType scalar_type ;
662
663 typedef KokkosSparse::CrsMatrix< ScalarType , OrdinalType , DeviceType , MemoryTraits , SizeType > sparse_matrix_type ;
664 typedef typename sparse_matrix_type::StaticCrsGraphType sparse_graph_type ;
665 typedef typename sparse_matrix_type::values_type matrix_values_type ;
666 typedef Kokkos::View< scalar_type* , Kokkos::LayoutLeft, execution_space > vector_type ;
667
668 //------------------------------------
669
670 typedef typename scalar_type::value_type scalar_value_type;
671 typedef typename scalar_type::ordinal_type ordinal_type;
672 static const int EnsembleSize = 32;
673 typedef Stokhos::StaticFixedStorage<ordinal_type,scalar_value_type,EnsembleSize,ExecutionSpace> ensemble_storage_type;
674 typedef Sacado::MP::Vector<ensemble_storage_type> ensemble_scalar_type;
675 typedef typename Sacado::mpl::apply<CoeffFunctionType, ensemble_scalar_type>::type scalar_coeff_function_type;
676 typedef ElementComputation<
677 Kokkos::Example::BoxElemFixture< ExecutionSpace , Order , CoordinateMap >,
678 KokkosSparse::CrsMatrix< ensemble_scalar_type , OrdinalType , DeviceType , MemoryTraits , SizeType >,
679 scalar_coeff_function_type > scalar_element_computation_type;
680 typedef typename scalar_element_computation_type::sparse_matrix_type scalar_sparse_matrix_type ;
681 typedef typename scalar_sparse_matrix_type::values_type scalar_matrix_values_type ;
682 typedef typename scalar_element_computation_type::vector_type scalar_vector_type ;
683 typedef typename scalar_element_computation_type::elem_graph_type elem_graph_type;
684
685 //------------------------------------
686 // Computational data:
687
688 const vector_type solution ;
689 const vector_type residual ;
690 const sparse_matrix_type jacobian ;
691
692 const scalar_vector_type scalar_solution ;
693 const scalar_vector_type scalar_residual ;
694 const scalar_sparse_matrix_type scalar_jacobian ;
695 const scalar_coeff_function_type scalar_diffusion_coefficient;
696 const scalar_element_computation_type scalar_element_computation ;
697
698 typedef QuadratureData<ExecutionSpace> QD;
699 typename QD::quad_weights_type quad_weights;
700 typename QD::quad_values_type quad_points;
701 typename QD::quad_values_type quad_values;
702
703 ElementComputation( const ElementComputation & rhs )
704 : solution( rhs.solution )
705 , residual( rhs.residual )
706 , jacobian( rhs.jacobian )
707 , scalar_solution( rhs.scalar_solution )
708 , scalar_residual( rhs.scalar_residual )
709 , scalar_jacobian( rhs.scalar_jacobian )
710 , scalar_diffusion_coefficient( rhs.scalar_diffusion_coefficient )
711 , scalar_element_computation( rhs.scalar_element_computation )
712 , quad_weights( rhs.quad_weights )
713 , quad_points( rhs.quad_points )
714 , quad_values( rhs.quad_values )
715 {}
716
717 // If the element->sparse_matrix graph is provided then perform atomic updates
718 // Otherwise fill per-element contributions for subequent gather-add into a residual and jacobian.
719 ElementComputation( const mesh_type & arg_mesh ,
720 const CoeffFunctionType & arg_coeff_function ,
721 const bool arg_isotropic ,
722 const double & arg_coeff_source ,
723 const double & arg_coeff_advection ,
724 const vector_type & arg_solution ,
725 const elem_graph_type & arg_elem_graph ,
726 const sparse_matrix_type & arg_jacobian ,
727 const vector_type & arg_residual ,
728 const KokkosSparse::DeviceConfig arg_dev_config ,
729 const QD& qd )
730 : solution( arg_solution )
731 , residual( arg_residual )
732 , jacobian( arg_jacobian )
733 , scalar_solution( "scalar_solution", solution.extent(0) )
734 , scalar_residual( "scalar_residual", residual.extent(0) )
735 , scalar_jacobian( "scalar_jacobian", jacobian.graph, maximum_entry(jacobian.graph) + 1 )
736 , scalar_diffusion_coefficient( arg_coeff_function.m_mean,
737 arg_coeff_function.m_variance,
738 arg_coeff_function.m_corr_len,
739 arg_coeff_function.m_num_rv,
740 arg_coeff_function.m_use_exp,
741 arg_coeff_function.m_exp_shift,
742 arg_coeff_function.m_exp_scale,
743 arg_coeff_function.m_use_disc_exp_scale )
744 , scalar_element_computation( arg_mesh,
745 scalar_diffusion_coefficient,
746 arg_isotropic,
747 arg_coeff_source,
748 arg_coeff_advection,
749 scalar_solution,
750 arg_elem_graph,
751 scalar_jacobian,
752 scalar_residual,
753 arg_dev_config )
754 , quad_weights( qd.weights_view )
755 , quad_points( qd.points_view )
756 , quad_values( qd.values_view )
757 {
758 // Set global vector size -- this is mandatory
759 Kokkos::global_sacado_mp_vector_size = EnsembleSize;
760 }
761
762 //------------------------------------
763
764 void apply() const
765 {
766 typedef EvaluatePCE< vector_type, scalar_vector_type, typename QD::quad_values_type, ensemble_scalar_type, EnsembleSize> evaluate_solution_type;
767 typedef AssemblePCE< vector_type, scalar_vector_type, typename QD::quad_values_type, typename QD::quad_weights_type, EnsembleSize> assemble_residual_type;
768 typedef AssembleRightPCE< matrix_values_type, scalar_matrix_values_type, typename QD::quad_values_type, typename QD::quad_weights_type, EnsembleSize> assemble_jacobian_type;
769
770 typedef scalar_coeff_function_type KL;
771 typedef typename KL::RandomVariableView RV;
772 typedef typename RV::HostMirror HRV;
773 RV rv = scalar_diffusion_coefficient.getRandomVariables();
774 HRV hrv = Kokkos::create_mirror_view(rv);
775 auto hqp = Kokkos::create_mirror_view(quad_points);
776 Kokkos::deep_copy(hqp, quad_points);
777
778 // Note: num_quad_points is aligned to the ensemble size to make
779 // things easier
780 const unsigned num_quad_points = quad_points.extent(0);
781 const unsigned dim = quad_points.extent(1);
782
783 evaluate_solution_type evaluate_pce(solution, scalar_solution, quad_values);
784 assemble_residual_type assemble_res(residual, scalar_residual,
785 quad_values, quad_weights);
786 assemble_jacobian_type assemble_jac(jacobian.values, scalar_jacobian.values,
787 quad_values, quad_weights);
788
789 for (unsigned qp=0; qp<num_quad_points; qp+=EnsembleSize) {
790 // Zero out residual, and Jacobian
791 Kokkos::deep_copy( scalar_residual, 0.0 );
792 Kokkos::deep_copy( scalar_jacobian.values, 0.0 );
793
794 // Evaluate PCE solution at quadrature point
795 evaluate_pce.apply(qp);
796
797 // Set quadrature point in diffusion coefficient
798 for (unsigned i=0; i<dim; ++i)
799 for (unsigned j=0; j< unsigned(EnsembleSize); ++j)
800 hrv(i).fastAccessCoeff(j) = hqp(qp+j,i);
801 Kokkos::deep_copy( rv, hrv );
802
803 // Compute element residual/Jacobian at quadrature point
804 scalar_element_computation.apply();
805
806 // Assemble element residual/Jacobian into PCE residual/Jacobian
807 assemble_res.apply(qp);
808 assemble_jac.apply(qp);
809 }
810 }
811
812 //------------------------------------
813 }; /* ElementComputation */
814
815 } /* namespace FENL */
816 } /* namespace Example */
817 } /* namespace Kokkos */
818
819 //----------------------------------------------------------------------------
820 //----------------------------------------------------------------------------
821
822 namespace Kokkos {
823 namespace Example {
824
825 template <typename Storage>
826 inline
827 Sacado::UQ::PCE<Storage>
828 all_reduce( const Sacado::UQ::PCE<Storage>& local ,
829 const Teuchos::RCP<const Teuchos::Comm<int> >& comm )
830 {
831 const int sz = local.size();
832 Sacado::UQ::PCE<Storage> global(local.cijk(), sz) ;
833 Teuchos::reduceAll(
834 *comm , Teuchos::REDUCE_SUM , sz , local.coeff() , global.coeff() );
835 return global ;
836 }
837
838 } // namespace Example
839 } // namespace Kokkos
840
841 #endif /* #ifndef KOKKOS_EXAMPLE_FENLFUNCTORS_PCE_HPP */