Line data Source code
1 : /**********************************************************************/ 2 : /* DO NOT MODIFY THIS HEADER */ 3 : /* Swift, a Fourier spectral solver for MOOSE */ 4 : /* */ 5 : /* Copyright 2024 Battelle Energy Alliance, LLC */ 6 : /* ALL RIGHTS RESERVED */ 7 : /**********************************************************************/ 8 : 9 : #include "ComputeGroup.h" 10 : #include "MooseError.h" 11 : #include "TensorProblem.h" 12 : #include "SwiftUtils.h" 13 : #include <utility> 14 : 15 : registerMooseObject("SwiftApp", ComputeGroup); 16 : 17 : InputParameters 18 264 : ComputeGroup::validParams() 19 : { 20 264 : InputParameters params = TensorOperatorBase::validParams(); 21 264 : params.addClassDescription("Group of operators with internal dependency resolution."); 22 528 : params.addParam<std::vector<TensorComputeName>>( 23 : "computes", {}, "List of grouped tensor computes."); 24 264 : return params; 25 0 : } 26 : 27 132 : ComputeGroup::ComputeGroup(const InputParameters & parameters) 28 132 : : TensorOperatorBase(parameters), _visited(false), _compute_count(0) 29 : { 30 132 : } 31 : 32 : void 33 130 : ComputeGroup::init() 34 : { 35 : // grab requested computes 36 260 : const auto & computes = getParam<std::vector<TensorComputeName>>("computes"); 37 130 : std::set<TensorComputeName> requested_computes(computes.begin(), computes.end()); 38 712 : for (const auto & cmp : _tensor_problem.getComputes()) 39 : if (requested_computes.count(cmp->name())) 40 432 : _computes.push_back(cmp); 41 130 : } 42 : 43 : void 44 82864 : ComputeGroup::computeBuffer() 45 : { 46 599712 : for (const auto i : index_range(_computes)) 47 : { 48 516848 : if (_domain.debug()) 49 : { 50 : mooseInfoRepeated("check tensors"); 51 0 : for (const auto & [tensor, buffer_name, compute_name] : _checked_tensors[i]) 52 0 : if (!tensor->defined()) 53 0 : mooseError("The tensor '", 54 : buffer_name, 55 : "' requested by '", 56 : compute_name, 57 : "' is not defined yet. Initialize it first."); 58 : } 59 : 60 : const auto & cmp = _computes[i]; 61 : try 62 : { 63 516848 : cmp->computeBuffer(); 64 : } 65 0 : catch (const std::exception & e) 66 : { 67 0 : cmp->mooseError("Exception: ", e.what()); 68 0 : } 69 : } 70 : 71 82864 : _compute_count++; 72 82864 : } 73 : 74 : void 75 130 : ComputeGroup::updateDependencies() 76 : { 77 : // detect recursive self use 78 130 : if (!_visited) 79 130 : _visited = true; 80 : else 81 0 : paramError("computes", "Compute is using itself, creating an unresolvable dependency."); 82 : 83 : // recursively update dependencies of the constituent operators 84 562 : for (const auto & cmp : _computes) 85 432 : cmp->updateDependencies(); 86 : 87 : // dependency resolution of TensorComputes 88 130 : DependencyResolverInterface::sort(_computes); 89 : 90 : // determine total in/out 91 : std::set<std::string> in, out; 92 562 : for (const auto & cmp : _computes) 93 : { 94 432 : const auto & cin = cmp->getRequestedItems(); 95 432 : const auto & cout = cmp->getSuppliedItems(); 96 432 : in.insert(cin.begin(), cin.end()); 97 432 : out.insert(cout.begin(), cout.end()); 98 : 99 : // assemble list of requested buffers for diagnostic purposes 100 : CheckedTensorList cmp_checked_tensors; 101 1020 : for (const auto & buffer_name : cin) 102 588 : cmp_checked_tensors.emplace_back( 103 588 : &_tensor_problem.getRawBuffer(buffer_name), buffer_name, cmp->name()); 104 432 : _checked_tensors.push_back(cmp_checked_tensors); 105 432 : } 106 : 107 130 : std::set_difference(in.begin(), 108 : in.end(), 109 : out.begin(), 110 : out.end(), 111 130 : std::inserter(_requested_buffers, _requested_buffers.begin())); 112 130 : std::set_difference(out.begin(), 113 : out.end(), 114 : in.begin(), 115 : in.end(), 116 130 : std::inserter(_supplied_buffers, _supplied_buffers.begin())); 117 130 : }