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
47 private:
48 CudaContext(CUcontext ctx, int device_id, const CudaFunctions* cuda);
49
50 CUcontext ctx_;
53};
54
55// RAII push/pop for a CUDA context on the current thread.
57 public:
58 ScopedCudaContext(CUcontext ctx, const CudaFunctions* cuda);
60
61 bool ok() const { return push_succeeded_; }
62
67
68 private:
69 const CudaFunctions* cuda_ = nullptr;
70 bool push_succeeded_ = false;
71};
72
73} // namespace cuttlefish
Definition: cuda_context.h:33
CudaContext(CUcontext ctx, int device_id, const CudaFunctions *cuda)
Definition: cuda_context.cpp:89
static std::shared_ptr< CudaContext > Get(int device_id=0)
Definition: cuda_context.cpp:25
~CudaContext()
Definition: cuda_context.cpp:93
int device_id_
Definition: cuda_context.h:51
const CudaFunctions * cuda_
Definition: cuda_context.h:52
static Result< ScopedCudaContext > Acquire(int device_id=0)
Definition: cuda_context.cpp:80
CUcontext ctx_
Definition: cuda_context.h:50
CUcontext get() const
Definition: cuda_context.h:45
Definition: cuda_context.h:56
~ScopedCudaContext()
Definition: cuda_context.cpp:124
ScopedCudaContext(const ScopedCudaContext &)=delete
ScopedCudaContext & operator=(const ScopedCudaContext &)=delete
ScopedCudaContext(CUcontext ctx, const CudaFunctions *cuda)
Definition: cuda_context.cpp:103
ScopedCudaContext & operator=(ScopedCudaContext &&)=delete
bool ok() const
Definition: cuda_context.h:61
const CudaFunctions * cuda_
Definition: cuda_context.h:69
bool push_succeeded_
Definition: cuda_context.h:70
Definition: alloc_driver.h:20
tl::expected< T, StackTraceError > Result
Definition: result_type.h:30
Definition: cuda_loader.h:28