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


    1.元素初始化:

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

    2.代码:

    #include "cuda_runtime.h"
    #include "device_launch_parameters.h"
    
    #include <stdio.h>
    
    #include <thrust/host_vector.h>
    #include <thrust/device_vector.h>
    #include <thrust/copy.h>
    #include <thrust/fill.h>
    #include <thrust/sequence.h>
    #include <iostream>
    
    
    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函数可以复制内存(主机和设备)

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

  • 相关阅读:
    001-docker-net-网络设置分类、Bridge详解、mac docker说明
    010-Linux 磁盘信息查看
    011-Spring aop 002-核心说明-切点PointCut、通知Advice、切面Advisor
    【高并发】Akka 模型
    【大数据】Spark On Yarn
    【Git】git pull和git pull --rebase的使用
    【Zookeeper】连接ZooKeeper的方式
    【Cloud】IaaS、PaaS和SaaS
    【Linux】解决"no member named 'max_align_t'
    【神经网络】Dependency Parsing的两种解决方案
  • 原文地址:https://www.cnblogs.com/xuelanga000/p/13185989.html
Copyright © 2020-2023  润新知