Deep Neural Network Library (DNNL)  1.1.3
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  for (size_t i = 0; i < bytes; ++i)
63  ((uint8_t *)handle)[i] = src[i];
64  }
65 #if DNNL_GPU_RUNTIME == DNNL_RUNTIME_OCL
66  else if (eng.get_kind() == dnnl::engine::kind::gpu) {
67  dnnl::stream s(eng);
68  cl_command_queue q = s.get_ocl_command_queue();
69  cl_mem m = mem.get_ocl_mem_object();
70 
71  cl_int ret = clEnqueueReadBuffer(
72  q, m, CL_TRUE, 0, bytes, handle, 0, NULL, NULL);
73  if (ret != CL_SUCCESS)
74  throw std::runtime_error("clEnqueueReadBuffer failed. Status Code: "
75  + std::to_string(ret) + "\n");
76  }
77 #endif
78 }
79 
80 // Read from handle, write to memory
81 inline void write_to_dnnl_memory(void *handle, dnnl::memory &mem) {
82  dnnl::engine eng = mem.get_engine();
83  size_t bytes = mem.get_desc().get_size();
84 
85  if (eng.get_kind() == dnnl::engine::kind::cpu) {
86  uint8_t *dst = static_cast<uint8_t *>(mem.get_data_handle());
87  for (size_t i = 0; i < bytes; ++i)
88  dst[i] = ((uint8_t *)handle)[i];
89  }
90 #if DNNL_GPU_RUNTIME == DNNL_RUNTIME_OCL
91  else if (eng.get_kind() == dnnl::engine::kind::gpu) {
92  dnnl::stream s(eng);
93  cl_command_queue q = s.get_ocl_command_queue();
94  cl_mem m = mem.get_ocl_mem_object();
95  size_t bytes = mem.get_desc().get_size();
96 
97  cl_int ret = clEnqueueWriteBuffer(
98  q, m, CL_TRUE, 0, bytes, handle, 0, NULL, NULL);
99  if (ret != CL_SUCCESS)
100  throw std::runtime_error(
101  "clEnqueueWriteBuffer failed. Status Code: "
102  + std::to_string(ret) + "\n");
103  }
104 #endif
105 }
106 
107 #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