CUDA--Thrust--数学运算(5)

  Thrust有一个Transformations. 这个方法为我们进行数学运算提供了方便其中 thrust/functional.h 

包含大部分内建的算法和比较操作。代码如下:

#include <thrust/device_vector.h>
#include <thrust/transform.h>
#include <thrust/sequence.h>
#include <thrust/copy.h>
#include <thrust/fill.h>
#include <thrust/replace.h>
#include <thrust/functional.h>
#include <iostream>

int main(void) {

    //allocate three device vectors with 10 elements
    thrust::device_vector<int>X(10);
    thrust::device_vector<int>Y(10);
    thrust::device_vector<int>Z(10);

    //initialize X to 0,1,2,3.
    thrust::sequence(X.begin(), X.end());

    //compute Y=-X
    thrust::transform(X.begin(), X.end(), Y.begin(), thrust::negate<int>());

    //fill Z with twos
    thrust::fill(Z.begin(), Z.end(), 2);

    //compute Y=X mod 2
    thrust::transform(X.begin(), X.end(), Z.begin(), Y.begin(), thrust::modulus<int>());

    //replace all the ones in Y with tens

    thrust::replace(Y.begin(), Y.end(), 1, 10);

    //print Y
    thrust::copy(Y.begin(), Y.end(), std::ostream_iterator<int>(std::cout, "\n"));

    


    return 0;
}
operatoins

 

posted @ 2020-07-22 04:29  flyingswallow  阅读(390)  评论(0编辑  收藏  举报