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