Skip to content

Commit

Permalink
added out of memory handling
Browse files Browse the repository at this point in the history
  • Loading branch information
drexlerd committed Aug 20, 2024
1 parent fa63935 commit 93e2dbc
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 11 deletions.
5 changes: 3 additions & 2 deletions src/generator/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ file(GLOB_RECURSE GENERATOR_PUBLIC_HEADER_FILES
target_sources(dlplangenerator
PRIVATE
${GENERATOR_SRC_FILES} ${GENERATOR_PRIVATE_HEADER_FILES} ${GENERATOR_PUBLIC_HEADER_FILES}
../utils/logging.cpp
../utils/countdown_timer.cpp
"../utils/logging.h" "../utils/logging.cpp"
"../utils/countdown_timer.h" "../utils/countdown_timer.cpp"
"../utils/memory.h" "../utils/memory.cpp"
)
target_link_libraries(dlplangenerator
PUBLIC
Expand Down
10 changes: 9 additions & 1 deletion src/generator/generator.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "../../include/dlplan/generator.h"

#include "feature_generator.h"
#include "../utils/memory.h"


namespace dlplan::generator {
Expand Down Expand Up @@ -37,7 +38,14 @@ GeneratedFeatures FeatureGenerator::generate(
int distance_numerical_complexity_limit,
int time_limit,
int feature_limit) {
return m_pImpl->generate(factory, states, concept_complexity_limit, role_complexity_limit, boolean_complexity_limit, count_numerical_complexity_limit, distance_numerical_complexity_limit, time_limit, feature_limit);

utils::reserve_extra_memory_padding(1);

auto features = m_pImpl->generate(factory, states, concept_complexity_limit, role_complexity_limit, boolean_complexity_limit, count_numerical_complexity_limit, distance_numerical_complexity_limit, time_limit, feature_limit);

utils::release_extra_memory_padding();

return features;
}

void FeatureGenerator::set_generate_empty_boolean(bool enable) {
Expand Down
1 change: 1 addition & 0 deletions src/state_space/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ file(GLOB_RECURSE STATE_SPACE_PUBLIC_HEADER_FILES
target_sources(dlplanstatespace
PRIVATE
${STATE_SPACE_SRC_FILES} ${STATE_SPACE_PRIVATE_HEADER_FILES} ${STATE_SPACE_PUBLIC_HEADER_FILES}
"../utils/memory.h" "../utils/memory.cpp"
)

target_link_libraries(dlplanstatespace
Expand Down
9 changes: 8 additions & 1 deletion src/state_space/state_space.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,15 @@ GeneratorResult generate_state_space(
core::InstanceIndex index,
int max_time,
int max_num_states) {

utils::reserve_extra_memory_padding(1);

generator::generate_state_space_files(domain_file, instance_file, max_time, max_num_states);
return reader::read(vocabulary_info, index);
auto result = reader::read(vocabulary_info, index);

utils::release_extra_memory_padding();

return result;
}

}
34 changes: 34 additions & 0 deletions src/utils/memory.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "memory.h"

#include <cassert>
#include <iostream>

namespace dlplan::utils {
static char *extra_memory_padding = nullptr;

// Save standard out-of-memory handler.
static void (*standard_out_of_memory_handler)() = nullptr;

static void continuing_out_of_memory_handler() {
release_extra_memory_padding();
std::cout << "Failed to allocate memory. Released extra memory padding." << std::endl;
}

void reserve_extra_memory_padding(int memory_in_mb) {
assert(!extra_memory_padding);
extra_memory_padding = new char[memory_in_mb * 1024 * 1024];
standard_out_of_memory_handler = std::set_new_handler(continuing_out_of_memory_handler);
}

void release_extra_memory_padding() {
assert(extra_memory_padding);
delete[] extra_memory_padding;
extra_memory_padding = nullptr;
assert(standard_out_of_memory_handler);
std::set_new_handler(standard_out_of_memory_handler);
}

bool extra_memory_padding_is_reserved() {
return extra_memory_padding;
}
}
23 changes: 16 additions & 7 deletions src/utils/memory.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
#ifndef DLPLAN_SRC_UTILS_MEMORY_H
#define DLPLAN_SRC_UTILS_MEMORY_H

#include <memory>
#include <utility>

namespace dlplan::utils {

/**
* Uses swap-trick to free memory of container.
*/
template<typename CONTAINER_TYPE>
void free_memory(CONTAINER_TYPE& container) {
CONTAINER_TYPE().swap(container);
}
/*
Taken from Fast-Downward (https://www.fast-downward.org/)
Reserve some memory that we can release and be able to continue
afterwards, once we hit the memory limit. Due to memory fragmentation
the planner often doesn't have enough memory to continue if we don't
reserve enough memory. For CEGAR heuristics reserving 75 MB worked
best.
The interface assumes a single user. It is not possible for two parts
of the planner to reserve extra memory padding at the same time.
*/
extern void reserve_extra_memory_padding(int memory_in_mb);
extern void release_extra_memory_padding();
extern bool extra_memory_padding_is_reserved();
}

#endif

0 comments on commit 93e2dbc

Please sign in to comment.