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

Parser: Use malloc if Arena is not ready #4240

Merged
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
17 changes: 12 additions & 5 deletions Src/Base/Parser/AMReX_IParser.H
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,13 @@ private:
std::string m_expression;
struct amrex_iparser* m_iparser = nullptr;
int m_nvars = 0;
mutable char* m_host_executor = nullptr;
bool m_use_arena = true;
char* m_host_executor = nullptr;
#ifdef AMREX_USE_GPU
mutable char* m_device_executor = nullptr;
char* m_device_executor = nullptr;
#endif
mutable int m_max_stack_size = 0;
mutable int m_exe_size = 0;
int m_max_stack_size = 0;
int m_exe_size = 0;
Data () = default;
~Data ();
Data (Data const&) = delete;
Expand Down Expand Up @@ -129,6 +130,10 @@ IParser::compileHost () const
}

m_data->m_host_executor = (char*)The_Pinned_Arena()->alloc(m_data->m_exe_size);
if (m_data->m_host_executor == nullptr) { // Arena is not ready yet
m_data->m_host_executor = (char*) std::malloc(m_data->m_exe_size);
m_data->m_use_arena = false;
}

try {
iparser_compile(m_data->m_iparser, m_data->m_host_executor);
Expand All @@ -155,7 +160,9 @@ IParser::compile () const
auto exe = compileHost<N>();

#ifdef AMREX_USE_GPU
if (m_data && m_data->m_iparser && !(m_data->m_device_executor)) {
if (m_data && m_data->m_iparser && !(m_data->m_device_executor)
&& m_data->m_use_arena)
{
m_data->m_device_executor = (char*)The_Arena()->alloc(m_data->m_exe_size);
Gpu::htod_memcpy_async(m_data->m_device_executor, m_data->m_host_executor,
m_data->m_exe_size);
Expand Down
8 changes: 7 additions & 1 deletion Src/Base/Parser/AMReX_IParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@ IParser::Data::~Data ()
{
m_expression.clear();
if (m_iparser) { amrex_iparser_delete(m_iparser); }
if (m_host_executor) { The_Pinned_Arena()->free(m_host_executor); }
if (m_host_executor) {
if (m_use_arena) {
The_Pinned_Arena()->free(m_host_executor);
} else {
std::free(m_host_executor);
}
}
#ifdef AMREX_USE_GPU
if (m_device_executor) { The_Arena()->free(m_device_executor); }
#endif
Expand Down
19 changes: 13 additions & 6 deletions Src/Base/Parser/AMReX_Parser.H
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,14 @@ private:
std::string m_expression;
struct amrex_parser* m_parser = nullptr;
int m_nvars = 0;
mutable char* m_host_executor = nullptr;
bool m_use_arena = true;
char* m_host_executor = nullptr;
#ifdef AMREX_USE_GPU
mutable char* m_device_executor = nullptr;
char* m_device_executor = nullptr;
#endif
mutable int m_max_stack_size = 0;
mutable int m_exe_size = 0;
mutable Vector<char const*> m_locals;
int m_max_stack_size = 0;
int m_exe_size = 0;
Vector<char const*> m_locals;
Data () = default;
~Data ();
Data (Data const&) = delete;
Expand Down Expand Up @@ -142,6 +143,10 @@ Parser::compileHost () const
}

m_data->m_host_executor = (char*)The_Pinned_Arena()->alloc(m_data->m_exe_size);
if (m_data->m_host_executor == nullptr) { // Arena is not ready yet
m_data->m_host_executor = (char*) std::malloc(m_data->m_exe_size);
m_data->m_use_arena = false;
}

try {
m_data->m_locals = parser_compile(m_data->m_parser,
Expand Down Expand Up @@ -169,7 +174,9 @@ Parser::compile () const
auto exe = compileHost<N>();

#ifdef AMREX_USE_GPU
if (m_data && m_data->m_parser && !(m_data->m_device_executor)) {
if (m_data && m_data->m_parser && !(m_data->m_device_executor)
&& m_data->m_use_arena)
{
m_data->m_device_executor = (char*)The_Arena()->alloc(m_data->m_exe_size);
Gpu::htod_memcpy_async(m_data->m_device_executor, m_data->m_host_executor,
m_data->m_exe_size);
Expand Down
8 changes: 7 additions & 1 deletion Src/Base/Parser/AMReX_Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@ Parser::Data::~Data ()
{
m_expression.clear();
if (m_parser) { amrex_parser_delete(m_parser); }
if (m_host_executor) { The_Pinned_Arena()->free(m_host_executor); }
if (m_host_executor) {
if (m_use_arena) {
The_Pinned_Arena()->free(m_host_executor);
} else {
std::free(m_host_executor);
}
}
#ifdef AMREX_USE_GPU
if (m_device_executor) { The_Arena()->free(m_device_executor); }
#endif
Expand Down
Loading