From 25ab918ebc554249f93ace0f69460d529a664bbd Mon Sep 17 00:00:00 2001 From: Bruno Turcksin Date: Tue, 29 Sep 2020 01:32:49 +0000 Subject: [PATCH] Move MemorySpaceData to its own file to avoid circular dependency --- include/deal.II/base/memory_space.h | 172 --------------- include/deal.II/base/memory_space_data.h | 202 ++++++++++++++++++ include/deal.II/lac/la_parallel_vector.h | 1 + .../deal.II/lac/vector_operations_internal.h | 1 + 4 files changed, 204 insertions(+), 172 deletions(-) create mode 100644 include/deal.II/base/memory_space_data.h diff --git a/include/deal.II/base/memory_space.h b/include/deal.II/base/memory_space.h index 56c6084498..51c40f56d1 100644 --- a/include/deal.II/base/memory_space.h +++ b/include/deal.II/base/memory_space.h @@ -19,12 +19,6 @@ #include -#include -#include - -#include -#include - DEAL_II_NAMESPACE_OPEN /** @@ -45,172 +39,6 @@ namespace MemorySpace struct CUDA {}; - - - /** - * Data structure - */ - template - struct MemorySpaceData - { - MemorySpaceData() - { - static_assert(std::is_same::value || - std::is_same::value, - "MemorySpace should be Host or CUDA"); - } - - /** - * Copy the active data (values for Host and values_dev for CUDA) to @p begin. - * If the data is on the device it is moved to the host. - */ - virtual void - copy_to(Number *begin, std::size_t n_elements) - { - (void)begin; - (void)n_elements; - } - - /** - * Copy the data in @p begin to the active data of the structure (values for - * Host and values_dev for CUDA). The pointer @p begin must be on the host. - */ - virtual void - copy_from(Number *begin, std::size_t n_elements) - { - (void)begin; - (void)n_elements; - } - - /** - * Pointer to data on the host. - */ - std::unique_ptr> values; - - /** - * Pointer to data on the device. - */ - std::unique_ptr values_dev; - }; - - - - /** - * Swap function similar to std::swap. - */ - template - inline void - swap(MemorySpaceData &, - MemorySpaceData &) - { - static_assert(std::is_same::value || - std::is_same::value, - "MemorySpace should be Host or CUDA"); - } - -#ifndef DOXYGEN - - template - struct MemorySpaceData - { - MemorySpaceData() - : values(nullptr, &std::free) - {} - - virtual ~MemorySpaceData() = default; - - MemorySpaceData(MemorySpaceData &&) noexcept = default; - - MemorySpaceData & - operator=(MemorySpaceData &&) noexcept = default; - - virtual void - copy_to(Number *begin, std::size_t n_elements) - { - std::copy(values.get(), values.get() + n_elements, begin); - } - - virtual void - copy_from(Number *begin, std::size_t n_elements) - { - std::copy(begin, begin + n_elements, values.get()); - } - - std::unique_ptr> values; - - // This is not used but it allows to simplify the code until we start using - // CUDA-aware MPI. - std::unique_ptr values_dev; - }; - - - - template - inline void - swap(MemorySpaceData &u, MemorySpaceData &v) - { - std::swap(u.values, v.values); - } - - - -# ifdef DEAL_II_COMPILER_CUDA_AWARE - - template - struct MemorySpaceData - { - MemorySpaceData() - : values(nullptr, &std::free) - , values_dev(nullptr, Utilities::CUDA::delete_device_data) - {} - - virtual ~MemorySpaceData() = default; - - MemorySpaceData(MemorySpaceData &&) noexcept = default; - - MemorySpaceData & - operator=(MemorySpaceData &&) noexcept = default; - - virtual void - copy_to(Number *begin, std::size_t n_elements) - { - const cudaError_t cuda_error_code = - cudaMemcpy(begin, - values_dev.get(), - n_elements * sizeof(Number), - cudaMemcpyDeviceToHost); - AssertCuda(cuda_error_code); - } - - virtual void - copy_from(Number *begin, std::size_t n_elements) - { - const cudaError_t cuda_error_code = - cudaMemcpy(values_dev.get(), - begin, - n_elements * sizeof(Number), - cudaMemcpyHostToDevice); - AssertCuda(cuda_error_code); - } - - std::unique_ptr> values; - std::unique_ptr values_dev; - }; - - - - template - inline void - swap(MemorySpaceData &u, MemorySpaceData &v) - { - std::swap(u.values, v.values); - std::swap(u.values_dev, v.values_dev); - } - -# endif - -#endif - } // namespace MemorySpace DEAL_II_NAMESPACE_CLOSE diff --git a/include/deal.II/base/memory_space_data.h b/include/deal.II/base/memory_space_data.h new file mode 100644 index 0000000000..7cb37bc1fa --- /dev/null +++ b/include/deal.II/base/memory_space_data.h @@ -0,0 +1,202 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2020 by the deal.II authors +// +// This file is part of the deal.II library. +// +// The deal.II library is free software; you can use it, redistribute +// it, and/or modify it under the terms of the GNU Lesser General +// Public License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// The full text of the license can be found in the file LICENSE.md at +// the top level directory of deal.II. +// +// --------------------------------------------------------------------- + + +#ifndef dealii_memory_space_data_h +#define dealii_memory_space_data_h + +#include + +#include +#include + +#include +#include + +DEAL_II_NAMESPACE_OPEN + +/** + */ +namespace MemorySpace +{ + /** + * Data structure + */ + template + struct MemorySpaceData + { + MemorySpaceData() + { + static_assert(std::is_same::value || + std::is_same::value, + "MemorySpace should be Host or CUDA"); + } + + /** + * Copy the active data (values for Host and values_dev for CUDA) to @p begin. + * If the data is on the device it is moved to the host. + */ + virtual void + copy_to(Number *begin, std::size_t n_elements) + { + (void)begin; + (void)n_elements; + } + + /** + * Copy the data in @p begin to the active data of the structure (values for + * Host and values_dev for CUDA). The pointer @p begin must be on the host. + */ + virtual void + copy_from(Number *begin, std::size_t n_elements) + { + (void)begin; + (void)n_elements; + } + + /** + * Pointer to data on the host. + */ + std::unique_ptr> values; + + /** + * Pointer to data on the device. + */ + std::unique_ptr values_dev; + }; + + + + /** + * Swap function similar to std::swap. + */ + template + inline void + swap(MemorySpaceData &, + MemorySpaceData &) + { + static_assert(std::is_same::value || + std::is_same::value, + "MemorySpace should be Host or CUDA"); + } + +#ifndef DOXYGEN + + template + struct MemorySpaceData + { + MemorySpaceData() + : values(nullptr, &std::free) + {} + + virtual ~MemorySpaceData() = default; + + MemorySpaceData(MemorySpaceData &&) noexcept = default; + + MemorySpaceData & + operator=(MemorySpaceData &&) noexcept = default; + + virtual void + copy_to(Number *begin, std::size_t n_elements) + { + std::copy(values.get(), values.get() + n_elements, begin); + } + + virtual void + copy_from(Number *begin, std::size_t n_elements) + { + std::copy(begin, begin + n_elements, values.get()); + } + + std::unique_ptr> values; + + // This is not used but it allows to simplify the code until we start using + // CUDA-aware MPI. + std::unique_ptr values_dev; + }; + + + + template + inline void + swap(MemorySpaceData &u, MemorySpaceData &v) + { + std::swap(u.values, v.values); + } + + + +# ifdef DEAL_II_COMPILER_CUDA_AWARE + + template + struct MemorySpaceData + { + MemorySpaceData() + : values(nullptr, &std::free) + , values_dev(nullptr, Utilities::CUDA::delete_device_data) + {} + + virtual ~MemorySpaceData() = default; + + MemorySpaceData(MemorySpaceData &&) noexcept = default; + + MemorySpaceData & + operator=(MemorySpaceData &&) noexcept = default; + + virtual void + copy_to(Number *begin, std::size_t n_elements) + { + const cudaError_t cuda_error_code = + cudaMemcpy(begin, + values_dev.get(), + n_elements * sizeof(Number), + cudaMemcpyDeviceToHost); + AssertCuda(cuda_error_code); + } + + virtual void + copy_from(Number *begin, std::size_t n_elements) + { + const cudaError_t cuda_error_code = + cudaMemcpy(values_dev.get(), + begin, + n_elements * sizeof(Number), + cudaMemcpyHostToDevice); + AssertCuda(cuda_error_code); + } + + std::unique_ptr> values; + std::unique_ptr values_dev; + }; + + + + template + inline void + swap(MemorySpaceData &u, MemorySpaceData &v) + { + std::swap(u.values, v.values); + std::swap(u.values_dev, v.values_dev); + } + +# endif + +#endif + +} // namespace MemorySpace + +DEAL_II_NAMESPACE_CLOSE + +#endif diff --git a/include/deal.II/lac/la_parallel_vector.h b/include/deal.II/lac/la_parallel_vector.h index 1186423416..16e1251a2d 100644 --- a/include/deal.II/lac/la_parallel_vector.h +++ b/include/deal.II/lac/la_parallel_vector.h @@ -19,6 +19,7 @@ #include #include +#include #include #include #include diff --git a/include/deal.II/lac/vector_operations_internal.h b/include/deal.II/lac/vector_operations_internal.h index 7f6850212d..9f1d50fdd0 100644 --- a/include/deal.II/lac/vector_operations_internal.h +++ b/include/deal.II/lac/vector_operations_internal.h @@ -20,6 +20,7 @@ #include #include +#include #include #include #include -- 2.39.5