Skip to content

Commit adfb8ea

Browse files
committed
Deallocation of vector made more clear
1 parent a936749 commit adfb8ea

File tree

3 files changed

+20
-17
lines changed

3 files changed

+20
-17
lines changed

highs/ipm/hipo/auxiliary/Auxiliary.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ void printTest(const std::vector<T>& v, const std::string s) {
7171
out_file.close();
7272
}
7373

74+
template <typename T>
75+
void freeVector(std::vector<T> v) {
76+
// Give up memory allocated to v.
77+
// (technically shrink_to_fit does not guarantee to deallocate)
78+
v.clear();
79+
v.shrink_to_fit();
80+
}
81+
7482
class Clock {
7583
std::chrono::high_resolution_clock::time_point t0;
7684

highs/ipm/hipo/factorhighs/Factorise.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,8 +326,7 @@ void Factorise::processSupernode(Int sn) {
326326

327327
// Schur contribution of the child is no longer needed
328328
if (parallel) {
329-
// Swap with temporary empty vector to deallocate memory
330-
std::vector<double>().swap(schur_contribution_[child_sn]);
329+
freeVector(schur_contribution_[child_sn]);
331330
} else {
332331
stack_->popChild();
333332
}

highs/ipm/hipo/ipm/FactorHiGHSSolver.cpp

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -728,24 +728,20 @@ void FactorHiGHSSolver::getReg(std::vector<double>& reg) {
728728
}
729729

730730
void FactorHiGHSSolver::freeASmemory() {
731-
// Swap AS data structures with empty vectors, to guarantee that memory is
732-
// freed.
733-
734-
std::vector<Int>().swap(ptrAS_);
735-
std::vector<Int>().swap(rowsAS_);
736-
std::vector<double>().swap(valAS_);
731+
// Give up memory used for AS.
732+
freeVector(ptrAS_);
733+
freeVector(rowsAS_);
734+
freeVector(valAS_);
737735
}
738736

739737
void FactorHiGHSSolver::freeNEmemory() {
740-
// Swap NE data structures with empty vectors, to guarantee that memory is
741-
// freed.
742-
743-
std::vector<Int>().swap(ptrNE_);
744-
std::vector<Int>().swap(rowsNE_);
745-
std::vector<double>().swap(valNE_);
746-
std::vector<Int>().swap(ptrA_rw_);
747-
std::vector<Int>().swap(idxA_rw_);
748-
std::vector<Int>().swap(corr_A_);
738+
// Give up memory used for NE.
739+
freeVector(ptrNE_);
740+
freeVector(rowsNE_);
741+
freeVector(valNE_);
742+
freeVector(ptrA_rw_);
743+
freeVector(idxA_rw_);
744+
freeVector(corr_A_);
749745
}
750746

751747
} // namespace hipo

0 commit comments

Comments
 (0)