GPUMLib  0.2.2
GPU Machine Learning Library
DeviceAccessibleVariable.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_DeviceAccessibleVariable_h
22 #define GPUMLib_DeviceAccessibleVariable_h
23 
24 namespace GPUMLib {
25 
28 
30 template <class Type> class DeviceAccessibleVariable {
31  private:
32  Type * value;
33 
34  public:
37  cudaMallocHost((void**) &value, sizeof(Type));
38  }
39 
42  DeviceAccessibleVariable(const Type initialValue) {
43  cudaMallocHost((void**) &value, sizeof(Type));
44  *value = initialValue;
45  }
46 
49  cudaFreeHost(value);
50  }
51 
54  Type & Value() {
55  return *value;
56  }
57 
60  Type * Pointer() {
61  return value;
62  }
63 
66  void UpdateValue(Type * deviceValue) {
67  cudaMemcpy(value, deviceValue, sizeof(Type), cudaMemcpyDeviceToHost);
68  }
69 
73  void UpdateValueAsync(Type * deviceValue, cudaStream_t stream) {
74  cudaMemcpyAsync(value, deviceValue, sizeof(Type), cudaMemcpyDeviceToHost, stream);
75  }
76 };
77 
79 
80 }
81 
82 #endif
void UpdateValueAsync(Type *deviceValue, cudaStream_t stream)
DeviceAccessibleVariable(const Type initialValue)
Represents a variable residing in memory that is page-locked and accessible to the device...