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

Add set-host-dirty/copy-to-host to PythonExtensionGen #6869

Merged
merged 3 commits into from
Jul 25, 2022
Merged
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
15 changes: 15 additions & 0 deletions src/PythonExtensionGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,13 @@ void PythonExtensionGen::compile(const LoweredFunc &f) {
}
dest << " int result;\n";
dest << " Py_BEGIN_ALLOW_THREADS\n";
// Mark all input buffers as having a dirty host, so that the Halide call will
// do a lazy-copy-to-GPU if needed.
for (size_t i = 0; i < args.size(); i++) {
if (args[i].is_buffer() && args[i].is_input()) {
dest << " buffer_" << arg_names[i] << ".set_host_dirty();\n";
}
}
dest << " result = " << f.name << "(";
for (size_t i = 0; i < args.size(); i++) {
if (i > 0) {
Expand All @@ -364,6 +371,14 @@ void PythonExtensionGen::compile(const LoweredFunc &f) {
}
dest << ");\n";
dest << " Py_END_ALLOW_THREADS\n";
// Since the Python Buffer protocol is host-memory-only, we *must*
// flush results back to host, otherwise the output buffer will contain
// random garbage. (We need a better solution for this, see https://github.com/halide/Halide/issues/6868)
for (size_t i = 0; i < args.size(); i++) {
if (args[i].is_buffer() && args[i].is_output()) {
dest << " if (result == 0) result = halide_copy_to_host(nullptr, &buffer_" << arg_names[i] << ");\n";
}
}
release_buffers();
dest << R"INLINE_CODE(
if (result != 0) {
Expand Down