CUDA-Thrust(1)--函数初始化

1.元素初始化:

  通常初始都会用具体的值和向量,但是Thrust 提供了一些其他的初始化方法。

2.代码:

#include "cuda_runtime.h"
#include "device_launch_parameters.h"

#include 

#include 
#include 
#include 
#include 
#include 
#include 


int main(void) {

    //initialize all ten integers of a device_vector to 1
    thrust::device_vector<int>D(10, 1);

    //show the original D
    std::cout << "show the original D" << std::endl;
    for (int i = 0; i < D.size(); ++i)
        std::cout << "D[" << i << "]=" << D[i] << std::endl;

    //set the first seven elements of a vector to 9
    thrust::fill(D.begin(), D.begin() + 7, 9);

    std::cout << "show the fill D" << std::endl;
    for (int i = 0; i < D.size(); ++i)
        std::cout << "D[" << i << "]=" << D[i] << std::endl;



    //initialize a host_vector with the first five elements of D
    thrust::host_vector<int>H(D.begin(), D.begin() + 5);

    //show the original H
    std::cout << "show the original H" << std::endl;
    for (int i = 0; i < H.size(); ++i)
        std::cout << "H[" << i << "]=" << H[i] << std::endl;

    //set the elements of H to 0,1,2,3,...
    thrust::sequence(H.begin(), H.end());

    //show the sequence H
    std::cout << "show the sequence H " << std::endl;
    for (int i = 0; i < H.size(); ++i)
        std::cout << "H[" << i << "]=" << H[i] << std::endl;

    //copy all of H back to the beginning of D
    thrust::copy(H.begin(), H.end(), D.begin());

    //print D
    std::cout << "show the final D" << std::endl;
    for (int i = 0; i < D.size(); ++i)
        std::cout << "D[" << i << "]=" << D[i] << std::endl;

    
    return 0;
}
View Code

  在本例中演示了fill,copy和sequence函数的用法。copy函数可以复制内存(主机和设备)

范围内的元素到另一个主机或者设备的内存空间。

 

你可能感兴趣的:(CUDA-Thrust(1)--函数初始化)