Repository navigation
Expand file tree
/
Copy path_host_task_util.hpp
More file actions
96 lines (84 loc) · 3.32 KB
/
Copy path_host_task_util.hpp
File metadata and controls
96 lines (84 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//===--- _host_tasl_util.hpp - Implements async DECREF =//
//
// Data Parallel Control (dpctl)
//
// Copyright 2022 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 schedule host task to a sycl
/// queue depending on given array of sycl events to decrement reference counts
/// for the given array of Python objects.
///
/// N.B.: The host task attempts to acquire GIL, so queue wait, event wait and
/// other synchronization mechanisms should be called after releasing the GIL to
/// avoid deadlocks.
///
//===----------------------------------------------------------------------===//
#pragma once
#include <exception>
#include <stddef.h>
#include <sycl/sycl.hpp>
#include "Python.h"
#include "syclinterface/dpctl_data_types.h"
#include "syclinterface/dpctl_sycl_type_casters.hpp"
DPCTLSyclEventRef async_dec_ref(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;
sycl::queue *q = unwrap<sycl::queue>(QRef);
std::vector<PyObject *> obj_vec(obj_array, obj_array + obj_array_size);
try {
sycl::event ht_ev = q->submit([&](sycl::handler &cgh) {
for (size_t ev_id = 0; ev_id < nDepERefs; ++ev_id) {
cgh.depends_on(*(unwrap<sycl::event>(depERefs[ev_id])));
}
cgh.host_task([obj_array_size, obj_vec]() {
const bool initialized = Py_IsInitialized();
#if PY_VERSION_HEX < 0x30d0000
const bool finalizing = _Py_IsFinalizing();
#else
const bool finalizing = Py_IsFinalizing();
#endif
// if the main thread has not finalized the interpreter yet
if (initialized && !finalizing) {
PyGILState_STATE gstate;
gstate = PyGILState_Ensure();
for (size_t i = 0; i < obj_array_size; ++i) {
Py_DECREF(obj_vec[i]);
}
PyGILState_Release(gstate);
}
});
});
static constexpr int result_ok = 0;
*status = result_ok;
auto e_ptr = new sycl::event(ht_ev);
return wrap<sycl::event>(e_ptr);
} catch (const std::exception &e) {
static constexpr int result_std_exception = 1;
*status = result_std_exception;
return nullptr;
}
static constexpr int result_other_abnormal = 2;
*status = result_other_abnormal;
return nullptr;
}