2013-06-20 45 views
1

我寫的是什麼;Cuda的推力::錯誤說「終止叫做拋出的一個實例‘推力::系統:: SYSTEM_ERROR’後」

#include <thrust/system_error.h> 
#include <thrust/host_vector.h> 
#include <thrust/device_vector.h> 
#include <thrust/sequence.h> 
#include <thrust/transform.h> 
#include <thrust/replace.h> 
#include <thrust/copy.h> 
#include <thrust/functional.h> 
#include <iostream> 
#include <cmath> //std:sqr 

using namespace thrust; 

// Kernel Code 
template <class K> 
struct sum_square_functor{ 
    __host__ __device__ K operator()(const K& x, const K& y)const{ 
     return pow(x-y,2); 
    } 
}; 

//Test code on CPU 
//void perform_euclidean(){ 
// 
//} 


int main(){ 
    device_vector<float> p_vec(1 << 20); 
    device_vector<float> q_vec(1 << 20); 
    device_vector<float> r_vec(1 << 20); 
    generate(p_vec.begin(), p_vec.end(), rand); 
    generate(q_vec.begin(), q_vec.end(), rand); 
    // Current Thrust's transformations supports 2 input vectors, so we use it 
    transform(p_vec.begin(), p_vec.end(), q_vec.begin(), r_vec.begin(), sum_square_functor<float>()); 

    int sum = thrust::reduce(r_vec.begin(), r_vec.end(), (int)0, thrust::plus<float>()); 
    std::cout << "sqrt(" << sum << ")=" << sqrt(sum) << std::endl; 
    return 0; 
} 

and full error msg is;

terminate called after throwing an instance of 'thrust::system::system_error' 
    what(): unspecified launch failure 

代碼有什麼問題?任何想法?

我發現錯誤是由generate()引起的,但仍然無法擺脫錯誤?

回答

3

rand是一個基於主機庫的功能。您不能直接在設備代碼中使用它。當您嘗試generate在設備向量使用rand,你會建立一個試圖直接使用rand的設備代碼的核心。它是失敗的。

取而代之的是,在主機上創建這些向量並將它們複製到設備,否則使用兼容設備的隨機發生器(thrust has some)。

你應該能夠做到:

host_vector<float> h_p_vec(1 << 20); 
host_vector<float> h_q_vec(1 << 20); 
generate(h_p_vec.begin(), h_p_vec.end(), rand); 
generate(h_q_vec.begin(), h_q_vec.end(), rand); 
device_vector<float> p_vec = h_p_vec; 
device_vector<float> q_vec = h_q_vec;