You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
3.3 KiB

  1. #ifndef EIGEN_TEST_CUDA_COMMON_H
  2. #define EIGEN_TEST_CUDA_COMMON_H
  3. #include <cuda.h>
  4. #include <cuda_runtime.h>
  5. #include <cuda_runtime_api.h>
  6. #include <iostream>
  7. #ifndef __CUDACC__
  8. dim3 threadIdx, blockDim, blockIdx;
  9. #endif
  10. template<typename Kernel, typename Input, typename Output>
  11. void run_on_cpu(const Kernel& ker, int n, const Input& in, Output& out)
  12. {
  13. for(int i=0; i<n; i++)
  14. ker(i, in.data(), out.data());
  15. }
  16. template<typename Kernel, typename Input, typename Output>
  17. __global__
  18. void run_on_cuda_meta_kernel(const Kernel ker, int n, const Input* in, Output* out)
  19. {
  20. int i = threadIdx.x + blockIdx.x*blockDim.x;
  21. if(i<n) {
  22. ker(i, in, out);
  23. }
  24. }
  25. template<typename Kernel, typename Input, typename Output>
  26. void run_on_cuda(const Kernel& ker, int n, const Input& in, Output& out)
  27. {
  28. typename Input::Scalar* d_in;
  29. typename Output::Scalar* d_out;
  30. std::ptrdiff_t in_bytes = in.size() * sizeof(typename Input::Scalar);
  31. std::ptrdiff_t out_bytes = out.size() * sizeof(typename Output::Scalar);
  32. cudaMalloc((void**)(&d_in), in_bytes);
  33. cudaMalloc((void**)(&d_out), out_bytes);
  34. cudaMemcpy(d_in, in.data(), in_bytes, cudaMemcpyHostToDevice);
  35. cudaMemcpy(d_out, out.data(), out_bytes, cudaMemcpyHostToDevice);
  36. // Simple and non-optimal 1D mapping assuming n is not too large
  37. // That's only for unit testing!
  38. dim3 Blocks(128);
  39. dim3 Grids( (n+int(Blocks.x)-1)/int(Blocks.x) );
  40. cudaThreadSynchronize();
  41. run_on_cuda_meta_kernel<<<Grids,Blocks>>>(ker, n, d_in, d_out);
  42. cudaThreadSynchronize();
  43. // check inputs have not been modified
  44. cudaMemcpy(const_cast<typename Input::Scalar*>(in.data()), d_in, in_bytes, cudaMemcpyDeviceToHost);
  45. cudaMemcpy(out.data(), d_out, out_bytes, cudaMemcpyDeviceToHost);
  46. cudaFree(d_in);
  47. cudaFree(d_out);
  48. }
  49. template<typename Kernel, typename Input, typename Output>
  50. void run_and_compare_to_cuda(const Kernel& ker, int n, const Input& in, Output& out)
  51. {
  52. Input in_ref, in_cuda;
  53. Output out_ref, out_cuda;
  54. #ifndef __CUDA_ARCH__
  55. in_ref = in_cuda = in;
  56. out_ref = out_cuda = out;
  57. #endif
  58. run_on_cpu (ker, n, in_ref, out_ref);
  59. run_on_cuda(ker, n, in_cuda, out_cuda);
  60. #ifndef __CUDA_ARCH__
  61. VERIFY_IS_APPROX(in_ref, in_cuda);
  62. VERIFY_IS_APPROX(out_ref, out_cuda);
  63. #endif
  64. }
  65. void ei_test_init_cuda()
  66. {
  67. int device = 0;
  68. cudaDeviceProp deviceProp;
  69. cudaGetDeviceProperties(&deviceProp, device);
  70. std::cout << "CUDA device info:\n";
  71. std::cout << " name: " << deviceProp.name << "\n";
  72. std::cout << " capability: " << deviceProp.major << "." << deviceProp.minor << "\n";
  73. std::cout << " multiProcessorCount: " << deviceProp.multiProcessorCount << "\n";
  74. std::cout << " maxThreadsPerMultiProcessor: " << deviceProp.maxThreadsPerMultiProcessor << "\n";
  75. std::cout << " warpSize: " << deviceProp.warpSize << "\n";
  76. std::cout << " regsPerBlock: " << deviceProp.regsPerBlock << "\n";
  77. std::cout << " concurrentKernels: " << deviceProp.concurrentKernels << "\n";
  78. std::cout << " clockRate: " << deviceProp.clockRate << "\n";
  79. std::cout << " canMapHostMemory: " << deviceProp.canMapHostMemory << "\n";
  80. std::cout << " computeMode: " << deviceProp.computeMode << "\n";
  81. }
  82. #endif // EIGEN_TEST_CUDA_COMMON_H