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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Copyright (c) 2012-2017 VideoStitch SAS
// Copyright (c) 2018 stitchEm
#include "libvideostitch/gpu_device.hpp"
#include "gpu/buffer.hpp"
#include "cuda/memory.hpp"
#include "cuda/error.hpp"
#include <libgpudiscovery/cudaDeviceInfo.hpp>
#include <cuda_runtime.h>
#if !defined(__APPLE__) && !defined(TEGRA_DEMO)
#include <nvml.h>
#endif
#include <algorithm>
#include <functional>
#include <atomic>
#if defined(linux)
#include <sys/types.h>
#include <unistd.h>
#elif _MSC_VER
#include <Windows.h>
#endif
namespace VideoStitch {
#if !defined(__APPLE__) && !defined(TEGRA_DEMO)
namespace {
void init();
void done();
struct NVML {
NVML() { init(); }
~NVML() { done(); }
unsigned int device_count = 0;
};
NVML nvml;
#ifdef linux
pid_t pid;
#elif _MSC_VER
DWORD pid;
#endif
void init() {
nvmlReturn_t result = nvmlInit();
if (NVML_SUCCESS != result) {
printf("Failed to initialize NVML: %s\n", nvmlErrorString(result));
return;
}
result = nvmlDeviceGetCount(&nvml.device_count);
if (NVML_SUCCESS != result) {
printf("Failed to query device count: %s\n", nvmlErrorString(result));
return;
}
printf("Found %d device%s\n\n", (int)nvml.device_count, nvml.device_count != 1 ? "s" : "");
#ifdef linux
pid = getpid();
#elif _MSC_VER
pid = GetCurrentProcessId();
#endif
}
void done() {
nvmlReturn_t result = nvmlInit();
if (NVML_SUCCESS != result) {
printf("Failed to shutdown NVML: %s\n", nvmlErrorString(result));
}
}
} // namespace
#endif // __APPLE__
namespace GPU {
std::atomic<int> defaultDevice(-1);
Status checkDefaultBackendDeviceInitialization() { return CUDA_ERROR(cudaPeekAtLastError()); }
Status setDefaultBackendDeviceVS(int vsDevice) {
int device;
if (!Discovery::getBackendDeviceIndex(vsDevice, device)) {
return {Origin::GPU, ErrType::ImplementationError, "[CUDA] trying to set a device that does not exist"};
}
return setDefaultBackendDevice(device);
}
Status getDefaultBackendDeviceContext(void* context) { return CUDA_ERROR(cuCtxGetCurrent((CUcontext*)context)); }
Status useDefaultBackendDevice() {
if (defaultDevice == -1) {
return {Origin::GPU, ErrType::ImplementationError, "[CUDA] default device is not set"};
}
return CUDA_ERROR(cudaSetDevice(defaultDevice));
}
Status setDefaultBackendDevice(int device) {
if (defaultDevice != -1 && device != defaultDevice) {
return {Origin::GPU, ErrType::ImplementationError, "[CUDA] changing the default device is not permitted"};
}
defaultDevice = device;
return useDefaultBackendDevice();
}
Status getDefaultBackendDevice(int* device) {
if (defaultDevice == -1) {
return {Origin::GPU, ErrType::ImplementationError, "[CUDA] default device is not set"};
}
*device = defaultDevice;
return Status::OK();
}
Discovery::Framework getFramework() { return Discovery::Framework::CUDA; }
PotentialValue<size_t> getMemoryUsage() {
// add buffers, cached buffers and allocated size on device
size_t used_memory = 0;
used_memory += getBufferPoolCurrentSize();
// used_memory += getCachedBufferPoolCurrentSize();
used_memory += Cuda::getDevicePoolCurrentSize();
return PotentialValue<size_t>(used_memory);
}
PotentialValue<std::vector<size_t> > getMemoryUsageByDevices() {
std::vector<size_t> used_memory;
#if !defined(__APPLE__) && !defined(TEGRA_DEMO)
for (unsigned int i = 0; i < nvml.device_count; i++) {
nvmlDevice_t device;
nvmlDeviceGetHandleByIndex(i, &device);
#ifdef linux
unsigned int infoCount = 0;
nvmlDeviceGetComputeRunningProcesses(device, &infoCount, nullptr);
nvmlProcessInfo_t* infos = new nvmlProcessInfo_t[infoCount];
nvmlDeviceGetComputeRunningProcesses(device, &infoCount, infos);
for (unsigned int j = 0; j < infoCount; j++) {
if ((unsigned int)pid == infos[j].pid) {
used_memory.push_back(infos[j].usedGpuMemory);
}
}
delete[] infos;
#elif _MSC_VER
nvmlMemory_t memory;
nvmlDeviceGetMemoryInfo(device, &memory);
used_memory.push_back(memory.used);
#endif
}
#else
// add buffers, cached buffers and allocated size on device
// initialize with first buffers
used_memory = getBufferPoolCurrentSizeByDevices();
// accumulate other buffers
// std::transform(used_memory.begin(), used_memory.end(), getCachedBufferPoolCurrentSizeByDevices().begin(),
// used_memory.begin(), std::plus<size_t>());
std::transform(used_memory.begin(), used_memory.end(), Cuda::getDevicePoolCurrentSizeByDevices().begin(),
used_memory.begin(), std::plus<size_t>());
#endif
return PotentialValue<std::vector<size_t> >(used_memory);
}
} // namespace GPU
} // namespace VideoStitch