Optimized Belief Propagation (CPU and GPU)
MemoryManagement.h
Go to the documentation of this file.
1 /*
2 Copyright (C) 2024 Scott Grauer-Gray
3 
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 
29 #ifndef MEMORY_MANAGEMENT_H_
30 #define MEMORY_MANAGEMENT_H_
31 
32 #include <new>
33 #include <ranges>
36 
45 template <RunData_t T>
47 {
48 public:
49  virtual T* AllocateMemoryOnDevice(std::size_t numData) const {
50  return (new T[numData]);
51  }
52 
53  virtual void FreeMemoryOnDevice(T* array_to_free) const {
54  delete [] array_to_free;
55  }
56 
58  std::size_t numData,
59  run_environment::AccSetting acc_setting) const
60  {
61 #ifdef _WIN32
62  T* memoryData = static_cast<T*>(_aligned_malloc(
63  numData * sizeof(T), run_environment::GetBytesAlignMemory(acc_setting)));
64  return memoryData;
65 #else
66  T* memoryData = static_cast<T*>(std::aligned_alloc(
67  run_environment::GetBytesAlignMemory(acc_setting), numData * sizeof(T)));
68  return memoryData;
69 #endif
70  }
71 
72  virtual void FreeAlignedMemoryOnDevice(T* memory_to_free) const
73  {
74 #ifdef _WIN32
75  _aligned_free(memory_to_free);
76 #else
77  free(memory_to_free);
78 #endif
79  }
80 
82  T* dest_array,
83  const T* in_array,
84  std::size_t num_data_transfer) const
85  {
86  std::ranges::copy(in_array, in_array + num_data_transfer, dest_array);
87  }
88 
90  T* dest_array,
91  const T* in_array,
92  std::size_t num_data_transfer) const
93  {
94  std::ranges::copy(in_array, in_array + num_data_transfer, dest_array);
95  }
96 };
97 
98 #endif //MEMORY_MANAGEMENT_H_
Declares and defines structure that stores settings for current implementation run as well as functio...
Define constraints for data type in processing.
Class for memory management with functions defined for standard memory allocation using CPU....
virtual T * AllocateAlignedMemoryOnDevice(std::size_t numData, run_environment::AccSetting acc_setting) const
virtual T * AllocateMemoryOnDevice(std::size_t numData) const
virtual void TransferDataFromHostToDevice(T *dest_array, const T *in_array, std::size_t num_data_transfer) const
virtual void TransferDataFromDeviceToHost(T *dest_array, const T *in_array, std::size_t num_data_transfer) const
virtual void FreeMemoryOnDevice(T *array_to_free) const
virtual void FreeAlignedMemoryOnDevice(T *memory_to_free) const
AccSetting
Enum for acceleration setting.
unsigned int GetBytesAlignMemory(AccSetting accel_setting)
Definition: RunSettings.h:43