error-handling - Cuda::推力错误说 "terminate called after throwing an instance of '推力::系统::系统错误'”

标签 error-handling cuda runtime-error thrust

我写的是;

#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;
}

和完整的错误消息是;
terminate called after throwing an instance of 'thrust::system::system_error'
  what():  unspecified launch failure

代码有什么问题?任何的想法?

我发现错误是由 generate() 引起的但仍然无法摆脱错误?

最佳答案

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;

关于error-handling - Cuda::推力错误说 "terminate called after throwing an instance of '推力::系统::系统错误'”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17213412/

相关文章:

sql-server - 删除和添加列时纠正错误处理

vba - CopyPaste中的应用程序定义或对象定义的错误

debugging - 传递给设备函数的共享内存地址仍然是共享内存吗?

c++ - 如何使用 Clang 的 CUDA 编译器?

c - 在 C 中复制字符串数组时出错

c - 返回 "ENOTDIR"、 "EBUSY"等字符串的函数?

unit-testing - CakePHP fatal error : Class 'ErrorHandler' not found

node.js - Heroku 上的生产构建中显示开发构建错误

cuda - CUDA内存是如何管理的?

installation - 使用 "DownloadString"参数调用 "1"的异常 : "Unable to connect to the remote server"