forked from oneapi-src/unified-runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevice.hpp
More file actions
116 lines (93 loc) · 3.93 KB
/
device.hpp
File metadata and controls
116 lines (93 loc) · 3.93 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//===--------- device.hpp - HIP Adapter -----------------------------------===//
//
// Copyright (C) 2023 Intel Corporation
//
// Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM
// Exceptions. See LICENSE.TXT
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#pragma once
#include "common.hpp"
#include <ur/ur.hpp>
/// UR device mapping to a hipDevice_t.
/// Includes an observer pointer to the platform,
/// and implements the reference counting semantics since
/// HIP objects are not refcounted.
struct ur_device_handle_t_ {
private:
using native_type = hipDevice_t;
native_type HIPDevice;
std::atomic_uint32_t RefCount;
ur_platform_handle_t Platform;
hipEvent_t EvBase; // HIP event used as base counter
uint32_t DeviceIndex;
uint32_t NumComputeUnits{0};
int MaxWorkGroupSize{0};
int MaxBlockDimX{0};
int MaxBlockDimY{0};
int MaxBlockDimZ{0};
int DeviceMaxLocalMem{0};
int ManagedMemSupport{0};
int ConcurrentManagedAccess{0};
public:
ur_device_handle_t_(native_type HipDevice, hipEvent_t EvBase,
ur_platform_handle_t Platform, uint32_t DeviceIndex)
: HIPDevice(HipDevice), RefCount{1}, Platform(Platform), EvBase(EvBase),
DeviceIndex(DeviceIndex) {
UR_CHECK_ERROR(hipDeviceGetAttribute(
reinterpret_cast<int *>(&NumComputeUnits),
hipDeviceAttributeMultiprocessorCount, HIPDevice));
UR_CHECK_ERROR(hipDeviceGetAttribute(
&MaxWorkGroupSize, hipDeviceAttributeMaxThreadsPerBlock, HIPDevice));
UR_CHECK_ERROR(hipDeviceGetAttribute(
&MaxBlockDimX, hipDeviceAttributeMaxBlockDimX, HIPDevice));
UR_CHECK_ERROR(hipDeviceGetAttribute(
&MaxBlockDimY, hipDeviceAttributeMaxBlockDimY, HIPDevice));
UR_CHECK_ERROR(hipDeviceGetAttribute(
&MaxBlockDimZ, hipDeviceAttributeMaxBlockDimZ, HIPDevice));
UR_CHECK_ERROR(hipDeviceGetAttribute(
&DeviceMaxLocalMem, hipDeviceAttributeMaxSharedMemoryPerBlock,
HIPDevice));
UR_CHECK_ERROR(hipDeviceGetAttribute(
&ManagedMemSupport, hipDeviceAttributeManagedMemory, HIPDevice));
UR_CHECK_ERROR(hipDeviceGetAttribute(
&ConcurrentManagedAccess, hipDeviceAttributeConcurrentManagedAccess,
HIPDevice));
}
~ur_device_handle_t_() noexcept(false) {}
native_type get() const noexcept { return HIPDevice; };
uint32_t getReferenceCount() const noexcept { return RefCount; }
ur_platform_handle_t getPlatform() const noexcept { return Platform; };
uint64_t getElapsedTime(hipEvent_t) const;
// Returns the index of the device relative to the other devices in the same
// platform
uint32_t getIndex() const noexcept { return DeviceIndex; };
int getMaxWorkGroupSize() const noexcept { return MaxWorkGroupSize; };
int getMaxBlockDimX() const noexcept { return MaxBlockDimX; };
int getMaxBlockDimY() const noexcept { return MaxBlockDimY; };
int getMaxBlockDimZ() const noexcept { return MaxBlockDimZ; };
int getDeviceMaxLocalMem() const noexcept { return DeviceMaxLocalMem; };
int getManagedMemSupport() const noexcept { return ManagedMemSupport; };
uint32_t getNumComputeUnits() const noexcept { return NumComputeUnits; };
int getConcurrentManagedAccess() const noexcept {
return ConcurrentManagedAccess;
};
};
int getAttribute(ur_device_handle_t Device, hipDeviceAttribute_t Attribute);
namespace {
/// Scoped Device is used across all UR HIP plugin implementation to activate
/// the native Device on the current thread. The ScopedDevice does not
/// reinstate the previous device as all operations in the HIP adapter that
/// require an active device, set the active device and don't rely on device
/// reinstation
class ScopedDevice {
public:
ScopedDevice(ur_device_handle_t hDevice) {
if (!hDevice) {
throw UR_RESULT_ERROR_INVALID_DEVICE;
}
UR_CHECK_ERROR(hipSetDevice(hDevice->getIndex()));
}
};
} // namespace