tiny_dnn  1.0.0
A header only, dependency-free deep learning framework in C++11
device.h
1 /*
2  COPYRIGHT
3 
4  All contributions by Taiga Nomi
5  Copyright (c) 2013, Taiga Nomi
6  All rights reserved.
7 
8  All other contributions:
9  Copyright (c) 2013-2016, the respective contributors.
10  All rights reserved.
11 
12  Each contributor holds copyright over their respective contributions.
13  The project versioning (Git) records all such contribution source information.
14 
15  LICENSE
16 
17  The BSD 3-Clause License
18 
19 
20  Redistribution and use in source and binary forms, with or without
21  modification, are permitted provided that the following conditions are met:
22 
23  * Redistributions of source code must retain the above copyright notice, this
24  list of conditions and the following disclaimer.
25 
26  * Redistributions in binary form must reproduce the above copyright notice,
27  this list of conditions and the following disclaimer in the documentation
28  and/or other materials provided with the distribution.
29 
30  * Neither the name of tiny-dnn nor the names of its
31  contributors may be used to endorse or promote products derived from
32  this software without specific prior written permission.
33 
34  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
35  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
37  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
38  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
40  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
41  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
42  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
43  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 */
45 #pragma once
46 
47 #include "tiny_dnn/core/framework/device.fwd.h"
48 #include "tiny_dnn/core/framework/program_manager.h"
49 
50 namespace tiny_dnn {
51 
52 inline Device::Device(device_t type)
53  : type_(type), has_clcuda_api_(false) {
54  nn_info("Initializing Non-OpenCL device ...");
55  if (type == device_t::GPU) {
56  throw nn_error("Bad GPU device initialization. "
57  "Please provide platform_id and device_id");
58  }
59  nn_info("Initializing Non-OpenCL device ... OK");
60 }
61 
62 inline Device::Device(device_t type,
63  const int platform_id,
64  const int device_id)
65  : type_(type)
66  , has_clcuda_api_(true)
67  , platform_id_(platform_id)
68  , device_id_(device_id) {
69 #if defined(USE_OPENCL) || defined(USE_CUDA)
70  // Instantiate Platform and Device
71  nn_info("Initializing OpenCL platform ...");
72  auto platform = CLCudaAPI::Platform(platform_id);
73 
74  // Print short pltform info
75  nn_info("Initializing OpenCL platform ... OK");
76  nn_info("-- Running on platform "
77  + to_string(platform_id) +
78  ". Found " + to_string(platform.NumDevices()) + " devices.");
79 
80  // Create and retain device object
81  nn_info("Initializing OpenCL device ...");
82  device_.reset(new CLCudaAPI::Device(platform, device_id));
83 
84  // Print short device info
85  nn_info("Initializing OpenCL device ... OK");
86  nn_info("-- Running on device " + to_string(device_->Name()) +
87  " of " + to_string(device_->Vendor()));
88  nn_info("-- Device type: " + to_string(device_->Type()));
89  nn_info("-- Capabilities: " + to_string(device_->Capabilities()));
90 
91  // check device type
92  if (type == device_t::CPU && !device_->IsCPU()) {
93  throw nn_error("Not found a CPU device. You are on: " +
94  to_string(device_->Type()));
95  }
96  else if (type == device_t::GPU && !device_->IsGPU()) {
97  throw nn_error("Not found a GPU device. You are on: " +
98  to_string(device_->Type()));
99  }
100 
101  // Create and retain device context
102  nn_info("Initializing OpenCL device context ...");
103 
104  context_.reset(new CLCudaAPI::Context(*device_));
105  queue_.reset(new CLCudaAPI::Queue(*context_, *device_));
106 
107  nn_info("Initializing OpenCL device context ... OK");
108 #else
109  nn_error("TinyDNN has not been compiled with OpenCL or CUDA support.");
110 #endif
111 }
112 
113 inline void Device::registerOp(layer &l) {
114  // TODO(egdar/nyanp): Should we raise an error here?
115  if (!hasCLCudaAPI()) {
116  throw nn_error("Cannot register layer: " + l.layer_type()
117  + ". Device has disabled OpenCL support. Please "
118  "specify platform and device in "
119  "Device constructor");
120  }
121 
122  if (l.engine() != core::backend_t::opencl &&
123  l.engine() != core::backend_t::libdnn) {
124  throw nn_error("Cannot register layer: " + l.layer_type() +
125  ". Enabled engine: " + to_string(l.engine()) + ". OpenCL engine "
126  "(backend_t::opencl) should be used.");
127  }
128 
129  // Register the op to this device
130  ProgramManager::getInstance().registerOp(*this, l);
131 }
132 
133 } // namespace tiny_dnn