GPUMLib  0.2.2
GPU Machine Learning Library
HostArray.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_HostArray_h
22 #define GPUMLib_HostArray_h
23 
24 #include <new>
25 #include <assert.h>
26 #include <cuda_runtime.h>
27 
28 #include "BaseArray.h"
29 
30 using namespace std;
31 
32 namespace GPUMLib {
33 
34 template <class Type> class DeviceArray;
35 
38 
40 template <class Type> class HostArray : public BaseArray<Type> {
41  private:
42  void Alloc(int size) {
43  assert(size >= 0);
44 
45  if (size > 0) {
46  this->arrayData = new (nothrow) Type[size];
47  this->size = (this->arrayData != nullptr) ? size : 0;
48  } else {
49  this->Init();
50  }
51  }
52 
53  public:
54  void Dispose() {
55  if (this->size > 0) delete [] this->arrayData;
56  this->Init();
57  }
58 
60  HostArray() {}
61 
64  explicit HostArray(int size) {
65  Alloc(size);
66  }
67 
70  HostArray(const DeviceArray<Type> & originalArray) {
71  Alloc(originalArray.Length());
72  if (this->size > 0) cudaMemcpy(this->arrayData, originalArray.Pointer(), this->size * sizeof(Type), cudaMemcpyDeviceToHost);
73  }
74 
77  HostArray(const HostArray<Type> & originalArray) {
78  Alloc(originalArray.Length());
79  if (this->size > 0) memcpy(this->arrayData, originalArray.Pointer(), this->size * sizeof(Type));
80  }
81 
84  HostArray(HostArray<Type> && temporaryArray) {
85  this->MoveFrom(temporaryArray);
86  }
87 
90  Dispose();
91  }
92 
96  HostArray<Type> & operator = (const DeviceArray<Type> & originalArray) {
97  int newSize = originalArray.Length();
98 
99  this->ResizeWithoutPreservingData(newSize);
100  if (this->size > 0) cudaMemcpy(this->arrayData, originalArray.Pointer(), this->size * sizeof(Type), cudaMemcpyDeviceToHost);
101 
102  return *this;
103  }
104 
108  HostArray<Type> & operator = (const HostArray<Type> & originalArray) {
109  int newSize = originalArray.Length();
110 
111  this->ResizeWithoutPreservingData(newSize);
112  if (this->size > 0) memcpy(this->arrayData, originalArray.Pointer(), this->size * sizeof(Type));
113 
114  return *this;
115  }
116 
120  HostArray<Type> & operator = (const HostArray<Type> && temporaryArray) {
121  if (this != &temporaryArray) {
122  Dispose();
123  this->MoveFrom(temporaryArray);
124  }
125 
126  return *this;
127  }
128 
134  if (this != &other) {
135  Dispose();
136  this->size = other.size;
137  this->arrayData = other.arrayData;
138  other.Init();
139  }
140  }
141 
145  Type & operator [] (int element) {
146  assert(element < this->size);
147  return this->arrayData[element];
148  }
149 
153  Type operator [] (int element) const {
154  assert(element < this->size);
155  return this->arrayData[element];
156  }
157 };
158 
160 
161 }
162 
163 #endif
void Dispose()
Disposes the array.
Definition: HostArray.h:54
HostArray(const DeviceArray< Type > &originalArray)
Definition: HostArray.h:70
Create an array of any type, on the host, that automatically manages the memory used to hold its elem...
Definition: HostArray.h:40
Type * Pointer() const
Definition: BaseArray.h:70
~HostArray()
Destructor.
Definition: HostArray.h:89
HostArray(const HostArray< Type > &originalArray)
Definition: HostArray.h:77
Create an array of any type, on the device, that automatically manages the memory used to hold its el...
Definition: DeviceArray.h:33
HostArray(int size)
Definition: HostArray.h:64
Base class for HostArray and DeviceArray classes (Array base class)
Definition: BaseArray.h:32
void TransferOwnerShipFrom(HostArray< Type > &other)
Definition: HostArray.h:133
int Length() const
Definition: BaseArray.h:63
HostArray(HostArray< Type > &&temporaryArray)
Definition: HostArray.h:84
HostArray()
Constructs an array with no elements.
Definition: HostArray.h:60