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 <limits>
34#include <sstream>
35#include <string>
36#include <vector>
37
38#include "CoverageControl/Config.h"
39namespace CoverageControl {
40
46class CudaUtils {
47 private:
48 static bool is_cuda_initialized_;
49 static bool is_cuda_available_;
50 static int device_count_;
51 static int device_id_;
52 static bool use_cuda_;
53
54 public:
58 CudaUtils() = delete;
59
60 static bool UseCuda() { return use_cuda_; }
61
62 static void SetUseCuda(bool use_cuda) { use_cuda_ = use_cuda; }
63
68 static bool IsCudaAvailable() {
69#ifdef COVERAGECONTROL_WITH_CUDA
70 int device_count;
71 return !!GetDeviceCount(device_count);
72#endif
73 return false;
74 }
75
80 static bool IsCudaInitialized() {
81 if (!use_cuda_) {
82 return false;
83 }
84 return is_cuda_initialized_;
85 }
86
92 static bool InitializeCUDA() {
93#ifdef COVERAGECONTROL_WITH_CUDA
94 if (!use_cuda_) {
95 return false;
96 }
97 if (is_cuda_initialized_) {
98 return true;
99 }
100 auto devices = GetEnvironmentCUDA_VISIBLE_DEVICES();
101 if (devices.empty()) {
102 device_id_ = FindDevice();
103 } else {
104 device_id_ = GPUGetMaxGflopsDeviceId(devices);
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:46
static bool IsCudaAvailable()
Definition cuda_utils.h:68
static bool IsCudaInitialized()
Definition cuda_utils.h:80
static bool InitializeCUDA()
Initializes a CUDA device use_cuda_ must be set to true before calling this function.
Definition cuda_utils.h:92
Namespace for the CoverageControl library.