ランタイムの現在のデバイス -> ドライバー コンテキスト スタック
現在のデバイスを ( でcudaSetDevice()
) 設定すると、選択したデバイスのプライマリ コンテキストがスタックの一番上に配置されます。
- スタックが空だった場合は、スタックにプッシュされます。
- スタックが空でない場合は、スタックの一番上を置き換えます。
ドライバー コンテキスト スタック -> ランタイムの現在のデバイス
(この部分については 100% 確信があるわけではないので、大まかに理解してください。)
ランタイムは、現在のデバイスが現在のコンテキストのデバイスであると報告します (それがプライマリ コンテキストであるかどうかに関係なく)。
コンテキスト スタックが空の場合、ランタイムの現在のデバイスは 0 として報告されます。
この動作を説明するプログラム:
#include <cuda/api.hpp>
#include <iostream>
void report_current_device()
{
std::cout << "Runtime believes the current device index is: "
<< cuda::device::current::detail_::get_id() << '\n';
}
int main()
{
namespace context = cuda::context::detail_;
namespace cur_dev = cuda::device::current::detail_;
namespace pc = cuda::device::primary_context::detail_;
namespace cur_ctx = cuda::context::current::detail_;
using std::cout;
cuda::device::id_t dev_idx[2];
cuda::context::handle_t pc_handle[2];
cuda::initialize_driver();
dev_idx[0] = cur_dev::get_id();
report_current_device();
dev_idx[1] = (dev_idx[0] == 0) ? 1 : 0;
pc_handle[0] = pc::obtain_and_increase_refcount(dev_idx[0]);
cout << "Obtained primary context handle for device " << dev_idx[0]<< '\n';
pc_handle[1] = pc::obtain_and_increase_refcount(dev_idx[1]);
cout << "Obtained primary context handle for device " << dev_idx[1]<< '\n';
report_current_device();
cur_ctx::push(pc_handle[1]);
cout << "Pushed primary context handle for device " << dev_idx[1] << " onto the stack\n";
report_current_device();
auto ctx = context::create_and_push(dev_idx[0]);
cout << "Created a new context for device " << dev_idx[0] << " and pushed it onto the stack\n";
report_current_device();
cur_ctx::push(ctx);
cout << "Pushed primary context handle for device " << dev_idx[0] << " onto the stack\n";
report_current_device();
cur_ctx::push(pc_handle[1]);
cout << "Pushed primary context for device " << dev_idx[1] << " onto the stack\n";
report_current_device();
pc::decrease_refcount(dev_idx[1]);
cout << "Deactivated/destroyed primary context for device " << dev_idx[1] << '\n';
report_current_device();
}
...結果は次のとおりです。
Runtime believes the current device index is: 0
Obtained primary context handle for device 0
Obtained primary context handle for device 1
Runtime believes the current device index is: 0
Pushed primary context handle for device 1 onto the stack
Runtime believes the current device index is: 1
Created a new context for device 0 and pushed it onto the stack
Runtime believes the current device index is: 0
Pushed primary context handle for device 0 onto the stack
Runtime believes the current device index is: 0
Pushed primary context for device 1 onto the stack
Runtime believes the current device index is: 1
Deactivated/destroyed primary context for device 1
Runtime believes the current device index is: 1
プログラムは私のこのライブラリを使用します。