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

patchkernel: simplify initialization of the patch #372

Merged
merged 1 commit into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
108 changes: 0 additions & 108 deletions src/patchkernel/patch_kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,6 @@ PatchKernel::PatchKernel(const PatchKernel &other)
m_boxMaxCounter(other.m_boxMaxCounter),
m_adjacenciesBuildStrategy(other.m_adjacenciesBuildStrategy),
m_interfacesBuildStrategy(other.m_interfacesBuildStrategy),
m_spawnStatus(other.m_spawnStatus),
m_adaptionMode(other.m_adaptionMode),
m_adaptionStatus(other.m_adaptionStatus),
m_dimension(other.m_dimension),
Expand Down Expand Up @@ -330,7 +329,6 @@ PatchKernel::PatchKernel(PatchKernel &&other)
m_boxMaxCounter(std::move(other.m_boxMaxCounter)),
m_adjacenciesBuildStrategy(std::move(other.m_adjacenciesBuildStrategy)),
m_interfacesBuildStrategy(std::move(other.m_interfacesBuildStrategy)),
m_spawnStatus(std::move(other.m_spawnStatus)),
m_adaptionMode(std::move(other.m_adaptionMode)),
m_adaptionStatus(std::move(other.m_adaptionStatus)),
m_id(std::move(other.m_id)),
Expand Down Expand Up @@ -420,7 +418,6 @@ PatchKernel & PatchKernel::operator=(PatchKernel &&other)
m_boxMaxCounter = std::move(other.m_boxMaxCounter);
m_adjacenciesBuildStrategy = std::move(other.m_adjacenciesBuildStrategy);
m_interfacesBuildStrategy = std::move(other.m_interfacesBuildStrategy);
m_spawnStatus = std::move(other.m_spawnStatus);
m_adaptionMode = std::move(other.m_adaptionMode);
m_adaptionStatus = std::move(other.m_adaptionStatus);
m_id = std::move(other.m_id);
Expand Down Expand Up @@ -524,12 +521,6 @@ void PatchKernel::initialize()
// Set interfaces build strategy
setInterfacesBuildStrategy(INTERFACES_NONE);

// Set the spawn as unneeded
//
// Specific implementation will set the appropriate status during their
// initialization.
setSpawnStatus(SPAWN_UNNEEDED);

// Set the adaption as clean
setAdaptionStatus(ADAPTION_CLEAN);

