托管线程和操作系统的线程(CUDA用例)的强制关系线程、操作系统、关系、CUDA

2023-09-03 15:14:37 作者:世界的旁观者

问题

我试图创建一个CUDA应用程序很好用.NET集成。设计目标是拥有可以从管理code被叫来几个CUDA功能。数据也应该能够保持功能调用之间的设备上,以便它可以被传递到多个CUDA函数

I'm trying to create an CUDA application that is well integrated with .net. The design goal is to have several CUDA functions that can be called from managed code. Data should also be able to persist on a device between function calls, so that it can be passed to multiple CUDA functions.

有每个单独一块数据仅由一个单一的OS线程访问(根据需要由CUDA)重要性的

It is of importance that each individual piece of data is only accessed by a single OS thread (as required by CUDA)

我的策略

我包装CUDA功能和设备指针在托管C ++ code。 CUDA设备,指针可以被包裹在一个 DevicePointer 类写在MC ++。如果类跟踪哪些线程它正在使用,就可以实施,只有一个单独的线程可以访问该CUDA设备指针

I'm wrapping CUDA functionalities and device pointers in Managed C++ code. A CUDA device pointer can be wrapped in a DevicePointer class written in MC++. If the class tracks which thread it is using, it can enforce that only a single thread can access the CUDA device pointer.

我会再设计方案,以便只有一个线程会尝试访问任何给定的数据块。

I'll then design the program so that only a single thread would attempt to access any given piece of data.

当我需要帮助

我已经做了一些研究,并阅读有关管理线程和操作系统线程之间的区别。似乎有,在一般情况下,一个多向两者之间一对多的关系。

I've done some research, and read about the distinction between managed threads and OS threads. It seems that there is, in general, a many to many relationship between the two.

这意味着即使我只使用一个管理线程,它可以切换操作系统线程,我会失去访问设备的指针。

This means that even though I'm only using a single managed thread, it could switch OS threads, and I'll loose access to a device pointer.

有没有什么办法来强制CLR不动操作系统线程之间的管理线程?

Is there any way to force the CLR to not move a managed thread between OS threads?

推荐答案

使用的 BeginThreadAffinity 和的 EndThreadAffinity 方法:

Use the BeginThreadAffinity and EndThreadAffinity methods :

try
{
    Thread.BeginThreadAffinity(); // prevents OS thread switch

    // your code
    // ...
}
finally
{
    Thread.EndThreadAffinity();
}