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;
}
View Code

相关文章:

  • 2021-10-07
  • 2021-07-21
  • 2021-12-12
  • 2021-06-16
  • 2022-12-23
  • 2021-08-22
  • 2022-01-22
猜你喜欢
  • 2021-09-22
  • 2021-12-25
  • 2021-08-14
  • 2021-11-30
  • 2021-10-31
  • 2021-10-24
  • 2021-08-01
相关资源
相似解决方案