GPUMLib  0.2.2
GPU Machine Learning Library
CudaArray.h
1 /*
2  Noel Lopes is a Professor at the Polytechnic of Guarda, Portugal
3  Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 Noel de Jesus Mendonça Lopes
4 
5  This file is part of GPUMLib.
6 
7  GPUMLib is free software: you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation, either version 3 of the License, or
10  (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #ifndef GPUMLib_CudaArray_h
22 #define GPUMLib_CudaArray_h
23 
24 #include "DeviceArray.h"
25 
26 namespace GPUMLib {
27 
30 
32 template <class Type> class CudaArray {
33  private:
34  HostArray<Type> h;
35  DeviceArray<Type> d;
36 
37  void MoveFrom(BaseArray<Type> & other) {
38  h.MoveFrom(other.h);
39  d.MoveFrom(other.d);
40  }
41 
42  public:
44  CudaArray() { }
45 
48  explicit CudaArray(int size) : h(size), d(size) { }
49 
52  CudaArray(const DeviceArray<Type> & originalArray) : h(originalArray), d(originalArray) { }
53 
56  CudaArray(const HostArray<Type> & originalArray) : h(originalArray), d(originalArray) { }
57 
60  CudaArray(CudaArray<Type> && temporaryArray) {
61  MoveFrom(temporaryArray);
62  }
63 
67  CudaArray<Type> & operator = (const CudaArray<Type> & originalArray) {
68  h = originalArray.h;
69  d = originalArray.d;
70 
71  return *this;
72  }
73 
77  CudaArray<Type> & operator = (const DeviceArray<Type> & originalArray) {
78  d = originalArray;
79  h = d;
80 
81  return *this;
82  }
83 
87  CudaArray<Type> & operator = (const HostArray<Type> & originalArray) {
88  h = originalArray;
89  d = h;
90 
91  return *this;
92  }
93 
97  CudaArray<Type> & operator = (const CudaArray<Type> && temporaryArray) {
98  if (this != &temporaryArray) {
99  Dispose();
100  MoveFrom(temporaryArray);
101  }
102 
103  return *this;
104  }
105 
109  Type & operator [] (int element) {
110  return h[element];
111  }
112 
116  Type operator [] (int element) const {
117  return h[element];
118  }
119 
122  int Length() const {
123  return d.Length();
124  }
125 
129  Type * HostPointer() const {
130  return h.Pointer();
131  }
132 
136  Type * DevicePointer() const {
137  return d.Pointer();
138  }
139 
144  int he = h.ResizeWithoutPreservingData(size);
145  int de = d.ResizeWithoutPreservingData(size);
146 
147  return (he > de) ? de : he;
148  }
149 
151  void UpdateDevice() {
152  d = h;
153  }
154 
156  void UpdateHost() {
157  h = d;
158  }
159 
164  return d;
165  }
166 
171  return h;
172  }
173 
175  void Dispose() {
176  d.Dispose();
177  h.Dispose();
178  }
179 
181  void DisposeDevice() {
182  UpdateHost();
183  d.Dispose();
184  }
185 
187  void DisposeHost() {
188  UpdateDevice();
189  h.Dispose();
190  }
191 };
192 
194 
195 }
196 
197 #endif
CudaArray(CudaArray< Type > &&temporaryArray)
Definition: CudaArray.h:60
CudaArray< Type > & operator=(const CudaArray< Type > &originalArray)
Definition: CudaArray.h:67
CudaArray(const DeviceArray< Type > &originalArray)
Definition: CudaArray.h:52
Type * HostPointer() const
Definition: CudaArray.h:129
HostArray< Type > & GetHostArray()
Definition: CudaArray.h:170
Create an array of any type, on the host, that automatically manages the memory used to hold its elem...
Definition: HostArray.h:40
Type & operator[](int element)
Definition: CudaArray.h:109
Create an array of any type, that automatically manages the memory used to hold its elements (data wi...
Definition: BaseArray.h:29
void DisposeDevice()
Updates the host information and disposes the device array.
Definition: CudaArray.h:181
Create an array of any type, on the device, that automatically manages the memory used to hold its el...
Definition: DeviceArray.h:33
CudaArray()
Constructs an array with no elements.
Definition: CudaArray.h:44
void UpdateHost()
Updates the host array data with the device array data.
Definition: CudaArray.h:156
CudaArray(const HostArray< Type > &originalArray)
Definition: CudaArray.h:56
int Length() const
Definition: CudaArray.h:122
void DisposeHost()
Updates the device information and disposes the Host array.
Definition: CudaArray.h:187
int ResizeWithoutPreservingData(int size)
Definition: CudaArray.h:143
CudaArray(int size)
Definition: CudaArray.h:48
Type * DevicePointer() const
Definition: CudaArray.h:136
void Dispose()
Disposes the array.
Definition: CudaArray.h:175
DeviceArray< Type > & GetDeviceArray()
Definition: CudaArray.h:163
void UpdateDevice()
Updates the device array data with the host array data.
Definition: CudaArray.h:151