-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathBindingsCudaUtils.hpp
101 lines (88 loc) · 3.72 KB
/
BindingsCudaUtils.hpp
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
97
98
99
100
101
// Copyright 2024 Xanadu Quantum Technologies Inc.
// 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 BindingsCudaUtils.hpp
* Defines CUDA device - specific operations to export to Python, other
* utility functions interfacing with Pybind11 and support to agnostic bindings.
*/
#pragma once
#include "BindingsBase.hpp"
#include "DevTag.hpp"
#include "DevicePool.hpp"
#include "cuda_helpers.hpp"
/// @cond DEV
namespace {
using namespace Pennylane;
using namespace Pennylane::Bindings;
} // namespace
/// @endcond
namespace py = pybind11;
namespace Pennylane::LightningGPU::Util {
/**
* @brief Register bindings for CUDA utils.
*
* @param m Pybind11 module.
*/
void registerCudaUtils(py::module_ &m) {
m.def("device_reset", &deviceReset, "Reset all GPU devices and contexts.");
m.def("allToAllAccess", []() {
for (int i = 0; i < static_cast<int>(getGPUCount()); i++) {
cudaDeviceEnablePeerAccess(i, 0);
}
});
m.def("is_gpu_supported", &isCuQuantumSupported,
py::arg("device_number") = 0,
"Checks if the given GPU device meets the minimum architecture "
"support for the PennyLane-Lightning-GPU device.");
m.def("get_gpu_arch", &getGPUArch, py::arg("device_number") = 0,
"Returns the given GPU major and minor GPU support.");
py::class_<DevicePool<int>>(m, "DevPool")
.def(py::init<>())
.def("getActiveDevices", &DevicePool<int>::getActiveDevices)
.def("isActive", &DevicePool<int>::isActive)
.def("isInactive", &DevicePool<int>::isInactive)
.def("acquireDevice", &DevicePool<int>::acquireDevice)
.def("releaseDevice", &DevicePool<int>::releaseDevice)
.def("syncDevice", &DevicePool<int>::syncDevice)
.def("refresh", &DevicePool<int>::refresh)
.def_static("getTotalDevices", &DevicePool<int>::getTotalDevices)
.def_static("getDeviceUIDs", &DevicePool<int>::getDeviceUIDs)
.def_static("setDeviceID", &DevicePool<int>::setDeviceIdx)
.def(py::pickle(
[]([[maybe_unused]] const DevicePool<int> &self) { // __getstate__
return py::make_tuple();
},
[](py::tuple &t) { // __setstate__
if (t.size() != 0) {
throw std::runtime_error("Invalid state!");
}
return DevicePool<int>{};
}));
py::class_<DevTag<int>>(m, "DevTag")
.def(py::init<>())
.def(py::init<int>())
.def(py::init([](int device_id, void *stream_id) {
// Note, streams must be handled externally for now.
// Binding support provided through void* conversion to cudaStream_t
return new DevTag<int>(device_id,
static_cast<cudaStream_t>(stream_id));
}))
.def(py::init<const DevTag<int> &>())
.def("getDeviceID", &DevTag<int>::getDeviceID)
.def("getStreamID",
[](DevTag<int> &dev_tag) {
// default stream points to nullptr, so just return void* as
// type
return static_cast<void *>(dev_tag.getStreamID());
})
.def("refresh", &DevTag<int>::refresh);
}
} // namespace Pennylane::LightningGPU::Util