Skip to content

Commit

Permalink
CudaContext::CanCreate() を追加
Browse files Browse the repository at this point in the history
  • Loading branch information
melpon committed Feb 21, 2025
1 parent 4831cb3 commit 9b66cda
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
- Release 用の sumomo は C++ SDK のリリースバイナリを使用してビルドする
- リアルタイムメッセージング以外の機能がほぼ全て含まれている sumomo をリリース時に含めるようにする
- @torikizi
- [ADD] `CudaContext::CanCreate()` を追加
- @melpon

## 2025.1.0

Expand Down
10 changes: 7 additions & 3 deletions examples/sumomo/src/sumomo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,9 @@ int main(int argc, char* argv[]) {
// 表示して終了する系の処理はここに書く
if (show_video_codec_capability) {
sora::VideoCodecCapabilityConfig config;
config.cuda_context = sora::CudaContext::Create();
if (sora::CudaContext::CanCreate()) {
config.cuda_context = sora::CudaContext::Create();
}
config.openh264_path = openh264;
auto capability = sora::GetVideoCodecCapability(config);
for (const auto& engine : capability.engines) {
Expand Down Expand Up @@ -615,8 +617,10 @@ int main(int argc, char* argv[]) {
}
if (context_config.video_codec_factory_config.preference->HasImplementation(
sora::VideoCodecImplementation::kNvidiaVideoCodecSdk)) {
context_config.video_codec_factory_config.capability_config.cuda_context =
sora::CudaContext::Create();
if (sora::CudaContext::CanCreate()) {
context_config.video_codec_factory_config.capability_config
.cuda_context = sora::CudaContext::Create();
}
}
}

Expand Down
1 change: 1 addition & 0 deletions include/sora/cuda_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class CudaContext {
// CUDA コンテキスト生成する。
// CUDA に対応していないプラットフォームでは nullptr を返す。
static std::shared_ptr<CudaContext> Create();
static bool CanCreate();
};

enum class CudaVideoCodec {
Expand Down
33 changes: 33 additions & 0 deletions src/cuda_context_cuda.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ namespace sora {
std::shared_ptr<CudaContext> CudaContext::Create() {
return nullptr;
}
bool CudaContext::CanCreate() {
return false;
}

} // namespace sora

Expand Down Expand Up @@ -62,6 +65,36 @@ std::shared_ptr<CudaContext> CudaContext::Create() {
}
}

// Create() と同じことをするけど、エラーログを出さないようにする
bool CudaContext::CanCreate() {
CUdevice device;
CUcontext context;

if (!dyn::DynModule::Instance().IsLoadable(dyn::CUDA_SO)) {
return false;
}

CUresult r;
r = dyn::cuInit(0);
if (r != CUDA_SUCCESS) {
return false;
}

r = dyn::cuDeviceGet(&device, 0);
if (r != CUDA_SUCCESS) {
return false;
}

r = dyn::cuCtxCreate(&context, 0, device);
if (r != CUDA_SUCCESS) {
return false;
}

dyn::cuCtxDestroy(context);

return true;
}

CUdevice GetCudaDevice(std::shared_ptr<CudaContext> ctx) {
return std::static_pointer_cast<CudaContextImpl>(ctx)->device;
}
Expand Down

0 comments on commit 9b66cda

Please sign in to comment.