0

CUDA ランタイムには「現在のデバイス」の概念がありますが、CUDA ドライバーにはありません。代わりに、ドライバーにはコンテキストのスタックがあり、「現在のコンテキスト」がスタックの一番上にあります。

両者はどのように相互作用しますか?つまり、ドライバー API 呼び出しはランタイム API の現在のデバイスにどのように影響し、現在のデバイスを変更するとドライバー API のコンテキスト スタックまたはその他の状態にどのように影響するのでしょうか?

やや関連する質問: cuda ドライバー api と cuda ランタイム api を混在させるにはどうすればよいですか?

4

1 に答える 1

1

ランタイムの現在のデバイス -> ドライバー コンテキスト スタック

現在のデバイスを ( で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

プログラムは私のこのライブラリを使用します。

于 2021-12-01T19:01:58.833 に答える