Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SmartField usage to some legacy algorithms #1236

Merged
merged 6 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/Algorithm.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Realm;
class MasterElement;
class SupplementalAlgorithm;
class Kernel;
class FieldManager;

class Algorithm
{
Expand All @@ -43,6 +44,7 @@ class Algorithm

Realm& realm_;
stk::mesh::PartVector partVec_;
const FieldManager& fieldManager_;
std::vector<SupplementalAlgorithm*> supplementalAlg_;

std::vector<Kernel*> activeKernels_;
Expand Down
10 changes: 5 additions & 5 deletions include/FieldManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class FieldManager
const std::string& name,
const stk::mesh::PartVector& parts,
const void* init_val = nullptr,
stk::mesh::FieldState state = stk::mesh::FieldState::StateNone)
stk::mesh::FieldState state = stk::mesh::FieldState::StateNone) const
{
const int numStates = 0;
const int numComponents = 0;
Expand All @@ -70,7 +70,7 @@ class FieldManager
}

/// Check to see if the field has been registered.
bool field_exists(const std::string& name);
bool field_exists(const std::string& name) const;

unsigned size() const { return meta_.get_fields().size(); }
/// Register a Generic field.
Expand All @@ -83,7 +83,7 @@ class FieldManager
const int numStates,
const int numComponents,
const void* init_val = nullptr,
stk::mesh::FieldState state = stk::mesh::FieldState::StateNone)
stk::mesh::FieldState state = stk::mesh::FieldState::StateNone) const
{
register_field(name, parts, numStates, numComponents, init_val);
return get_field_ptr<GenericFieldType::value_type>(name, state);
Expand Down Expand Up @@ -119,12 +119,12 @@ class FieldManager
/// field Registry is a static compile-time definition. Care must be taken
/// not to re-register the same field on the same parts with a conflicting
/// number of states or components.
FieldPointerTypes register_field(
FieldPointerTypes register_field (
const std::string& name,
const stk::mesh::PartVector& parts,
const int numStates = 0,
const int numComponents = 0,
const void* init_val = nullptr);
const void* init_val = nullptr) const;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious how field registry can be a compile-time definition, if it depends on a part-vector which would have run-time contents.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking the type look up could be moved to compile time so that when fields are accessed we wouldn't have to include the type. Registry would still have to be runtime. However, looking at the dependencies I'm not sure I want to do that anymore. We'd have to expose more of the field information across the code base instead of isolating it to a single *.C file. This could wreak havoc on the compile times. After we convert to the simple fields the type information becomes way less cumbersome for accessing fields. So I think we should keep it as is.


/// Given the named field that has already been registered on the CPU
/// return the GPU version of the same field.
Expand Down
14 changes: 14 additions & 0 deletions include/SmartField.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ class SmartField<FieldType, tags::LEGACY, ACCESS>
return stk::mesh::field_data(stkField_, entity);
}

template <typename A = ACCESS>
inline typename std::enable_if_t<!std::is_same_v<A, READ>, T>*
operator()(const stk::mesh::Bucket& bucket) const
{
return stk::mesh::field_data(stkField_, bucket);
}

// --- Const Accessors
template <typename A = ACCESS>
inline const typename std::enable_if_t<std::is_same_v<A, READ>, T>*
Expand All @@ -125,6 +132,13 @@ class SmartField<FieldType, tags::LEGACY, ACCESS>
return stk::mesh::field_data(stkField_, entity);
}

template <typename A = ACCESS>
inline const typename std::enable_if_t<std::is_same_v<A, READ>, T>*
operator()(const stk::mesh::Bucket& bucket) const
{
return stk::mesh::field_data(stkField_, bucket);
}

~SmartField()
{
if (is_write_) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,11 @@ class ThermalConductivityFromPrandtlPropAlgorithm : public Algorithm
ThermalConductivityFromPrandtlPropAlgorithm(
Realm& realm,
const stk::mesh::PartVector& part_vec,
ScalarFieldType* thermalCond,
ScalarFieldType* specificHeat,
ScalarFieldType* viscosity,
const double Pr);

virtual ~ThermalConductivityFromPrandtlPropAlgorithm() {}

virtual void execute();

ScalarFieldType* thermalCond_;
ScalarFieldType* specHeat_;
ScalarFieldType* viscosity_;

const double Pr_;
};

Expand Down
5 changes: 3 additions & 2 deletions src/Algorithm.C
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <Algorithm.h>
#include <SupplementalAlgorithm.h>
#include <Realm.h>
#include <kernel/Kernel.h>

