47 #include "tiny_dnn/core/framework/device.fwd.h"
48 #include "tiny_dnn/core/framework/program_manager.h"
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");
59 nn_info(
"Initializing Non-OpenCL device ... OK");
62 inline Device::Device(device_t type,
63 const int platform_id,
66 , has_clcuda_api_(true)
67 , platform_id_(platform_id)
68 , device_id_(device_id) {
69 #if defined(USE_OPENCL) || defined(USE_CUDA)
71 nn_info(
"Initializing OpenCL platform ...");
72 auto platform = CLCudaAPI::Platform(platform_id);
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.");
81 nn_info(
"Initializing OpenCL device ...");
82 device_.reset(
new CLCudaAPI::Device(platform, device_id));
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()));
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()));
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()));
102 nn_info(
"Initializing OpenCL device context ...");
104 context_.reset(
new CLCudaAPI::Context(*device_));
105 queue_.reset(
new CLCudaAPI::Queue(*context_, *device_));
107 nn_info(
"Initializing OpenCL device context ... OK");
109 nn_error(
"TinyDNN has not been compiled with OpenCL or CUDA support.");
113 inline void Device::registerOp(layer &l) {
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");
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.");
130 ProgramManager::getInstance().registerOp(*
this, l);