oneAPI Deep Neural Network Library (oneDNN)  1.4.0
Performance library for Deep Learning
example_utils.h
1 /*******************************************************************************
2 * Copyright 2019-2020 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_H
18 #define EXAMPLE_UTILS_H
19 
20 #include <assert.h>
21 #include <stdbool.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 
26 #include "dnnl.h"
27 #include "dnnl_debug.h"
28 
29 #define COMPLAIN_DNNL_ERROR_AND_EXIT(what, status) \
30  do { \
31  printf("[%s:%d] `%s` returns oneDNN error: %s.\n", __FILE__, __LINE__, \
32  what, dnnl_status2str(status)); \
33  printf("Example failed.\n"); \
34  exit(1); \
35  } while (0)
36 
37 #define COMPLAIN_EXAMPLE_ERROR_AND_EXIT(complain_fmt, ...) \
38  do { \
39  printf("[%s:%d] Error in the example: " complain_fmt ".\n", __FILE__, \
40  __LINE__, __VA_ARGS__); \
41  printf("Example failed.\n"); \
42  exit(2); \
43  } while (0)
44 
45 #define CHECK(f) \
46  do { \
47  dnnl_status_t s_ = f; \
48  if (s_ != dnnl_success) COMPLAIN_DNNL_ERROR_AND_EXIT(#f, s_); \
49  } while (0)
50 
51 static inline dnnl_engine_kind_t parse_engine_kind(int argc, char **argv) {
52  // Returns default engine kind, i.e. CPU, if none given
53  if (argc == 1) {
54  return dnnl_cpu;
55  } else if (argc == 2) {
56  // Checking the engine type, i.e. CPU or GPU
57  char *engine_kind_str = argv[1];
58  if (!strcmp(engine_kind_str, "cpu")) {
59  return dnnl_cpu;
60  } else if (!strcmp(engine_kind_str, "gpu")) {
61  // Checking if a GPU exists on the machine
63  COMPLAIN_EXAMPLE_ERROR_AND_EXIT("%s",
64  "could not find compatible GPU\nPlease run the example "
65  "with CPU instead");
66  return dnnl_gpu;
67  }
68  }
69 
70  // If all above fails, the example should be run properly
71  COMPLAIN_EXAMPLE_ERROR_AND_EXIT(
72  "inappropriate engine kind.\n"
73  "Please run the example like this: %s [cpu|gpu].",
74  argv[0]);
75 }
76 
77 static inline const char *engine_kind2str_upper(dnnl_engine_kind_t kind) {
78  if (kind == dnnl_cpu) return "CPU";
79  if (kind == dnnl_gpu) return "GPU";
80  return "<Unknown engine>";
81 }
82 
83 // Read from memory, write to handle
84 static inline void read_from_dnnl_memory(void *handle, dnnl_memory_t mem) {
85  dnnl_engine_t eng;
86  dnnl_engine_kind_t eng_kind;
87  const dnnl_memory_desc_t *md;
88 
89  CHECK(dnnl_memory_get_engine(mem, &eng));
90  CHECK(dnnl_engine_get_kind(eng, &eng_kind));
91  CHECK(dnnl_memory_get_memory_desc(mem, &md));
92  size_t bytes = dnnl_memory_desc_get_size(md);
93 
94  if (eng_kind == dnnl_cpu) {
95  void *ptr = NULL;
96  CHECK(dnnl_memory_get_data_handle(mem, &ptr));
97  if (ptr) {
98  for (size_t i = 0; i < bytes; ++i) {
99  ((char *)handle)[i] = ((char *)ptr)[i];
100  }
101  } else {
102  handle = NULL;
103  }
104  }
105 #if DNNL_GPU_RUNTIME == DNNL_RUNTIME_OCL
106  else if (eng_kind == dnnl_gpu) {
107  dnnl_stream_t s;
108  cl_command_queue q;
109  cl_mem m;
110 
111  CHECK(dnnl_memory_get_ocl_mem_object(mem, &m));
113  CHECK(dnnl_stream_get_ocl_command_queue(s, &q));
114 
115  cl_int ret = clEnqueueReadBuffer(
116  q, m, CL_TRUE, 0, bytes, handle, 0, NULL, NULL);
117  if (ret != CL_SUCCESS)
118  COMPLAIN_EXAMPLE_ERROR_AND_EXIT(
119  "clEnqueueReadBuffer failed (status code: %d)", ret);
120 
122  }
123 #endif
124 }
125 
126 // Read from handle, write to memory
127 static inline void write_to_dnnl_memory(void *handle, dnnl_memory_t mem) {
128  dnnl_engine_t eng;
129  dnnl_engine_kind_t eng_kind;
130  const dnnl_memory_desc_t *md;
131 
132  CHECK(dnnl_memory_get_engine(mem, &eng));
133  CHECK(dnnl_engine_get_kind(eng, &eng_kind));
134  CHECK(dnnl_memory_get_memory_desc(mem, &md));
135  size_t bytes = dnnl_memory_desc_get_size(md);
136 
137  if (eng_kind == dnnl_cpu) {
138  void *ptr = NULL;
139  CHECK(dnnl_memory_get_data_handle(mem, &ptr));
140  if (ptr) {
141  for (size_t i = 0; i < bytes; ++i) {
142  ((char *)ptr)[i] = ((char *)handle)[i];
143  }
144  } else {
145  handle = NULL;
146  }
147  }
148 #if DNNL_GPU_RUNTIME == DNNL_RUNTIME_OCL
149  else if (eng_kind == dnnl_gpu) {
150  dnnl_stream_t s;
151  cl_command_queue q;
152  cl_mem m;
153 
154  CHECK(dnnl_memory_get_ocl_mem_object(mem, &m));
156  CHECK(dnnl_stream_get_ocl_command_queue(s, &q));
157 
158  cl_int ret = clEnqueueWriteBuffer(
159  q, m, CL_TRUE, 0, bytes, handle, 0, NULL, NULL);
160  if (ret != CL_SUCCESS)
161  COMPLAIN_EXAMPLE_ERROR_AND_EXIT(
162  "clEnqueueWriteBuffer failed (status code: %d)", ret);
163 
165  }
166 #endif
167 }
168 
169 #endif
CPU engine.
Definition: dnnl_types.h:1650
An opaque structure to describe a memory.
dnnl_engine_kind_t
Kinds of engines.
Definition: dnnl_types.h:1646
dnnl_status_t DNNL_API dnnl_memory_get_data_handle(const_dnnl_memory_t memory, void **handle)
Returns memory object&#39;s data handle.
C API.
dnnl_status_t DNNL_API dnnl_stream_create(dnnl_stream_t *stream, dnnl_engine_t engine, unsigned flags)
Creates an execution stream.
GPU engine.
Definition: dnnl_types.h:1652
An opaque structure to describe an engine.
size_t DNNL_API dnnl_memory_desc_get_size(const dnnl_memory_desc_t *memory_desc)
Returns the size of a memory descriptor.
dnnl_status_t DNNL_API dnnl_memory_get_memory_desc(const_dnnl_memory_t memory, const dnnl_memory_desc_t **memory_desc)
Returns the memory descriptor for a memory object.
Default stream configuration.
Definition: dnnl_types.h:2077
dnnl_status_t DNNL_API dnnl_memory_get_engine(const_dnnl_memory_t memory, dnnl_engine_t *engine)
Returns the engine of a memory object.
Memory descriptor.
Definition: dnnl_types.h:1050
dnnl_status_t DNNL_API dnnl_memory_get_ocl_mem_object(const_dnnl_memory_t memory, cl_mem *mem_object)
Returns an OpenCL memory object associated with a memory object.
Debug capabilities.
dnnl_status_t DNNL_API dnnl_engine_get_kind(dnnl_engine_t engine, dnnl_engine_kind_t *kind)
Returns the kind of an engine.
size_t DNNL_API dnnl_engine_get_count(dnnl_engine_kind_t kind)
Returns the number of engines of a particular kind.
An opaque structure to describe an execution stream.
dnnl_status_t DNNL_API dnnl_stream_destroy(dnnl_stream_t stream)
Destroys an execution stream.
dnnl_status_t DNNL_API dnnl_stream_get_ocl_command_queue(dnnl_stream_t stream, cl_command_queue *queue)
Returns the OpenCL command queue associated with an execution stream.