From 7fa70419cf0d1ff397a1630081ea0854bb9bc6c3 Mon Sep 17 00:00:00 2001 From: Bruno Turcksin Date: Mon, 13 Aug 2018 14:41:30 +0000 Subject: [PATCH] Better guard CUDA code Use __CUDACC__ guard around CUDA to make sure that the CUDA code is only sent to nvcc and not the host compiler when CUDA support is enable. --- include/deal.II/base/cuda.h | 31 +++++++++++++++---- include/deal.II/base/numbers.h | 3 +- include/deal.II/lac/cuda_atomic.h | 2 +- include/deal.II/lac/cuda_kernels.h | 2 +- include/deal.II/lac/cuda_kernels.templates.h | 4 +++ include/deal.II/lac/cuda_solver_direct.h | 2 +- include/deal.II/lac/cuda_sparse_matrix.h | 2 +- .../deal.II/lac/read_write_vector.templates.h | 4 +-- .../deal.II/matrix_free/cuda_fe_evaluation.h | 2 +- .../matrix_free/cuda_hanging_nodes_internal.h | 2 +- .../deal.II/matrix_free/cuda_matrix_free.h | 4 +-- .../matrix_free/cuda_matrix_free.templates.h | 2 +- .../matrix_free/cuda_tensor_product_kernels.h | 3 ++ tests/all-headers/CMakeLists.txt | 11 ------- 14 files changed, 43 insertions(+), 31 deletions(-) diff --git a/include/deal.II/base/cuda.h b/include/deal.II/base/cuda.h index 7f9ad5f759..fa26cacf2a 100644 --- a/include/deal.II/base/cuda.h +++ b/include/deal.II/base/cuda.h @@ -18,15 +18,15 @@ #include -#ifdef DEAL_II_WITH_CUDA - -# include +#include +#if defined(DEAL_II_WITH_CUDA) && defined(__CUDACC__) # include # include # include +#endif -# include +#include DEAL_II_NAMESPACE_OPEN namespace Utilities @@ -58,6 +58,7 @@ namespace Utilities */ ~Handle(); +#if defined(DEAL_II_WITH_CUDA) && defined(__CUDACC__) cusolverDnHandle_t cusolver_dn_handle; cusolverSpHandle_t cusolver_sp_handle; @@ -66,6 +67,7 @@ namespace Utilities * Handle to the cuSPARSE library. */ cusparseHandle_t cusparse_handle; +#endif }; /** @@ -75,9 +77,14 @@ namespace Utilities inline void malloc(T *&pointer, const unsigned int n_elements) { +#if defined(DEAL_II_WITH_CUDA) && defined(__CUDACC__) cudaError_t cuda_error_code = cudaMalloc(&pointer, n_elements * sizeof(T)); AssertCuda(cuda_error_code); +#else + (void)pointer; + (void)n_elements; +#endif } /** @@ -87,9 +94,13 @@ namespace Utilities inline void free(T *&pointer) { +#if defined(DEAL_II_WITH_CUDA) && defined(__CUDACC__) cudaError_t cuda_error_code = cudaFree(pointer); AssertCuda(cuda_error_code); pointer = nullptr; +#else + (void)pointer; +#endif } /** @@ -99,11 +110,16 @@ namespace Utilities inline void copy_to_host(const T *pointer_dev, std::vector &vector_host) { +#if defined(DEAL_II_WITH_CUDA) && defined(__CUDACC__) cudaError_t cuda_error_code = cudaMemcpy(vector_host.data(), pointer_dev, vector_host.size() * sizeof(T), cudaMemcpyDeviceToHost); AssertCuda(cuda_error_code); +#else + (void)pointer_dev; + (void)vector_host; +#endif } /** @@ -114,11 +130,16 @@ namespace Utilities inline void copy_to_dev(const std::vector &vector_host, T *pointer_dev) { +#if defined(DEAL_II_WITH_CUDA) && defined(__CUDACC__) cudaError_t cuda_error_code = cudaMemcpy(pointer_dev, vector_host.data(), vector_host.size() * sizeof(T), cudaMemcpyHostToDevice); AssertCuda(cuda_error_code); +#else + (void)vector_host; + (void)pointer_dev; +#endif } } // namespace CUDA } // namespace Utilities @@ -126,5 +147,3 @@ namespace Utilities DEAL_II_NAMESPACE_CLOSE #endif - -#endif diff --git a/include/deal.II/base/numbers.h b/include/deal.II/base/numbers.h index 5123c9ed12..1bc05e6345 100644 --- a/include/deal.II/base/numbers.h +++ b/include/deal.II/base/numbers.h @@ -25,8 +25,7 @@ #include #include -#ifdef DEAL_II_WITH_CUDA -# include +#if defined(DEAL_II_WITH_CUDA) && defined(__CUDACC__) # define DEAL_II_CUDA_HOST_DEV __host__ __device__ #else # define DEAL_II_CUDA_HOST_DEV diff --git a/include/deal.II/lac/cuda_atomic.h b/include/deal.II/lac/cuda_atomic.h index f018365923..e717b633b9 100644 --- a/include/deal.II/lac/cuda_atomic.h +++ b/include/deal.II/lac/cuda_atomic.h @@ -18,7 +18,7 @@ #include -#ifdef DEAL_II_WITH_CUDA +#if defined(DEAL_II_WITH_CUDA) && defined(__CUDACC__) DEAL_II_NAMESPACE_OPEN diff --git a/include/deal.II/lac/cuda_kernels.h b/include/deal.II/lac/cuda_kernels.h index 2e29e06b39..367df1c081 100644 --- a/include/deal.II/lac/cuda_kernels.h +++ b/include/deal.II/lac/cuda_kernels.h @@ -18,7 +18,7 @@ #include -#ifdef DEAL_II_WITH_CUDA +#if defined(DEAL_II_WITH_CUDA) && defined(__CUDACC__) # include diff --git a/include/deal.II/lac/cuda_kernels.templates.h b/include/deal.II/lac/cuda_kernels.templates.h index 8ae0e51ab7..81d8ef9bcf 100644 --- a/include/deal.II/lac/cuda_kernels.templates.h +++ b/include/deal.II/lac/cuda_kernels.templates.h @@ -18,6 +18,8 @@ #include +#if defined(DEAL_II_WITH_CUDA) && defined(__CUDACC__) + DEAL_II_NAMESPACE_OPEN namespace LinearAlgebra @@ -573,3 +575,5 @@ namespace LinearAlgebra DEAL_II_NAMESPACE_CLOSE #endif + +#endif diff --git a/include/deal.II/lac/cuda_solver_direct.h b/include/deal.II/lac/cuda_solver_direct.h index 2a4956918c..493a70e9f1 100644 --- a/include/deal.II/lac/cuda_solver_direct.h +++ b/include/deal.II/lac/cuda_solver_direct.h @@ -18,7 +18,7 @@ #include -#ifdef DEAL_II_WITH_CUDA +#if defined(DEAL_II_WITH_CUDA) && defined(__CUDACC__) # include # include diff --git a/include/deal.II/lac/cuda_sparse_matrix.h b/include/deal.II/lac/cuda_sparse_matrix.h index 60ff84d7d2..0be2bf0e82 100644 --- a/include/deal.II/lac/cuda_sparse_matrix.h +++ b/include/deal.II/lac/cuda_sparse_matrix.h @@ -20,7 +20,7 @@ #include -#ifdef DEAL_II_WITH_CUDA +#if defined(DEAL_II_WITH_CUDA) && defined(__CUDACC__) # include # include diff --git a/include/deal.II/lac/read_write_vector.templates.h b/include/deal.II/lac/read_write_vector.templates.h index c5792d876d..0b6f003b21 100644 --- a/include/deal.II/lac/read_write_vector.templates.h +++ b/include/deal.II/lac/read_write_vector.templates.h @@ -40,7 +40,7 @@ # include #endif -#ifdef DEAL_II_WITH_CUDA +#if defined(DEAL_II_WITH_CUDA) # include # include @@ -568,7 +568,7 @@ namespace LinearAlgebra -#ifdef DEAL_II_WITH_CUDA +#if defined(DEAL_II_WITH_CUDA) template void ReadWriteVector::import( diff --git a/include/deal.II/matrix_free/cuda_fe_evaluation.h b/include/deal.II/matrix_free/cuda_fe_evaluation.h index 08d6e1c83f..2f7c46a766 100644 --- a/include/deal.II/matrix_free/cuda_fe_evaluation.h +++ b/include/deal.II/matrix_free/cuda_fe_evaluation.h @@ -18,7 +18,7 @@ #include -#ifdef DEAL_II_WITH_CUDA +#if defined(DEAL_II_WITH_CUDA) && defined(__CUDACC__) # include # include diff --git a/include/deal.II/matrix_free/cuda_hanging_nodes_internal.h b/include/deal.II/matrix_free/cuda_hanging_nodes_internal.h index 39a8589535..f9ee32cc69 100644 --- a/include/deal.II/matrix_free/cuda_hanging_nodes_internal.h +++ b/include/deal.II/matrix_free/cuda_hanging_nodes_internal.h @@ -18,7 +18,7 @@ #include -#ifdef DEAL_II_WITH_CUDA +#if defined(DEAL_II_WITH_CUDA) && defined(__CUDACC__) # include diff --git a/include/deal.II/matrix_free/cuda_matrix_free.h b/include/deal.II/matrix_free/cuda_matrix_free.h index c790337846..0bc0dacf57 100644 --- a/include/deal.II/matrix_free/cuda_matrix_free.h +++ b/include/deal.II/matrix_free/cuda_matrix_free.h @@ -19,7 +19,7 @@ #include -#ifdef DEAL_II_WITH_CUDA +#if defined(DEAL_II_WITH_CUDA) && defined(__CUDACC__) # include # include @@ -33,8 +33,6 @@ # include # include -# include - DEAL_II_NAMESPACE_OPEN diff --git a/include/deal.II/matrix_free/cuda_matrix_free.templates.h b/include/deal.II/matrix_free/cuda_matrix_free.templates.h index c9615aef3e..a227dff179 100644 --- a/include/deal.II/matrix_free/cuda_matrix_free.templates.h +++ b/include/deal.II/matrix_free/cuda_matrix_free.templates.h @@ -19,7 +19,7 @@ #include -#ifdef DEAL_II_WITH_CUDA +#if defined(DEAL_II_WITH_CUDA) && defined(__CUDACC__) # include # include diff --git a/include/deal.II/matrix_free/cuda_tensor_product_kernels.h b/include/deal.II/matrix_free/cuda_tensor_product_kernels.h index ef39036149..e1232ad56d 100644 --- a/include/deal.II/matrix_free/cuda_tensor_product_kernels.h +++ b/include/deal.II/matrix_free/cuda_tensor_product_kernels.h @@ -19,6 +19,7 @@ #include +#if defined(DEAL_II_WITH_CUDA) && defined(__CUDACC__) DEAL_II_NAMESPACE_OPEN @@ -439,3 +440,5 @@ namespace CUDAWrappers DEAL_II_NAMESPACE_CLOSE #endif + +#endif diff --git a/tests/all-headers/CMakeLists.txt b/tests/all-headers/CMakeLists.txt index 3b5e51b6e0..68cb3c1609 100644 --- a/tests/all-headers/CMakeLists.txt +++ b/tests/all-headers/CMakeLists.txt @@ -45,17 +45,6 @@ FILE(GLOB_RECURSE _headers RELATIVE ${_include_dir}/deal.II ${_include_dir}/deal.II/*.h ) -# -# Files that contain CUDA code cannot be sent to the host compiler so we removed -# them from the list -# -LIST(REMOVE_ITEM _headers "lac/cuda_atomic.h" - "lac/cuda_kernels.h" - "lac/cuda_kernels.templates.h" - "matrix_free/cuda_fe_evaluation.h" - "matrix_free/cuda_hanging_nodes_internal.h" - "matrix_free/cuda_matrix_free.templates.h" - "matrix_free/cuda_tensor_product_kernels.h") # Do not test bundled headers to avoid issues when tests are run # for an already installed library -- 2.39.5