Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在推力函子中使用CURAND_C++_Cuda_Thrust - Fatal编程技术网

C++ 在推力函子中使用CURAND

C++ 在推力函子中使用CURAND,c++,cuda,thrust,C++,Cuda,Thrust,在一个设备函子中可以使用CURAND和推力吗?最低限度的代码示例可以是: #include <thrust/device_vector.h> struct Move { Move() {} using Position = thrust::tuple<double, double>; __host__ __device__ Position operator()(Position p) { thrust::ge

在一个设备函子中可以使用CURAND和推力吗?最低限度的代码示例可以是:

#include <thrust/device_vector.h>

struct Move
{
    Move() {}

    using Position = thrust::tuple<double, double>;

    __host__ __device__
    Position operator()(Position p)
    {
        thrust::get<0>(p) += 1.0; // use CURAND to add a random N(0,1)
        thrust::get<1>(p) += 1.0; // use CURAND to add a random N(0,1)
        return p;
    }
};

int main()
{
    // Create vectors on device
    thrust::device_vector<double> p1(10, 0.0);
    thrust::device_vector<double> p2(10, 0.0);

    // Create zip iterators
    auto pBeg = thrust::make_zip_iterator(thrust::make_tuple(p1.begin(), p2.begin()));
    auto pEnd = thrust::make_zip_iterator(thrust::make_tuple(p1.end(),   p2.end()  ));

    // Move points in the vectors
    thrust::transform(pBeg, pEnd, pBeg, Move());

    // Print result (just for debug)
    thrust::copy(p1.begin(), p1.end(), std::ostream_iterator<double>(std::cout, "\n"));
    thrust::copy(p2.begin(), p2.end(), std::ostream_iterator<double>(std::cout, "\n"));

    return 0;
}
#包括
结构移动
{
移动(){}
使用位置=推力::元组;
__主机设备__
位置运算符()(位置p)
{
推力::get(p)+=1.0;//使用CURAND添加随机N(0,1)
推力::get(p)+=1.0;//使用CURAND添加随机N(0,1)
返回p;
}
};
int main()
{
//在设备上创建向量
推力:装置_矢量p1(10,0.0);
推力:器件_矢量p2(10,0.0);
//创建zip迭代器
auto-pBeg=推力::make_-zip_迭代器(推力::make_元组(p1.begin(),p2.begin());
auto-pEnd=推力::make_-zip_迭代器(推力::make_元组(p1.end(),p2.end());
//在向量中移动点
推力::变换(pBeg,pEnd,pBeg,Move());
//打印结果(仅用于调试)
推力::复制(p1.begin(),p1.end(),std::ostream_迭代器(std::cout,“\n”);
推力::复制(p2.begin(),p2.end(),std::ostream_迭代器(std::cout,“\n”);
返回0;
}
在运算符函数中创建随机数的正确方法是什么

在一个设备函子中可以使用CURAND和推力吗

是的,有可能。如@m.s.所示,您需要从curand获得的大部分信息都可以从curand文档中的中获得。(事实上,文档中甚至有完整的推力/推力示例代码)

我们可以通过一个推力算法调用来模拟那里指示的安装内核的行为,例如为每个设备向量元素设置初始的curand状态变量

之后,只需通过zip迭代器中的附加迭代器将初始化的curand状态传递给
Move
函子,然后在函子中调用
curand\u uniform
(例如)

下面是一个基于您的代码的完整示例:

$ cat t20.cu
#include <thrust/device_vector.h>
#include <curand_kernel.h>
#include <iostream>
#include <thrust/iterator/counting_iterator.h>
#include <thrust/transform.h>
#include <thrust/for_each.h>

const int seed = 1234;
const int ds = 10;
const int offset = 0;

struct Move
{
    Move() {}

    using Position = thrust::tuple<double, double, curandState>;

    __device__
    Position operator()(Position p)
    {
        curandState s = thrust::get<2>(p);
        thrust::get<0>(p) += curand_uniform(&s); // use CURAND to add a random N(0,1)
        thrust::get<1>(p) += curand_uniform(&s); // use CURAND to add a random N(0,1)
        thrust::get<2>(p) = s;
        return p;
    }
};

struct curand_setup
{
    using init_tuple = thrust::tuple<int, curandState &>;
    __device__
    void operator()(init_tuple t){
      curandState s;
      int id = thrust::get<0>(t);
      curand_init(seed, id, offset, &s);
      thrust::get<1>(t) = s;
      }
};

int main()
{
    // Create vectors on device
    thrust::device_vector<double> p1(ds, 0.0);
    thrust::device_vector<double> p2(ds, 0.0);
    thrust::device_vector<curandState> s1(ds);

    // Create zip iterators
    auto pBeg = thrust::make_zip_iterator(thrust::make_tuple(p1.begin(), p2.begin(), s1.begin()));
    auto pEnd = thrust::make_zip_iterator(thrust::make_tuple(p1.end(),   p2.end(), s1.end()  ));
    auto pInit = thrust::make_zip_iterator(thrust::make_tuple(thrust::counting_iterator<int>(0), s1.begin()));
    // initialize random generator
    thrust::for_each_n(pInit, ds, curand_setup());
    // Move points in the vectors
    thrust::transform(pBeg, pEnd, pBeg, Move());

    // Print result (just for debug)
    thrust::copy(p1.begin(), p1.end(), std::ostream_iterator<double>(std::cout, "\n"));
    thrust::copy(p2.begin(), p2.end(), std::ostream_iterator<double>(std::cout, "\n"));

    return 0;
}
$ nvcc -arch=sm_61 -std=c++11 t20.cu -o t20 -lcurand
$ ./t20
0.145468
0.820181
0.550399
0.29483
0.914733
0.868979
0.321921
0.782857
0.0113023
0.28545
0.434899
0.926417
0.811845
0.308556
0.557235
0.501246
0.206681
0.123377
0.539587
0.198575
$
$cat t20.cu
#包括
#包括
#包括
#包括
#包括
#包括
常数int seed=1234;
常数int ds=10;
常数int offset=0;
结构移动
{
移动(){}
使用位置=推力::元组;
__装置__
位置运算符()(位置p)
{
curandState s=推力::获取(p);
推力::get(p)+=curand_uniform(&s);//使用curand添加随机N(0,1)
推力::get(p)+=curand_uniform(&s);//使用curand添加随机N(0,1)
推力:get(p)=s;
返回p;
}
};
结构库设置
{
使用init_tuple=推力::tuple;
__装置__
void运算符()(初始元组t){
库兰德斯州;
int id=推力::get(t);
curand_init(种子、id、偏移和s);
推力:get(t)=s;
}
};
int main()
{
//在设备上创建向量
推力:装置_矢量p1(ds,0.0);
推力:器件_矢量p2(ds,0.0);
推力:装置_矢量s1(ds);
//创建zip迭代器
auto-pBeg=推力::make_-zip_迭代器(推力::make_元组(p1.begin(),p2.begin(),s1.begin());
auto pEnd=推力::make_zip_迭代器(推力::make_元组(p1.end(),p2.end(),s1.end());
auto-pInit=推力::make_-zip_迭代器(推力::make_元组(推力::计数迭代器(0),s1.begin());
//初始化随机发生器
推力:用于每个(pInit、ds、curand_setup());
//在向量中移动点
推力::变换(pBeg,pEnd,pBeg,Move());
//打印结果(仅用于调试)
推力::复制(p1.begin(),p1.end(),std::ostream_迭代器(std::cout,“\n”);
推力::复制(p2.begin(),p2.end(),std::ostream_迭代器(std::cout,“\n”);
返回0;
}
$nvcc-arch=sm_61-std=c++11 t20.cu-o t20-lcurand
美元/t20
0.145468
0.820181
0.550399
0.29483
0.914733
0.868979
0.321921
0.782857
0.0113023
0.28545
0.434899
0.926417
0.811845
0.308556
0.557235
0.501246
0.206681
0.123377
0.539587
0.198575
$
关于这个问题:

在运算符函数中创建随机数的正确方法是什么

在推力中使用curand没有问题,但是您可能还想知道推力有一个内置的,并且有一个完整的使用示例

在一个设备函子中可以使用CURAND和推力吗

是的,有可能。如@m.s.所示,您需要从curand获得的大部分信息都可以从curand文档中的中获得。(事实上,文档中甚至有完整的推力/推力示例代码)

我们可以通过一个推力算法调用来模拟那里指示的安装内核的行为,例如为每个设备向量元素设置初始的curand状态变量

之后,只需通过zip迭代器中的附加迭代器将初始化的curand状态传递给
Move
函子,然后在函子中调用
curand\u uniform
(例如)

下面是一个基于您的代码的完整示例:

$ cat t20.cu
#include <thrust/device_vector.h>
#include <curand_kernel.h>
#include <iostream>
#include <thrust/iterator/counting_iterator.h>
#include <thrust/transform.h>
#include <thrust/for_each.h>

const int seed = 1234;
const int ds = 10;
const int offset = 0;

struct Move
{
    Move() {}

    using Position = thrust::tuple<double, double, curandState>;

    __device__
    Position operator()(Position p)
    {
        curandState s = thrust::get<2>(p);
        thrust::get<0>(p) += curand_uniform(&s); // use CURAND to add a random N(0,1)
        thrust::get<1>(p) += curand_uniform(&s); // use CURAND to add a random N(0,1)
        thrust::get<2>(p) = s;
        return p;
    }
};

struct curand_setup
{
    using init_tuple = thrust::tuple<int, curandState &>;
    __device__
    void operator()(init_tuple t){
      curandState s;
      int id = thrust::get<0>(t);
      curand_init(seed, id, offset, &s);
      thrust::get<1>(t) = s;
      }
};

int main()
{
    // Create vectors on device
    thrust::device_vector<double> p1(ds, 0.0);
    thrust::device_vector<double> p2(ds, 0.0);
    thrust::device_vector<curandState> s1(ds);

    // Create zip iterators
    auto pBeg = thrust::make_zip_iterator(thrust::make_tuple(p1.begin(), p2.begin(), s1.begin()));
    auto pEnd = thrust::make_zip_iterator(thrust::make_tuple(p1.end(),   p2.end(), s1.end()  ));
    auto pInit = thrust::make_zip_iterator(thrust::make_tuple(thrust::counting_iterator<int>(0), s1.begin()));
    // initialize random generator
    thrust::for_each_n(pInit, ds, curand_setup());
    // Move points in the vectors
    thrust::transform(pBeg, pEnd, pBeg, Move());

    // Print result (just for debug)
    thrust::copy(p1.begin(), p1.end(), std::ostream_iterator<double>(std::cout, "\n"));
    thrust::copy(p2.begin(), p2.end(), std::ostream_iterator<double>(std::cout, "\n"));

    return 0;
}
$ nvcc -arch=sm_61 -std=c++11 t20.cu -o t20 -lcurand
$ ./t20
0.145468
0.820181
0.550399
0.29483
0.914733
0.868979
0.321921
0.782857
0.0113023
0.28545
0.434899
0.926417
0.811845
0.308556
0.557235
0.501246
0.206681
0.123377
0.539587
0.198575
$
$cat t20.cu
#包括
#包括
#包括
#包括
#包括
#包括
常数int seed=1234;
常数int ds=10;
常数int offset=0;
结构移动
{
移动(){}
使用位置=推力::元组;
__装置__
位置运算符()(位置p)
{
curandState s=推力::获取(p);
推力::get(p)+=curand_uniform(&s);//使用curand添加随机N(0,1)
推力::get(p)+=curand_uniform(&s);//使用curand添加随机N(0,1)
推力:get(p)=s;
返回p;
}
};
结构库设置
{
使用init_tuple=推力::tuple;
__装置__
void运算符()(初始元组t){
库兰德斯州;
int id=推力::get(t);
curand_init(种子、id、偏移和s);
推力:get(t)=s;
}
};
int main()
{
//在设备上创建向量
推力:装置_矢量p1(ds,0.0);
推力:器件_矢量p2(ds,0.0);
推力:装置_矢量s1(ds);
//创建zip迭代器
auto-pBeg=推力::make_-zip_迭代器(推力::make_元组(p1.begin(),p2.begin(),s1.begi