GPUMLib  0.2.2
GPU Machine Learning Library
DeviceArray.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_DeviceArray_h
22 #define GPUMLib_DeviceArray_h
23 
24 #include <string.h>
25 #include "HostArray.h"
26 
27 namespace GPUMLib {
28 
31 
33 template <class Type> class DeviceArray : public BaseArray<Type> {
34  private:
35  void Alloc(int size) {
36  assert(size >= 0);
37 
38  if (size > 0 && cudaMalloc((void **) &(this->arrayData), size * sizeof(Type)) == cudaSuccess) {
39  this->size = size;
40  } else {
41  this->Init();
42  }
43  }
44 
45  public:
46  void Dispose() {
47  if (this->size > 0) cudaFree(this->arrayData);
48  this->Init();
49  }
50 
53 
56  explicit DeviceArray(int size) {
57  Alloc(size);
58  }
59 
62  DeviceArray(const HostArray<Type> & originalArray) {
63  Alloc(originalArray.Length());
64  if (this->size > 0) cudaMemcpy(this->arrayData, originalArray.Pointer(), this->size * sizeof(Type), cudaMemcpyHostToDevice);
65  }
66 
69  DeviceArray(const DeviceArray<Type> & originalArray) {
70  Alloc(originalArray.Length());
71  if (this->size > 0) cudaMemcpy(this->arrayData, originalArray.arrayData, this->size * sizeof(Type), cudaMemcpyDeviceToDevice);
72  }
73 
77  DeviceArray(const Type * originalArray, int size) {
78  Alloc(size);
79  if (this->size > 0) cudaMemcpy(this->arrayData, originalArray, size * sizeof(Type), cudaMemcpyHostToDevice);
80  }
81 
84  DeviceArray(DeviceArray<Type> && temporaryArray) {
85  MoveFrom(temporaryArray);
86  }
87 
91  DeviceArray<Type> & operator = (const HostArray<Type> & originalArray) {
92  int newSize = originalArray.Length();
93 
94  this->ResizeWithoutPreservingData(newSize);
95  if (this->size > 0) cudaMemcpy(this->arrayData, originalArray.Pointer(), this->size * sizeof(Type), cudaMemcpyHostToDevice);
96 
97  return *this;
98  }
99 
104  int newSize = originalArray.Length();
105 
106  this->ResizeWithoutPreservingData(newSize);
107  if (this->size > 0) cudaMemcpy(this->arrayData, originalArray.arrayData, this->size * sizeof(Type), cudaMemcpyDeviceToDevice);
108 
109  return *this;
110  }
111 
116  if (this != &temporaryArray) {
117  Dispose();
118  this->MoveFrom(temporaryArray);
119  }
120 
121  return *this;
122  }
123 
129  if (this != &other) {
130  Dispose();
131  this->size = other.size;
132  this->arrayData = other.arrayData;
133  other.Init();
134  }
135  }
136 
139  Dispose();
140  }
141 };
142 
144 
145 }
146 
147 #endif
DeviceArray(const DeviceArray< Type > &originalArray)
Definition: DeviceArray.h:69
DeviceArray(const Type *originalArray, int size)
Definition: DeviceArray.h:77
DeviceArray(int size)
Definition: DeviceArray.h:56
DeviceArray()
Constructs an array with no elements.
Definition: DeviceArray.h:52
DeviceArray< Type > & operator=(const HostArray< Type > &originalArray)
Definition: DeviceArray.h:91
void Dispose()
Disposes the array.
Definition: DeviceArray.h:46
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
int ResizeWithoutPreservingData(int size)
Definition: BaseArray.h:77
~DeviceArray()
Destructor.
Definition: DeviceArray.h:138
Create an array of any type, on the device, that automatically manages the memory used to hold its el...
Definition: DeviceArray.h:33
DeviceArray(const HostArray< Type > &originalArray)
Definition: DeviceArray.h:62
DeviceArray(DeviceArray< Type > &&temporaryArray)
Definition: DeviceArray.h:84
Base class for HostArray and DeviceArray classes (Array base class)
Definition: BaseArray.h:32
int Length() const
Definition: BaseArray.h:63
void TransferOwnerShipFrom(DeviceArray< Type > &other)
Definition: DeviceArray.h:128