Skip to content

Commit

Permalink
Adds memory cleanup functions for some Omega classes (#45)
Browse files Browse the repository at this point in the history
* Adds memory cleanup functions for some Omega classes

For Omega classes that store all instances, this adds some
erase/clear functions to clean up memory before the yakl finalize
call. Updates developer guide to note the requirement for this cleanup.
  • Loading branch information
philipwjones authored Jan 18, 2024
1 parent 60b8f55 commit f09a0d5
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 28 deletions.
12 changes: 12 additions & 0 deletions components/omega/doc/devGuide/Decomp.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,15 @@ yakl::c::parallel_for( yakl::c::Bounds<2>(NCellsOwned,MaxEdges),
}
});
```
Any defined decomposition can be removed by name using
```c++
Decomp::erase(Name);
```
and all decompositions *must* be removed before the yakl finalize call using
```c++
Decomp::clear();
```
which destroys all host and device arrays before YAKL finalizes and removes
the memory pool in which all the arrays are allocated. Failure to call clear
before `yakl::finalize()` will result in an error.
4 changes: 3 additions & 1 deletion components/omega/doc/devGuide/MachEnv.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ Because the new environments are subsets of the default environment, one
additional function `OMEGA::MachEnv::isMember()` is provided so that
non-member tasks can be excluded from calculations. The retrieval functions
call from non-member tasks will return invalid values. Finally, there is
a `removeEnv` function that can delete any defined environment.
a `removeEnv(Name)` function that can delete any individual defined
environment by name and a `removeAll()` function that cleans up by
removing all defined environments.
As a class that is basically a container for the environment parameters,
the implementation is a simple class with several scalar data members and
Expand Down
63 changes: 42 additions & 21 deletions components/omega/src/base/Decomp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -630,30 +630,30 @@ Decomp::Decomp(

// Create device copies of all arrays

// NCellsHalo = NCellsHaloH.createDeviceCopy();
// CellID = CellIDH.createDeviceCopy();
// CellLoc = CellLocH.createDeviceCopy();
NCellsHalo = NCellsHaloH.createDeviceCopy();
CellID = CellIDH.createDeviceCopy();
CellLoc = CellLocH.createDeviceCopy();

// NEdgesHalo = NEdgesHaloH.createDeviceCopy();
// EdgeID = EdgeIDH.createDeviceCopy();
// EdgeLoc = EdgeLocH.createDeviceCopy();
NEdgesHalo = NEdgesHaloH.createDeviceCopy();
EdgeID = EdgeIDH.createDeviceCopy();
EdgeLoc = EdgeLocH.createDeviceCopy();

// NVerticesHalo = NVerticesHaloH.createDeviceCopy();
// VertexID = VertexIDH.createDeviceCopy();
// VertexLoc = VertexLocH.createDeviceCopy();
NVerticesHalo = NVerticesHaloH.createDeviceCopy();
VertexID = VertexIDH.createDeviceCopy();
VertexLoc = VertexLocH.createDeviceCopy();

// CellsOnCell = CellsOnCellH.createDeviceCopy();
// EdgesOnCell = EdgesOnCellH.createDeviceCopy();
// VerticesOnCell = VerticesOnCellH.createDeviceCopy();
// NEdgesOnCell = NEdgesOnCellH.createDeviceCopy();
CellsOnCell = CellsOnCellH.createDeviceCopy();
EdgesOnCell = EdgesOnCellH.createDeviceCopy();
VerticesOnCell = VerticesOnCellH.createDeviceCopy();
NEdgesOnCell = NEdgesOnCellH.createDeviceCopy();

// CellsOnEdge = CellsOnEdgeH.createDeviceCopy();
// EdgesOnEdge = EdgesOnEdgeH.createDeviceCopy();
// VerticesOnEdge = VerticesOnEdgeH.createDeviceCopy();
// NEdgesOnEdge = NEdgesOnEdgeH.createDeviceCopy();
CellsOnEdge = CellsOnEdgeH.createDeviceCopy();
EdgesOnEdge = EdgesOnEdgeH.createDeviceCopy();
VerticesOnEdge = VerticesOnEdgeH.createDeviceCopy();
NEdgesOnEdge = NEdgesOnEdgeH.createDeviceCopy();

// CellsOnVertex = CellsOnVertexH.createDeviceCopy();
// EdgesOnVertex = EdgesOnVertexH.createDeviceCopy();
CellsOnVertex = CellsOnVertexH.createDeviceCopy();
EdgesOnVertex = EdgesOnVertexH.createDeviceCopy();

// Assign this as the default decomposition
AllDecomps.emplace(Name, *this);
Expand All @@ -666,9 +666,30 @@ Decomp::Decomp(

Decomp::~Decomp() {

// TODO: add deletes for all arrays and remove from AllDecomps map
// No operations needed, YAKL arrays removed when no longer in scope

} // end removeEnv
} // end decomp destructor

//------------------------------------------------------------------------------
// Removes a decomposition from list and destroys it

void Decomp::erase(std::string InName // [in] name of decomp to remove
) {

AllDecomps.erase(InName); // removes the decomp from the list (map) and in
// the process, calls the destructor

} // end decomp erase

//------------------------------------------------------------------------------
// Removes all decompositions to clean up before exit

void Decomp::clear() {

AllDecomps.clear(); // removes all decomps from the list (map) and in
// the process, calls the destructors for each

} // end decomp clear

// Retrieval functions
//------------------------------------------------------------------------------
Expand Down
7 changes: 7 additions & 0 deletions components/omega/src/base/Decomp.h
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,13 @@ class Decomp {
/// Destructor - deallocates all memory and deletes a Decomp.
~Decomp();

/// Erase - removes a defined decomposition
static void erase(std::string InName ///< [in] name of decomp to remove
);

/// Clear - removes all defined decompositions to clean up
static void clear();

// Retrieval functions

/// Retrieves a pointer the default decomposition. The preference is
Expand Down
9 changes: 9 additions & 0 deletions components/omega/src/base/MachEnv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,15 @@ void MachEnv::removeEnv(const std::string Name // [in] name of env to remove

} // end removeEnv

//------------------------------------------------------------------------------
// Remove all environments

void MachEnv::removeAll() {

AllEnvs.clear(); // removes all environments by removing them from map

} // end removeAll

// Retrieval functions
//------------------------------------------------------------------------------
// Get default environment
Expand Down
3 changes: 3 additions & 0 deletions components/omega/src/base/MachEnv.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ class MachEnv {
removeEnv(const std::string Name ///< [in] name of environment to remove
);

/// Removes all MachEnvs to clean up
static void removeAll();

// Retrieval functions

/// Retrieve the default environment
Expand Down
1 change: 1 addition & 0 deletions components/omega/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ set_tests_properties(
MACHINE_ENV_TEST
BROADCAST_TEST
LOGGING_TEST
DECOMP_TEST
IO_TEST
YAKL_TEST
PROPERTIES FAIL_REGULAR_EXPRESSION "FAIL"
Expand Down
5 changes: 3 additions & 2 deletions components/omega/test/base/DecompTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,10 @@ int main(int argc, char *argv[]) {
RefSumVertices);
}

// Test that device arrays are identical
// Clean up
OMEGA::Decomp::clear();
OMEGA::MachEnv::removeAll();

// MPI_Status status;
if (Err == 0)
LOG_INFO("DecompTest: Successful completion");
yakl::finalize();
Expand Down
6 changes: 2 additions & 4 deletions components/omega/test/base/MachEnvTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -449,11 +449,9 @@ int main(int argc, char *argv[]) {
}
#endif

// finalize environments
// finalize and clean up environments (test both removal functions)
OMEGA::MachEnv::removeEnv("Contig");
OMEGA::MachEnv::removeEnv("Contig2");
OMEGA::MachEnv::removeEnv("Stride");
OMEGA::MachEnv::removeEnv("Subset");
OMEGA::MachEnv::removeAll();

// MPI_Status status;
MPI_Finalize();
Expand Down

0 comments on commit f09a0d5

Please sign in to comment.