gpu_common.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #ifndef EIGEN_TEST_GPU_COMMON_H
  2. #define EIGEN_TEST_GPU_COMMON_H
  3. #ifdef EIGEN_USE_HIP
  4. #include <hip/hip_runtime.h>
  5. #include <hip/hip_runtime_api.h>
  6. #else
  7. #include <cuda.h>
  8. #include <cuda_runtime.h>
  9. #include <cuda_runtime_api.h>
  10. #endif
  11. #include <iostream>
  12. #define EIGEN_USE_GPU
  13. #include <unsupported/Eigen/CXX11/src/Tensor/TensorGpuHipCudaDefines.h>
  14. #if !defined(__CUDACC__) && !defined(__HIPCC__)
  15. dim3 threadIdx, blockDim, blockIdx;
  16. #endif
  17. template<typename Kernel, typename Input, typename Output>
  18. void run_on_cpu(const Kernel& ker, int n, const Input& in, Output& out)
  19. {
  20. for(int i=0; i<n; i++)
  21. ker(i, in.data(), out.data());
  22. }
  23. template<typename Kernel, typename Input, typename Output>
  24. __global__
  25. EIGEN_HIP_LAUNCH_BOUNDS_1024
  26. void run_on_gpu_meta_kernel(const Kernel ker, int n, const Input* in, Output* out)
  27. {
  28. int i = threadIdx.x + blockIdx.x*blockDim.x;
  29. if(i<n) {
  30. ker(i, in, out);
  31. }
  32. }
  33. template<typename Kernel, typename Input, typename Output>
  34. void run_on_gpu(const Kernel& ker, int n, const Input& in, Output& out)
  35. {
  36. typename Input::Scalar* d_in;
  37. typename Output::Scalar* d_out;
  38. std::ptrdiff_t in_bytes = in.size() * sizeof(typename Input::Scalar);
  39. std::ptrdiff_t out_bytes = out.size() * sizeof(typename Output::Scalar);
  40. gpuMalloc((void**)(&d_in), in_bytes);
  41. gpuMalloc((void**)(&d_out), out_bytes);
  42. gpuMemcpy(d_in, in.data(), in_bytes, gpuMemcpyHostToDevice);
  43. gpuMemcpy(d_out, out.data(), out_bytes, gpuMemcpyHostToDevice);
  44. // Simple and non-optimal 1D mapping assuming n is not too large
  45. // That's only for unit testing!
  46. dim3 Blocks(128);
  47. dim3 Grids( (n+int(Blocks.x)-1)/int(Blocks.x) );
  48. gpuDeviceSynchronize();
  49. #ifdef EIGEN_USE_HIP
  50. hipLaunchKernelGGL(HIP_KERNEL_NAME(run_on_gpu_meta_kernel<Kernel,
  51. typename std::decay<decltype(*d_in)>::type,
  52. typename std::decay<decltype(*d_out)>::type>),
  53. dim3(Grids), dim3(Blocks), 0, 0, ker, n, d_in, d_out);
  54. #else
  55. run_on_gpu_meta_kernel<<<Grids,Blocks>>>(ker, n, d_in, d_out);
  56. #endif
  57. // Pre-launch errors.
  58. gpuError_t err = gpuGetLastError();
  59. if (err != gpuSuccess) {
  60. printf("%s: %s\n", gpuGetErrorName(err), gpuGetErrorString(err));
  61. gpu_assert(false);
  62. }
  63. // Kernel execution errors.
  64. err = gpuDeviceSynchronize();
  65. if (err != gpuSuccess) {
  66. printf("%s: %s\n", gpuGetErrorName(err), gpuGetErrorString(err));
  67. gpu_assert(false);
  68. }
  69. // check inputs have not been modified
  70. gpuMemcpy(const_cast<typename Input::Scalar*>(in.data()), d_in, in_bytes, gpuMemcpyDeviceToHost);
  71. gpuMemcpy(out.data(), d_out, out_bytes, gpuMemcpyDeviceToHost);
  72. gpuFree(d_in);
  73. gpuFree(d_out);
  74. }
  75. template<typename Kernel, typename Input, typename Output>
  76. void run_and_compare_to_gpu(const Kernel& ker, int n, const Input& in, Output& out)
  77. {
  78. Input in_ref, in_gpu;
  79. Output out_ref, out_gpu;
  80. #if !defined(EIGEN_GPU_COMPILE_PHASE)
  81. in_ref = in_gpu = in;
  82. out_ref = out_gpu = out;
  83. #else
  84. EIGEN_UNUSED_VARIABLE(in);
  85. EIGEN_UNUSED_VARIABLE(out);
  86. #endif
  87. run_on_cpu (ker, n, in_ref, out_ref);
  88. run_on_gpu(ker, n, in_gpu, out_gpu);
  89. #if !defined(EIGEN_GPU_COMPILE_PHASE)
  90. VERIFY_IS_APPROX(in_ref, in_gpu);
  91. VERIFY_IS_APPROX(out_ref, out_gpu);
  92. #endif
  93. }
  94. struct compile_time_device_info {
  95. EIGEN_DEVICE_FUNC
  96. void operator()(int i, const int* /*in*/, int* info) const
  97. {
  98. if (i == 0) {
  99. EIGEN_UNUSED_VARIABLE(info)
  100. #if defined(__CUDA_ARCH__)
  101. info[0] = int(__CUDA_ARCH__ +0);
  102. #endif
  103. #if defined(EIGEN_HIP_DEVICE_COMPILE)
  104. info[1] = int(EIGEN_HIP_DEVICE_COMPILE +0);
  105. #endif
  106. }
  107. }
  108. };
  109. void ei_test_init_gpu()
  110. {
  111. int device = 0;
  112. gpuDeviceProp_t deviceProp;
  113. gpuGetDeviceProperties(&deviceProp, device);
  114. ArrayXi dummy(1), info(10);
  115. info = -1;
  116. run_on_gpu(compile_time_device_info(),10,dummy,info);
  117. std::cout << "GPU compile-time info:\n";
  118. #ifdef EIGEN_CUDACC
  119. std::cout << " EIGEN_CUDACC: " << int(EIGEN_CUDACC) << "\n";
  120. #endif
  121. #ifdef EIGEN_CUDA_SDK_VER
  122. std::cout << " EIGEN_CUDA_SDK_VER: " << int(EIGEN_CUDA_SDK_VER) << "\n";
  123. #endif
  124. #ifdef EIGEN_COMP_NVCC
  125. std::cout << " EIGEN_COMP_NVCC: " << int(EIGEN_COMP_NVCC) << "\n";
  126. #endif
  127. #ifdef EIGEN_HIPCC
  128. std::cout << " EIGEN_HIPCC: " << int(EIGEN_HIPCC) << "\n";
  129. #endif
  130. std::cout << " EIGEN_CUDA_ARCH: " << info[0] << "\n";
  131. std::cout << " EIGEN_HIP_DEVICE_COMPILE: " << info[1] << "\n";
  132. std::cout << "GPU device info:\n";
  133. std::cout << " name: " << deviceProp.name << "\n";
  134. std::cout << " capability: " << deviceProp.major << "." << deviceProp.minor << "\n";
  135. std::cout << " multiProcessorCount: " << deviceProp.multiProcessorCount << "\n";
  136. std::cout << " maxThreadsPerMultiProcessor: " << deviceProp.maxThreadsPerMultiProcessor << "\n";
  137. std::cout << " warpSize: " << deviceProp.warpSize << "\n";
  138. std::cout << " regsPerBlock: " << deviceProp.regsPerBlock << "\n";
  139. std::cout << " concurrentKernels: " << deviceProp.concurrentKernels << "\n";
  140. std::cout << " clockRate: " << deviceProp.clockRate << "\n";
  141. std::cout << " canMapHostMemory: " << deviceProp.canMapHostMemory << "\n";
  142. std::cout << " computeMode: " << deviceProp.computeMode << "\n";
  143. }
  144. #endif // EIGEN_TEST_GPU_COMMON_H