• 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函数可以复制内存(主机和设备)

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

  • 相关阅读:
    .net core 操作IngestAttachment插件
    CF1106F Lunar New Year and a Recursive Sequence
    PyQt5开发环境搭建和配置 何苦
    centos7安装GitLab 何苦
    GitLab 使用介绍 何苦
    git_stats web代码图形统计工具详解 何苦
    在 Windows 上安装 Python 何苦
    .gitignore 何苦
    使用Pyinstaller转换.py文件为.exe可执行程序 何苦
    Docker安装 各系统安装 ubuntu mac windows 何苦
  • 原文地址:https://www.cnblogs.com/xuelanga000/p/13185989.html
Copyright © 2020-2023  润新知