1. nvidia提供了一个c++的类库thrust用来简化编程,在安装cuda toolkit时候已经包含了thrust
这个库全是头文件,不需要添加任何库文件的依赖
测试程序
#include <thrust/host_vector.h> #include <thrust/device_vector.h> #include <thrust/generate.h> #include <thrust/sort.h> #include <thrust/copy.h> #include <algorithm> #include <cstdlib> ///////////cpu #include <windows.h> #include <algorithm> template <class T> void cpu_sort(T begin, T end) { std::sort(begin, end); } void gpu_sort(thrust::host_vector<int> &h_vec) { // transfer data to the device thrust::device_vector<int> d_vec = h_vec; // sort data on the device (846M keys per second on GeForce GTX 480) thrust::sort(d_vec.begin(), d_vec.end()); // transfer data back to host thrust::copy(d_vec.begin(), d_vec.end(), h_vec.begin()); } #define CHK_TIME(x) {int t1=GetTickCount();x;int t2=GetTickCount();printf(#x ": %d\n", t2-t1);} int main(void) { // generate 32M random numbers serially thrust::host_vector<int> h_vec(32 << 20); std::generate(h_vec.begin(), h_vec.end(), rand); thrust::host_vector<int> h_vec_1(h_vec); CHK_TIME(cpu_sort(h_vec_1.begin(), h_vec_1.end())); thrust::host_vector<int> h_vec_2(h_vec); CHK_TIME(gpu_sort(h_vec_2)); return 0; }