CUDA--Thrust(3)--List
CUDA List 实现:
尽管Thrust没有提供像C++中的List容器,但是Thrust却和C++兼容。
代码:
#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> #include <list> #include <vector> int main(void) { int i = 0,j=0; // create an STL list with 4 values std::list<int> stl_list; std::list<int>::iterator it_1; stl_list.push_back(10); stl_list.push_back(20); stl_list.push_back(30); stl_list.push_back(40); //show the sequence stl_list std::cout << "show the original stl_list" << std::endl; for (it_1 =stl_list.begin(); it_1 !=stl_list.end(); it_1++) std::cout << "stl_list[" << (++i) << "]=" << *it_1 << std::endl; // initialize a device_vector with the list thrust::device_vector<int> D(stl_list.begin(), stl_list.end()); //show the sequence 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; // copy a device_vector into an STL vector std::vector<int> stl_vector(D.size()); std::vector<int>::iterator it_2; thrust::copy(D.begin(), D.end(), stl_vector.begin()); //show the sequence STL vector std::cout << "show the original STL vector" << std::endl; for (it_2 = stl_vector.begin(); it_2 != stl_vector.end(); it_2++) std::cout << "STL_vector[" << (++j) << "]=" << *it_2 << std::endl; return 0; }