-
Notifications
You must be signed in to change notification settings - Fork 41
Remove host_task object management
#2359
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
ndgrigorian
wants to merge
7
commits into
master
Choose a base branch
from
feature/drop-host-task-object-management
base: master
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
7 commits
Select commit
Hold shift + click to select a range
8b0e96d
Migrate host_tasks to a thread pool based approach
ndgrigorian 3390e98
Deprecate APIs which require events from Python object management
ndgrigorian c9c82a6
address review comments
ndgrigorian 637c490
Apply review feedback
ndgrigorian 94378f9
Use a polling thread for KeepAlivePool
ndgrigorian 9a6f156
KeepAlivePool changed to KeepAliveWatcher
ndgrigorian 84bf7e3
fix pre-commit
ndgrigorian 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
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
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 |
|---|---|---|
| @@ -0,0 +1,246 @@ | ||
| //===--- _async_dec_ref.hpp - Implements async DECREF ---------------------===// | ||
| // | ||
| // Data Parallel Control (dpctl) | ||
| // | ||
| // Copyright 2026 Intel Corporation | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
| /// | ||
| /// \file | ||
| /// This file implements a utility function to decrement reference counts for a | ||
| /// given array of Python objects once a given array of sycl events has | ||
| /// completed. | ||
| /// | ||
| /// N.B.: The reference counts are dropped by whichever thread next enters | ||
| /// `dpctl` from Python, and never by the thread that finds the events complete, | ||
| /// which must not touch Python. Waiting for a decrement to happen therefore | ||
| /// deadlocks, and nothing in `dpctl` does. | ||
| /// | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #pragma once | ||
| #include <atomic> | ||
| #include <memory> | ||
| #include <stddef.h> | ||
| #include <stdexcept> | ||
| #include <sycl/sycl.hpp> | ||
| #include <utility> | ||
| #include <vector> | ||
|
|
||
| #include "Python.h" | ||
|
|
||
| #include "detail/keep_alive_watcher.hpp" | ||
| #include "syclinterface/dpctl_data_types.h" | ||
| #include "syclinterface/dpctl_sycl_type_casters.hpp" | ||
|
|
||
| namespace dpctl | ||
| { | ||
| namespace detail | ||
| { | ||
|
|
||
| namespace | ||
| { | ||
|
|
||
| /*! | ||
| * @brief The watcher, once there is one, for those who must not create it. | ||
| */ | ||
| std::atomic<KeepAliveWatcher *> created_watcher{nullptr}; | ||
|
|
||
| } // namespace | ||
|
|
||
| /*! | ||
| * @brief The watcher of the `dpctl._sycl_queue` module, which everyone shares. | ||
| * | ||
| * `KeepAliveWatcher` befriends this, making it the only creator of a watcher. | ||
| */ | ||
| KeepAliveWatcher &local_keep_alive_watcher() | ||
| { | ||
| // deliberately leaked: the polling thread is detached and holds a bare | ||
| // `this`, so the watcher must outlive it | ||
| static KeepAliveWatcher *instance = []() { | ||
| KeepAliveWatcher *watcher = new KeepAliveWatcher(); | ||
| created_watcher.store(watcher, std::memory_order_release); | ||
| return watcher; | ||
| }(); | ||
| return *instance; | ||
| } | ||
|
|
||
| } // namespace detail | ||
| } // namespace dpctl | ||
|
|
||
| /*! | ||
| * @brief Address of the `KeepAliveWatcher`. | ||
| * | ||
| * Returns nullptr if the watcher could not be created. | ||
| */ | ||
| void *keep_alive_watcher_ptr() | ||
| { | ||
| try { | ||
| return static_cast<void *>(&dpctl::detail::local_keep_alive_watcher()); | ||
| } catch (...) { | ||
| // nothing may escape into the calling Cython code, which is not | ||
| // prepared to handle a C++ exception | ||
| return nullptr; | ||
| } | ||
| } | ||
|
|
||
| /*! | ||
| * @brief Drop the references of the DECREFs that have come due. | ||
| * | ||
| * Does nothing until there is a watcher, and never creates one. | ||
| * | ||
| * Expects the caller to hold the GIL. | ||
| * | ||
| * @return Whether there were any. | ||
| */ | ||
| bool drain_retired_references() | ||
| { | ||
| // a watcher being created right now is left to the next drain | ||
| dpctl::detail::KeepAliveWatcher *watcher = | ||
| dpctl::detail::created_watcher.load(std::memory_order_acquire); | ||
| if (!watcher) { | ||
| return false; | ||
| } | ||
|
|
||
| try { | ||
| return watcher->drain_retired(); | ||
| } catch (...) { | ||
| // nothing may escape into the calling Cython code, which is not | ||
| // prepared to handle a C++ exception | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| namespace | ||
| { | ||
|
|
||
| /*! | ||
| * @brief Copy `nDepERefs` event references into a vector. | ||
| */ | ||
| std::vector<sycl::event> unwrap_events(DPCTLSyclEventRef *depERefs, | ||
| size_t nDepERefs) | ||
| { | ||
| using dpctl::syclinterface::unwrap; | ||
|
|
||
| std::vector<sycl::event> depends; | ||
| depends.reserve(nDepERefs); | ||
| for (size_t ev_id = 0; ev_id < nDepERefs; ++ev_id) { | ||
| depends.push_back(*(unwrap<sycl::event>(depERefs[ev_id]))); | ||
| } | ||
|
|
||
| return depends; | ||
| } | ||
|
|
||
| /*! | ||
| * @brief Schedule DECREFs of `obj_vec` for once `depends` have completed. | ||
| */ | ||
| void submit_dec_ref(std::vector<PyObject *> obj_vec, | ||
| std::vector<sycl::event> depends) | ||
| { | ||
| auto &watcher = dpctl::detail::local_keep_alive_watcher(); | ||
|
|
||
| // the caller holds the GIL, so this is an opportunity to drain references | ||
| watcher.drain_retired(); | ||
|
|
||
| watcher.submit(std::move(depends), | ||
| [obj_vec = std::move(obj_vec)]() mutable { | ||
| // handed to a thread that holds the GIL | ||
| dpctl::detail::local_keep_alive_watcher().retire( | ||
| [obj_vec = std::move(obj_vec)]() { | ||
| for (PyObject *obj : obj_vec) { | ||
| Py_DECREF(obj); | ||
| } | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| /*! | ||
| * @brief Schedule DECREFs of `obj_array` for once `depERefs` have completed. | ||
| * | ||
| * Sets `*status` to 0 on success and 1 if scheduling threw. | ||
| */ | ||
| void async_dec_ref(PyObject **obj_array, | ||
| size_t obj_array_size, | ||
| DPCTLSyclEventRef *depERefs, | ||
| size_t nDepERefs, | ||
| int *status) | ||
| { | ||
| try { | ||
| submit_dec_ref( | ||
| std::vector<PyObject *>(obj_array, obj_array + obj_array_size), | ||
| unwrap_events(depERefs, nDepERefs)); | ||
|
|
||
| static constexpr int result_ok = 0; | ||
| *status = result_ok; | ||
| } catch (...) { | ||
| // nothing may escape into the calling Cython code, which is not | ||
| // prepared to handle a C++ exception | ||
| static constexpr int result_exception = 1; | ||
| *status = result_exception; | ||
| } | ||
| } | ||
|
|
||
| /*! | ||
| * @brief Queue-bound form of `async_dec_ref`. | ||
| * | ||
| * Returns an event for an empty kernel submitted to `QRef` after `depERefs`, | ||
| * which is what the DECREFs wait for. It has completed once `obj_array` is no | ||
| * longer in use, and, if `QRef` is in-order, once the work already submitted | ||
| * to the queue has completed as well. The DECREFs themselves are dropped | ||
| * afterwards, by whichever thread next enters `dpctl` from Python. | ||
| * | ||
| * Returns nullptr on failure, with `*status` set. | ||
| */ | ||
| DPCTLSyclEventRef async_dec_ref_event(DPCTLSyclQueueRef QRef, | ||
| PyObject **obj_array, | ||
| size_t obj_array_size, | ||
| DPCTLSyclEventRef *depERefs, | ||
| size_t nDepERefs, | ||
| int *status) | ||
| { | ||
| using dpctl::syclinterface::unwrap; | ||
| using dpctl::syclinterface::wrap; | ||
|
|
||
| try { | ||
| sycl::queue *q = unwrap<sycl::queue>(QRef); | ||
| if (!q) { | ||
| throw std::runtime_error("Queue reference is null"); | ||
| } | ||
|
|
||
| const sycl::event marker = dpctl::detail::submit_keep_alive_marker( | ||
| *q, unwrap_events(depERefs, nDepERefs)); | ||
|
|
||
| // allocated before scheduling, as failing afterwards could not be | ||
| // reported: the caller would drop a reference the scheduled DECREFs own | ||
| std::unique_ptr<sycl::event> e_ptr(new sycl::event(marker)); | ||
|
|
||
| submit_dec_ref( | ||
| std::vector<PyObject *>(obj_array, obj_array + obj_array_size), | ||
| {marker}); | ||
|
|
||
| static constexpr int result_ok = 0; | ||
| *status = result_ok; | ||
|
|
||
| return wrap<sycl::event>(e_ptr.release()); | ||
| } catch (...) { | ||
| // nothing may escape into the calling Cython code, which is not | ||
| // prepared to handle a C++ exception | ||
| static constexpr int result_exception = 1; | ||
| *status = result_exception; | ||
| return nullptr; | ||
| } | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.