RAJA
RAJA provides a collection of platform portability abstractions for C++ HPC applications.
MemUtils_CPU.hpp
Go to the documentation of this file.
1 
12 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
13 // Copyright (c) Lawrence Livermore National Security, LLC and other
14 // RAJA Project Developers. See top-level LICENSE and COPYRIGHT
15 // files for dates and other details. No copyright assignment is required
16 // to contribute to RAJA.
17 //
18 // SPDX-License-Identifier: (BSD-3-Clause)
19 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
20 
21 #ifndef RAJA_MemUtils_CPU_HPP
22 #define RAJA_MemUtils_CPU_HPP
23 
24 #include "RAJA/config.hpp"
25 
26 #include <cstddef>
27 #include <cstdlib>
28 #include <memory>
29 
30 #include "RAJA/util/types.hpp"
31 
32 #if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || \
33  defined(__MINGW32__) || defined(__BORLANDC__)
34 #define RAJA_PLATFORM_WINDOWS
35 #include <malloc.h>
36 #endif
37 
38 namespace RAJA
39 {
40 
44 inline void* allocate_aligned(size_t alignment, size_t size)
45 {
46 #if defined(RAJA_HAVE_POSIX_MEMALIGN)
47  // posix_memalign available
48  void* ret = nullptr;
49  int err = posix_memalign(&ret, alignment, size);
50  return err ? nullptr : ret;
51 #elif defined(RAJA_HAVE_ALIGNED_ALLOC)
52  return std::aligned_alloc(alignment, size);
53 #elif defined(RAJA_HAVE_MM_MALLOC)
54  return _mm_malloc(size, alignment);
55 #elif defined(RAJA_PLATFORM_WINDOWS)
56  return _aligned_malloc(size, alignment);
57 #else
58  char* mem = (char*)malloc(size + alignment + sizeof(void*));
59  if (nullptr == mem) return nullptr;
60  void** ptr = (void**)((std::uintptr_t)(mem + alignment + sizeof(void*)) &
61  ~(alignment - 1));
62  // Store the original address one position behind what we give the user.
63  ptr[-1] = mem;
64  return ptr;
65 #endif
66 }
67 
71 template<typename T>
72 inline T* allocate_aligned_type(size_t alignment, size_t size)
73 {
74  return reinterpret_cast<T*>(allocate_aligned(alignment, size));
75 }
76 
80 inline void free_aligned(void* ptr)
81 {
82 #if defined(RAJA_HAVE_POSIX_MEMALIGN) || defined(RAJA_HAVE_ALIGNED_ALLOC)
83  free(ptr);
84 #elif defined(RAJA_HAVE_MM_MALLOC)
85  _mm_free(ptr);
86 #elif defined(RAJA_PLATFORM_WINDOWS)
87  _aligned_free(ptr);
88 #else
89  // Free the address stored one position behind the user data in ptr.
90  // This is valid for pointers allocated with allocate_aligned
91  free(((void**)ptr)[-1]);
92 #endif
93 }
94 
99 {
100  void operator()(void* ptr) { free_aligned(ptr); }
101 };
102 
107 template<typename T, typename index_type>
109 {
110  index_type size = 0;
111 
112  void operator()(T* ptr)
113  {
114  for (index_type i = size; i > 0; --i)
115  {
116  ptr[i - 1].~T();
117  }
119  }
120 };
121 
122 } // namespace RAJA
123 
124 #endif // closing endif for header file include guard
Definition: AlignedRangeIndexSetBuilders.cpp:35
void * allocate_aligned(size_t alignment, size_t size)
Definition: MemUtils_CPU.hpp:44
void free_aligned(void *ptr)
Definition: MemUtils_CPU.hpp:80
T * allocate_aligned_type(size_t alignment, size_t size)
Definition: MemUtils_CPU.hpp:72
Definition: MemUtils_CPU.hpp:109
index_type size
Definition: MemUtils_CPU.hpp:110
void operator()(T *ptr)
Definition: MemUtils_CPU.hpp:112
Definition: MemUtils_CPU.hpp:99
void operator()(void *ptr)
Definition: MemUtils_CPU.hpp:100
Header file for RAJA type definitions.