Expand Down Expand Up @@ -646,12 +637,6 @@ std::vector<adaption::Info> PatchKernel::update(bool trackAdaption, bool squeeze
// Finalize alterations
finalizeAlterations(squeezeStorage);

// Spawn
bool spawnNeeed = (getSpawnStatus() == SPAWN_NEEDED);
if (spawnNeeed) {
mergeAdaptionInfo(spawn(trackAdaption), updateInfo);
}

// Adaption
bool adaptionDirty = (getAdaptionStatus(true) == ADAPTION_DIRTY);
if (adaptionDirty) {
Expand Down Expand Up @@ -681,44 +666,6 @@ void PatchKernel::simulateCellUpdate(const long id, adaption::Marker marker, std
throw std::runtime_error ("This function has not been implemented for the specified patch.");
}

/*!
Generates the patch.

\param trackSpawn if set to true the changes to the patch will be tracked
\result Returns a vector of adaption::Info that can be used to track
the changes done during the spawn.
*/
std::vector<adaption::Info> PatchKernel::spawn(bool trackSpawn)
{
std::vector<adaption::Info> spawnInfo;

#if BITPIT_ENABLE_MPI==1
// This is a collevtive operation and should be called by all processes
if (isPartitioned()) {
const auto &communicator = getCommunicator();
MPI_Barrier(communicator);
}
#endif

// Check spawn status
SpawnStatus spawnStatus = getSpawnStatus();
if (spawnStatus == SPAWN_UNNEEDED || spawnStatus == SPAWN_DONE) {
return spawnInfo;
}

// Spawn the patch
spawnInfo = _spawn(trackSpawn);

// Finalize patch alterations
finalizeAlterations(true);

// Spwan is done
setSpawnStatus(SPAWN_DONE);

// Done
return spawnInfo;
}

/*!
Execute patch adaption.

Expand Down Expand Up @@ -1335,33 +1282,6 @@ void PatchKernel::_writeFinalize()
// Nothing to do
}

/*!
Returns the current spawn status.

Span functionality is obsolete. The patch dosn't need to be spawned
anymore.

\return The current spawn status.
*/
PatchKernel::SpawnStatus PatchKernel::getSpawnStatus() const
{
// There is no need to check the spawn status globally because the spawn
// status will always be the same on all the processes.

return m_spawnStatus;
}

/*!
Set the current spawn status.

\param status is the spawn status that will be set
*/
void PatchKernel::setSpawnStatus(SpawnStatus status)
{
m_spawnStatus = status;
}


/*!
Checks if the patch supports adaption.

Expand Down Expand Up @@ -1470,10 +1390,6 @@ bool PatchKernel::isDirty(bool global) const
assert(isDirty || m_alteredInterfaces.empty());
}

if (!isDirty) {
isDirty |= (getSpawnStatus() == SPAWN_NEEDED);
}

if (!isDirty) {
isDirty |= (getAdaptionStatus(false) == ADAPTION_DIRTY);
}
Expand Down Expand Up @@ -5265,24 +5181,6 @@ void PatchKernel::restoreInterfaces(std::istream &stream)
setAdaptionMode(previousAdaptionMode);
}

/*!
Generates the patch.

Default implementation is a no-op function.

\param trackSpawn if set to true the changes to the patch will be tracked
\result Returns a vector of adaption::Info that can be used to track
the changes done during the spawn.
*/
std::vector<adaption::Info> PatchKernel::_spawn(bool trackSpawn)
{
BITPIT_UNUSED(trackSpawn);

assert(false && "The patch needs to implement _spawn");

return std::vector<adaption::Info>();
}

/*!
Prepares the patch for performing the adaption.

Expand Down Expand Up @@ -8345,9 +8243,6 @@ bool PatchKernel::dump(std::ostream &stream) const
utils::binary::write(stream, 0);
#endif

// Spawn status
utils::binary::write(stream, m_spawnStatus);

// Adaption information
utils::binary::write(stream, m_adaptionMode);
utils::binary::write(stream, m_adaptionStatus);
Expand Down Expand Up @@ -8446,9 +8341,6 @@ void PatchKernel::restore(std::istream &stream, bool reregister)
utils::binary::read(stream, dummyHaloSize);
#endif

// Spawn status
utils::binary::read(stream, m_spawnStatus);

// Adaption information
utils::binary::read(stream, m_adaptionMode);
utils::binary::read(stream, m_adaptionStatus);
Expand Down
17 changes: 0 additions & 17 deletions src/patchkernel/patch_kernel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,15 +329,6 @@ friend class PatchManager;
INTERFACES_AUTOMATIC
};

/*!
Spawn status
*/
enum SpawnStatus {
SPAWN_UNNEEDED = -1,
SPAWN_NEEDED,
SPAWN_DONE
};

/*!
Adaption mode
*/
Expand Down Expand Up @@ -403,9 +394,6 @@ friend class PatchManager;

virtual void simulateCellUpdate(const long id, adaption::Marker marker, std::vector<Cell> *virtualCells, PiercedVector<Vertex, long> *virtualVertices) const;

SpawnStatus getSpawnStatus() const;
std::vector<adaption::Info> spawn(bool trackSpawn);

bool isAdaptionSupported() const;
AdaptionMode getAdaptionMode() const;
AdaptionStatus getAdaptionStatus(bool global = false) const;
Expand Down Expand Up @@ -917,9 +905,6 @@ friend class PatchManager;

bool testAlterationFlags(AlterationFlags availableFlags, AlterationFlags requestedFlags) const;

void setSpawnStatus(SpawnStatus status);
virtual std::vector<adaption::Info> _spawn(bool trackAdaption);

void setAdaptionMode(AdaptionMode mode);
void setAdaptionStatus(AdaptionStatus status);
virtual std::vector<adaption::Info> _adaptionPrepare(bool trackAdaption);
Expand Down Expand Up @@ -1027,8 +1012,6 @@ friend class PatchManager;

InterfacesBuildStrategy m_interfacesBuildStrategy;

SpawnStatus m_spawnStatus;

AdaptionMode m_adaptionMode;
AdaptionStatus m_adaptionStatus;

Expand Down
95 changes: 19 additions & 76 deletions src/volcartesian/volcartesian.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -470,9 +470,6 @@ void VolCartesian::initialize()
// Set the bounding box as frozen
setBoundingBoxFrozen(true);

// This patch need to be spawn
setSpawnStatus(SPAWN_NEEDED);

// Set the light memory mode
setMemoryMode(MemoryMode::MEMORY_LIGHT);

Expand Down Expand Up @@ -930,34 +927,44 @@ std::array<double, 3> VolCartesian::getSpacing() const
*/
void VolCartesian::switchMemoryMode(MemoryMode mode)
{
// Early return if the current memory mode matches the requested one
if (mode == getMemoryMode()) {
return;
}

// Update the data structures
// Update patch data structures
switch (mode) {

case MemoryMode::MEMORY_NORMAL:
// Spawn the patch to activate normal memory mode
spawn(false);
{
// Enable manual adaption
AdaptionMode previousAdaptionMode = getAdaptionMode();
setAdaptionMode(ADAPTION_MANUAL);

// Create the mesh
addVertices();
addCells();

// Restore previous adaption mode
setAdaptionMode(previousAdaptionMode);

break;
}

case MemoryMode::MEMORY_LIGHT:
{
// To put the patch in memory mode we need to reset the generic data
// of the patch, therefore we can call the 'reset' implementation of
// the kernel.
VolumeKernel::reset();

// Now the patch needs to be spawn
setSpawnStatus(SPAWN_NEEDED);

// Set the light memory mode
setMemoryMode(mode);

break;
}

}

// Set the requested memory mode
setMemoryMode(mode);
}

/*!
Expand Down Expand Up @@ -994,67 +1001,6 @@ double VolCartesian::getSpacing(int direction) const
return m_cellSpacings[direction];
}

/*!
Generates the patch.

\param trackSpawn if set to true the changes to the patch will be tracked
\result Returns a vector of adaption::Info that can be used to track
the changes done during the update.
*/
std::vector<adaption::Info> VolCartesian::_spawn(bool trackSpawn)
{
std::vector<adaption::Info> updateInfo;

// If the patch is in 'normal' mode there is nothing to do.
if (getMemoryMode() == MEMORY_NORMAL) {
return updateInfo;
}

// Enable manual adaption
AdaptionMode previousAdaptionMode = getAdaptionMode();
setAdaptionMode(ADAPTION_MANUAL);

// Definition of the mesh
addVertices();
addCells();

// Restore previous adaption mode
setAdaptionMode(previousAdaptionMode);

// Adaption info
if (trackSpawn) {
updateInfo.emplace_back();
adaption::Info &adaptionCellInfo = updateInfo.back();
adaptionCellInfo.type = adaption::TYPE_CREATION;
adaptionCellInfo.entity = adaption::ENTITY_CELL;
adaptionCellInfo.current.reserve(m_cells.size());
for (auto &cell : m_cells) {
adaptionCellInfo.current.emplace_back();
long &cellId = adaptionCellInfo.current.back();
cellId = cell.getId();
}

updateInfo.emplace_back();
adaption::Info &adaptionInterfaceInfo = updateInfo.back();
adaptionInterfaceInfo.type = adaption::TYPE_CREATION;
adaptionInterfaceInfo.entity = adaption::ENTITY_INTERFACE;
adaptionInterfaceInfo.current.reserve(m_interfaces.size());
for (auto &interface : m_interfaces) {
adaptionInterfaceInfo.current.emplace_back();
long &interfaceId = adaptionInterfaceInfo.current.back();
interfaceId = interface.getId();
}
} else {
updateInfo.emplace_back();
}

// Updating the adaption brings the patch is in normal memory mode
setMemoryMode(MemoryMode::MEMORY_NORMAL);

// Done
return updateInfo;
}

/*!
Creates the vertices of the patch.
*/
Expand Down Expand Up @@ -1173,9 +1119,6 @@ void VolCartesian::_dump(std::ostream &stream) const
*/
void VolCartesian::_restore(std::istream &stream)
{
// This patch need to be spawn
setSpawnStatus(SPAWN_NEEDED);

// Origin
std::array<double, 3> origin;
utils::binary::read(stream, origin[0]);
Expand Down
2 changes: 0 additions & 2 deletions src/volcartesian/volcartesian.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,6 @@ class VolCartesian : public VolumeKernel {
long getCellFaceNeighsLinearId(long id, int face) const;

protected:
std::vector<adaption::Info> _spawn(bool trackSpawn) override;

void _updateAdjacencies() override;

void _updateInterfaces() override;
Expand Down
Loading