| 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_FENL_IMPL_HPP |
| 18 |
#define KOKKOS_EXAMPLE_FENL_IMPL_HPP |
| 19 |
|
| 20 |
#include <math.h> |
| 21 |
|
| 22 |
// Kokkos libraries' headers: |
| 23 |
|
| 24 |
#include <Kokkos_UnorderedMap.hpp> |
| 25 |
#include <Kokkos_StaticCrsGraph.hpp> |
| 26 |
#include <KokkosSparse_CrsMatrix.hpp> |
| 27 |
#include <Kokkos_Timer.hpp> |
| 28 |
#include <Kokkos_ArithTraits.hpp> |
| 29 |
|
| 30 |
#include <Teuchos_CommHelpers.hpp> |
| 31 |
#include <Teuchos_ParameterList.hpp> |
| 32 |
#include <Teuchos_XMLParameterListHelpers.hpp> |
| 33 |
|
| 34 |
// Examples headers: |
| 35 |
|
| 36 |
#include <BoxElemFixture.hpp> |
| 37 |
#include <fenl.hpp> |
| 38 |
#include <fenl_functors.hpp> |
| 39 |
#include <Tpetra_KokkosCompat_DefaultNode.hpp> |
| 40 |
|
| 41 |
#include <Tpetra_Vector.hpp> |
| 42 |
#include "Tpetra_MultiVector.hpp" |
| 43 |
|
| 44 |
#include <CGSolve.hpp> |
| 45 |
#include <BelosSolve.hpp> |
| 46 |
|
| 47 |
//---------------------------------------------------------------------------- |
| 48 |
|
| 49 |
namespace Kokkos { |
| 50 |
namespace Example { |
| 51 |
namespace FENL { |
| 52 |
|
| 53 |
/* Builds a map from LIDs to GIDs suitable for Tpetra */ |
| 54 |
template < class Map, class Fixture > |
| 55 |
class BuildLocalToGlobalMap { |
| 56 |
const Map m_lid_to_gid; |
| 57 |
const Fixture m_fixture; |
| 58 |
public: |
| 59 |
typedef typename Map::execution_space execution_space; |
| 60 |
typedef typename execution_space::size_type size_type; |
| 61 |
|
| 62 |
BuildLocalToGlobalMap(const Map& lid_to_gid, const Fixture& fixture) : |
| 63 |
m_lid_to_gid(lid_to_gid), |
| 64 |
m_fixture(fixture) |
| 65 |
{} |
| 66 |
|
| 67 |
KOKKOS_INLINE_FUNCTION |
| 68 |
void operator() (const size_type i) const { |
| 69 |
m_lid_to_gid(i) = m_fixture.node_global_index(i); |
| 70 |
} |
| 71 |
}; |
| 72 |
|
| 73 |
template <class Map, class Fixture > |
| 74 |
void build_lid_to_gid(const Map& lid_to_gid, const Fixture& fixture) { |
| 75 |
typedef BuildLocalToGlobalMap<Map,Fixture> F; |
| 76 |
Kokkos::parallel_for(lid_to_gid.extent(0), F(lid_to_gid, fixture)); |
| 77 |
} |
| 78 |
|
| 79 |
} /* namespace FENL */ |
| 80 |
} /* namespace Example */ |
| 81 |
} /* namespace Kokkos */ |
| 82 |
|
| 83 |
//---------------------------------------------------------------------------- |
| 84 |
|
| 85 |
namespace Kokkos { |
| 86 |
namespace Example { |
| 87 |
namespace FENL { |
| 88 |
|
| 89 |
template < class Scalar, class Device , BoxElemPart::ElemOrder ElemOrder > |
| 90 |
class Problem { |
| 91 |
public: |
| 92 |
|
| 93 |
typedef Scalar ScalarType; |
| 94 |
typedef Device DeviceType; |
| 95 |
typedef BoxElemFixture< Device , ElemOrder > FixtureType ; |
| 96 |
|
| 97 |
typedef typename Kokkos::ArithTraits<Scalar>::mag_type Magnitude; |
| 98 |
|
| 99 |
typedef Tpetra::KokkosCompat::KokkosDeviceWrapperNode< Device > NodeType; |
| 100 |
|
| 101 |
typedef Kokkos::View< Scalar * , Kokkos::LayoutLeft, Device > LocalVectorType ; |
| 102 |
typedef Kokkos::View< Scalar** , Kokkos::LayoutLeft, Device > LocalMultiVectorType ; |
| 103 |
typedef Kokkos::DualView< Scalar** , Kokkos::LayoutLeft, Device > LocalDualVectorType; |
| 104 |
|
| 105 |
typedef Tpetra::Map<>::local_ordinal_type LocalOrdinalType; |
| 106 |
typedef Tpetra::Map<>::global_ordinal_type GlobalOrdinalType; |
| 107 |
typedef Tpetra::Map<LocalOrdinalType,GlobalOrdinalType,NodeType> MapType; |
| 108 |
typedef Tpetra::Vector<Scalar,LocalOrdinalType,GlobalOrdinalType,NodeType> GlobalVectorType; |
| 109 |
typedef Tpetra::MultiVector<Scalar,LocalOrdinalType,GlobalOrdinalType,NodeType> GlobalMultiVectorType; |
| 110 |
typedef Tpetra::CrsMatrix<Scalar,LocalOrdinalType,GlobalOrdinalType,NodeType> GlobalMatrixType; |
| 111 |
typedef typename GlobalMatrixType::local_matrix_device_type LocalMatrixType; |
| 112 |
typedef typename LocalMatrixType::StaticCrsGraphType LocalGraphType; |
| 113 |
|
| 114 |
|
| 115 |
typedef NodeNodeGraph< typename FixtureType::elem_node_type |
| 116 |
, LocalGraphType |
| 117 |
, FixtureType::ElemNode |
| 118 |
> NodeNodeGraphType ; |
| 119 |
|
| 120 |
typedef typename NodeNodeGraphType::ElemGraphType ElemGraphType ; |
| 121 |
|
| 122 |
typedef Teuchos::RCP<const MapType> rcpMapType; |
| 123 |
typedef Teuchos::RCP<const Teuchos::Comm<int> > rcpCommType ; |
| 124 |
typedef Teuchos::RCP<Tpetra::KokkosCompat::KokkosDeviceWrapperNode<Device> > rcpNodeType ; |
| 125 |
|
| 126 |
typedef Tpetra::Import< |
| 127 |
typename GlobalVectorType::local_ordinal_type, |
| 128 |
typename GlobalVectorType::global_ordinal_type, |
| 129 |
typename GlobalVectorType::node_type |
| 130 |
> import_type; |
| 131 |
|
| 132 |
private: |
| 133 |
|
| 134 |
typedef Kokkos::View<GlobalOrdinalType*,Device> lid_to_gid_type; |
| 135 |
|
| 136 |
rcpMapType create_row_map() |
| 137 |
{ |
| 138 |
lid_to_gid_type lid_to_gid_row( "lig_to_gid_row", fixture.node_count_owned() ); |
| 139 |
|
| 140 |
build_lid_to_gid(lid_to_gid_row, fixture); |
| 141 |
|
| 142 |
typename lid_to_gid_type::HostMirror lid_to_gid_row_host = |
| 143 |
Kokkos::create_mirror_view(lid_to_gid_row); |
| 144 |
|
| 145 |
Kokkos::deep_copy(lid_to_gid_row_host, lid_to_gid_row); |
| 146 |
|
| 147 |
return Teuchos::rcp (new MapType( fixture.node_count_global(), |
| 148 |
Teuchos::arrayView(lid_to_gid_row_host.data(), |
| 149 |
lid_to_gid_row_host.extent(0)), |
| 150 |
0, comm)); |
| 151 |
} |
| 152 |
|
| 153 |
rcpMapType create_col_map() |
| 154 |
{ |
| 155 |
lid_to_gid_type lid_to_gid_all( "lig_to_gid_all", fixture.node_count()); |
| 156 |
|
| 157 |
build_lid_to_gid(lid_to_gid_all, fixture); |
| 158 |
|
| 159 |
typename lid_to_gid_type::HostMirror lid_to_gid_all_host = |
| 160 |
Kokkos::create_mirror_view(lid_to_gid_all); |
| 161 |
|
| 162 |
Kokkos::deep_copy(lid_to_gid_all_host, lid_to_gid_all); |
| 163 |
|
| 164 |
return Teuchos::rcp (new MapType ( |
| 165 |
Teuchos::OrdinalTraits<Tpetra::global_size_t>::invalid(), |
| 166 |
Teuchos::arrayView( lid_to_gid_all_host.data(), |
| 167 |
lid_to_gid_all_host.extent(0)), |
| 168 |
0,comm) ); |
| 169 |
} |
| 170 |
|
| 171 |
public: |
| 172 |
|
| 173 |
const rcpCommType comm ; |
| 174 |
const FixtureType fixture ; |
| 175 |
|
| 176 |
private: |
| 177 |
|
| 178 |
typename NodeNodeGraphType::Times graph_times; |
| 179 |
|
| 180 |
const NodeNodeGraphType mesh_to_graph ; |
| 181 |
|
| 182 |
public: |
| 183 |
|
| 184 |
const ElemGraphType elem_graph ; |
| 185 |
|
| 186 |
const rcpMapType RowMap ; |
| 187 |
const rcpMapType ColMap ; |
| 188 |
const import_type import ; |
| 189 |
|
| 190 |
GlobalVectorType g_nodal_solution ; |
| 191 |
GlobalVectorType g_nodal_residual ; |
| 192 |
GlobalVectorType g_nodal_delta ; |
| 193 |
GlobalVectorType g_nodal_solution_no_overlap ; |
| 194 |
GlobalMultiVectorType g_nodal_solution_dp ; |
| 195 |
GlobalMultiVectorType g_nodal_residual_dp ; |
| 196 |
GlobalMultiVectorType g_nodal_delta_dp ; |
| 197 |
GlobalMultiVectorType g_nodal_solution_no_overlap_dp ; |
| 198 |
GlobalMatrixType g_jacobian ; |
| 199 |
|
| 200 |
Perf perf ; |
| 201 |
bool print_flag ; |
| 202 |
unsigned num_sensitivities ; |
| 203 |
|
| 204 |
Problem( const rcpCommType & use_comm |
| 205 |
, const int use_nodes[] |
| 206 |
, const double grid_bubble[] |
| 207 |
, const bool use_print |
| 208 |
, const unsigned num_sens |
| 209 |
) |
| 210 |
: comm( use_comm ) |
| 211 |
// Decompose by node to avoid parallel communication in assembly |
| 212 |
, fixture( BoxElemPart::DecomposeNode |
| 213 |
, use_comm->getSize() , use_comm->getRank() |
| 214 |
, use_nodes[0] , use_nodes[1] , use_nodes[2] |
| 215 |
, grid_bubble[0] , grid_bubble[1] , grid_bubble[2] |
| 216 |
) |
| 217 |
, graph_times() |
| 218 |
, mesh_to_graph( fixture.elem_node() |
| 219 |
, fixture.node_count_owned() |
| 220 |
, graph_times ) |
| 221 |
, elem_graph( mesh_to_graph.elem_graph ) |
| 222 |
, RowMap( create_row_map() ) |
| 223 |
, ColMap( create_col_map() ) |
| 224 |
, import( RowMap , ColMap ) |
| 225 |
, g_nodal_solution( ColMap, 1 ) |
| 226 |
, g_nodal_residual( RowMap, 1 ) |
| 227 |
, g_nodal_delta( RowMap, 1 ) |
| 228 |
, g_nodal_solution_no_overlap (g_nodal_solution, RowMap) |
| 229 |
, g_jacobian( RowMap, ColMap, LocalMatrixType( "jacobian" , mesh_to_graph.graph , maximum_entry(mesh_to_graph.graph) + 1 ) ) |
| 230 |
, perf() |
| 231 |
, num_sensitivities(num_sens) |
| 232 |
{ |
| 233 |
if ( maximum(*comm, ( fixture.ok() ? 0 : 1 ) ) ) { |
| 234 |
throw std::runtime_error(std::string("Problem fixture setup failed")); |
| 235 |
} |
| 236 |
|
| 237 |
print_flag = use_print && Kokkos::SpaceAccessibility< Kokkos::HostSpace::execution_space , typename Device::memory_space >::accessible ; |
| 238 |
|
| 239 |
perf.global_elem_count = fixture.elem_count_global(); |
| 240 |
perf.global_node_count = fixture.node_count_global(); |
| 241 |
|
| 242 |
perf.map_ratio = graph_times.ratio; |
| 243 |
perf.fill_node_set = graph_times.fill_node_set; |
| 244 |
perf.scan_node_count = graph_times.scan_node_count; |
| 245 |
perf.fill_graph_entries = graph_times.fill_graph_entries; |
| 246 |
perf.sort_graph_entries = graph_times.sort_graph_entries; |
| 247 |
perf.fill_element_graph = graph_times.fill_element_graph; |
| 248 |
|
| 249 |
if ( print_flag ) { |
| 250 |
std::cout << "ElemNode {" << std::endl ; |
| 251 |
for ( unsigned ielem = 0 ; ielem < fixture.elem_count() ; ++ielem ) { |
| 252 |
std::cout << " elem[" << ielem << "]{" ; |
| 253 |
for ( unsigned inode = 0 ; inode < FixtureType::ElemNode ; ++inode ) { |
| 254 |
std::cout << " " << fixture.elem_node(ielem,inode); |
| 255 |
} |
| 256 |
std::cout << " }" << std::endl ; |
| 257 |
} |
| 258 |
std::cout << "}" << std::endl ; |
| 259 |
|
| 260 |
//------------------------------ |
| 261 |
|
| 262 |
LocalMatrixType jacobian = g_jacobian.getLocalMatrixDevice(); |
| 263 |
|
| 264 |
const unsigned nrow = jacobian.numRows(); |
| 265 |
std::cout << "JacobianGraph[ " |
| 266 |
<< jacobian.numRows() << " x " << jacobian.numCols() |
| 267 |
<< " ] {" << std::endl ; |
| 268 |
for ( unsigned irow = 0 ; irow < nrow ; ++irow ) { |
| 269 |
std::cout << " row[" << irow << "]{" ; |
| 270 |
const unsigned entry_end = jacobian.graph.row_map(irow+1); |
| 271 |
for ( unsigned entry = jacobian.graph.row_map(irow) ; entry < entry_end ; ++entry ) { |
| 272 |
std::cout << " " << jacobian.graph.entries(entry); |
| 273 |
} |
| 274 |
std::cout << " }" << std::endl ; |
| 275 |
} |
| 276 |
std::cout << "}" << std::endl ; |
| 277 |
|
| 278 |
std::cout << "ElemGraph {" << std::endl ; |
| 279 |
for ( unsigned ielem = 0 ; ielem < elem_graph.extent(0) ; ++ielem ) { |
| 280 |
std::cout << " elem[" << ielem << "]{" ; |
| 281 |
for ( unsigned irow = 0 ; irow < elem_graph.extent(1) ; ++irow ) { |
| 282 |
std::cout << " {" ; |
| 283 |
for ( unsigned icol = 0 ; icol < elem_graph.extent(2) ; ++icol ) { |
| 284 |
std::cout << " " << elem_graph(ielem,irow,icol); |
| 285 |
} |
| 286 |
std::cout << " }" ; |
| 287 |
} |
| 288 |
std::cout << " }" << std::endl ; |
| 289 |
} |
| 290 |
std::cout << "}" << std::endl ; |
| 291 |
} |
| 292 |
|
| 293 |
if (num_sensitivities > 0) { |
| 294 |
g_nodal_solution_dp = |
| 295 |
GlobalMultiVectorType( ColMap, num_sensitivities ); |
| 296 |
g_nodal_residual_dp = |
| 297 |
GlobalMultiVectorType( RowMap, num_sensitivities ); |
| 298 |
g_nodal_delta_dp = |
| 299 |
GlobalMultiVectorType( RowMap, num_sensitivities ); |
| 300 |
g_nodal_solution_no_overlap_dp = |
| 301 |
GlobalMultiVectorType(g_nodal_solution_dp, *RowMap); |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
//---------------------------------------- |
| 306 |
|
| 307 |
template < class CoeffFunctionType > |
| 308 |
void solve( const CoeffFunctionType & coeff_function |
| 309 |
, const bool isotropic |
| 310 |
, const double coeff_source |
| 311 |
, const double coeff_advection |
| 312 |
, const double bc_lower_value |
| 313 |
, const double bc_upper_value |
| 314 |
, const unsigned newton_iteration_limit |
| 315 |
, const Magnitude newton_iteration_tolerance |
| 316 |
, const unsigned cg_iteration_limit |
| 317 |
, const Magnitude cg_iteration_tolerance |
| 318 |
, const QuadratureData<Device>& qd |
| 319 |
, const bool use_atomic |
| 320 |
, const bool use_belos |
| 321 |
, const bool use_muelu |
| 322 |
, const bool use_mean_based |
| 323 |
, const Teuchos::RCP<Teuchos::ParameterList>& fenlParams |
| 324 |
, Scalar& response |
| 325 |
, Teuchos::Array<Scalar>& response_gradient |
| 326 |
) |
| 327 |
{ |
| 328 |
typedef ElementComputation< FixtureType , LocalMatrixType , CoeffFunctionType > ElementComputationType ; |
| 329 |
typedef DirichletComputation< FixtureType , LocalMatrixType > DirichletComputationType ; |
| 330 |
typedef ResponseComputation< FixtureType , LocalVectorType > ResponseComputationType ; |
| 331 |
|
| 332 |
Kokkos::Timer wall_clock ; |
| 333 |
Kokkos::Timer newton_clock ; |
| 334 |
newton_clock.reset(); |
| 335 |
|
| 336 |
LocalMatrixType jacobian = g_jacobian.getLocalMatrixDevice(); |
| 337 |
|
| 338 |
const auto k_nodal_solution = g_nodal_solution.getLocalViewDevice(Tpetra::Access::ReadWrite); |
| 339 |
const auto k_nodal_residual = g_nodal_residual.getLocalViewDevice(Tpetra::Access::ReadWrite); |
| 340 |
const auto k_nodal_delta = g_nodal_delta .getLocalViewDevice(Tpetra::Access::ReadWrite); |
| 341 |
|
| 342 |
const LocalVectorType nodal_solution = |
| 343 |
Kokkos::subview(k_nodal_solution,Kokkos::ALL(),0); |
| 344 |
const LocalVectorType nodal_residual = |
| 345 |
Kokkos::subview(k_nodal_residual,Kokkos::ALL(),0); |
| 346 |
const LocalVectorType nodal_delta = |
| 347 |
Kokkos::subview(k_nodal_delta,Kokkos::ALL(),0); |
| 348 |
|
| 349 |
LocalVectorType nodal_solution_no_overlap = |
| 350 |
Kokkos::subview(nodal_solution,std::pair<unsigned,unsigned>(0,fixture.node_count_owned())); |
| 351 |
|
| 352 |
// Get DeviceConfig structs used by some functors |
| 353 |
KokkosSparse::DeviceConfig dev_config_elem, dev_config_gath, dev_config_bc; |
| 354 |
|
| 355 |
CreateDeviceConfigs<Scalar>::eval( dev_config_elem, |
| 356 |
dev_config_gath, |
| 357 |
dev_config_bc ); |
| 358 |
|
| 359 |
// Create element computation functor |
| 360 |
const ElementComputationType elemcomp( fixture , coeff_function , isotropic , |
| 361 |
coeff_source , coeff_advection , |
| 362 |
nodal_solution , |
| 363 |
elem_graph , |
| 364 |
jacobian , nodal_residual , |
| 365 |
dev_config_elem , qd ); |
| 366 |
|
| 367 |
// Create boundary condition functor |
| 368 |
// This also sets the boundary conditions in the solution vector |
| 369 |
// and zeros out non BC values |
| 370 |
const DirichletComputationType dirichlet( |
| 371 |
fixture , nodal_solution , jacobian , nodal_residual , |
| 372 |
2 /* apply at 'z' ends */ , |
| 373 |
bc_lower_value , |
| 374 |
bc_upper_value , |
| 375 |
dev_config_bc ); |
| 376 |
|
| 377 |
// Create response function |
| 378 |
const ResponseComputationType responseFunc( |
| 379 |
fixture, nodal_solution_no_overlap ); |
| 380 |
|
| 381 |
//---------------------------------- |
| 382 |
// Nonlinear Newton iteration: |
| 383 |
|
| 384 |
Magnitude residual_norm_init = 0 ; |
| 385 |
|
| 386 |
// Teuchos::RCP<Teuchos::FancyOStream> out = |
| 387 |
// Teuchos::fancyOStream(Teuchos::rcp(&std::cout,false)); |
| 388 |
// out->setShowProcRank(true); |
| 389 |
|
| 390 |
Teuchos::RCP< Tpetra::Operator<Scalar,LocalOrdinalType,GlobalOrdinalType,NodeType> > precOp; |
| 391 |
for ( perf.newton_iter_count = 0 ; |
| 392 |
perf.newton_iter_count < newton_iteration_limit ; |
| 393 |
++perf.newton_iter_count ) { |
| 394 |
|
| 395 |
//-------------------------------- |
| 396 |
|
| 397 |
Device().fence(); |
| 398 |
wall_clock.reset(); |
| 399 |
g_nodal_solution.doImport (g_nodal_solution_no_overlap, import, Tpetra::REPLACE); |
| 400 |
|
| 401 |
// Take minimum import time across newton steps -- resolves strange |
| 402 |
// timings on titan where time after first solve is much larger |
| 403 |
Device().fence(); |
| 404 |
if (perf.newton_iter_count == 0) |
| 405 |
perf.import_time = wall_clock.seconds(); |
| 406 |
else |
| 407 |
perf.import_time = std::min( perf.import_time, wall_clock.seconds() ); |
| 408 |
|
| 409 |
// if (itrial == 0 && perf.newton_iter_count == 0) |
| 410 |
// g_nodal_solution_no_overlap.describe(*out, Teuchos::VERB_EXTREME); |
| 411 |
|
| 412 |
//-------------------------------- |
| 413 |
// Element contributions to residual and jacobian |
| 414 |
|
| 415 |
Device().fence(); |
| 416 |
wall_clock.reset(); |
| 417 |
|
| 418 |
Kokkos::deep_copy( nodal_residual , 0.0 ); |
| 419 |
Kokkos::deep_copy( jacobian.values ,0.0 ); |
| 420 |
|
| 421 |
elemcomp.apply(); |
| 422 |
|
| 423 |
Device().fence(); |
| 424 |
if (perf.newton_iter_count == 0) |
| 425 |
perf.fill_time = wall_clock.seconds(); |
| 426 |
else |
| 427 |
perf.fill_time = std::min( perf.fill_time, wall_clock.seconds() ); |
| 428 |
|
| 429 |
//-------------------------------- |
| 430 |
// Apply boundary conditions |
| 431 |
|
| 432 |
Device().fence(); |
| 433 |
wall_clock.reset(); |
| 434 |
|
| 435 |
dirichlet.apply(); |
| 436 |
|
| 437 |
Device().fence(); |
| 438 |
if (perf.newton_iter_count == 0) |
| 439 |
perf.bc_time = wall_clock.seconds(); |
| 440 |
else |
| 441 |
perf.bc_time = std::min( perf.bc_time, wall_clock.seconds() ); |
| 442 |
|
| 443 |
//-------------------------------- |
| 444 |
// Evaluate convergence |
| 445 |
|
| 446 |
const Magnitude residual_norm = g_nodal_residual.norm2(); |
| 447 |
|
| 448 |
perf.newton_residual = scalar_norm(residual_norm) ; |
| 449 |
perf.error_max = scalar_norm(residual_norm) ; |
| 450 |
|
| 451 |
if ( 0 == perf.newton_iter_count ) { |
| 452 |
residual_norm_init = residual_norm ; |
| 453 |
} |
| 454 |
|
| 455 |
if ( residual_norm < residual_norm_init * newton_iteration_tolerance ) { |
| 456 |
break ; |
| 457 |
} |
| 458 |
|
| 459 |
//-------------------------------- |
| 460 |
// Solve for nonlinear update |
| 461 |
|
| 462 |
// Zero out newton update vector before solve |
| 463 |
g_nodal_delta.putScalar(0.0); |
| 464 |
|
| 465 |
result_struct cgsolve; |
| 466 |
if (use_belos) { |
| 467 |
// Destroy previous preconditioner (we could make reusing it optional) |
| 468 |
precOp = Teuchos::null; |
| 469 |
cgsolve = belos_solve(g_jacobian, |
| 470 |
g_nodal_residual, |
| 471 |
g_nodal_delta, |
| 472 |
precOp, |
| 473 |
fixture, |
| 474 |
use_muelu, |
| 475 |
use_mean_based , |
| 476 |
fenlParams , |
| 477 |
cg_iteration_limit , |
| 478 |
cg_iteration_tolerance); |
| 479 |
|
| 480 |
// Don't accumulate Belos times as the internal Teuchos timers |
| 481 |
// already accumulate |
| 482 |
perf.mat_vec_time = cgsolve.matvec_time ; |
| 483 |
perf.cg_iter_time = cgsolve.iter_time ; |
| 484 |
perf.prec_setup_time = cgsolve.prec_setup_time ; |
| 485 |
perf.prec_apply_time = cgsolve.prec_apply_time ; |
| 486 |
perf.cg_total_time = cgsolve.total_time ; |
| 487 |
} |
| 488 |
else { |
| 489 |
cgsolve = cg_solve(rcpFromRef(g_jacobian), |
| 490 |
rcpFromRef(g_nodal_residual), |
| 491 |
rcpFromRef(g_nodal_delta), |
| 492 |
cg_iteration_limit, |
| 493 |
cg_iteration_tolerance, |
| 494 |
print_flag); |
| 495 |
perf.mat_vec_time += cgsolve.matvec_time ; |
| 496 |
perf.cg_iter_time += cgsolve.iter_time ; |
| 497 |
perf.prec_setup_time += cgsolve.prec_setup_time ; |
| 498 |
perf.prec_apply_time += cgsolve.prec_apply_time ; |
| 499 |
perf.cg_total_time += cgsolve.total_time ; |
| 500 |
} |
| 501 |
perf.cg_iter_count += cgsolve.iteration ; |
| 502 |
const int ne = cgsolve.ensemble_its.size(); |
| 503 |
perf.ensemble_cg_iter_count.resize(ne); |
| 504 |
for (int i=0; i<ne; ++i) |
| 505 |
perf.ensemble_cg_iter_count[i] += cgsolve.ensemble_its[i]; |
| 506 |
|
| 507 |
// Update solution vector |
| 508 |
|
| 509 |
g_nodal_solution_no_overlap.update(-1.0,g_nodal_delta,1.0); |
| 510 |
|
| 511 |
//-------------------------------- |
| 512 |
|
| 513 |
if ( print_flag ) { |
| 514 |
const Magnitude delta_norm = g_nodal_delta.norm2(); |
| 515 |
|
| 516 |
std::cout << "Newton iteration[" << perf.newton_iter_count << "]" |
| 517 |
<< " residual[" << perf.newton_residual << "]" |
| 518 |
<< " update[" << delta_norm << "]" |
| 519 |
<< " cg_iteration[" << cgsolve.iteration << "]" |
| 520 |
<< " cg_residual[" << cgsolve.norm_res << "]" |
| 521 |
<< std::endl ; |
| 522 |
|
| 523 |
const unsigned nrow = jacobian.numRows(); |
| 524 |
|
| 525 |
std::cout << "Residual {" ; |
| 526 |
for ( unsigned irow = 0 ; irow < nrow ; ++irow ) { |
| 527 |
std::cout << " " << nodal_residual(irow); |
| 528 |
} |
| 529 |
std::cout << " }" << std::endl ; |
| 530 |
|
| 531 |
std::cout << "Delta {" ; |
| 532 |
for ( unsigned irow = 0 ; irow < nrow ; ++irow ) { |
| 533 |
std::cout << " " << nodal_delta(irow); |
| 534 |
} |
| 535 |
std::cout << " }" << std::endl ; |
| 536 |
|
| 537 |
std::cout << "Solution {" ; |
| 538 |
for ( unsigned irow = 0 ; irow < nrow ; ++irow ) { |
| 539 |
std::cout << " " << nodal_solution(irow); |
| 540 |
} |
| 541 |
std::cout << " }" << std::endl ; |
| 542 |
|
| 543 |
std::cout << "Jacobian[ " |
| 544 |
<< jacobian.numRows() << " x " << jacobian.numCols() |
| 545 |
<< " ] {" << std::endl ; |
| 546 |
for ( unsigned irow = 0 ; irow < nrow ; ++irow ) { |
| 547 |
std::cout << " {" ; |
| 548 |
const unsigned entry_end = jacobian.graph.row_map(irow+1); |
| 549 |
for ( unsigned entry = jacobian.graph.row_map(irow) ; entry < entry_end ; ++entry ) { |
| 550 |
std::cout << " (" << jacobian.graph.entries(entry) |
| 551 |
<< "," << jacobian.values(entry) |
| 552 |
<< ")" ; |
| 553 |
} |
| 554 |
std::cout << " }" << std::endl ; |
| 555 |
} |
| 556 |
std::cout << "}" << std::endl ; |
| 557 |
} |
| 558 |
//break; |
| 559 |
} |
| 560 |
|
| 561 |
// Evaluate response function -- currently 2-norm of solution vector |
| 562 |
|
| 563 |
response = responseFunc.apply(); |
| 564 |
response = Kokkos::Example::all_reduce( response , comm ); |
| 565 |
|
| 566 |
// Compute sensitivity of response function if requested |
| 567 |
if (response_gradient.size() > 0 && num_sensitivities > 0) { |
| 568 |
#ifdef HAVE_TRILINOSCOUPLINGS_SACADO |
| 569 |
typedef Sacado::Fad::SLFad<Scalar,10> FadType; |
| 570 |
typedef FadCoeffFunctionTraits<CoeffFunctionType, FadType> CoeffTraits; |
| 571 |
typedef typename CoeffTraits::type FadCoeffFunctionType; |
| 572 |
typedef ParamSensitivityGatherScatterOp<LocalMultiVectorType> SensGatherScatter; |
| 573 |
typedef ElementComputation< FixtureType , LocalMatrixType , FadCoeffFunctionType, SensGatherScatter, FadType > FadElementComputationType ; |
| 574 |
|
| 575 |
// Check sizes match |
| 576 |
TEUCHOS_TEST_FOR_EXCEPTION( |
| 577 |
response_gradient.size() != num_sensitivities, |
| 578 |
std::logic_error, |
| 579 |
"Response gradient length must match number of sensitivities specified in constuctor"); |
| 580 |
|
| 581 |
const auto nodal_solution_dp = |
| 582 |
g_nodal_solution_dp.getLocalViewDevice(Tpetra::Access::ReadWrite); |
| 583 |
const auto nodal_residual_dp = |
| 584 |
g_nodal_residual_dp.getLocalViewDevice(Tpetra::Access::ReadWrite); |
| 585 |
const auto nodal_delta_dp = |
| 586 |
g_nodal_delta_dp.getLocalViewDevice(Tpetra::Access::ReadWrite); |
| 587 |
|
| 588 |
LocalMultiVectorType nodal_solution_no_overlap_dp = |
| 589 |
Kokkos::subview( |
| 590 |
nodal_solution_dp, |
| 591 |
std::pair<unsigned,unsigned>(0,fixture.node_count_owned()), |
| 592 |
Kokkos::ALL()); |
| 593 |
|
| 594 |
const FadCoeffFunctionType coeff_function_dp = |
| 595 |
CoeffTraits::eval(coeff_function); |
| 596 |
|
| 597 |
const SensGatherScatter gather_scatter(nodal_solution_dp, |
| 598 |
nodal_residual_dp); |
| 599 |
const FadElementComputationType elemcomp_dp( |
| 600 |
fixture , coeff_function_dp , isotropic , |
| 601 |
coeff_source , coeff_advection , |
| 602 |
nodal_solution , |
| 603 |
elem_graph , |
| 604 |
jacobian , nodal_residual , |
| 605 |
dev_config_elem , qd , gather_scatter , false ); |
| 606 |
|
| 607 |
const DirichletComputationType dirichlet_dp( |
| 608 |
fixture , nodal_solution , jacobian , nodal_residual , |
| 609 |
2 /* apply at 'z' ends */ , |
| 610 |
bc_lower_value , |
| 611 |
bc_upper_value , |
| 612 |
dev_config_bc , |
| 613 |
nodal_solution_dp , nodal_residual_dp , false , true ); |
| 614 |
|
| 615 |
const ResponseComputationType responseFunc_dp( |
| 616 |
fixture , nodal_solution_no_overlap , nodal_solution_no_overlap_dp ); |
| 617 |
|
| 618 |
g_nodal_solution.doImport (g_nodal_solution_no_overlap, import, Tpetra::REPLACE); |
| 619 |
g_nodal_solution_dp.doImport (g_nodal_solution_no_overlap_dp, import, Tpetra::REPLACE); |
| 620 |
|
| 621 |
Device().fence(); |
| 622 |
wall_clock.reset(); |
| 623 |
|
| 624 |
Kokkos::deep_copy( nodal_residual , 0.0 ); |
| 625 |
Kokkos::deep_copy( nodal_residual_dp , 0.0 ); |
| 626 |
|
| 627 |
elemcomp_dp.apply(); |
| 628 |
dirichlet_dp.apply(); |
| 629 |
|
| 630 |
Device().fence(); |
| 631 |
perf.tangent_fill_time = wall_clock.seconds(); |
| 632 |
|
| 633 |
result_struct cgsolve; |
| 634 |
if (use_belos) { |
| 635 |
// Reuse preconditioner from forward solve |
| 636 |
cgsolve = belos_solve(g_jacobian, |
| 637 |
g_nodal_residual_dp, |
| 638 |
g_nodal_delta_dp, |
| 639 |
precOp, |
| 640 |
fixture, |
| 641 |
use_muelu, |
| 642 |
use_mean_based , |
| 643 |
fenlParams , |
| 644 |
cg_iteration_limit , |
| 645 |
cg_iteration_tolerance); |
| 646 |
|
| 647 |
// Don't accumulate Belos times as the internal Teuchos timers |
| 648 |
// already accumulate |
| 649 |
perf.mat_vec_time = cgsolve.matvec_time ; |
| 650 |
perf.cg_iter_time = cgsolve.iter_time ; |
| 651 |
perf.prec_setup_time = cgsolve.prec_setup_time ; |
| 652 |
perf.prec_apply_time = cgsolve.prec_apply_time ; |
| 653 |
perf.cg_total_time = cgsolve.total_time ; |
| 654 |
perf.cg_iter_count += cgsolve.iteration ; |
| 655 |
} |
| 656 |
else { |
| 657 |
for (unsigned i=0; i<num_sensitivities; ++i) { |
| 658 |
cgsolve = cg_solve(rcpFromRef(g_jacobian), |
| 659 |
g_nodal_residual_dp.getVectorNonConst(i), |
| 660 |
g_nodal_delta_dp.getVectorNonConst(i), |
| 661 |
cg_iteration_limit, |
| 662 |
cg_iteration_tolerance, |
| 663 |
print_flag); |
| 664 |
perf.mat_vec_time += cgsolve.matvec_time ; |
| 665 |
perf.cg_iter_time += cgsolve.iter_time ; |
| 666 |
perf.prec_setup_time += cgsolve.prec_setup_time ; |
| 667 |
perf.prec_apply_time += cgsolve.prec_apply_time ; |
| 668 |
perf.cg_total_time += cgsolve.total_time ; |
| 669 |
perf.cg_iter_count += cgsolve.iteration ; |
| 670 |
} |
| 671 |
} |
| 672 |
const int ne = cgsolve.ensemble_its.size(); |
| 673 |
perf.ensemble_cg_iter_count.resize(ne); |
| 674 |
for (int i=0; i<ne; ++i) |
| 675 |
perf.ensemble_cg_iter_count[i] += cgsolve.ensemble_its[i]; |
| 676 |
|
| 677 |
g_nodal_solution_no_overlap_dp.update(-1.0,g_nodal_delta_dp,1.0); |
| 678 |
|
| 679 |
Teuchos::Array<Scalar> response_and_gradient = |
| 680 |
responseFunc_dp.apply_gradient(); |
| 681 |
|
| 682 |
response_and_gradient = |
| 683 |
Kokkos::Example::all_reduce( response_and_gradient , comm ); |
| 684 |
for (unsigned i=0; i<num_sensitivities; ++i) |
| 685 |
response_gradient[i] = response_and_gradient[i+1]; |
| 686 |
response = response_and_gradient[0]; |
| 687 |
#endif |
| 688 |
} |
| 689 |
|
| 690 |
Device().fence(); |
| 691 |
perf.newton_total_time = newton_clock.seconds(); |
| 692 |
} |
| 693 |
}; |
| 694 |
|
| 695 |
template < class Scalar, class Device , BoxElemPart::ElemOrder ElemOrder, |
| 696 |
class CoeffFunctionType > |
| 697 |
Perf fenl( |
| 698 |
Problem< Scalar, Device , ElemOrder >& problem, |
| 699 |
const Teuchos::RCP<Teuchos::ParameterList>& fenlParams, |
| 700 |
const int use_print , |
| 701 |
const int use_trials , |
| 702 |
const int use_atomic , |
| 703 |
const int use_belos , |
| 704 |
const int use_muelu , |
| 705 |
const int use_mean_based , |
| 706 |
const CoeffFunctionType& coeff_function , |
| 707 |
const bool isotropic, |
| 708 |
const double coeff_source , |
| 709 |
const double coeff_advection , |
| 710 |
const double bc_lower_value , |
| 711 |
const double bc_upper_value , |
| 712 |
Scalar& response, |
| 713 |
Teuchos::Array<Scalar>& response_gradient, |
| 714 |
const QuadratureData<Device>& qd = QuadratureData<Device>() ) |
| 715 |
{ |
| 716 |
typedef typename Kokkos::ArithTraits<Scalar>::mag_type Magnitude; |
| 717 |
|
| 718 |
const unsigned newton_iteration_limit = |
| 719 |
fenlParams->get("Max Nonlinear Iterations", 10) ; |
| 720 |
const Magnitude newton_iteration_tolerance = |
| 721 |
fenlParams->get("Nonlinear Solver Tolerance", 1e-7) ; |
| 722 |
const unsigned cg_iteration_limit = |
| 723 |
fenlParams->get("Max Linear Iterations", 2000) ; |
| 724 |
const Magnitude cg_iteration_tolerance = |
| 725 |
fenlParams->get("Linear Solver Tolerance", 1e-7) ; |
| 726 |
|
| 727 |
//------------------------------------ |
| 728 |
|
| 729 |
Kokkos::Timer wall_clock ; |
| 730 |
|
| 731 |
Perf perf_stats = Perf() ; |
| 732 |
|
| 733 |
// Since the perf struc inside Problem is reused each time solve() is called |
| 734 |
// zero out some stats that we don't want to accumulate |
| 735 |
problem.perf.mat_vec_time = 0; |
| 736 |
problem.perf.cg_iter_time = 0; |
| 737 |
problem.perf.prec_setup_time = 0; |
| 738 |
problem.perf.prec_apply_time = 0; |
| 739 |
problem.perf.cg_total_time = 0; |
| 740 |
problem.perf.newton_total_time = 0; |
| 741 |
problem.perf.cg_iter_count = 0; |
| 742 |
problem.perf.ensemble_cg_iter_count.clear(); |
| 743 |
problem.perf.import_time = 0; |
| 744 |
problem.perf.fill_time = 0; |
| 745 |
problem.perf.tangent_fill_time = 0; |
| 746 |
problem.perf.bc_time = 0; |
| 747 |
|
| 748 |
for ( int itrial = 0 ; itrial < use_trials ; ++itrial ) { |
| 749 |
|
| 750 |
problem.solve( coeff_function |
| 751 |
, isotropic |
| 752 |
, coeff_source |
| 753 |
, coeff_advection |
| 754 |
, bc_lower_value |
| 755 |
, bc_upper_value |
| 756 |
, newton_iteration_limit |
| 757 |
, newton_iteration_tolerance |
| 758 |
, cg_iteration_limit |
| 759 |
, cg_iteration_tolerance |
| 760 |
, qd |
| 761 |
, use_atomic |
| 762 |
, use_belos |
| 763 |
, use_muelu |
| 764 |
, use_mean_based |
| 765 |
, fenlParams |
| 766 |
, response |
| 767 |
, response_gradient |
| 768 |
); |
| 769 |
|
| 770 |
problem.perf.reduceMax(*problem.comm); |
| 771 |
|
| 772 |
if ( 0 == itrial ) { |
| 773 |
perf_stats = problem.perf ; |
| 774 |
} |
| 775 |
else { |
| 776 |
perf_stats.min(problem.perf); |
| 777 |
} |
| 778 |
} |
| 779 |
|
| 780 |
return perf_stats ; |
| 781 |
} |
| 782 |
|
| 783 |
template < class Scalar, class Device , BoxElemPart::ElemOrder ElemOrder, |
| 784 |
class CoeffFunctionType > |
| 785 |
Perf fenl( |
| 786 |
const Teuchos::RCP<const Teuchos::Comm<int> >& comm , |
| 787 |
const std::string& fenl_xml_file, |
| 788 |
const int use_print , |
| 789 |
const int use_trials , |
| 790 |
const int use_atomic , |
| 791 |
const int use_belos , |
| 792 |
const int use_muelu , |
| 793 |
const int use_mean_based , |
| 794 |
const int use_nodes[] , |
| 795 |
const CoeffFunctionType& coeff_function , |
| 796 |
const bool isotropic, |
| 797 |
const double coeff_source , |
| 798 |
const double coeff_advection , |
| 799 |
const double bc_lower_value , |
| 800 |
const double bc_upper_value , |
| 801 |
Scalar& response, |
| 802 |
Teuchos::Array<Scalar>& response_gradient, |
| 803 |
const QuadratureData<Device>& qd = QuadratureData<Device>() ) |
| 804 |
{ |
| 805 |
|
| 806 |
typedef Problem< Scalar, Device , ElemOrder > ProblemType ; |
| 807 |
|
| 808 |
//------------------------------------ |
| 809 |
// Read in any params from xml file |
| 810 |
Teuchos::RCP<Teuchos::ParameterList> fenlParams = Teuchos::parameterList(); |
| 811 |
Teuchos::updateParametersFromXmlFileAndBroadcast( |
| 812 |
fenl_xml_file, fenlParams.ptr(), *comm); |
| 813 |
|
| 814 |
//------------------------------------ |
| 815 |
// Problem setup: |
| 816 |
|
| 817 |
const double geom_bubble[3] = { 1.0 , 1.0 , 1.0 }; |
| 818 |
ProblemType problem( comm , use_nodes , geom_bubble , use_print , |
| 819 |
response_gradient.size() ); |
| 820 |
|
| 821 |
//------------------------------------ |
| 822 |
// Solve |
| 823 |
|
| 824 |
return fenl( problem, |
| 825 |
fenlParams, |
| 826 |
use_print, |
| 827 |
use_trials, |
| 828 |
use_atomic, |
| 829 |
use_belos, |
| 830 |
use_muelu, |
| 831 |
use_mean_based, |
| 832 |
coeff_function, |
| 833 |
isotropic, |
| 834 |
coeff_source, |
| 835 |
coeff_advection, |
| 836 |
bc_lower_value, |
| 837 |
bc_upper_value, |
| 838 |
response, |
| 839 |
response_gradient, |
| 840 |
qd ); |
| 841 |
} |
| 842 |
|
| 843 |
} /* namespace FENL */ |
| 844 |
} /* namespace Example */ |
| 845 |
} /* namespace Kokkos */ |
| 846 |
|
| 847 |
#endif /* #ifndef KOKKOS_EXAMPLE_FENL_IMPL_HPP */ |