forked from AliceO2Group/AliceO2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGPUReconstructionLibrary.cxx
More file actions
186 lines (165 loc) · 6.02 KB
/
GPUReconstructionLibrary.cxx
File metadata and controls
186 lines (165 loc) · 6.02 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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
/// \file GPUReconstruction.cxx
/// \author David Rohr
#ifdef _WIN32
#include <windows.h>
#include <winbase.h>
#include <conio.h>
#else
#include <dlfcn.h>
#include <pthread.h>
#include <unistd.h>
#endif
#include "GPUReconstruction.h"
#include "GPUReconstructionAvailableBackends.h"
#include "utils/qlibload.h"
#include "GPULogging.h"
using namespace o2::gpu;
GPUReconstruction* GPUReconstruction::CreateInstance(DeviceType type, bool forceType, GPUReconstruction* master)
{
GPUSettingsDeviceBackend cfg;
new (&cfg) GPUSettingsDeviceBackend;
cfg.deviceType = type;
cfg.forceDeviceType = forceType;
cfg.master = master;
return CreateInstance(cfg);
}
GPUReconstruction* GPUReconstruction::CreateInstance(const GPUSettingsDeviceBackend& cfg)
{
GPUReconstruction* retVal = nullptr;
DeviceType type = (DeviceType)cfg.deviceType;
#ifdef DEBUG_STREAMER
if (type != DeviceType::CPU) {
GPUError("Cannot create GPUReconstruction for a non-CPU device if DEBUG_STREAMER are enabled");
return nullptr;
}
#endif
if (type == DeviceType::CPU) {
retVal = GPUReconstruction_Create_CPU(cfg);
} else {
auto* loader = GetLibraryInstance(type, true);
if (loader && (retVal = (*loader)->GetPtr(cfg))) {
retVal->mMyLib = *loader;
}
}
if (retVal == nullptr) {
if (cfg.forceDeviceType) {
GPUError("Error: Could not load GPUReconstruction for specified device: %s (%u)", GPUDataTypes::DEVICE_TYPE_NAMES[type], cfg.deviceType);
} else if (type != DeviceType::CPU) {
GPUError("Could not load GPUReconstruction for device type %s (%u), falling back to CPU version", GPUDataTypes::DEVICE_TYPE_NAMES[type], cfg.deviceType);
GPUSettingsDeviceBackend cfg2 = cfg;
cfg2.deviceType = DeviceType::CPU;
retVal = CreateInstance(cfg2);
}
} else {
GPUInfo("Created GPUReconstruction instance for device type %s (%u)%s", GPUDataTypes::DEVICE_TYPE_NAMES[type], cfg.deviceType, cfg.master ? " (slave)" : "");
}
return retVal;
}
bool GPUReconstruction::CheckInstanceAvailable(DeviceType type, bool verbose)
{
if (type == DeviceType::CPU) {
return true;
} else {
auto* loader = GetLibraryInstance(type, verbose);
return loader != nullptr && (*loader)->LoadLibrary() == 0;
}
}
std::shared_ptr<GPUReconstruction::LibraryLoader>* GPUReconstruction::GetLibraryInstance(DeviceType type, bool verbose)
{
if (type == DeviceType::CPU) {
return nullptr;
} else if (type == DeviceType::CUDA) {
#ifdef CUDA_ENABLED
return &sLibCUDA;
#endif
} else if (type == DeviceType::HIP) {
#ifdef HIP_ENABLED
return &sLibHIP;
#endif
} else if (type == DeviceType::OCL) {
#ifdef OPENCL_ENABLED
return &sLibOCL;
#endif
} else {
GPUError("Error: Invalid device type %u", (uint32_t)type);
return nullptr;
}
if (verbose) {
GPUInfo("%s Support not compiled in for device type %u (%s)", GPUDataTypes::DEVICE_TYPE_NAMES[type], (uint32_t)type, GPUDataTypes::DEVICE_TYPE_NAMES[type]);
}
return nullptr;
}
GPUReconstruction* GPUReconstruction::CreateInstance(const char* type, bool forceType, GPUReconstruction* master)
{
DeviceType t = GPUDataTypes::GetDeviceType(type);
if (t == DeviceType::INVALID_DEVICE) {
GPUError("Invalid device type: %s", type);
return nullptr;
}
return CreateInstance(t, forceType, master);
}
std::shared_ptr<GPUReconstruction::LibraryLoader> GPUReconstruction::sLibCUDA(new GPUReconstruction::LibraryLoader("lib" LIBRARY_PREFIX "GPUTrackingCUDA" LIBRARY_EXTENSION, "GPUReconstruction_Create_CUDA"));
std::shared_ptr<GPUReconstruction::LibraryLoader> GPUReconstruction::sLibHIP(new GPUReconstruction::LibraryLoader("lib" LIBRARY_PREFIX "GPUTrackingHIP" LIBRARY_EXTENSION, "GPUReconstruction_Create_HIP"));
std::shared_ptr<GPUReconstruction::LibraryLoader> GPUReconstruction::sLibOCL(new GPUReconstruction::LibraryLoader("lib" LIBRARY_PREFIX "GPUTrackingOCL" LIBRARY_EXTENSION, "GPUReconstruction_Create_OCL"));
GPUReconstruction::LibraryLoader::LibraryLoader(const char* lib, const char* func) : mLibName(lib), mFuncName(func), mGPULib(nullptr), mGPUEntry(nullptr) {}
GPUReconstruction::LibraryLoader::~LibraryLoader() { CloseLibrary(); }
int32_t GPUReconstruction::LibraryLoader::LoadLibrary()
{
static std::mutex mut;
std::lock_guard<std::mutex> lock(mut);
if (mGPUEntry) {
return 0;
}
LIBRARY_TYPE hGPULib;
hGPULib = LIBRARY_LOAD(mLibName);
if (hGPULib == nullptr) {
#ifndef _WIN32
GPUImportant("The following error occured during dlopen: %s", dlerror());
#endif
GPUError("Error Opening cagpu library for GPU Tracker (%s)", mLibName);
return 1;
} else {
void* createFunc = LIBRARY_FUNCTION(hGPULib, mFuncName);
if (createFunc == nullptr) {
GPUError("Error fetching entry function in GPU library\n");
LIBRARY_CLOSE(hGPULib);
return 1;
} else {
mGPULib = (void*)(size_t)hGPULib;
mGPUEntry = createFunc;
GPUInfo("GPU Tracker library loaded and GPU tracker object created sucessfully");
}
}
return 0;
}
GPUReconstruction* GPUReconstruction::LibraryLoader::GetPtr(const GPUSettingsDeviceBackend& cfg)
{
if (LoadLibrary()) {
return nullptr;
}
if (mGPUEntry == nullptr) {
return nullptr;
}
GPUReconstruction* (*tmp)(const GPUSettingsDeviceBackend& cfg) = (GPUReconstruction * (*)(const GPUSettingsDeviceBackend& cfg)) mGPUEntry;
return tmp(cfg);
}
int32_t GPUReconstruction::LibraryLoader::CloseLibrary()
{
if (mGPUEntry == nullptr) {
return 1;
}
LIBRARY_CLOSE((LIBRARY_TYPE)(size_t)mGPULib);
mGPULib = nullptr;
mGPUEntry = nullptr;
return 0;
}