-
Notifications
You must be signed in to change notification settings - Fork 102
Avoid dynamic memory allocation in kernel launch #3861
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
Open
pfultz2
wants to merge
10
commits into
develop
Choose a base branch
from
fast-kernel-launch
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f61fe02
Avoid dynamic memory allocation in kernel launch
pfultz2 a58658a
Format
pfultz2 441e10d
Update the license
pfultz2 6db9cab
Use pmr vector
pfultz2 14747d7
Avoid allocation for argument map as well
pfultz2 ffa14da
Format
pfultz2 6c3814b
Update license
pfultz2 728a715
Use global results map
pfultz2 349bda2
Format
pfultz2 50f3e4f
Merge branch 'develop' into fast-kernel-launch
causten File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,6 +52,7 @@ | |
#include <unordered_set> | ||
#include <map> | ||
#include <cassert> | ||
#include <memory_resource> | ||
|
||
namespace migraphx { | ||
inline namespace MIGRAPHX_INLINE_NS { | ||
|
@@ -462,24 +463,27 @@ | |
std::vector<argument> generic_eval(const module* mod, | ||
std::vector<context>& ctx, | ||
std::unordered_map<std::string, argument> params, | ||
std::unordered_map<instruction_ref, argument> results, | ||
std::pmr::unordered_map<instruction_ref, argument>& results, | ||
F trace) | ||
{ | ||
assert(mod->validate() == mod->end()); | ||
results.reserve(mod->size() * 2); | ||
std::vector<argument> values; | ||
values.reserve(16); | ||
for(auto ins : iterator_for(*mod)) | ||
{ | ||
assert(results.find(ins) == results.end()); | ||
assert(mod->name() != "main" or results.find(ins) == results.end()); | ||
#ifndef NDEBUG | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a comment here, for the uninitiated, why is this under a flag. Thanks. |
||
results.emplace(ins, argument{}); | ||
#endif | ||
const auto& name = ins->name(); | ||
if(name == "@literal") | ||
{ | ||
results.emplace(ins, trace(ins, [&] { return ins->get_literal().get_argument(); })); | ||
results.insert_or_assign(ins, | ||
trace(ins, [&] { return ins->get_literal().get_argument(); })); | ||
} | ||
else if(name == "@param") | ||
{ | ||
results.emplace( | ||
results.insert_or_assign( | ||
ins, trace(ins, [&] { | ||
auto param_name = any_cast<builtin::param>(ins->get_operator()).parameter; | ||
if(not contains(params, param_name)) | ||
|
@@ -498,7 +502,8 @@ | |
} | ||
else if(name == "@outline") | ||
{ | ||
results.emplace(ins, trace(ins, [&] { return argument{ins->get_shape(), nullptr}; })); | ||
results.insert_or_assign( | ||
ins, trace(ins, [&] { return argument{ins->get_shape(), nullptr}; })); | ||
} | ||
else if(name == "@return") | ||
{ | ||
|
@@ -527,7 +532,7 @@ | |
return generic_eval(smod, ctx, inputs, results, trace); | ||
}; | ||
|
||
results.emplace( | ||
results.insert_or_assign( | ||
ins, trace(ins, [&] { | ||
auto op = ins->normalized_operator(); | ||
if(op.is_context_free()) | ||
|
@@ -551,14 +556,28 @@ | |
F trace) | ||
{ | ||
const module* mm = p.get_main_module(); | ||
return generic_eval(mm, ctx, params, {}, trace); | ||
std::size_t n = p.total_instructions(); | ||
std::vector<char> buffer(n * (sizeof(instruction_ref) + sizeof(argument)) * 4); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Raw constants (e.g. 4 here) create a mystery. |
||
std::pmr::monotonic_buffer_resource bres( | ||
buffer.data(), buffer.size(), std::pmr::null_memory_resource()); | ||
std::pmr::unordered_map<instruction_ref, argument> results(&bres); | ||
results.reserve(n); | ||
return generic_eval(mm, ctx, params, results, trace); | ||
} | ||
|
||
std::size_t program::total_instructions() const | ||
{ | ||
return transform_accumulate(impl->modules.begin(), | ||
impl->modules.end(), | ||
std::size_t{0}, | ||
std::plus<>{}, | ||
[](const auto& p) { return p.second.size(); }); | ||
} | ||
|
||
std::vector<argument> program::eval_with_context(std::vector<context>& ctx, | ||
parameter_map params) const | ||
{ | ||
const module* mm = this->get_main_module(); | ||
return generic_eval(mm, ctx, std::move(params), {}, [](auto&&, auto f) { return f(); }); | ||
return generic_eval(*this, ctx, std::move(params), [](auto&&, auto f) { return f(); }); | ||
} | ||
|
||
std::vector<argument> program::eval(parameter_map params, execution_environment exec_env) const | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The line above can take an efficiency gain of using a const reference: i.e. for
const std::unordered_map<std::string, argument> & params', as for
results`.