Deep Neural Network Library (DNNL)  1.1.0
Performance library for Deep Learning
example_utils.hpp
1 /*******************************************************************************
2 * Copyright 2019 Intel Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *******************************************************************************/
16 
17 #ifndef EXAMPLE_UTILS_HPP
18 #define EXAMPLE_UTILS_HPP
19 
20 #include <algorithm>
21 #include <iostream>
22 #include <stdlib.h>
23 #include <string>
24 
25 #include "dnnl.hpp"
26 
27 static dnnl::engine::kind parse_engine_kind(
28  int argc, char **argv, int extra_args = 0) {
29  // Returns default engine kind, i.e. CPU, if none given
30  if (argc == 1) {
32  } else if (argc <= extra_args + 2) {
33  std::string engine_kind_str = argv[1];
34  // Checking the engine type, i.e. CPU or GPU
35  if (engine_kind_str == "cpu") {
37  } else if (engine_kind_str == "gpu") {
38  // Checking if a GPU exists on the machine
40  std::cerr << "Application couldn't find GPU, please run with "
41  "CPU instead. Thanks!\n";
42  exit(1);
43  }
45  }
46  }
47 
48  // If all above fails, the example should be ran properly
49  std::cerr << "Please run example like this" << argv[0] << " cpu|gpu";
50  if (extra_args) { std::cerr << " [extra arguments]"; }
51  std::cerr << "\n";
52  exit(1);
53 }
54 
55 // Read from memory, write to handle
56 inline void read_from_dnnl_memory(void *handle, dnnl::memory &mem) {
57  dnnl::engine eng = mem.get_engine();
58  size_t bytes = mem.get_desc().get_size();
59 
60  if (eng.get_kind() == dnnl::engine::kind::cpu) {
61  uint8_t *src = static_cast<uint8_t *>(mem.get_data_handle());
62  std::copy(src, src + bytes, (uint8_t *)handle);
63  }
64 #if DNNL_GPU_RUNTIME == DNNL_RUNTIME_OCL
65  else if (eng.get_kind() == dnnl::engine::kind::gpu) {
66  dnnl::stream s(eng);
67  cl_command_queue q = s.get_ocl_command_queue();
68  cl_mem m = mem.get_ocl_mem_object();
69 
70  cl_int ret = clEnqueueReadBuffer(
71  q, m, CL_TRUE, 0, bytes, handle, 0, NULL, NULL);
72  if (ret != CL_SUCCESS)
73  throw std::runtime_error("clEnqueueReadBuffer failed. Status Code: "
74  + std::to_string(ret) + "\n");
75  }
76 #endif
77 }
78 
79 // Read from handle, write to memory
80 inline void write_to_dnnl_memory(void *handle, dnnl::memory &mem) {
81  dnnl::engine eng = mem.get_engine();
82  size_t bytes = mem.get_desc().get_size();
83 
84  if (eng.get_kind() == dnnl::engine::kind::cpu) {
85  uint8_t *dst = static_cast<uint8_t *>(mem.get_data_handle());
86  std::copy((uint8_t *)handle, (uint8_t *)handle + bytes, dst);
87  }
88 #if DNNL_GPU_RUNTIME == DNNL_RUNTIME_OCL
89  else if (eng.get_kind() == dnnl::engine::kind::gpu) {
90  dnnl::stream s(eng);
91  cl_command_queue q = s.get_ocl_command_queue();
92  cl_mem m = mem.get_ocl_mem_object();
93  size_t bytes = mem.get_desc().get_size();
94 
95  cl_int ret = clEnqueueWriteBuffer(
96  q, m, CL_TRUE, 0, bytes, handle, 0, NULL, NULL);
97  if (ret != CL_SUCCESS)
98  throw std::runtime_error(
99  "clEnqueueWriteBuffer failed. Status Code: "
100  + std::to_string(ret) + "\n");
101  }
102 #endif
103 }
104 
105 #endif
void * get_data_handle() const
Returns a handle of the data contained in the memory.
Definition: dnnl.hpp:1491
static size_t get_count(kind akind)
Returns the number of engines of a certain kind.
Definition: dnnl.hpp:840
C++ API.
kind get_kind() const
Returns the kind of the engine.
Definition: dnnl.hpp:885
An execution engine.
Definition: dnnl.hpp:821
kind
Kinds of engines.
Definition: dnnl.hpp:826
engine get_engine() const
Returns the engine of the memory.
Definition: dnnl.hpp:1481
Memory that describes the data.
Definition: dnnl.hpp:1031
desc get_desc() const
Returns the descriptor of the memory.
Definition: dnnl.hpp:1473
cl_mem get_ocl_mem_object() const
Returns the OpenCL memory object associated with the memory.
Definition: dnnl.hpp:1541
size_t get_size() const
Returns the number of bytes required to allocate the memory described including the padding area...
Definition: dnnl.hpp:1438
An execution stream.
Definition: dnnl.hpp:947