2018-07-11 22:39:54 +08:00
|
|
|
#ifndef EIGEN_TEST_GPU_COMMON_H
|
|
|
|
#define EIGEN_TEST_GPU_COMMON_H
|
|
|
|
|
|
|
|
#ifdef EIGEN_USE_HIP
|
|
|
|
#include <hip/hip_runtime.h>
|
|
|
|
#include <hip/hip_runtime_api.h>
|
|
|
|
#else
|
|
|
|
#include <cuda.h>
|
|
|
|
#include <cuda_runtime.h>
|
|
|
|
#include <cuda_runtime_api.h>
|
|
|
|
#endif
|
2013-11-05 22:41:45 +08:00
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
2018-07-11 22:39:54 +08:00
|
|
|
#define EIGEN_USE_GPU
|
|
|
|
#include <unsupported/Eigen/CXX11/src/Tensor/TensorGpuHipCudaDefines.h>
|
|
|
|
|
|
|
|
#if !defined(__CUDACC__) && !defined(__HIPCC__)
|
2013-11-05 22:41:45 +08:00
|
|
|
dim3 threadIdx, blockDim, blockIdx;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
template<typename Kernel, typename Input, typename Output>
|
|
|
|
void run_on_cpu(const Kernel& ker, int n, const Input& in, Output& out)
|
|
|
|
{
|
|
|
|
for(int i=0; i<n; i++)
|
|
|
|
ker(i, in.data(), out.data());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<typename Kernel, typename Input, typename Output>
|
|
|
|
__global__
|
2020-08-20 04:06:39 +08:00
|
|
|
EIGEN_HIP_LAUNCH_BOUNDS_1024
|
2018-07-11 22:39:54 +08:00
|
|
|
void run_on_gpu_meta_kernel(const Kernel ker, int n, const Input* in, Output* out)
|
2013-11-05 22:41:45 +08:00
|
|
|
{
|
|
|
|
int i = threadIdx.x + blockIdx.x*blockDim.x;
|
|
|
|
if(i<n) {
|
|
|
|
ker(i, in, out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<typename Kernel, typename Input, typename Output>
|
2018-07-11 22:39:54 +08:00
|
|
|
void run_on_gpu(const Kernel& ker, int n, const Input& in, Output& out)
|
2013-11-05 22:41:45 +08:00
|
|
|
{
|
|
|
|
typename Input::Scalar* d_in;
|
|
|
|
typename Output::Scalar* d_out;
|
|
|
|
std::ptrdiff_t in_bytes = in.size() * sizeof(typename Input::Scalar);
|
|
|
|
std::ptrdiff_t out_bytes = out.size() * sizeof(typename Output::Scalar);
|
|
|
|
|
2018-07-11 22:39:54 +08:00
|
|
|
gpuMalloc((void**)(&d_in), in_bytes);
|
|
|
|
gpuMalloc((void**)(&d_out), out_bytes);
|
2013-11-05 22:41:45 +08:00
|
|
|
|
2018-07-11 22:39:54 +08:00
|
|
|
gpuMemcpy(d_in, in.data(), in_bytes, gpuMemcpyHostToDevice);
|
|
|
|
gpuMemcpy(d_out, out.data(), out_bytes, gpuMemcpyHostToDevice);
|
2013-11-05 22:41:45 +08:00
|
|
|
|
|
|
|
// Simple and non-optimal 1D mapping assuming n is not too large
|
|
|
|
// That's only for unit testing!
|
|
|
|
dim3 Blocks(128);
|
|
|
|
dim3 Grids( (n+int(Blocks.x)-1)/int(Blocks.x) );
|
|
|
|
|
2018-07-11 22:39:54 +08:00
|
|
|
gpuDeviceSynchronize();
|
|
|
|
|
|
|
|
#ifdef EIGEN_USE_HIP
|
2018-08-31 04:22:16 +08:00
|
|
|
hipLaunchKernelGGL(HIP_KERNEL_NAME(run_on_gpu_meta_kernel<Kernel,
|
|
|
|
typename std::decay<decltype(*d_in)>::type,
|
|
|
|
typename std::decay<decltype(*d_out)>::type>),
|
2018-07-11 22:39:54 +08:00
|
|
|
dim3(Grids), dim3(Blocks), 0, 0, ker, n, d_in, d_out);
|
|
|
|
#else
|
|
|
|
run_on_gpu_meta_kernel<<<Grids,Blocks>>>(ker, n, d_in, d_out);
|
|
|
|
#endif
|
2020-12-23 14:49:06 +08:00
|
|
|
// Pre-launch errors.
|
|
|
|
gpuError_t err = gpuGetLastError();
|
|
|
|
if (err != gpuSuccess) {
|
|
|
|
printf("%s: %s\n", gpuGetErrorName(err), gpuGetErrorString(err));
|
|
|
|
gpu_assert(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Kernel execution errors.
|
|
|
|
err = gpuDeviceSynchronize();
|
|
|
|
if (err != gpuSuccess) {
|
|
|
|
printf("%s: %s\n", gpuGetErrorName(err), gpuGetErrorString(err));
|
|
|
|
gpu_assert(false);
|
|
|
|
}
|
2018-07-11 22:39:54 +08:00
|
|
|
|
2013-11-05 22:41:45 +08:00
|
|
|
|
|
|
|
// check inputs have not been modified
|
2018-07-11 22:39:54 +08:00
|
|
|
gpuMemcpy(const_cast<typename Input::Scalar*>(in.data()), d_in, in_bytes, gpuMemcpyDeviceToHost);
|
|
|
|
gpuMemcpy(out.data(), d_out, out_bytes, gpuMemcpyDeviceToHost);
|
2013-11-05 22:41:45 +08:00
|
|
|
|
2018-07-11 22:39:54 +08:00
|
|
|
gpuFree(d_in);
|
|
|
|
gpuFree(d_out);
|
2013-11-05 22:41:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<typename Kernel, typename Input, typename Output>
|
2018-07-11 22:39:54 +08:00
|
|
|
void run_and_compare_to_gpu(const Kernel& ker, int n, const Input& in, Output& out)
|
2013-11-05 22:41:45 +08:00
|
|
|
{
|
2018-07-11 22:39:54 +08:00
|
|
|
Input in_ref, in_gpu;
|
|
|
|
Output out_ref, out_gpu;
|
2020-12-23 14:49:06 +08:00
|
|
|
#if !defined(EIGEN_GPU_COMPILE_PHASE)
|
2018-07-11 22:39:54 +08:00
|
|
|
in_ref = in_gpu = in;
|
|
|
|
out_ref = out_gpu = out;
|
2018-07-12 23:02:18 +08:00
|
|
|
#else
|
|
|
|
EIGEN_UNUSED_VARIABLE(in);
|
|
|
|
EIGEN_UNUSED_VARIABLE(out);
|
2013-11-05 22:41:45 +08:00
|
|
|
#endif
|
|
|
|
run_on_cpu (ker, n, in_ref, out_ref);
|
2018-07-11 22:39:54 +08:00
|
|
|
run_on_gpu(ker, n, in_gpu, out_gpu);
|
2020-12-23 14:49:06 +08:00
|
|
|
#if !defined(EIGEN_GPU_COMPILE_PHASE)
|
2018-07-11 22:39:54 +08:00
|
|
|
VERIFY_IS_APPROX(in_ref, in_gpu);
|
|
|
|
VERIFY_IS_APPROX(out_ref, out_gpu);
|
2013-11-05 22:41:45 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-07-13 22:05:07 +08:00
|
|
|
struct compile_time_device_info {
|
|
|
|
EIGEN_DEVICE_FUNC
|
2020-12-23 14:49:06 +08:00
|
|
|
void operator()(int i, const int* /*in*/, int* info) const
|
2018-07-13 22:05:07 +08:00
|
|
|
{
|
2020-12-23 14:49:06 +08:00
|
|
|
if (i == 0) {
|
2021-01-07 01:41:15 +08:00
|
|
|
EIGEN_UNUSED_VARIABLE(info)
|
2020-12-23 14:49:06 +08:00
|
|
|
#if defined(__CUDA_ARCH__)
|
|
|
|
info[0] = int(__CUDA_ARCH__ +0);
|
|
|
|
#endif
|
|
|
|
#if defined(EIGEN_HIP_DEVICE_COMPILE)
|
|
|
|
info[1] = int(EIGEN_HIP_DEVICE_COMPILE +0);
|
|
|
|
#endif
|
|
|
|
}
|
2018-07-13 22:05:07 +08:00
|
|
|
}
|
|
|
|
};
|
2013-11-05 22:41:45 +08:00
|
|
|
|
2018-07-11 22:39:54 +08:00
|
|
|
void ei_test_init_gpu()
|
2013-11-05 22:41:45 +08:00
|
|
|
{
|
|
|
|
int device = 0;
|
2018-07-11 22:39:54 +08:00
|
|
|
gpuDeviceProp_t deviceProp;
|
|
|
|
gpuGetDeviceProperties(&deviceProp, device);
|
2018-07-13 22:05:07 +08:00
|
|
|
|
|
|
|
ArrayXi dummy(1), info(10);
|
|
|
|
info = -1;
|
|
|
|
run_on_gpu(compile_time_device_info(),10,dummy,info);
|
|
|
|
|
|
|
|
|
|
|
|
std::cout << "GPU compile-time info:\n";
|
|
|
|
|
|
|
|
#ifdef EIGEN_CUDACC
|
|
|
|
std::cout << " EIGEN_CUDACC: " << int(EIGEN_CUDACC) << "\n";
|
|
|
|
#endif
|
|
|
|
|
2019-06-01 06:26:06 +08:00
|
|
|
#ifdef EIGEN_CUDA_SDK_VER
|
|
|
|
std::cout << " EIGEN_CUDA_SDK_VER: " << int(EIGEN_CUDA_SDK_VER) << "\n";
|
2018-07-13 22:05:07 +08:00
|
|
|
#endif
|
|
|
|
|
2019-06-01 06:26:06 +08:00
|
|
|
#ifdef EIGEN_COMP_NVCC
|
|
|
|
std::cout << " EIGEN_COMP_NVCC: " << int(EIGEN_COMP_NVCC) << "\n";
|
|
|
|
#endif
|
|
|
|
|
2018-07-13 22:05:07 +08:00
|
|
|
#ifdef EIGEN_HIPCC
|
|
|
|
std::cout << " EIGEN_HIPCC: " << int(EIGEN_HIPCC) << "\n";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
std::cout << " EIGEN_CUDA_ARCH: " << info[0] << "\n";
|
|
|
|
std::cout << " EIGEN_HIP_DEVICE_COMPILE: " << info[1] << "\n";
|
|
|
|
|
2018-07-11 22:39:54 +08:00
|
|
|
std::cout << "GPU device info:\n";
|
2014-01-24 19:51:33 +08:00
|
|
|
std::cout << " name: " << deviceProp.name << "\n";
|
2013-11-05 22:41:45 +08:00
|
|
|
std::cout << " capability: " << deviceProp.major << "." << deviceProp.minor << "\n";
|
|
|
|
std::cout << " multiProcessorCount: " << deviceProp.multiProcessorCount << "\n";
|
|
|
|
std::cout << " maxThreadsPerMultiProcessor: " << deviceProp.maxThreadsPerMultiProcessor << "\n";
|
2014-01-24 19:51:33 +08:00
|
|
|
std::cout << " warpSize: " << deviceProp.warpSize << "\n";
|
2013-11-05 22:41:45 +08:00
|
|
|
std::cout << " regsPerBlock: " << deviceProp.regsPerBlock << "\n";
|
|
|
|
std::cout << " concurrentKernels: " << deviceProp.concurrentKernels << "\n";
|
|
|
|
std::cout << " clockRate: " << deviceProp.clockRate << "\n";
|
2014-01-24 19:51:33 +08:00
|
|
|
std::cout << " canMapHostMemory: " << deviceProp.canMapHostMemory << "\n";
|
2013-11-05 22:41:45 +08:00
|
|
|
std::cout << " computeMode: " << deviceProp.computeMode << "\n";
|
|
|
|
}
|
|
|
|
|
2018-07-11 22:39:54 +08:00
|
|
|
#endif // EIGEN_TEST_GPU_COMMON_H
|