diff --git a/src/gpu/unified_allocator.hh b/src/core/fftw_allocator.hh similarity index 78% copy from src/gpu/unified_allocator.hh copy to src/core/fftw_allocator.hh index 8ccac47..3f9dc3f 100644 --- a/src/gpu/unified_allocator.hh +++ b/src/core/fftw_allocator.hh @@ -1,51 +1,54 @@ /** * @file * * @author Lucas Frérot * * @section LICENSE * * Copyright (©) 2017 EPFL (Ecole Polytechnique Fédérale de * Lausanne) Laboratory (LSMS - Laboratoire de Simulation en Mécanique des * Solides) * * Tamaas is free software: you can 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 3 of the License, or (at your option) any * later version. * * Tamaas is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more * details. * * You should have received a copy of the GNU Lesser General Public License * along with Tamaas. If not, see . * */ /* -------------------------------------------------------------------------- */ -#ifndef __UNIFIED_ALLOCATOR_HH__ -#define __UNIFIED_ALLOCATOR_HH__ +#ifndef __FFTW_ALLOCATOR_HH__ +#define __FFTW_ALLOCATOR_HH__ /* -------------------------------------------------------------------------- */ #include -#include +#include /* -------------------------------------------------------------------------- */ namespace tamaas { +/// Class allocating [SIMD](http://www.fftw.org/fftw3_doc/SIMD-alignment-and-fftw_005fmalloc.html#SIMD-alignment-and-fftw_005fmalloc) aligned memory template -struct UnifiedAllocator { +struct FFTWAllocator { + /// Allocate memory T * allocate(std::size_t n) { T * p = nullptr; - cudaMallocManaged(&p, n * sizeof(T)); + p = (T*) fftw_malloc(sizeof(T) * n); return p; } + /// Free memory void deallocate(T * p, __attribute__((unused)) std::size_t n) { - cudaFree(p); + fftw_free(p); } }; } -#endif // __UNIFIED_ALLOCATOR_HH__ +#endif // __FFTW_ALLOCATOR_HH__ diff --git a/src/core/tamaas.hh b/src/core/tamaas.hh index 7599b31..24b9144 100644 --- a/src/core/tamaas.hh +++ b/src/core/tamaas.hh @@ -1,155 +1,157 @@ /** * @mainpage Welcome to Tamaas ! * * @section Introduction * Tamaas is a spectral-boundary-element based contact library. It is made with * love to be fast and friendly! * * @author Guillaume Anciaux * @author Lucas Frérot * @author Valentine Rey * * @section LICENSE * * Copyright (©) 2016-2017 EPFL (Ecole Polytechnique Fédérale de * Lausanne) Laboratory (LSMS - Laboratoire de Simulation en Mécanique des * Solides) * * Tamaas is free software: you can 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 3 of the License, or (at your option) any * later version. * * Tamaas is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more * details. * * You should have received a copy of the GNU Lesser General Public License * along with Tamaas. If not, see . * */ /* -------------------------------------------------------------------------- */ #ifndef __TAMAAS_HH__ #define __TAMAAS_HH__ /* -------------------------------------------------------------------------- */ // Standard includes #include #include #include #include /* -------------------------------------------------------------------------- */ // Special thrust includes #include "unified_allocator.hh" #include #include +#include "fftw_allocator.hh" + /// @section Namespace macros #define __BEGIN_TAMAAS__ namespace tamaas { #define __END_TAMAAS__ } /// @section Cuda specific definitions #define CUDA_LAMBDA __device__ __host__ #ifdef USE_CUDA #define DEFAULT_ALLOCATOR UnifiedAllocator #else -#define DEFAULT_ALLOCATOR std::allocator +#define DEFAULT_ALLOCATOR FFTWAllocator #endif /* -------------------------------------------------------------------------- */ /// @section Standard definitions pulled from C++14 namespace std { #if __cplusplus < 201402L /// exchange template T exchange(T& obj, U&& new_value) { T old_value = move(obj); obj = forward(new_value); return old_value; } /// make_unique template unique_ptr make_unique(Args&&... args) { return unique_ptr(new T(forward(args)...)); } #endif } /* -------------------------------------------------------------------------- */ __BEGIN_TAMAAS__ /* -------------------------------------------------------------------------- */ /// @section Common types definitions using Real = double; ///< default floating point type using UInt = unsigned int; ///< default unsigned integer type using Int = int; ///< default signed integer type template using complex = thrust::complex; ///< template complex wrapper using Complex = complex; ///< default floating point complex type static constexpr Real zero_threshold = 1e-14; /// @section Defining random toolbox using ::thrust::random::normal_distribution; using random_engine = ::thrust::random::default_random_engine; /* -------------------------------------------------------------------------- */ /// initialize tamaas (0 threads => let OMP_NUM_THREADS decide) void initialize(UInt num_threads = 0); /// cleanup tamaas void finalize(); /* -------------------------------------------------------------------------- */ /// Generic exception class class Exception : public std::exception { public: /// Constructor Exception(const std::string & mesg):msg(mesg) {} virtual const char* what() const noexcept { return msg.c_str(); } virtual ~Exception() = default; private: std::string msg; ///< message of exception }; /* -------------------------------------------------------------------------- */ /// Enumeration of reduction operations enum class operation { plus, times, min, max }; /* -------------------------------------------------------------------------- */ __END_TAMAAS__ /* -------------------------------------------------------------------------- */ /// @section Convenience macros #define TAMAAS_EXCEPTION(mesg) { \ std::stringstream sstr; \ sstr \ << __FILE__ \ << ":" << __LINE__ << ":FATAL: " \ << mesg << std::endl; \ std::cerr.flush(); \ throw ::tamaas::Exception(sstr.str()); \ } #define SURFACE_FATAL(mesg) TAMAAS_EXCEPTION(mesg) #if defined(TAMAAS_DEBUG) #define TAMAAS_ASSERT(cond, reason) do { \ if (!(cond)) { \ TAMAAS_EXCEPTION(#cond " assert failed: " << reason); \ } } while(0) #define TAMAAS_DEBUG_EXCEPTION(reason) TAMAAS_EXCEPTION(reason) #else #define TAMAAS_ASSERT(cond, reason) #define TAMAAS_DEBUG_EXCEPTION(reason) #endif #define TAMAAS_ACCESSOR(var, type, name) type & get##name() { return var; } \ void set##name(const type & new_var) { var = new_var; } /* -------------------------------------------------------------------------- */ #endif // TAMAAS_HH diff --git a/src/gpu/unified_allocator.hh b/src/gpu/unified_allocator.hh index 8ccac47..7c19d40 100644 --- a/src/gpu/unified_allocator.hh +++ b/src/gpu/unified_allocator.hh @@ -1,51 +1,54 @@ /** * @file * * @author Lucas Frérot * * @section LICENSE * * Copyright (©) 2017 EPFL (Ecole Polytechnique Fédérale de * Lausanne) Laboratory (LSMS - Laboratoire de Simulation en Mécanique des * Solides) * * Tamaas is free software: you can 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 3 of the License, or (at your option) any * later version. * * Tamaas is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more * details. * * You should have received a copy of the GNU Lesser General Public License * along with Tamaas. If not, see . * */ /* -------------------------------------------------------------------------- */ #ifndef __UNIFIED_ALLOCATOR_HH__ #define __UNIFIED_ALLOCATOR_HH__ /* -------------------------------------------------------------------------- */ #include #include /* -------------------------------------------------------------------------- */ namespace tamaas { +/// Class allocating [unified memory](http://docs.nvidia.com/cuda/cuda-runtime-api/group__CUDART__MEMORY.html#group__CUDART__MEMORY_1gd228014f19cc0975ebe3e0dd2af6dd1b) template struct UnifiedAllocator { + /// Allocate memory T * allocate(std::size_t n) { T * p = nullptr; cudaMallocManaged(&p, n * sizeof(T)); return p; } + /// Free memory void deallocate(T * p, __attribute__((unused)) std::size_t n) { cudaFree(p); } }; } #endif // __UNIFIED_ALLOCATOR_HH__ diff --git a/tests/SConscript b/tests/SConscript index 8844b9d..e0e8d1c 100644 --- a/tests/SConscript +++ b/tests/SConscript @@ -1,53 +1,52 @@ from __future__ import print_function from os.path import join, abspath Import('main_env') -print("Environment for tests") test_env = main_env.Clone( PRINT_CMD_LINE_FUNC=main_env['gen_print']("Copying", "red", main_env)) test_files = Split(""" run_tests.sh test_hertz_pressure.py test_westergaard.py test_patch_westergaard.py test_surface.py test_autocorrelation.py test_hertz_disp.py test_hertz_kato.py test_hertz_adhesion.py test_saturated_pressure.py test_fftransform.py test_bem_grid.py test_flood_fill.py """) src_dir = "#/tests" build_dir = 'build-' + main_env['build_type'] + '/tests' for file in test_files: source = join(src_dir, file) test_env.Command(file, source, Copy("$TARGET", "$SOURCE")) crit_env = main_env.Clone(tools=[criterion]) if 'SHCXXCOMSTR' in main_env: crit_env['CXXCOMSTR'] = main_env['SHCXXCOMSTR'] if 'SHLINKCOMSTR' in main_env: crit_env['LINKCOMSTR'] = main_env['SHLINKCOMSTR'] crit_env.AppendUnique(LIBS=['Tamaas'], LIBPATH=[abspath('build-' + main_env['build_type'] + '/src')]) cpp_test_files = Split(""" test_grid.cpp test_loop.cpp test_cuda.cpp test_fft.cpp test_model.cpp """) for file in cpp_test_files: crit_env.Program(file)