Optimized Belief Propagation (CPU and GPU)
MemoryManagementCUDA.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_CUDA_H_
30 #define MEMORY_MANAGEMENT_CUDA_H_
31 
32 #include <cuda_runtime.h>
35 
43 template <RunData_t T>
44 class MemoryManagementCUDA final : public MemoryManagement<T>
45 {
46 public:
53  T* AllocateMemoryOnDevice(std::size_t numData) const override
54  {
55  T* array_to_allocate;
56  cudaMalloc((void **) &array_to_allocate, numData * sizeof(T));
57  return array_to_allocate;
58  }
59 
65  void FreeMemoryOnDevice(T* array_to_free) const override
66  {
67  cudaFree(array_to_free);
68  }
69 
79  std::size_t numData,
80  run_environment::AccSetting acc_setting) const override
81  {
82  return AllocateMemoryOnDevice(numData);
83  }
84 
91  void FreeAlignedMemoryOnDevice(T* memory_to_free) const override
92  {
93  FreeMemoryOnDevice(memory_to_free);
94  }
95 
97  T* dest_array,
98  const T* in_array,
99  std::size_t num_data_transfer) const override
100  {
101  cudaMemcpy(
102  dest_array, in_array, num_data_transfer * sizeof(T), cudaMemcpyDeviceToHost);
103  }
104 
106  T* dest_array,
107  const T* in_array,
108  std::size_t num_data_transfer) const override
109  {
110  cudaMemcpy(
111  dest_array, in_array, num_data_transfer * sizeof(T), cudaMemcpyHostToDevice);
112  }
113 };
114 
115 #endif //MEMORY_MANAGEMENT_CUDA_H_
Declares class for memory management with functions defined for standard memory allocation using CPU ...
Define constraints for data type in processing.
Child class of MemoryManagement with overriden member functions to manage memory on CUDA device inclu...
void FreeMemoryOnDevice(T *array_to_free) const override
Free memory on CUDA device.
void TransferDataFromHostToDevice(T *dest_array, const T *in_array, std::size_t num_data_transfer) const override
void FreeAlignedMemoryOnDevice(T *memory_to_free) const override
Free aligned memory on CUDA device (same as default free memory for CUDA)
void TransferDataFromDeviceToHost(T *dest_array, const T *in_array, std::size_t num_data_transfer) const override
T * AllocateMemoryOnDevice(std::size_t numData) const override
Allocate specified amount of data of type T on CUDA device.
T * AllocateAlignedMemoryOnDevice(std::size_t numData, run_environment::AccSetting acc_setting) const override
Allocate aligned memory on CUDA device (same as default allocating of memory for CUDA)
Class for memory management with functions defined for standard memory allocation using CPU....
AccSetting
Enum for acceleration setting.