Coverage Control Library
Loading...
Searching...
No Matches
cuda_utils.h
Go to the documentation of this file.
1/*
2 * This file is part of the CoverageControl library
3 *
4 * Author: Saurav Agarwal
5 * Contact: sauravag@seas.upenn.edu, agr.saurav1@gmail.com
6 * Repository: https://github.com/KumarRobotics/CoverageControl
7 *
8 * Copyright (c) 2024, Saurav Agarwal
9 *
10 * The CoverageControl library is free software: you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or (at your
13 * option) any later version.
14 *
15 * The CoverageControl library is distributed in the hope that it will be
16 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
18 * Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along with
21 * CoverageControl library. If not, see <https://www.gnu.org/licenses/>.
22 */
23
29#ifndef CPPSRC_CORE_INCLUDE_COVERAGECONTROL_CUDA_UTILS_H_
30#define CPPSRC_CORE_INCLUDE_COVERAGECONTROL_CUDA_UTILS_H_
31
32#include <iostream>
33#include <sstream>
34#include <string>
35#include <vector>
36
37#include "CoverageControl/Config.h"
38namespace CoverageControl {
39
45class CudaUtils {
46 private:
47 static bool is_cuda_initialized_;
48 static bool is_cuda_available_;
49 static int device_count_;
50 static int device_id_;
51 static bool use_cuda_;
52
53 public:
57 CudaUtils() = delete;
58
59 static bool UseCuda() { return use_cuda_; }
60
61 static void SetUseCuda(bool use_cuda) { use_cuda_ = use_cuda; }
62
67 static bool IsCudaAvailable() {
68#ifdef COVERAGECONTROL_WITH_CUDA
69 int device_count;
70 return !!GetDeviceCount(device_count);
71#endif
72 return false;
73 }
74
79 static bool IsCudaInitialized() {
80 if (!use_cuda_) {
81 return false;
82 }
83 return is_cuda_initialized_;
84 }
85
91 static bool InitializeCUDA() {
92#ifdef COVERAGECONTROL_WITH_CUDA
93 if (!use_cuda_) {
94 return false;
95 }
96 if (is_cuda_initialized_) {
97 return true;
98 }
99 auto devices = GetEnvironmentCUDA_VISIBLE_DEVICES();
100 if (devices.empty()) {
101 device_id_ = FindDevice();
102 } else {
103 /* device_id_ = GPUGetMaxGflopsDeviceId(devices); */
104 device_id_ = 0;
105 }
106 if (device_id_ < 0) {
107 std::cerr << "No CUDA device found" << std::endl;
108 return false;
109 }
110 /* std::cout << "Initializing CUDA device " << device_id_ << std::endl; */
111 if (GPUDeviceInit(device_id_) != device_id_) {
112 std::cerr << "Failed to initialize CUDA device" << std::endl;
113 return false;
114 }
115 is_cuda_initialized_ = true;
116 return true;
117#endif
118 return false;
119 }
120
121#ifdef COVERAGECONTROL_WITH_CUDA
126 static std::vector<int> GetEnvironmentCUDA_VISIBLE_DEVICES() {
127 std::vector<int> cuda_visible_devices;
128 char *env_cuda_visible_devices = std::getenv("CUDA_VISIBLE_DEVICES");
129 if (env_cuda_visible_devices != nullptr) {
130 std::string str_cuda_visible_devices(env_cuda_visible_devices);
131 std::istringstream ss(str_cuda_visible_devices);
132 std::string token;
133 while (std::getline(ss, token, ',')) {
134 cuda_visible_devices.push_back(std::stoi(token));
135 }
136 }
137 return cuda_visible_devices;
138 }
139
145 static bool GetDeviceCount(int &device_count);
146
152 static int GPUDeviceInit(int dev_id);
153
162 static int FindDevice();
163
168 static int FindIntegratedGPU();
169
176 static int GPUGetMaxGflopsDeviceId(std::vector<int> device_list = {});
177#endif
178};
179} /* namespace CoverageControl */
180
181#endif // CPPSRC_CORE_INCLUDE_COVERAGECONTROL_CUDA_UTILS_H_
Static class for CUDA utilities This class provides utility functions for CUDA It is a static class a...
Definition cuda_utils.h:45
static void SetUseCuda(bool use_cuda)
Definition cuda_utils.h:61
static bool IsCudaAvailable()
Definition cuda_utils.h:67
static bool IsCudaInitialized()
Definition cuda_utils.h:79
static bool InitializeCUDA()
Initializes a CUDA device use_cuda_ must be set to true before calling this function.
Definition cuda_utils.h:91
Namespace for the CoverageControl library.