ManagedCuda:IllegalAddress;而执行内核 [英] ManagedCuda: IllegalAddress; While Executing a Kernel

查看:409
本文介绍了ManagedCuda:IllegalAddress;而执行内核的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#项目中使用ManagedCuda库来使用GPU,目前我正在跟踪此

一切似乎都与我的代码工作正常,内核被发现,内置和方法调用执行,但我得到一个错误:



ManagedCuda.dll中发生未处理的类型为ManagedCuda.CudaException的异常

其他信息:ErrorIllegalAddress:一个内核,设备
在一个无效的内存地址上遇到一个加载或存储指令。

上下文不能使用,所以它必须被销毁(并且应该创建一个新的)。

我理解C#抱怨在尝试传递设备指针时找不到有效地址到内核​​,唯一的区别,我可以告诉我的代码和在引用的教程中的帖子,是ManagedCuda似乎最近有一个整容,允许用户使用Lambdas,我做了一些阅读,没有找到任何东西以澄清这是否导致我的问题:

  static Func< int,int,int& cudaAdd =(a,b)=> 
{
// init输出参数
CudaDeviceVariable< int> result_dev = 0;
int result_host = 0;
//运行CUDA方法
addWithCuda.Run(a,b,result_dev.DevicePointer); < ---代码在这里抛出错误
//复制返回主机
result_dev.CopyToHost(ref result_host);
return result_host;
};在原始教程代码中,OP使用 CudaDeviceVariable result_dev = 0; code>。这可能是问题吗?我不知道为什么会这样,但也许我的演员是错误的?



为了清楚这里是被调用的内核:

  __ global__ void kernel(int a,int b,int * c)
{
* c =(a + b)* a + b);
}


解决方案

抛出的异常是由于运行在不同的线程。在单线程环境中,示例代码运行良好,返回正确的结果。为了在多线程应用程序中使用Cuda,必须正确同步线程并将cuda上下文绑定到当前活动的线程。


I am using the ManagedCuda library in a C# project to utilise the GPU, currently I am following along with this tutorial regarding how to write code compatible between C# and C++ after failing to achieve it with OpenCV.

Everything seems to be working fine with my code, the kernel is found, built and the method call is executed however I am getting an error:

An unhandled exception of type 'ManagedCuda.CudaException' occurred in ManagedCuda.dll

Additional information: ErrorIllegalAddress: While executing a kernel, the device 
encountered a load or store instruction on an invalid memory address.

The context cannot be used, so it must be destroyed (and a new one should be created).

I understand that C# is complaining that a valid address is not found when it attempts to pass the device pointer to the kernel, the only difference I can tell between my code and the post in the cited tutorial, is that ManagedCuda seems to have recently had a facelift which allows users to use Lambdas, I've done some reading and haven't found anything to clarify whether or not this is what's causing my problem:

static Func<int, int, int> cudaAdd = (a, b) =>
{
    // init output parameters
    CudaDeviceVariable<int> result_dev = 0;
    int result_host = 0;
    // run CUDA method
    addWithCuda.Run(a, b, result_dev.DevicePointer);   <--- Code throws error here
    // copy return to host
    result_dev.CopyToHost(ref result_host);
    return result_host;
};

In the original tutorial code the OP uses CudaDeviceVariable result_dev = 0;. Could this be the problem? I don't see why it would be, but maybe my cast is wrong?

For clarity here is the kernel which is being called:

__global__ void kernel(int a, int b, int *c)
{
    *c = (a + b)*(a + b);
}

解决方案

From the comments it seems that the thrown exception is due to running on different threads. In a single threaded environment the sample code runs fine returning the right result. In order to use Cuda in a multithreaded application, proper synchronization of the threads and binding the cuda context to the currently active threads would be necessary.

这篇关于ManagedCuda:IllegalAddress;而执行内核的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