Android-cuttlefish cvd tool
cuda_context.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2026 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include <cuda.h>
20
21#include <map>
22#include <memory>
23#include <mutex>
24
26
27namespace cuttlefish {
28
29struct CudaFunctions;
30class ScopedCudaContext;
31
32// Shared CUDA primary context for a GPU device. Thread-safe.
34 public:
35 // Returns nullptr if the device is not available.
36 static std::shared_ptr<CudaContext> Get(int device_id = 0);
37
38 // Pushes a CUDA context onto the calling thread's stack. Returns a
39 // ScopedCudaContext that pops it on destruction. Safe to nest multiple
40 // calls per thread; each push/pop is balanced by the destructor.
41 static Result<ScopedCudaContext> Acquire(int device_id = 0);
42
44
45 CUcontext get() const { return ctx_; }
46 const CudaFunctions* cuda() const { return cuda_; }
47 std::recursive_mutex& mutex() const { return mutex_; }
48
49 private:
50 CudaContext(CUcontext ctx, int device_id, const CudaFunctions* cuda);
51
52 CUcontext ctx_;
55 mutable std::recursive_mutex mutex_;
56};
57
58// RAII lock and push/pop for a CUDA context on the current thread.
60 public:
61 explicit ScopedCudaContext(const CudaContext& context);
62 ScopedCudaContext(CUcontext ctx, const CudaFunctions* cuda,
63 std::recursive_mutex* mutex = nullptr);
65
66 bool ok() const { return push_succeeded_; }
67
72
73 private:
74 std::unique_lock<std::recursive_mutex> lock_;
75 const CudaFunctions* cuda_ = nullptr;
76 bool push_succeeded_ = false;
77};
78
79} // namespace cuttlefish
Definition: cuda_context.h:33
static std::shared_ptr< CudaContext > Get(int device_id=0)
Definition: cuda_context.cpp:28
~CudaContext()
Definition: cuda_context.cpp:103
std::recursive_mutex & mutex() const
Definition: cuda_context.h:47
CudaContext(CUcontext ctx, int device_id, const CudaFunctions *cuda)
Definition: cuda_context.cpp:99
std::recursive_mutex mutex_
Definition: cuda_context.h:55
int device_id_
Definition: cuda_context.h:53
const CudaFunctions * cuda_
Definition: cuda_context.h:54
static Result< ScopedCudaContext > Acquire(int device_id=0)
Definition: cuda_context.cpp:90
CUcontext ctx_
Definition: cuda_context.h:52
const CudaFunctions * cuda() const
Definition: cuda_context.h:46
CUcontext get() const
Definition: cuda_context.h:45
Definition: cuda_context.h:59
ScopedCudaContext(const ScopedCudaContext &)=delete
ScopedCudaContext(const CudaContext &context)
Definition: cuda_context.cpp:113
ScopedCudaContext & operator=(const ScopedCudaContext &)=delete
ScopedCudaContext & operator=(ScopedCudaContext &&)=delete
~ScopedCudaContext()
Definition: cuda_context.cpp:147
std::unique_lock< std::recursive_mutex > lock_
Definition: cuda_context.h:74
bool ok() const
Definition: cuda_context.h:66
const CudaFunctions * cuda_
Definition: cuda_context.h:75
bool push_succeeded_
Definition: cuda_context.h:76
Definition: alloc_driver.h:21
tl::expected< T, StackTraceError< E > > Result
Definition: result_type.h:30
Definition: cuda_loader.h:28