Shark machine learning library
Installation
Tutorials
Benchmarks
Documentation
Quick references
Class list
Global functions
include
shark
Data
DeviceSupport.h
Go to the documentation of this file.
1
//===========================================================================
2
/*!
3
*
4
*
5
* \brief Supporting routines for transfering dataset between devices, e.g. cpu to opencl
6
7
*
8
* \author O. Krause
9
* \date 2018
10
*
11
*
12
* \par Copyright 1995-2017 Shark Development Team
13
*
14
* <BR><HR>
15
* This file is part of Shark.
16
* <https://shark-ml.github.io/Shark/>
17
*
18
* Shark is free software: you can redistribute it and/or modify
19
* it under the terms of the GNU Lesser General Public License as published
20
* by the Free Software Foundation, either version 3 of the License, or
21
* (at your option) any later version.
22
*
23
* Shark is distributed in the hope that it will be useful,
24
* but WITHOUT ANY WARRANTY; without even the implied warranty of
25
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26
* GNU Lesser General Public License for more details.
27
*
28
* You should have received a copy of the GNU Lesser General Public License
29
* along with Shark. If not, see <http://www.gnu.org/licenses/>.
30
*
31
*/
32
//===========================================================================
33
34
#ifndef SHARK_DATA_DEVICE_SUPPORT_H
35
#define SHARK_DATA_DEVICE_SUPPORT_H
36
37
#include <
shark/Data/Dataset.h
>
38
#include <
shark/LinAlg/BLAS/device_copy.hpp
>
39
40
namespace
shark
{
41
42
/**
43
* \addtogroup shark_globals
44
* @{
45
*/
46
47
///\brief Transfers a dataset from CPU to the GPU/OpenCL device
48
template
<
class
Type,
class
T>
49
Data<blas::vector<Type, blas::gpu_tag>
>
toGPU
(
Data
<blas::vector<T, blas::cpu_tag> >
const
& data){
50
Data<blas::vector<Type, blas::gpu_tag>
> data_gpu(data.numberOfBatches());
51
for
(std::size_t i = 0; i != data.numberOfBatches(); ++i){
52
data_gpu.
batch
(i) = blas::copy_to_gpu(data.batch(i));
53
}
54
data_gpu.
shape
() = data.shape();
55
return
data_gpu;
56
}
57
58
///\brief Transfers a dataset from CPU to the GPU/OpenCL device
59
///
60
/// class labels are converted to one-hot encoding with a given Type
61
template
<
class
Type>
62
Data<blas::vector<Type, blas::gpu_tag>
>
toGPU
(
Data<unsigned int >
const
& data){
63
Data<blas::vector<Type, blas::gpu_tag>
> data_gpu(data.
numberOfBatches
());
64
std::size_t numClasses =
numberOfClasses
(data);
65
for
(std::size_t i = 0; i != data.
numberOfBatches
(); ++i){
66
auto
const
& labels = data.
batch
(i);
67
blas::matrix<Type> batch(labels.size(),numClasses, 0.0);
68
for
(std::size_t j = 0; j != labels.size(); ++j){
69
batch(j,labels(j)) = Type(1);
70
}
71
data_gpu.
batch
(i) = blas::copy_to_gpu(batch);
72
}
73
data_gpu.
shape
() = data.
shape
();
74
return
data_gpu;
75
}
76
77
///\brief Transfers a labeled dataset from CPU to the GPU/OpenCL device
78
template
<
class
Type,
class
I,
class
L>
79
LabeledData<blas::vector<Type, blas::gpu_tag>
, blas::vector<Type, blas::gpu_tag> >
toGPU
(
LabeledData<I,L>
const
& data){
80
typedef
LabeledData<blas::vector<Type, blas::gpu_tag>
, blas::vector<Type, blas::gpu_tag> > DatasetType;
81
return
DatasetType(toGPU<Type>(data.
inputs
()),toGPU<Type>(data.
labels
()));
82
}
83
84
85
/** @*/
86
}
87
88
#endif