| 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_HPP |
| 18 |
#define KOKKOS_EXAMPLE_FENLFUNCTORS_HPP |
| 19 |
|
| 20 |
#include <stdio.h> |
| 21 |
|
| 22 |
#include <iostream> |
| 23 |
#include <fstream> |
| 24 |
#include <iomanip> |
| 25 |
#include <cstdlib> |
| 26 |
#include <cmath> |
| 27 |
#include <limits> |
| 28 |
|
| 29 |
#include <Kokkos_Pair.hpp> |
| 30 |
#include <Kokkos_UnorderedMap.hpp> |
| 31 |
|
| 32 |
#include <Kokkos_Timer.hpp> |
| 33 |
|
| 34 |
#include <fenl.hpp> |
| 35 |
#include <BoxElemFixture.hpp> |
| 36 |
#include <HexElement.hpp> |
| 37 |
|
| 38 |
//---------------------------------------------------------------------------- |
| 39 |
//---------------------------------------------------------------------------- |
| 40 |
|
| 41 |
namespace Kokkos { |
| 42 |
namespace Example { |
| 43 |
namespace FENL { |
| 44 |
|
| 45 |
template< class ElemNodeIdView , class CrsGraphType , unsigned ElemNode > |
| 46 |
class NodeNodeGraph { |
| 47 |
public: |
| 48 |
|
| 49 |
typedef typename ElemNodeIdView::execution_space execution_space ; |
| 50 |
typedef pair<unsigned,unsigned> key_type ; |
| 51 |
|
| 52 |
typedef Kokkos::UnorderedMap< key_type, void , execution_space > SetType ; |
| 53 |
typedef typename CrsGraphType::row_map_type::non_const_type RowMapType ; |
| 54 |
typedef Kokkos::View< unsigned , execution_space > UnsignedValue ; |
| 55 |
|
| 56 |
// Static dimensions of 0 generate compiler warnings or errors. |
| 57 |
typedef Kokkos::View< unsigned*[ElemNode][ElemNode] , execution_space > |
| 58 |
ElemGraphType ; |
| 59 |
|
| 60 |
struct TagFillNodeSet {}; |
| 61 |
|
| 62 |
private: |
| 63 |
|
| 64 |
enum PhaseType { PHASE_NONE , |
| 65 |
SCAN_NODE_COUNT , |
| 66 |
FILL_GRAPH_ENTRIES , |
| 67 |
SORT_GRAPH_ENTRIES , |
| 68 |
FILL_ELEMENT_GRAPH }; |
| 69 |
|
| 70 |
const unsigned node_count ; |
| 71 |
const ElemNodeIdView elem_node_id ; |
| 72 |
UnsignedValue row_total ; |
| 73 |
RowMapType row_count ; |
| 74 |
RowMapType row_map ; |
| 75 |
SetType node_node_set ; |
| 76 |
PhaseType phase ; |
| 77 |
|
| 78 |
public: |
| 79 |
|
| 80 |
CrsGraphType graph ; |
| 81 |
ElemGraphType elem_graph ; |
| 82 |
|
| 83 |
struct Times |
| 84 |
{ |
| 85 |
double ratio; |
| 86 |
double fill_node_set; |
| 87 |
double scan_node_count; |
| 88 |
double fill_graph_entries; |
| 89 |
double sort_graph_entries; |
| 90 |
double fill_element_graph; |
| 91 |
}; |
| 92 |
|
| 93 |
NodeNodeGraph( const ElemNodeIdView & arg_elem_node_id , |
| 94 |
const unsigned arg_node_count, |
| 95 |
Times & results |
| 96 |
) |
| 97 |
: node_count(arg_node_count) |
| 98 |
, elem_node_id( arg_elem_node_id ) |
| 99 |
, row_total( "row_total" ) |
| 100 |
, row_count(Kokkos::ViewAllocateWithoutInitializing("row_count"), node_count ) // will deep_copy to 0 inside loop |
| 101 |
, row_map( "graph_row_map" , node_count + 1 ) |
| 102 |
, node_node_set() |
| 103 |
, phase( PHASE_NONE ) |
| 104 |
, graph() |
| 105 |
, elem_graph() |
| 106 |
{ |
| 107 |
//-------------------------------- |
| 108 |
// Guess at capacity required for the map: |
| 109 |
|
| 110 |
Kokkos::Timer wall_clock ; |
| 111 |
|
| 112 |
wall_clock.reset(); |
| 113 |
|
| 114 |
// upper bound on the capacity |
| 115 |
size_t set_capacity = (((28ull * node_count) / 2ull)*4ull)/3ull; |
| 116 |
unsigned failed_insert_count = 0 ; |
| 117 |
|
| 118 |
// Increase capacity until the (node,node) map is successfully filled. |
| 119 |
do { |
| 120 |
// Zero the row count to restart the fill |
| 121 |
Kokkos::deep_copy( row_count , 0u ); |
| 122 |
|
| 123 |
node_node_set = SetType( ( set_capacity += failed_insert_count ) ); |
| 124 |
|
| 125 |
// May be larger that requested: |
| 126 |
set_capacity = node_node_set.capacity(); |
| 127 |
|
| 128 |
Kokkos::parallel_reduce( Kokkos::RangePolicy<execution_space,TagFillNodeSet>(0,elem_node_id.extent(0)) |
| 129 |
, *this |
| 130 |
, failed_insert_count ); |
| 131 |
|
| 132 |
} while ( failed_insert_count ); |
| 133 |
|
| 134 |
execution_space().fence(); |
| 135 |
results.ratio = (double)node_node_set.size() / (double)node_node_set.capacity(); |
| 136 |
results.fill_node_set = wall_clock.seconds(); |
| 137 |
//-------------------------------- |
| 138 |
|
| 139 |
wall_clock.reset(); |
| 140 |
phase = SCAN_NODE_COUNT ; |
| 141 |
|
| 142 |
// Exclusive scan of row_count into row_map |
| 143 |
// including the final total in the 'node_count + 1' position. |
| 144 |
// Zero the 'row_count' values. |
| 145 |
Kokkos::parallel_scan( node_count , *this ); |
| 146 |
|
| 147 |
// Zero the row count for the fill: |
| 148 |
Kokkos::deep_copy( row_count , 0u ); |
| 149 |
|
| 150 |
unsigned graph_entry_count = 0 ; |
| 151 |
|
| 152 |
Kokkos::deep_copy( graph_entry_count , row_total ); |
| 153 |
|
| 154 |
// Assign graph's row_map and allocate graph's entries |
| 155 |
graph.row_map = row_map ; |
| 156 |
graph.entries = typename CrsGraphType::entries_type( "graph_entries" , graph_entry_count ); |
| 157 |
|
| 158 |
//-------------------------------- |
| 159 |
// Fill graph's entries from the (node,node) set. |
| 160 |
|
| 161 |
execution_space().fence(); |
| 162 |
results.scan_node_count = wall_clock.seconds(); |
| 163 |
|
| 164 |
wall_clock.reset(); |
| 165 |
phase = FILL_GRAPH_ENTRIES ; |
| 166 |
Kokkos::parallel_for( node_node_set.capacity() , *this ); |
| 167 |
|
| 168 |
execution_space().fence(); |
| 169 |
results.fill_graph_entries = wall_clock.seconds(); |
| 170 |
|
| 171 |
//-------------------------------- |
| 172 |
// Done with the temporary sets and arrays |
| 173 |
wall_clock.reset(); |
| 174 |
|
| 175 |
row_total = UnsignedValue(); |
| 176 |
row_count = RowMapType(); |
| 177 |
row_map = RowMapType(); |
| 178 |
node_node_set.clear(); |
| 179 |
|
| 180 |
//-------------------------------- |
| 181 |
|
| 182 |
phase = SORT_GRAPH_ENTRIES ; |
| 183 |
Kokkos::parallel_for( node_count , *this ); |
| 184 |
|
| 185 |
execution_space().fence(); |
| 186 |
results.sort_graph_entries = wall_clock.seconds(); |
| 187 |
|
| 188 |
//-------------------------------- |
| 189 |
// Element-to-graph mapping: |
| 190 |
wall_clock.reset(); |
| 191 |
phase = FILL_ELEMENT_GRAPH ; |
| 192 |
elem_graph = ElemGraphType("elem_graph", elem_node_id.extent(0) ); |
| 193 |
Kokkos::parallel_for( elem_node_id.extent(0) , *this ); |
| 194 |
|
| 195 |
execution_space().fence(); |
| 196 |
results.fill_element_graph = wall_clock.seconds(); |
| 197 |
} |
| 198 |
|
| 199 |
//------------------------------------ |
| 200 |
// parallel_for: create map and count row length |
| 201 |
|
| 202 |
KOKKOS_INLINE_FUNCTION |
| 203 |
void operator()( const TagFillNodeSet & , const unsigned ielem , unsigned & count ) const |
| 204 |
{ |
| 205 |
// Loop over element's (row_local_node,col_local_node) pairs: |
| 206 |
for ( unsigned row_local_node = 0 ; row_local_node < elem_node_id.extent(1) ; ++row_local_node ) { |
| 207 |
|
| 208 |
const unsigned row_node = elem_node_id( ielem , row_local_node ); |
| 209 |
|
| 210 |
for ( unsigned col_local_node = row_local_node ; col_local_node < elem_node_id.extent(1) ; ++col_local_node ) { |
| 211 |
|
| 212 |
const unsigned col_node = elem_node_id( ielem , col_local_node ); |
| 213 |
|
| 214 |
// If either node is locally owned then insert the pair into the unordered map: |
| 215 |
|
| 216 |
if ( row_node < row_count.extent(0) || col_node < row_count.extent(0) ) { |
| 217 |
|
| 218 |
const key_type key = (row_node < col_node) ? make_pair( row_node, col_node ) : make_pair( col_node, row_node ) ; |
| 219 |
|
| 220 |
const typename SetType::insert_result result = node_node_set.insert( key ); |
| 221 |
|
| 222 |
if ( result.success() ) { |
| 223 |
// First time this pair was inserted |
| 224 |
if ( row_node < row_count.extent(0) ) { Kokkos::atomic_inc( & row_count( row_node ) ); } |
| 225 |
if ( col_node < row_count.extent(0) && col_node != row_node ) { Kokkos::atomic_inc( & row_count( col_node ) ); } |
| 226 |
} |
| 227 |
else if ( result.failed() ) { |
| 228 |
// Ran out of memory for insertion. |
| 229 |
++count ; |
| 230 |
} |
| 231 |
} |
| 232 |
} |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
KOKKOS_INLINE_FUNCTION |
| 237 |
void fill_graph_entries( const unsigned iset ) const |
| 238 |
{ |
| 239 |
if ( node_node_set.valid_at(iset) ) { |
| 240 |
const key_type key = node_node_set.key_at(iset) ; |
| 241 |
const unsigned row_node = key.first ; |
| 242 |
const unsigned col_node = key.second ; |
| 243 |
|
| 244 |
using atomic_incr_type = typename std::remove_reference< decltype( row_count(0) ) >::type; |
| 245 |
|
| 246 |
if ( row_node < row_count.extent(0) ) { |
| 247 |
const unsigned offset = graph.row_map( row_node ) + Kokkos::atomic_fetch_add( & row_count( row_node ) , atomic_incr_type(1) ); |
| 248 |
graph.entries( offset ) = col_node ; |
| 249 |
} |
| 250 |
|
| 251 |
if ( col_node < row_count.extent(0) && col_node != row_node ) { |
| 252 |
const unsigned offset = graph.row_map( col_node ) + Kokkos::atomic_fetch_add( & row_count( col_node ) , atomic_incr_type(1) ); |
| 253 |
graph.entries( offset ) = row_node ; |
| 254 |
} |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
KOKKOS_INLINE_FUNCTION |
| 259 |
void sort_graph_entries( const unsigned irow ) const |
| 260 |
{ |
| 261 |
typedef typename CrsGraphType::size_type size_type; |
| 262 |
const size_type row_beg = graph.row_map( irow ); |
| 263 |
const size_type row_end = graph.row_map( irow + 1 ); |
| 264 |
for ( size_type i = row_beg + 1 ; i < row_end ; ++i ) { |
| 265 |
const typename CrsGraphType::data_type col = graph.entries(i); |
| 266 |
size_type j = i ; |
| 267 |
for ( ; row_beg < j && col < graph.entries(j-1) ; --j ) { |
| 268 |
graph.entries(j) = graph.entries(j-1); |
| 269 |
} |
| 270 |
graph.entries(j) = col ; |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
KOKKOS_INLINE_FUNCTION |
| 275 |
void fill_elem_graph_map( const unsigned ielem ) const |
| 276 |
{ |
| 277 |
typedef typename CrsGraphType::data_type entry_type; |
| 278 |
for ( unsigned row_local_node = 0 ; row_local_node < elem_node_id.extent(1) ; ++row_local_node ) { |
| 279 |
|
| 280 |
const unsigned row_node = elem_node_id( ielem , row_local_node ); |
| 281 |
|
| 282 |
for ( unsigned col_local_node = 0 ; col_local_node < elem_node_id.extent(1) ; ++col_local_node ) { |
| 283 |
|
| 284 |
const unsigned col_node = elem_node_id( ielem , col_local_node ); |
| 285 |
|
| 286 |
entry_type entry = 0 ; |
| 287 |
|
| 288 |
if ( row_node + 1 < graph.row_map.extent(0) ) { |
| 289 |
|
| 290 |
const entry_type entry_end = static_cast<entry_type> (graph.row_map( row_node + 1 )); |
| 291 |
|
| 292 |
entry = graph.row_map( row_node ); |
| 293 |
|
| 294 |
for ( ; entry < entry_end && graph.entries(entry) != static_cast<entry_type> (col_node) ; ++entry ); |
| 295 |
|
| 296 |
if ( entry == entry_end ) entry = ~0u ; |
| 297 |
} |
| 298 |
|
| 299 |
elem_graph( ielem , row_local_node , col_local_node ) = entry ; |
| 300 |
} |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
KOKKOS_INLINE_FUNCTION |
| 305 |
void operator()( const unsigned iwork ) const |
| 306 |
{ |
| 307 |
if ( phase == FILL_GRAPH_ENTRIES ) { |
| 308 |
fill_graph_entries( iwork ); |
| 309 |
} |
| 310 |
else if ( phase == SORT_GRAPH_ENTRIES ) { |
| 311 |
sort_graph_entries( iwork ); |
| 312 |
} |
| 313 |
else if ( phase == FILL_ELEMENT_GRAPH ) { |
| 314 |
fill_elem_graph_map( iwork ); |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
//------------------------------------ |
| 319 |
// parallel_scan: row offsets |
| 320 |
|
| 321 |
typedef unsigned value_type ; |
| 322 |
|
| 323 |
KOKKOS_INLINE_FUNCTION |
| 324 |
void operator()( const unsigned irow , unsigned & update , const bool final ) const |
| 325 |
{ |
| 326 |
// exclusive scan |
| 327 |
if ( final ) { row_map( irow ) = update ; } |
| 328 |
|
| 329 |
update += row_count( irow ); |
| 330 |
|
| 331 |
if ( final ) { |
| 332 |
if ( irow + 1 == row_count.extent(0) ) { |
| 333 |
row_map( irow + 1 ) = update ; |
| 334 |
row_total() = update ; |
| 335 |
} |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
KOKKOS_INLINE_FUNCTION |
| 340 |
void init( unsigned & update ) const { update = 0 ; } |
| 341 |
|
| 342 |
KOKKOS_INLINE_FUNCTION |
| 343 |
void join( unsigned & update , const unsigned & input ) const { update += input ; } |
| 344 |
|
| 345 |
//------------------------------------ |
| 346 |
}; |
| 347 |
|
| 348 |
} /* namespace FENL */ |
| 349 |
} /* namespace Example */ |
| 350 |
} /* namespace Kokkos */ |
| 351 |
|
| 352 |
//---------------------------------------------------------------------------- |
| 353 |
//---------------------------------------------------------------------------- |
| 354 |
|
| 355 |
namespace Kokkos { |
| 356 |
namespace Example { |
| 357 |
namespace FENL { |
| 358 |
|
| 359 |
class DefaultGatherScatterOp { |
| 360 |
public: |
| 361 |
template <typename VectorType, typename ScalarType> |
| 362 |
KOKKOS_INLINE_FUNCTION |
| 363 |
void |
| 364 |
gather_solution(const unsigned i, const VectorType& solution, |
| 365 |
ScalarType& val) const { |
| 366 |
val = solution(i); |
| 367 |
} |
| 368 |
|
| 369 |
template <typename VectorType, typename ScalarType> |
| 370 |
KOKKOS_INLINE_FUNCTION |
| 371 |
void |
| 372 |
scatter_residual(const unsigned i, const VectorType& residual, |
| 373 |
const ScalarType& val) const { |
| 374 |
Kokkos::atomic_add( &residual(i), val ); |
| 375 |
} |
| 376 |
|
| 377 |
template <typename MatrixType, typename ScalarType> |
| 378 |
KOKKOS_INLINE_FUNCTION |
| 379 |
void |
| 380 |
scatter_jacobian(const unsigned i, const MatrixType& jacobian, |
| 381 |
const ScalarType& val) const { |
| 382 |
Kokkos::atomic_add( &jacobian(i), val ); |
| 383 |
} |
| 384 |
}; |
| 385 |
|
| 386 |
template <typename GatherScatterType> |
| 387 |
struct LocalGatherScatterTraits { |
| 388 |
typedef GatherScatterType gather_scatter_type; |
| 389 |
typedef const gather_scatter_type& local_gather_scatter_type; |
| 390 |
KOKKOS_INLINE_FUNCTION |
| 391 |
static local_gather_scatter_type create_local_gather_scatter( |
| 392 |
const gather_scatter_type& v, const unsigned local_rank) { return v; } |
| 393 |
}; |
| 394 |
|
| 395 |
#ifdef HAVE_TRILINOSCOUPLINGS_SACADO |
| 396 |
template <typename MultiVectorType> |
| 397 |
class ParamSensitivityGatherScatterOp { |
| 398 |
public: |
| 399 |
KOKKOS_DEFAULTED_FUNCTION |
| 400 |
ParamSensitivityGatherScatterOp() = default; |
| 401 |
|
| 402 |
KOKKOS_INLINE_FUNCTION |
| 403 |
ParamSensitivityGatherScatterOp( |
| 404 |
const MultiVectorType& sol_dp, const MultiVectorType& res_dp) : |
| 405 |
solution_dp(sol_dp), residual_dp(res_dp), |
| 406 |
num_p(solution_dp.extent(1)) {} |
| 407 |
|
| 408 |
KOKKOS_DEFAULTED_FUNCTION |
| 409 |
ParamSensitivityGatherScatterOp(const ParamSensitivityGatherScatterOp&) = default; |
| 410 |
|
| 411 |
KOKKOS_DEFAULTED_FUNCTION |
| 412 |
ParamSensitivityGatherScatterOp& operator=(const ParamSensitivityGatherScatterOp&) = default; |
| 413 |
|
| 414 |
template <typename VectorType, typename ScalarType> |
| 415 |
KOKKOS_INLINE_FUNCTION |
| 416 |
void |
| 417 |
gather_solution(const unsigned i, const VectorType& solution, |
| 418 |
ScalarType& val) const { |
| 419 |
val.resize(num_p); |
| 420 |
val.val() = solution(i); |
| 421 |
for (unsigned j=0; j<num_p; ++j) |
| 422 |
val.fastAccessDx(j) = solution_dp(i,j); |
| 423 |
} |
| 424 |
|
| 425 |
template <typename VectorType, typename ScalarType> |
| 426 |
KOKKOS_INLINE_FUNCTION |
| 427 |
void |
| 428 |
scatter_residual(const unsigned i, const VectorType& residual, |
| 429 |
const ScalarType& val) const { |
| 430 |
Kokkos::atomic_add( &residual(i), val.val() ); |
| 431 |
for (unsigned j=0; j<num_p; ++j) |
| 432 |
Kokkos::atomic_add( &residual_dp(i,j), val.fastAccessDx(j) ); |
| 433 |
} |
| 434 |
|
| 435 |
template <typename MatrixType, typename ScalarType> |
| 436 |
KOKKOS_INLINE_FUNCTION |
| 437 |
void |
| 438 |
scatter_jacobian(const unsigned i, const MatrixType& jacobian, |
| 439 |
const ScalarType& val) const { |
| 440 |
Kokkos::atomic_add( &jacobian(i), val.val() ); |
| 441 |
} |
| 442 |
|
| 443 |
public: |
| 444 |
const MultiVectorType solution_dp; |
| 445 |
const MultiVectorType residual_dp; |
| 446 |
const unsigned num_p; |
| 447 |
}; |
| 448 |
|
| 449 |
template <typename MultiVectorType> |
| 450 |
struct LocalGatherScatterTraits< |
| 451 |
ParamSensitivityGatherScatterOp<MultiVectorType> > { |
| 452 |
typedef ParamSensitivityGatherScatterOp<MultiVectorType> GatherScatterType; |
| 453 |
typedef GatherScatterType gather_scatter_type; |
| 454 |
typedef LocalViewTraits<MultiVectorType> local_vector_traits; |
| 455 |
typedef typename local_vector_traits::local_view_type local_multi_vector_type; |
| 456 |
typedef ParamSensitivityGatherScatterOp<local_multi_vector_type> local_gather_scatter_type; |
| 457 |
KOKKOS_INLINE_FUNCTION |
| 458 |
static local_gather_scatter_type create_local_gather_scatter( |
| 459 |
const gather_scatter_type& v, const unsigned local_rank) { |
| 460 |
return local_gather_scatter_type( |
| 461 |
local_vector_traits::create_local_view(v.solution_dp, local_rank), |
| 462 |
local_vector_traits::create_local_view(v.residual_dp, local_rank)); |
| 463 |
} |
| 464 |
}; |
| 465 |
#endif |
| 466 |
|
| 467 |
template< class FiniteElementMeshType |
| 468 |
, class SparseMatrixType |
| 469 |
, class CoeffFunctionType = ElementComputationConstantCoefficient |
| 470 |
, class GatherScatterOp = DefaultGatherScatterOp |
| 471 |
, class ScalarType = typename SparseMatrixType::value_type |
| 472 |
> |
| 473 |
class ElementComputation ; |
| 474 |
|
| 475 |
|
| 476 |
template< class ExecutionSpace , BoxElemPart::ElemOrder Order , class CoordinateMap , |
| 477 |
typename MatrixVectorScalarType , typename OrdinalType , class MemoryTraits , typename SizeType , |
| 478 |
class CoeffFunctionType , class GatherScatterOp , class ScalarType > |
| 479 |
class ElementComputation |
| 480 |
< Kokkos::Example::BoxElemFixture< ExecutionSpace , Order , CoordinateMap > |
| 481 |
, KokkosSparse::CrsMatrix< MatrixVectorScalarType , OrdinalType , Kokkos::Device<ExecutionSpace, typename ExecutionSpace::memory_space> , MemoryTraits , SizeType > |
| 482 |
, CoeffFunctionType |
| 483 |
, GatherScatterOp |
| 484 |
, ScalarType > |
| 485 |
{ |
| 486 |
public: |
| 487 |
|
| 488 |
typedef Kokkos::Example::BoxElemFixture< ExecutionSpace, Order, CoordinateMap > mesh_type ; |
| 489 |
typedef Kokkos::Example::HexElement_Data< mesh_type::ElemNode > element_data_type ; |
| 490 |
|
| 491 |
//------------------------------------ |
| 492 |
|
| 493 |
typedef ExecutionSpace execution_space ; |
| 494 |
typedef Kokkos::Device<execution_space, typename execution_space::memory_space> DeviceType; |
| 495 |
typedef ScalarType scalar_type ; |
| 496 |
|
| 497 |
typedef KokkosSparse::CrsMatrix< MatrixVectorScalarType , OrdinalType , DeviceType , MemoryTraits , SizeType > sparse_matrix_type ; |
| 498 |
typedef typename sparse_matrix_type::StaticCrsGraphType sparse_graph_type ; |
| 499 |
typedef typename sparse_matrix_type::values_type matrix_values_type ; |
| 500 |
typedef Kokkos::View< MatrixVectorScalarType* , Kokkos::LayoutLeft, execution_space > vector_type ; |
| 501 |
|
| 502 |
//------------------------------------ |
| 503 |
|
| 504 |
typedef LocalViewTraits< vector_type > local_vector_view_traits; |
| 505 |
typedef LocalViewTraits< matrix_values_type> local_matrix_view_traits; |
| 506 |
typedef typename local_vector_view_traits::local_view_type local_vector_type; |
| 507 |
typedef typename local_matrix_view_traits::local_view_type local_matrix_type; |
| 508 |
typedef typename local_vector_view_traits::local_value_type local_matrix_vector_scalar_type; |
| 509 |
typedef typename ReplaceLocalScalarType<ScalarType,local_matrix_vector_scalar_type>::type local_scalar_type; |
| 510 |
static const bool use_team = local_vector_view_traits::use_team; |
| 511 |
typedef LocalGatherScatterTraits<GatherScatterOp> local_gather_scatter_traits; |
| 512 |
typedef typename local_gather_scatter_traits::local_gather_scatter_type local_gather_scatter_type; |
| 513 |
|
| 514 |
static const unsigned SpatialDim = element_data_type::spatial_dimension ; |
| 515 |
static const unsigned TensorDim = SpatialDim * SpatialDim ; |
| 516 |
static const unsigned ElemNodeCount = element_data_type::element_node_count ; |
| 517 |
static const unsigned FunctionCount = element_data_type::function_count ; |
| 518 |
static const unsigned IntegrationCount = element_data_type::integration_count ; |
| 519 |
|
| 520 |
//------------------------------------ |
| 521 |
|
| 522 |
typedef typename mesh_type::node_coord_type node_coord_type ; |
| 523 |
typedef typename mesh_type::elem_node_type elem_node_type ; |
| 524 |
typedef typename NodeNodeGraph< elem_node_type , sparse_graph_type , ElemNodeCount >::ElemGraphType elem_graph_type ; |
| 525 |
|
| 526 |
//------------------------------------ |
| 527 |
|
| 528 |
|
| 529 |
//------------------------------------ |
| 530 |
// Computational data: |
| 531 |
|
| 532 |
const element_data_type elem_data ; |
| 533 |
const elem_node_type elem_node_ids ; |
| 534 |
const node_coord_type node_coords ; |
| 535 |
const elem_graph_type elem_graph ; |
| 536 |
const vector_type solution ; |
| 537 |
const vector_type residual ; |
| 538 |
const sparse_matrix_type jacobian ; |
| 539 |
const GatherScatterOp gather_scatter ; |
| 540 |
const bool assemble_jacobian ; |
| 541 |
const CoeffFunctionType coeff_function ; |
| 542 |
const bool isotropic ; |
| 543 |
const double coeff_source ; |
| 544 |
const double coeff_advection ; |
| 545 |
const KokkosSparse::DeviceConfig dev_config ; |
| 546 |
|
| 547 |
ElementComputation( const ElementComputation & rhs ) |
| 548 |
: elem_data() |
| 549 |
, elem_node_ids( rhs.elem_node_ids ) |
| 550 |
, node_coords( rhs.node_coords ) |
| 551 |
, elem_graph( rhs.elem_graph ) |
| 552 |
, solution( rhs.solution ) |
| 553 |
, residual( rhs.residual ) |
| 554 |
, jacobian( rhs.jacobian ) |
| 555 |
, gather_scatter( rhs.gather_scatter ) |
| 556 |
, assemble_jacobian( rhs.assemble_jacobian ) |
| 557 |
, coeff_function( rhs.coeff_function ) |
| 558 |
, isotropic( rhs.isotropic ) |
| 559 |
, coeff_source( rhs.coeff_source ) |
| 560 |
, coeff_advection( rhs.coeff_advection ) |
| 561 |
, dev_config( rhs.dev_config ) |
| 562 |
{} |
| 563 |
|
| 564 |
// If the element->sparse_matrix graph is provided then perform atomic updates |
| 565 |
// Otherwise fill per-element contributions for subequent gather-add into a residual and jacobian. |
| 566 |
ElementComputation( const mesh_type & arg_mesh , |
| 567 |
const CoeffFunctionType & arg_coeff_function , |
| 568 |
const bool arg_isotropic , |
| 569 |
const double & arg_coeff_source , |
| 570 |
const double & arg_coeff_advection , |
| 571 |
const vector_type & arg_solution , |
| 572 |
const elem_graph_type & arg_elem_graph , |
| 573 |
const sparse_matrix_type & arg_jacobian , |
| 574 |
const vector_type & arg_residual , |
| 575 |
const KokkosSparse::DeviceConfig arg_dev_config , |
| 576 |
const QuadratureData<ExecutionSpace>& qd = |
| 577 |
QuadratureData<ExecutionSpace>() , |
| 578 |
const GatherScatterOp & arg_gather_scatter = |
| 579 |
GatherScatterOp(), |
| 580 |
const bool arg_assemble_jacobian = true ) |
| 581 |
: elem_data() |
| 582 |
, elem_node_ids( arg_mesh.elem_node() ) |
| 583 |
, node_coords( arg_mesh.node_coord() ) |
| 584 |
, elem_graph( arg_elem_graph ) |
| 585 |
, solution( arg_solution ) |
| 586 |
, residual( arg_residual ) |
| 587 |
, jacobian( arg_jacobian ) |
| 588 |
, gather_scatter( arg_gather_scatter ) |
| 589 |
, assemble_jacobian( arg_assemble_jacobian ) |
| 590 |
, coeff_function( arg_coeff_function ) |
| 591 |
, isotropic( arg_isotropic ) |
| 592 |
, coeff_source( arg_coeff_source ) |
| 593 |
, coeff_advection( arg_coeff_advection ) |
| 594 |
, dev_config( arg_dev_config ) |
| 595 |
{} |
| 596 |
|
| 597 |
//------------------------------------ |
| 598 |
|
| 599 |
void apply() const |
| 600 |
{ |
| 601 |
const size_t nelem = elem_node_ids.extent(0); |
| 602 |
if ( use_team ) { |
| 603 |
const size_t team_size = dev_config.block_dim.x * dev_config.block_dim.y; |
| 604 |
const size_t league_size = |
| 605 |
(nelem + dev_config.block_dim.y-1) / dev_config.block_dim.y; |
| 606 |
Kokkos::TeamPolicy< execution_space > config( league_size, team_size ); |
| 607 |
parallel_for( config , *this ); |
| 608 |
} |
| 609 |
else { |
| 610 |
parallel_for( nelem , *this ); |
| 611 |
} |
| 612 |
} |
| 613 |
|
| 614 |
//------------------------------------ |
| 615 |
|
| 616 |
KOKKOS_INLINE_FUNCTION |
| 617 |
double transform_gradients( |
| 618 |
const double grad[][ FunctionCount ] , // Gradient of bases master element |
| 619 |
const double x[] , |
| 620 |
const double y[] , |
| 621 |
const double z[] , |
| 622 |
double dpsidx[] , |
| 623 |
double dpsidy[] , |
| 624 |
double dpsidz[] ) const |
| 625 |
{ |
| 626 |
enum { j11 = 0 , j12 = 1 , j13 = 2 , |
| 627 |
j21 = 3 , j22 = 4 , j23 = 5 , |
| 628 |
j31 = 6 , j32 = 7 , j33 = 8 }; |
| 629 |
|
| 630 |
// Jacobian accumulation: |
| 631 |
|
| 632 |
double J[ TensorDim ] = { 0, 0, 0, 0, 0, 0, 0, 0, 0 }; |
| 633 |
|
| 634 |
for( unsigned i = 0; i < FunctionCount ; ++i ) { |
| 635 |
const double x1 = x[i] ; |
| 636 |
const double x2 = y[i] ; |
| 637 |
const double x3 = z[i] ; |
| 638 |
|
| 639 |
const double g1 = grad[0][i] ; |
| 640 |
const double g2 = grad[1][i] ; |
| 641 |
const double g3 = grad[2][i] ; |
| 642 |
|
| 643 |
J[j11] += g1 * x1 ; |
| 644 |
J[j12] += g1 * x2 ; |
| 645 |
J[j13] += g1 * x3 ; |
| 646 |
|
| 647 |
J[j21] += g2 * x1 ; |
| 648 |
J[j22] += g2 * x2 ; |
| 649 |
J[j23] += g2 * x3 ; |
| 650 |
|
| 651 |
J[j31] += g3 * x1 ; |
| 652 |
J[j32] += g3 * x2 ; |
| 653 |
J[j33] += g3 * x3 ; |
| 654 |
} |
| 655 |
|
| 656 |
// Inverse jacobian: |
| 657 |
|
| 658 |
double invJ[ TensorDim ] = { |
| 659 |
static_cast<double>( J[j22] * J[j33] - J[j23] * J[j32] ) , |
| 660 |
static_cast<double>( J[j13] * J[j32] - J[j12] * J[j33] ) , |
| 661 |
static_cast<double>( J[j12] * J[j23] - J[j13] * J[j22] ) , |
| 662 |
|
| 663 |
static_cast<double>( J[j23] * J[j31] - J[j21] * J[j33] ) , |
| 664 |
static_cast<double>( J[j11] * J[j33] - J[j13] * J[j31] ) , |
| 665 |
static_cast<double>( J[j13] * J[j21] - J[j11] * J[j23] ) , |
| 666 |
|
| 667 |
static_cast<double>( J[j21] * J[j32] - J[j22] * J[j31] ) , |
| 668 |
static_cast<double>( J[j12] * J[j31] - J[j11] * J[j32] ) , |
| 669 |
static_cast<double>( J[j11] * J[j22] - J[j12] * J[j21] ) }; |
| 670 |
|
| 671 |
const double detJ = J[j11] * invJ[j11] + |
| 672 |
J[j21] * invJ[j12] + |
| 673 |
J[j31] * invJ[j13] ; |
| 674 |
|
| 675 |
const double detJinv = 1.0 / detJ ; |
| 676 |
|
| 677 |
for ( unsigned i = 0 ; i < TensorDim ; ++i ) { invJ[i] *= detJinv ; } |
| 678 |
|
| 679 |
// Transform gradients: |
| 680 |
|
| 681 |
for( unsigned i = 0; i < FunctionCount ; ++i ) { |
| 682 |
const double g0 = grad[0][i]; |
| 683 |
const double g1 = grad[1][i]; |
| 684 |
const double g2 = grad[2][i]; |
| 685 |
|
| 686 |
dpsidx[i] = g0 * invJ[j11] + g1 * invJ[j12] + g2 * invJ[j13]; |
| 687 |
dpsidy[i] = g0 * invJ[j21] + g1 * invJ[j22] + g2 * invJ[j23]; |
| 688 |
dpsidz[i] = g0 * invJ[j31] + g1 * invJ[j32] + g2 * invJ[j33]; |
| 689 |
} |
| 690 |
|
| 691 |
return detJ ; |
| 692 |
} |
| 693 |
|
| 694 |
KOKKOS_INLINE_FUNCTION |
| 695 |
void contributeResidualJacobian( |
| 696 |
const local_scalar_type dof_values[] , |
| 697 |
const double dpsidx[] , |
| 698 |
const double dpsidy[] , |
| 699 |
const double dpsidz[] , |
| 700 |
const double detJ , |
| 701 |
const double integ_weight , |
| 702 |
const double bases_vals[] , |
| 703 |
const local_scalar_type coeff_k , |
| 704 |
const double coeff_src , |
| 705 |
const double advection[] , |
| 706 |
local_scalar_type elem_res[] , |
| 707 |
local_scalar_type elem_mat[][ FunctionCount ] ) const |
| 708 |
{ |
| 709 |
local_scalar_type value_at_pt = 0 ; |
| 710 |
local_scalar_type gradx_at_pt = 0 ; |
| 711 |
local_scalar_type grady_at_pt = 0 ; |
| 712 |
local_scalar_type gradz_at_pt = 0 ; |
| 713 |
|
| 714 |
for ( unsigned m = 0 ; m < FunctionCount ; m++ ) { |
| 715 |
value_at_pt += dof_values[m] * bases_vals[m] ; |
| 716 |
gradx_at_pt += dof_values[m] * dpsidx[m] ; |
| 717 |
grady_at_pt += dof_values[m] * dpsidy[m] ; |
| 718 |
gradz_at_pt += dof_values[m] * dpsidz[m] ; |
| 719 |
} |
| 720 |
|
| 721 |
const double detJ_weight = detJ * integ_weight; |
| 722 |
|
| 723 |
const local_scalar_type source_term = |
| 724 |
coeff_src * value_at_pt * value_at_pt; |
| 725 |
const local_scalar_type source_deriv = |
| 726 |
2.0 * coeff_src * value_at_pt; |
| 727 |
|
| 728 |
const local_scalar_type advection_term = |
| 729 |
advection[0]*gradx_at_pt + |
| 730 |
advection[1]*grady_at_pt + |
| 731 |
advection[2]*gradz_at_pt; |
| 732 |
|
| 733 |
for ( unsigned m = 0; m < FunctionCount; ++m) { |
| 734 |
local_scalar_type * const mat = elem_mat[m] ; |
| 735 |
const double bases_val_m = bases_vals[m]; |
| 736 |
const double dpsidx_m = dpsidx[m] ; |
| 737 |
const double dpsidy_m = dpsidy[m] ; |
| 738 |
const double dpsidz_m = dpsidz[m] ; |
| 739 |
|
| 740 |
elem_res[m] += |
| 741 |
detJ_weight * ( coeff_k * ( dpsidx_m * gradx_at_pt + |
| 742 |
dpsidy_m * grady_at_pt + |
| 743 |
dpsidz_m * gradz_at_pt ) + |
| 744 |
( advection_term + source_term ) * bases_val_m ) ; |
| 745 |
|
| 746 |
if (assemble_jacobian) { |
| 747 |
for( unsigned n = 0; n < FunctionCount; n++) { |
| 748 |
|
| 749 |
mat[n] += |
| 750 |
detJ_weight * ( coeff_k * ( dpsidx_m * dpsidx[n] + |
| 751 |
dpsidy_m * dpsidy[n] + |
| 752 |
dpsidz_m * dpsidz[n] ) + |
| 753 |
( advection[0] * dpsidx[n] + |
| 754 |
advection[1] * dpsidy[n] + |
| 755 |
advection[2] * dpsidz[n] + |
| 756 |
source_deriv * bases_vals[n] ) * bases_val_m ); |
| 757 |
} |
| 758 |
} |
| 759 |
} |
| 760 |
} |
| 761 |
|
| 762 |
KOKKOS_INLINE_FUNCTION |
| 763 |
void contributeResidualJacobianAnisotropic( |
| 764 |
const local_scalar_type dof_values[] , |
| 765 |
const double dpsidx[] , |
| 766 |
const double dpsidy[] , |
| 767 |
const double dpsidz[] , |
| 768 |
const double detJ , |
| 769 |
const double integ_weight , |
| 770 |
const double bases_vals[] , |
| 771 |
const local_scalar_type coeff_k , |
| 772 |
const double coeff_src , |
| 773 |
const double advection[] , |
| 774 |
local_scalar_type elem_res[] , |
| 775 |
local_scalar_type elem_mat[][ FunctionCount ] ) const |
| 776 |
{ |
| 777 |
local_scalar_type value_at_pt = 0 ; |
| 778 |
local_scalar_type gradx_at_pt = 0 ; |
| 779 |
local_scalar_type grady_at_pt = 0 ; |
| 780 |
local_scalar_type gradz_at_pt = 0 ; |
| 781 |
|
| 782 |
for ( unsigned m = 0 ; m < FunctionCount ; m++ ) { |
| 783 |
value_at_pt += dof_values[m] * bases_vals[m] ; |
| 784 |
gradx_at_pt += dof_values[m] * dpsidx[m] ; |
| 785 |
grady_at_pt += dof_values[m] * dpsidy[m] ; |
| 786 |
gradz_at_pt += dof_values[m] * dpsidz[m] ; |
| 787 |
} |
| 788 |
|
| 789 |
const double detJ_weight = detJ * integ_weight; |
| 790 |
|
| 791 |
const local_scalar_type source_term = |
| 792 |
coeff_src * value_at_pt * value_at_pt; |
| 793 |
const local_scalar_type source_deriv = |
| 794 |
2.0 * coeff_src * value_at_pt; |
| 795 |
|
| 796 |
const local_scalar_type advection_term = |
| 797 |
advection[0]*gradx_at_pt + |
| 798 |
advection[1]*grady_at_pt + |
| 799 |
advection[2]*gradz_at_pt; |
| 800 |
|
| 801 |
for ( unsigned m = 0; m < FunctionCount; ++m) { |
| 802 |
local_scalar_type * const mat = elem_mat[m] ; |
| 803 |
const double bases_val_m = bases_vals[m]; |
| 804 |
const double dpsidx_m = dpsidx[m] ; |
| 805 |
const double dpsidy_m = dpsidy[m] ; |
| 806 |
const double dpsidz_m = dpsidz[m] ; |
| 807 |
|
| 808 |
elem_res[m] += |
| 809 |
detJ_weight * ( dpsidx_m * gradx_at_pt + |
| 810 |
dpsidy_m * grady_at_pt + |
| 811 |
coeff_k * dpsidz_m * gradz_at_pt + |
| 812 |
( advection_term + source_term ) * bases_val_m ) ; |
| 813 |
|
| 814 |
if (assemble_jacobian) { |
| 815 |
for( unsigned n = 0; n < FunctionCount; n++) { |
| 816 |
|
| 817 |
mat[n] += |
| 818 |
detJ_weight * ( dpsidx_m * dpsidx[n] + |
| 819 |
dpsidy_m * dpsidy[n] + |
| 820 |
coeff_k * dpsidz_m * dpsidz[n] + |
| 821 |
( advection[0] * dpsidx[n] + |
| 822 |
advection[1] * dpsidy[n] + |
| 823 |
advection[2] * dpsidz[n] + |
| 824 |
source_deriv * bases_vals[n] ) * bases_val_m ); |
| 825 |
} |
| 826 |
} |
| 827 |
} |
| 828 |
} |
| 829 |
|
| 830 |
typedef typename Kokkos::TeamPolicy< execution_space >::member_type team_member ; |
| 831 |
KOKKOS_INLINE_FUNCTION |
| 832 |
void operator()( const team_member & dev ) const |
| 833 |
{ |
| 834 |
|
| 835 |
const unsigned num_ensemble_threads = dev_config.block_dim.x ; |
| 836 |
const unsigned num_element_threads = dev_config.block_dim.y ; |
| 837 |
const unsigned element_rank = dev.team_rank() / num_ensemble_threads ; |
| 838 |
const unsigned ensemble_rank = dev.team_rank() % num_ensemble_threads ; |
| 839 |
|
| 840 |
const unsigned ielem = |
| 841 |
dev.league_rank() * num_element_threads + element_rank; |
| 842 |
|
| 843 |
if (ielem >= elem_node_ids.extent(0)) |
| 844 |
return; |
| 845 |
|
| 846 |
(*this)( ielem, ensemble_rank ); |
| 847 |
|
| 848 |
} |
| 849 |
|
| 850 |
KOKKOS_INLINE_FUNCTION |
| 851 |
void operator()( const unsigned ielem , |
| 852 |
const unsigned ensemble_rank = 0 ) const |
| 853 |
{ |
| 854 |
local_vector_type local_solution = |
| 855 |
local_vector_view_traits::create_local_view(solution, |
| 856 |
ensemble_rank); |
| 857 |
local_vector_type local_residual = |
| 858 |
local_vector_view_traits::create_local_view(residual, |
| 859 |
ensemble_rank); |
| 860 |
local_matrix_type local_jacobian_values = |
| 861 |
local_matrix_view_traits::create_local_view(jacobian.values, |
| 862 |
ensemble_rank); |
| 863 |
|
| 864 |
local_gather_scatter_type local_gather_scatter = |
| 865 |
local_gather_scatter_traits::create_local_gather_scatter( gather_scatter, |
| 866 |
ensemble_rank ); |
| 867 |
|
| 868 |
// Gather nodal coordinates and solution vector: |
| 869 |
|
| 870 |
double x[ FunctionCount ] ; |
| 871 |
double y[ FunctionCount ] ; |
| 872 |
double z[ FunctionCount ] ; |
| 873 |
local_scalar_type val[ FunctionCount ] ; |
| 874 |
unsigned node_index[ ElemNodeCount ]; |
| 875 |
|
| 876 |
for ( unsigned i = 0 ; i < ElemNodeCount ; ++i ) { |
| 877 |
const unsigned ni = elem_node_ids( ielem , i ); |
| 878 |
|
| 879 |
node_index[i] = ni ; |
| 880 |
|
| 881 |
x[i] = node_coords( ni , 0 ); |
| 882 |
y[i] = node_coords( ni , 1 ); |
| 883 |
z[i] = node_coords( ni , 2 ); |
| 884 |
|
| 885 |
//val[i] = local_solution( ni ); |
| 886 |
local_gather_scatter.gather_solution( ni , local_solution , val[i] ); |
| 887 |
} |
| 888 |
|
| 889 |
|
| 890 |
local_scalar_type elem_vec[ FunctionCount ] ; |
| 891 |
local_scalar_type elem_mat[ FunctionCount ][ FunctionCount ] ; |
| 892 |
|
| 893 |
for( unsigned i = 0; i < FunctionCount ; i++ ) { |
| 894 |
elem_vec[i] = 0 ; |
| 895 |
if (assemble_jacobian) |
| 896 |
for( unsigned j = 0; j < FunctionCount ; j++){ |
| 897 |
elem_mat[i][j] = 0 ; |
| 898 |
} |
| 899 |
} |
| 900 |
|
| 901 |
// advection = [ 1 , 1 , 1 ] * coeff_advection |
| 902 |
double advection[] = { coeff_advection , |
| 903 |
coeff_advection , |
| 904 |
coeff_advection }; |
| 905 |
|
| 906 |
for ( unsigned i = 0 ; i < IntegrationCount ; ++i ) { |
| 907 |
double dpsidx[ FunctionCount ] ; |
| 908 |
double dpsidy[ FunctionCount ] ; |
| 909 |
double dpsidz[ FunctionCount ] ; |
| 910 |
|
| 911 |
const double detJ = |
| 912 |
transform_gradients( elem_data.gradients[i] , x , y , z , |
| 913 |
dpsidx , dpsidy , dpsidz ); |
| 914 |
|
| 915 |
// Compute physical coordinates of integration point |
| 916 |
double pt[] = { 0 , 0 , 0 } ; |
| 917 |
for ( unsigned j = 0 ; j < FunctionCount ; ++j ) { |
| 918 |
pt[0] += x[j] * elem_data.values[i][j] ; |
| 919 |
pt[1] += y[j] * elem_data.values[i][j] ; |
| 920 |
pt[2] += z[j] * elem_data.values[i][j] ; |
| 921 |
} |
| 922 |
|
| 923 |
// Evaluate diffusion coefficient |
| 924 |
local_scalar_type coeff_k = coeff_function(pt, ensemble_rank); |
| 925 |
|
| 926 |
if (isotropic) |
| 927 |
contributeResidualJacobian( val , dpsidx , dpsidy , dpsidz , detJ , |
| 928 |
elem_data.weights[i] , elem_data.values[i] , |
| 929 |
coeff_k , coeff_source , advection , |
| 930 |
elem_vec , elem_mat ); |
| 931 |
else |
| 932 |
contributeResidualJacobianAnisotropic( val , dpsidx , dpsidy , dpsidz , detJ , |
| 933 |
elem_data.weights[i] , elem_data.values[i] , |
| 934 |
coeff_k , coeff_source , advection , |
| 935 |
elem_vec , elem_mat ); |
| 936 |
} |
| 937 |
|
| 938 |
for( unsigned i = 0 ; i < FunctionCount ; i++ ) { |
| 939 |
const unsigned row = node_index[i] ; |
| 940 |
if ( row < residual.extent(0) ) { |
| 941 |
//atomic_add( & local_residual( row ) , elem_vec[i] ); |
| 942 |
local_gather_scatter.scatter_residual( |
| 943 |
row , local_residual , elem_vec[i] ); |
| 944 |
|
| 945 |
if (assemble_jacobian) { |
| 946 |
for( unsigned j = 0 ; j < FunctionCount ; j++ ) { |
| 947 |
const unsigned entry = elem_graph( ielem , i , j ); |
| 948 |
if ( entry != ~0u ) { |
| 949 |
//atomic_add( & local_jacobian_values( entry ) , elem_mat[i][j] ); |
| 950 |
local_gather_scatter.scatter_jacobian( |
| 951 |
entry , local_jacobian_values , elem_mat[i][j] ); |
| 952 |
} |
| 953 |
} |
| 954 |
} |
| 955 |
} |
| 956 |
} |
| 957 |
} |
| 958 |
}; /* ElementComputation */ |
| 959 |
|
| 960 |
//---------------------------------------------------------------------------- |
| 961 |
|
| 962 |
template< class FixtureType , class SparseMatrixType > |
| 963 |
class DirichletComputation ; |
| 964 |
|
| 965 |
template< class ExecutionSpace , BoxElemPart::ElemOrder Order , class CoordinateMap , |
| 966 |
typename ScalarType , typename OrdinalType , class MemoryTraits , typename SizeType > |
| 967 |
class DirichletComputation< |
| 968 |
Kokkos::Example::BoxElemFixture< ExecutionSpace , Order , CoordinateMap > , |
| 969 |
KokkosSparse::CrsMatrix< ScalarType , OrdinalType , Kokkos::Device<ExecutionSpace, typename ExecutionSpace::memory_space> , MemoryTraits , SizeType > > |
| 970 |
{ |
| 971 |
public: |
| 972 |
|
| 973 |
typedef Kokkos::Example::BoxElemFixture< ExecutionSpace, Order, CoordinateMap > mesh_type ; |
| 974 |
typedef typename mesh_type::node_coord_type node_coord_type ; |
| 975 |
typedef typename node_coord_type::value_type scalar_coord_type ; |
| 976 |
|
| 977 |
typedef ExecutionSpace execution_space ; |
| 978 |
typedef Kokkos::Device<execution_space, typename execution_space::memory_space> DeviceType; |
| 979 |
typedef ScalarType scalar_type ; |
| 980 |
|
| 981 |
typedef KokkosSparse::CrsMatrix< ScalarType , OrdinalType , DeviceType , MemoryTraits , SizeType > sparse_matrix_type ; |
| 982 |
typedef typename sparse_matrix_type::StaticCrsGraphType sparse_graph_type ; |
| 983 |
typedef typename sparse_matrix_type::values_type matrix_values_type ; |
| 984 |
typedef Kokkos::View< scalar_type* , execution_space > vector_type ; |
| 985 |
typedef Kokkos::View< scalar_type** , Kokkos::LayoutLeft, execution_space > multi_vector_type ; |
| 986 |
|
| 987 |
//------------------------------------ |
| 988 |
|
| 989 |
typedef LocalViewTraits< vector_type > local_vector_view_traits; |
| 990 |
typedef LocalViewTraits< multi_vector_type > local_multi_vector_view_traits; |
| 991 |
typedef LocalViewTraits< matrix_values_type> local_matrix_view_traits; |
| 992 |
typedef typename local_vector_view_traits::local_view_type local_vector_type; |
| 993 |
typedef typename local_multi_vector_view_traits::local_view_type local_multi_vector_type; |
| 994 |
typedef typename local_matrix_view_traits::local_view_type local_matrix_type; |
| 995 |
static const bool use_team = local_vector_view_traits::use_team; |
| 996 |
|
| 997 |
typedef double bc_scalar_type ; |
| 998 |
|
| 999 |
//------------------------------------ |
| 1000 |
// Computational data: |
| 1001 |
|
| 1002 |
const node_coord_type node_coords ; |
| 1003 |
const vector_type solution ; |
| 1004 |
const multi_vector_type solution_dp ; |
| 1005 |
const sparse_matrix_type jacobian ; |
| 1006 |
const vector_type residual ; |
| 1007 |
const multi_vector_type residual_dp ; |
| 1008 |
const bc_scalar_type bc_lower_value ; |
| 1009 |
const bc_scalar_type bc_upper_value ; |
| 1010 |
const scalar_coord_type bc_lower_limit ; |
| 1011 |
const scalar_coord_type bc_upper_limit ; |
| 1012 |
const unsigned bc_plane ; |
| 1013 |
const unsigned node_count ; |
| 1014 |
bool init ; |
| 1015 |
const KokkosSparse::DeviceConfig dev_config ; |
| 1016 |
const bool assemble_jacobian ; |
| 1017 |
const unsigned num_param ; |
| 1018 |
|
| 1019 |
|
| 1020 |
DirichletComputation( const mesh_type & arg_mesh , |
| 1021 |
const vector_type & arg_solution , |
| 1022 |
const sparse_matrix_type & arg_jacobian , |
| 1023 |
const vector_type & arg_residual , |
| 1024 |
const unsigned arg_bc_plane , |
| 1025 |
const bc_scalar_type arg_bc_lower_value , |
| 1026 |
const bc_scalar_type arg_bc_upper_value , |
| 1027 |
const KokkosSparse::DeviceConfig arg_dev_config , |
| 1028 |
const multi_vector_type & arg_solution_dp = |
| 1029 |
multi_vector_type() , |
| 1030 |
const multi_vector_type & arg_residual_dp = |
| 1031 |
multi_vector_type() , |
| 1032 |
const bool arg_assemble_jacobian = true, |
| 1033 |
const bool arg_init = false ) |
| 1034 |
: node_coords( arg_mesh.node_coord() ) |
| 1035 |
, solution( arg_solution ) |
| 1036 |
, solution_dp( arg_solution_dp ) |
| 1037 |
, jacobian( arg_jacobian ) |
| 1038 |
, residual( arg_residual ) |
| 1039 |
, residual_dp( arg_residual_dp ) |
| 1040 |
, bc_lower_value( arg_bc_lower_value ) |
| 1041 |
, bc_upper_value( arg_bc_upper_value ) |
| 1042 |
, bc_lower_limit( std::numeric_limits<scalar_coord_type>::epsilon() ) |
| 1043 |
, bc_upper_limit( scalar_coord_type(1) - std::numeric_limits<scalar_coord_type>::epsilon() ) |
| 1044 |
, bc_plane( arg_bc_plane ) |
| 1045 |
, node_count( arg_mesh.node_count_owned() ) |
| 1046 |
, init( arg_init ) |
| 1047 |
, dev_config( arg_dev_config ) |
| 1048 |
, assemble_jacobian( arg_assemble_jacobian ) |
| 1049 |
, num_param( solution_dp.extent(1) ) |
| 1050 |
{ |
| 1051 |
if (!init) { |
| 1052 |
parallel_for( node_count , *this ); |
| 1053 |
init = true ; |
| 1054 |
} |
| 1055 |
} |
| 1056 |
|
| 1057 |
void apply() const |
| 1058 |
{ |
| 1059 |
if ( use_team ) { |
| 1060 |
const size_t team_size = dev_config.block_dim.x * dev_config.block_dim.y; |
| 1061 |
const size_t league_size = |
| 1062 |
(node_count + dev_config.block_dim.y-1) / dev_config.block_dim.y; |
| 1063 |
Kokkos::TeamPolicy< execution_space > config( league_size, team_size ); |
| 1064 |
parallel_for( config , *this ); |
| 1065 |
} |
| 1066 |
else |
| 1067 |
parallel_for( node_count , *this ); |
| 1068 |
} |
| 1069 |
|
| 1070 |
//------------------------------------ |
| 1071 |
|
| 1072 |
typedef typename Kokkos::TeamPolicy< execution_space >::member_type team_member ; |
| 1073 |
KOKKOS_INLINE_FUNCTION |
| 1074 |
void operator()( const team_member & dev ) const |
| 1075 |
{ |
| 1076 |
|
| 1077 |
const unsigned num_ensemble_threads = dev_config.block_dim.x ; |
| 1078 |
const unsigned num_node_threads = dev_config.block_dim.y ; |
| 1079 |
const unsigned node_rank = dev.team_rank() / num_ensemble_threads ; |
| 1080 |
const unsigned ensemble_rank = dev.team_rank() % num_ensemble_threads ; |
| 1081 |
|
| 1082 |
const unsigned inode = dev.league_rank() * num_node_threads + node_rank; |
| 1083 |
|
| 1084 |
if (inode >= node_count) |
| 1085 |
return; |
| 1086 |
|
| 1087 |
(*this)( inode, ensemble_rank ); |
| 1088 |
|
| 1089 |
} |
| 1090 |
|
| 1091 |
KOKKOS_INLINE_FUNCTION |
| 1092 |
void operator()( const unsigned inode , |
| 1093 |
const unsigned ensemble_rank = 0) const |
| 1094 |
{ |
| 1095 |
local_vector_type local_residual = |
| 1096 |
local_vector_view_traits::create_local_view(residual, |
| 1097 |
ensemble_rank); |
| 1098 |
local_multi_vector_type local_residual_dp = |
| 1099 |
local_multi_vector_view_traits::create_local_view(residual_dp, |
| 1100 |
ensemble_rank); |
| 1101 |
local_matrix_type local_jacobian_values = |
| 1102 |
local_matrix_view_traits::create_local_view(jacobian.values, |
| 1103 |
ensemble_rank); |
| 1104 |
|
| 1105 |
// Apply dirichlet boundary condition on the Solution and Residual vectors. |
| 1106 |
// To maintain the symmetry of the original global stiffness matrix, |
| 1107 |
// zero out the columns that correspond to boundary conditions, and |
| 1108 |
// update the residual vector accordingly |
| 1109 |
|
| 1110 |
const unsigned iBeg = jacobian.graph.row_map[inode]; |
| 1111 |
const unsigned iEnd = jacobian.graph.row_map[inode+1]; |
| 1112 |
|
| 1113 |
const scalar_coord_type c = node_coords(inode,bc_plane); |
| 1114 |
const bool bc_lower = c <= bc_lower_limit ; |
| 1115 |
const bool bc_upper = bc_upper_limit <= c ; |
| 1116 |
|
| 1117 |
if ( ! init ) { |
| 1118 |
solution(inode) = bc_lower ? bc_lower_value : ( |
| 1119 |
bc_upper ? bc_upper_value : 0 ); |
| 1120 |
for (unsigned i=0; i<num_param; ++i) |
| 1121 |
solution_dp(inode,i) = 0 ; |
| 1122 |
} |
| 1123 |
else { |
| 1124 |
if ( bc_lower || bc_upper ) { |
| 1125 |
|
| 1126 |
local_residual(inode) = 0 ; |
| 1127 |
for (unsigned i=0; i<num_param; ++i) |
| 1128 |
local_residual_dp(inode,i) = 0 ; |
| 1129 |
|
| 1130 |
// zero each value on the row, and leave a one |
| 1131 |
// on the diagonal |
| 1132 |
|
| 1133 |
if (assemble_jacobian) |
| 1134 |
for( unsigned i = iBeg ; i < iEnd ; ++i ) { |
| 1135 |
local_jacobian_values(i) = int(inode) == int(jacobian.graph.entries(i)) ? 1 : 0 ; |
| 1136 |
} |
| 1137 |
} |
| 1138 |
else { |
| 1139 |
|
| 1140 |
// Find any columns that are boundary conditions. |
| 1141 |
// Clear them and adjust the residual vector |
| 1142 |
|
| 1143 |
if (assemble_jacobian) |
| 1144 |
for( unsigned i = iBeg ; i < iEnd ; ++i ) { |
| 1145 |
const unsigned cnode = jacobian.graph.entries(i) ; |
| 1146 |
const scalar_coord_type cc = node_coords(cnode,bc_plane); |
| 1147 |
|
| 1148 |
if ( ( cc <= bc_lower_limit ) || ( bc_upper_limit <= cc ) ) { |
| 1149 |
local_jacobian_values(i) = 0 ; |
| 1150 |
} |
| 1151 |
} |
| 1152 |
} |
| 1153 |
} |
| 1154 |
} |
| 1155 |
}; |
| 1156 |
|
| 1157 |
template< typename FixtureType , typename VectorType , |
| 1158 |
typename MultiVectorType = Kokkos::View<typename VectorType::value_type**, Kokkos::LayoutLeft, typename VectorType::execution_space> > |
| 1159 |
class ResponseComputation |
| 1160 |
{ |
| 1161 |
public: |
| 1162 |
|
| 1163 |
typedef FixtureType fixture_type ; |
| 1164 |
typedef VectorType vector_type ; |
| 1165 |
typedef MultiVectorType multi_vector_type; |
| 1166 |
typedef typename vector_type::execution_space execution_space ; |
| 1167 |
typedef typename vector_type::value_type scalar_type ; |
| 1168 |
typedef scalar_type value_type[]; |
| 1169 |
|
| 1170 |
typedef Kokkos::Example::HexElement_Data< fixture_type::ElemNode > element_data_type ; |
| 1171 |
static const unsigned SpatialDim = element_data_type::spatial_dimension ; |
| 1172 |
static const unsigned TensorDim = SpatialDim * SpatialDim ; |
| 1173 |
static const unsigned ElemNodeCount = element_data_type::element_node_count ; |
| 1174 |
static const unsigned IntegrationCount = element_data_type::integration_count ; |
| 1175 |
|
| 1176 |
//------------------------------------ |
| 1177 |
// Computational data: |
| 1178 |
|
| 1179 |
const element_data_type elem_data ; |
| 1180 |
const fixture_type fixture ; |
| 1181 |
const vector_type solution ; |
| 1182 |
const multi_vector_type solution_dp ; |
| 1183 |
const unsigned num_param ; |
| 1184 |
const unsigned value_count ; |
| 1185 |
|
| 1186 |
ResponseComputation( const ResponseComputation & rhs ) |
| 1187 |
: elem_data() |
| 1188 |
, fixture( rhs.fixture ) |
| 1189 |
, solution( rhs.solution ) |
| 1190 |
, solution_dp( rhs.solution_dp ) |
| 1191 |
, num_param( rhs.num_param ) |
| 1192 |
, value_count( rhs.value_count ) |
| 1193 |
{} |
| 1194 |
|
| 1195 |
ResponseComputation( const fixture_type& arg_fixture , |
| 1196 |
const vector_type & arg_solution , |
| 1197 |
const multi_vector_type& arg_solution_dp = |
| 1198 |
multi_vector_type() ) |
| 1199 |
: elem_data() |
| 1200 |
, fixture( arg_fixture ) |
| 1201 |
, solution( arg_solution ) |
| 1202 |
, solution_dp( arg_solution_dp ) |
| 1203 |
, num_param( solution_dp.extent(1) ) |
| 1204 |
, value_count( num_param+1 ) |
| 1205 |
{} |
| 1206 |
|
| 1207 |
//------------------------------------ |
| 1208 |
|
| 1209 |
scalar_type apply() const |
| 1210 |
{ |
| 1211 |
Teuchos::Array<scalar_type> response(value_count, 0.0); |
| 1212 |
//Kokkos::parallel_reduce( fixture.elem_count() , *this , response ); |
| 1213 |
Kokkos::View<scalar_type*, Kokkos::HostSpace> resp_view(&response[0], value_count); |
| 1214 |
Kokkos::parallel_reduce( solution.extent(0) , *this , resp_view ); |
| 1215 |
return response[0]; |
| 1216 |
} |
| 1217 |
|
| 1218 |
Teuchos::Array<scalar_type> apply_gradient() const |
| 1219 |
{ |
| 1220 |
Teuchos::Array<scalar_type> response(value_count, 0.0); |
| 1221 |
//Kokkos::parallel_reduce( fixture.elem_count() , *this , response ); |
| 1222 |
Kokkos::View<scalar_type*, Kokkos::HostSpace> resp_view(&response[0], value_count); |
| 1223 |
Kokkos::parallel_reduce( solution.extent(0) , *this , resp_view ); |
| 1224 |
return response; |
| 1225 |
} |
| 1226 |
|
| 1227 |
//------------------------------------ |
| 1228 |
|
| 1229 |
/* |
| 1230 |
KOKKOS_INLINE_FUNCTION |
| 1231 |
double compute_detJ( |
| 1232 |
const double grad[][ ElemNodeCount ] , // Gradient of bases master element |
| 1233 |
const double x[] , |
| 1234 |
const double y[] , |
| 1235 |
const double z[] ) const |
| 1236 |
{ |
| 1237 |
enum { j11 = 0 , j12 = 1 , j13 = 2 , |
| 1238 |
j21 = 3 , j22 = 4 , j23 = 5 , |
| 1239 |
j31 = 6 , j32 = 7 , j33 = 8 }; |
| 1240 |
|
| 1241 |
// Jacobian accumulation: |
| 1242 |
|
| 1243 |
double J[ TensorDim ] = { 0, 0, 0, 0, 0, 0, 0, 0, 0 }; |
| 1244 |
|
| 1245 |
for( unsigned i = 0; i < ElemNodeCount ; ++i ) { |
| 1246 |
const double x1 = x[i] ; |
| 1247 |
const double x2 = y[i] ; |
| 1248 |
const double x3 = z[i] ; |
| 1249 |
|
| 1250 |
const double g1 = grad[0][i] ; |
| 1251 |
const double g2 = grad[1][i] ; |
| 1252 |
const double g3 = grad[2][i] ; |
| 1253 |
|
| 1254 |
J[j11] += g1 * x1 ; |
| 1255 |
J[j12] += g1 * x2 ; |
| 1256 |
J[j13] += g1 * x3 ; |
| 1257 |
|
| 1258 |
J[j21] += g2 * x1 ; |
| 1259 |
J[j22] += g2 * x2 ; |
| 1260 |
J[j23] += g2 * x3 ; |
| 1261 |
|
| 1262 |
J[j31] += g3 * x1 ; |
| 1263 |
J[j32] += g3 * x2 ; |
| 1264 |
J[j33] += g3 * x3 ; |
| 1265 |
} |
| 1266 |
|
| 1267 |
// Inverse jacobian: |
| 1268 |
|
| 1269 |
double invJ[ TensorDim ] = { |
| 1270 |
static_cast<double>( J[j22] * J[j33] - J[j23] * J[j32] ) , |
| 1271 |
static_cast<double>( J[j13] * J[j32] - J[j12] * J[j33] ) , |
| 1272 |
static_cast<double>( J[j12] * J[j23] - J[j13] * J[j22] ) , |
| 1273 |
|
| 1274 |
static_cast<double>( J[j23] * J[j31] - J[j21] * J[j33] ) , |
| 1275 |
static_cast<double>( J[j11] * J[j33] - J[j13] * J[j31] ) , |
| 1276 |
static_cast<double>( J[j13] * J[j21] - J[j11] * J[j23] ) , |
| 1277 |
|
| 1278 |
static_cast<double>( J[j21] * J[j32] - J[j22] * J[j31] ) , |
| 1279 |
static_cast<double>( J[j12] * J[j31] - J[j11] * J[j32] ) , |
| 1280 |
static_cast<double>( J[j11] * J[j22] - J[j12] * J[j21] ) }; |
| 1281 |
|
| 1282 |
const double detJ = J[j11] * invJ[j11] + |
| 1283 |
J[j21] * invJ[j12] + |
| 1284 |
J[j31] * invJ[j13] ; |
| 1285 |
|
| 1286 |
return detJ ; |
| 1287 |
} |
| 1288 |
|
| 1289 |
KOKKOS_INLINE_FUNCTION |
| 1290 |
scalar_type contributeResponse( |
| 1291 |
const scalar_type dof_values[] , |
| 1292 |
const double detJ , |
| 1293 |
const double integ_weight , |
| 1294 |
const double bases_vals[] ) const |
| 1295 |
{ |
| 1296 |
// $$ g_i = \int_{\Omega} T^2 d \Omega $$ |
| 1297 |
|
| 1298 |
scalar_type value_at_pt = 0 ; |
| 1299 |
for ( unsigned m = 0 ; m < ElemNodeCount ; m++ ) { |
| 1300 |
value_at_pt += dof_values[m] * bases_vals[m] ; |
| 1301 |
} |
| 1302 |
|
| 1303 |
scalar_type elem_response = |
| 1304 |
value_at_pt * value_at_pt * detJ * integ_weight ; |
| 1305 |
|
| 1306 |
return elem_response; |
| 1307 |
} |
| 1308 |
|
| 1309 |
KOKKOS_INLINE_FUNCTION |
| 1310 |
void operator()( const unsigned ielem , scalar_type& response ) const |
| 1311 |
{ |
| 1312 |
// Gather nodal coordinates and solution vector: |
| 1313 |
|
| 1314 |
double x[ ElemNodeCount ] ; |
| 1315 |
double y[ ElemNodeCount ] ; |
| 1316 |
double z[ ElemNodeCount ] ; |
| 1317 |
scalar_type val[ ElemNodeCount ] ; |
| 1318 |
|
| 1319 |
for ( unsigned i = 0 ; i < ElemNodeCount ; ++i ) { |
| 1320 |
const unsigned ni = fixture.elem_node( ielem , i ); |
| 1321 |
|
| 1322 |
x[i] = fixture.node_coord( ni , 0 ); |
| 1323 |
y[i] = fixture.node_coord( ni , 1 ); |
| 1324 |
z[i] = fixture.node_coord( ni , 2 ); |
| 1325 |
|
| 1326 |
val[i] = solution( ni ); |
| 1327 |
} |
| 1328 |
|
| 1329 |
for ( unsigned i = 0 ; i < IntegrationCount ; ++i ) { |
| 1330 |
|
| 1331 |
const double detJ = compute_detJ( elem_data.gradients[i] , x , y , z ); |
| 1332 |
|
| 1333 |
response += contributeResponse( val , detJ , elem_data.weights[i] , |
| 1334 |
elem_data.values[i] ); |
| 1335 |
} |
| 1336 |
} |
| 1337 |
*/ |
| 1338 |
|
| 1339 |
KOKKOS_INLINE_FUNCTION |
| 1340 |
void operator()( const unsigned i , value_type response ) const |
| 1341 |
{ |
| 1342 |
const scalar_type& u = solution(i); |
| 1343 |
response[0] += (u * u) / fixture.node_count_global(); |
| 1344 |
for (unsigned j=0; j<num_param; ++j) |
| 1345 |
response[j+1] += 2.0 * u * solution_dp(i,j) / fixture.node_count_global(); |
| 1346 |
} |
| 1347 |
|
| 1348 |
KOKKOS_INLINE_FUNCTION |
| 1349 |
void init( value_type response ) const { |
| 1350 |
for (unsigned j=0; j<value_count; ++j) |
| 1351 |
response[j] = 0 ; |
| 1352 |
} |
| 1353 |
|
| 1354 |
KOKKOS_INLINE_FUNCTION |
| 1355 |
void join( value_type response , |
| 1356 |
const value_type input ) const { |
| 1357 |
for (unsigned j=0; j<value_count; ++j) |
| 1358 |
response[j] += input[j] ; |
| 1359 |
} |
| 1360 |
|
| 1361 |
}; /* ResponseComputation */ |
| 1362 |
|
| 1363 |
} /* namespace FENL */ |
| 1364 |
} /* namespace Example */ |
| 1365 |
} /* namespace Kokkos */ |
| 1366 |
|
| 1367 |
//---------------------------------------------------------------------------- |
| 1368 |
|
| 1369 |
#endif /* #ifndef KOKKOS_EXAMPLE_FENLFUNCTORS_HPP */ |