namespace sierra {
Expand All @@ -23,14 +24,14 @@ namespace nalu {
//-------- constructor -----------------------------------------------------
//--------------------------------------------------------------------------
Algorithm::Algorithm(Realm& realm, stk::mesh::Part* part)
: realm_(realm), partVec_(1, part)
: realm_(realm), partVec_(1, part), fieldManager_(*(realm.fieldManager_.get()))
{
// nothing to do
}

// alternative; provide full partVec
Algorithm::Algorithm(Realm& realm, const stk::mesh::PartVector& partVec)
: realm_(realm), partVec_(partVec)
: realm_(realm), partVec_(partVec), fieldManager_(*(realm.fieldManager_.get()))
{
// nothing to do
}
Expand Down
2 changes: 1 addition & 1 deletion src/EnthalpyEquationSystem.C
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ EnthalpyEquationSystem::register_nodal_fields(
"this constant value"
<< std::endl;
Algorithm* propAlg = new ThermalConductivityFromPrandtlPropAlgorithm(
realm_, part_vec, thermalCond_, specHeat_, visc_, providedPr);
realm_, part_vec, providedPr);
propertyAlg_.push_back(propAlg);
} else {
// no Pr provided, simply augment property map and expect lambda to be
Expand Down
4 changes: 2 additions & 2 deletions src/FieldManager.C
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ FieldManager::FieldManager(stk::mesh::MetaData& meta, const int numStates)
}

bool
FieldManager::field_exists(const std::string& name)
FieldManager::field_exists(const std::string& name) const
{
auto definition = FieldRegistry::query(numDimensions_, numStates_, name);

Expand All @@ -37,7 +37,7 @@ FieldManager::register_field(
const stk::mesh::PartVector& parts,
const int numStates,
const int numComponents,
const void* init_val)
const void* init_val) const
{
auto definition = FieldRegistry::query(numDimensions_, numStates_, name);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <Algorithm.h>
#include <property_evaluator/ThermalConductivityFromPrandtlPropAlgorithm.h>
#include <FieldManager.h>
#include <FieldTypeDef.h>
#include <Realm.h>

Expand All @@ -33,17 +34,13 @@ ThermalConductivityFromPrandtlPropAlgorithm::
ThermalConductivityFromPrandtlPropAlgorithm(
Realm& realm,
const stk::mesh::PartVector& part_vec,
ScalarFieldType* thermalCond,
ScalarFieldType* specHeat,
ScalarFieldType* viscosity,
const double Pr)
: Algorithm(realm, part_vec),
thermalCond_(thermalCond),
specHeat_(specHeat),
viscosity_(viscosity),
Pr_(Pr)
{
// does nothing
fieldManager_.register_field<ScalarFieldType>("thermal_conductivity", part_vec);
fieldManager_.register_field<ScalarFieldType>("specific_heat", part_vec);
fieldManager_.register_field<ScalarFieldType>("viscosity", part_vec);
}

//--------------------------------------------------------------------------
Expand All @@ -60,25 +57,19 @@ ThermalConductivityFromPrandtlPropAlgorithm::execute()
stk::mesh::BucketVector const& node_buckets =
realm_.get_buckets(stk::topology::NODE_RANK, selector);

thermalCond_->sync_to_host();
specHeat_->sync_to_host();
viscosity_->sync_to_host();
auto thermalCond = fieldManager_.get_legacy_smart_field<sierra::nalu::ScalarFieldType, tags::READ_WRITE>("thermal_conductivity");
const auto specHeat = fieldManager_.get_legacy_smart_field<sierra::nalu::ScalarFieldType, tags::READ>("specific_heat");
const auto viscosity = fieldManager_.get_legacy_smart_field<sierra::nalu::ScalarFieldType, tags::READ>("viscosity");

for (stk::mesh::BucketVector::const_iterator ib = node_buckets.begin();
ib != node_buckets.end(); ++ib) {
stk::mesh::Bucket& b = **ib;
const stk::mesh::Bucket::size_type length = b.size();

double* thermalCond = stk::mesh::field_data(*thermalCond_, b);
const double* specHeat = stk::mesh::field_data(*specHeat_, b);
const double* viscosity = stk::mesh::field_data(*viscosity_, b);

for (stk::mesh::Bucket::size_type k = 0; k < length; ++k) {
thermalCond[k] = specHeat[k] * viscosity[k] / Pr_;
thermalCond(b)[k] = specHeat(b)[k] * viscosity(b)[k] / Pr_;
}
}
thermalCond_->modify_on_host();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's nice to be able to remove these modify and sync calls.

thermalCond_->sync_to_device();
}

} // namespace nalu
Expand Down
Loading