2015-11-04 70 views
1

我寫使用類device_vector用於初始化向量CUDA內核分配()這些代碼的。這個內核是通過一個類的成員函數推出作爲解決這樣的問題:推力異常:「推力::系統:: SYSTEM_ERROR在存儲器位置00000000」

CUDA kernel as member function of a class

和根據

https://devtalk.nvidia.com/default/topic/573289/mixing-c-and-cuda/

我正在使用GTX650Ti GPU,Windows 8.1,Visual Studio 2013社區和CUDA Toolkit 7.5。

代碼initTest.cu沒有編譯,但拋出一個異常的文件trivial_copy.inl做參考。

「在0x775B5B68在initTest.exe第一次機會異常:微軟C++異常:推力::系統:: SYSTEM_ERROR內存位置0x0116F3C8 如果這個異常的處理程序,該程序可以安全地繼續。 「

有誰知道爲什麼會出現這個問題?

頭文件foo.cuh是:

#ifndef FOO_CUH 
#define FOO_CUH 
#include "cuda_runtime.h" 
#include "device_launch_parameters.h" 
#include <thrust/device_vector.h> 
#include <vector> 
using namespace thrust; 
using namespace std; 

__global__ void assign(float *x, const float &constant, const unsigned int &n) 
{ 
    int i = blockDim.x * blockIdx.x + threadIdx.x; 
    if (i < n) 
     x[i] = constant; 
} 
class foo 
{ 
    public: 
    foo(const unsigned int &); 
    void init(const float &); 
    vector<float> domain; 
private: 
    unsigned int samples; 
}; 
foo::foo(const unsigned int &n) 
{ 
    vector<float> result(n); 
    domain = result; 
    samples = n; 
} 
void foo::init(const float &value) 
{ 
    device_vector<float> result(samples); 
    assign <<< 1, domain.size() >>>(raw_pointer_cast(result.data()), value, samples); 
    thrust::copy(result.begin(), result.end(), domain.begin()); 
} 
#endif 

定義的主要功能initTest.cu是:

#include "foo.cuh" 
#include <iostream> 

int main() 
{ 
    foo a(10); 
    a.init(0.5); 
    for (unsigned int i = 0; i < a.domain.size(); i++) 
    { 
     if (i == 0) 
      cout << "{ "; 
     else if (i == a.domain.size() - 1) 
      cout << a.domain[i] << " }"; 
     else 
      cout << a.domain[i] << ", "; 
    } 
    cin.get(); 
    return 0; 
} 
+1

「但是,當我將它集成在一個更長的代碼中」對不起,但您將不得不提供一些關於準確含義的細節。從編譯的角度來看,這裏顯示的代碼沒有任何問題。你究竟如何將它整合到更長的代碼中?你想''將這個文件包含在'.cpp'文件中,也許? (順便提一句,你稱之爲「未解決」**的問題已經解決了。那裏提出的解決方案是正確的。) –

+0

是的你是對的。我還沒有嘗試過單獨的編譯。當我說「更長的代碼」時,我的意思是這段代碼是另一段代碼的一小部分。正如我所說,這段代碼完美工作,我的問題是因爲當我將它集成到該代碼時,出現此錯誤。所以,這是與使用像這樣啓動內核的方法相同的主題。我不明白爲什麼什麼時候分離它編譯和集成它不。 – Vitrion

+0

對不起,我沒跟着你。我會建議提供一個簡短的,完整的例子** **不起作用**。那麼有人可能會提供建議。現在你的問題只是包含一個很好的例子。我不認爲這很有用。 –

回答

1

這是非法的:

__global__ void assign(float *x, const float &constant, const unsigned int &n) 
              ^       ^

內核參數不能通過引用傳遞。

當我刪除&號:

__global__ void assign(float *x, const float constant, const unsigned int n) 

您的代碼運行正常的我。我建議你使用proper cuda error checking。這樣做會把注意力集中在內核上。相反,錯誤未被捕獲,直到推力檢測到並拋出一個system_error,這無助於確定錯誤的來源。

+0

非常感謝。我總是使用CUDA錯誤檢查,但我刪除了說明以保持示例簡短。我工作,可能這是我的其他代碼的解決方案。我會試試看。 – Vitrion