Deep Neural Network Library (DNNL)  1.90.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 <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 "example_macros.h"
28 
29 dnnl_engine_kind_t validate_engine_kind(dnnl_engine_kind_t akind) {
30  // Checking if a GPU exists on the machine
31  if (akind == dnnl_gpu) {
33  fprintf(stderr,
34  "Application couldn't find GPU, please run with CPU "
35  "instead. Thanks!\n");
36  exit(0);
37  }
38  }
39  return akind;
40 }
41 
42 dnnl_engine_kind_t parse_engine_kind(int argc, char **argv) {
43  // Returns default engine kind, i.e. CPU, if none given
44  if (argc == 1) {
45  return validate_engine_kind(dnnl_cpu);
46  } else if (argc == 2) {
47  // Checking the engine type, i.e. CPU or GPU
48  char *engine_kind_str = argv[1];
49  if (!strcmp(engine_kind_str, "cpu")) {
50  return validate_engine_kind(dnnl_cpu);
51  } else if (!strcmp(engine_kind_str, "gpu")) {
52  return validate_engine_kind(dnnl_gpu);
53  }
54  }
55 
56  // If all above fails, the example should be ran properly
57  fprintf(stderr, "Please run example like this: %s cpu|gpu\n", argv[0]);
58  exit(1);
59 }
60 
61 // Read from memory, write to handle
62 static inline void read_from_dnnl_memory(void *handle, dnnl_memory_t mem) {
63  dnnl_engine_t eng;
64  dnnl_engine_kind_t eng_kind;
65  const dnnl_memory_desc_t *md;
66 
67  CHECK(dnnl_memory_get_engine(mem, &eng));
68  CHECK(dnnl_engine_get_kind(eng, &eng_kind));
69  CHECK(dnnl_memory_get_memory_desc(mem, &md));
70  size_t bytes = dnnl_memory_desc_get_size(md);
71 
72 #if DNNL_WITH_SYCL
73  bool is_cpu_sycl
74  = (DNNL_CPU_RUNTIME == DNNL_RUNTIME_SYCL && eng_kind == dnnl_cpu);
75  bool is_gpu_sycl
76  = (DNNL_GPU_RUNTIME == DNNL_RUNTIME_SYCL && eng_kind == dnnl_gpu);
77  if (is_cpu_sycl || is_gpu_sycl) {
78  void *mapped_ptr = NULL;
79  CHECK(dnnl_memory_map_data(mem, &mapped_ptr));
80  if (mapped_ptr) {
81  for (size_t i = 0; i < bytes; ++i) {
82  ((char *)handle)[i] = ((char *)mapped_ptr)[i];
83  }
84  }
85  CHECK(dnnl_memory_unmap_data(mem, mapped_ptr));
86  return;
87  }
88 #endif
89 
90 #if DNNL_GPU_RUNTIME == DNNL_RUNTIME_OCL
91  if (eng_kind == dnnl_gpu) {
92  dnnl_stream_t s;
93  cl_command_queue q;
94  cl_mem m;
95 
96  CHECK(dnnl_memory_get_ocl_mem_object(mem, &m));
99 
100  cl_int ret = clEnqueueReadBuffer(
101  q, m, CL_TRUE, 0, bytes, handle, 0, NULL, NULL);
102  if (ret != CL_SUCCESS) {
103  fprintf(stderr,
104  "clEnqueueReadBuffer failed.\nStatus Code: "
105  "%d\n",
106  ret);
108  exit(1);
109  }
110 
112  }
113 #endif
114 
115  if (eng_kind == dnnl_cpu) {
116  void *ptr = NULL;
117  CHECK(dnnl_memory_get_data_handle(mem, &ptr));
118  if (ptr) {
119  for (size_t i = 0; i < bytes; ++i) {
120  ((char *)handle)[i] = ((char *)ptr)[i];
121  }
122  }
123  return;
124  }
125 
126  assert(!"not expected");
127 }
128 
129 // Read from handle, write to memory
130 static inline void write_to_dnnl_memory(void *handle, dnnl_memory_t mem) {
131  dnnl_engine_t eng;
132  dnnl_engine_kind_t eng_kind;
133  const dnnl_memory_desc_t *md;
134 
135  CHECK(dnnl_memory_get_engine(mem, &eng));
136  CHECK(dnnl_engine_get_kind(eng, &eng_kind));
137  CHECK(dnnl_memory_get_memory_desc(mem, &md));
138  size_t bytes = dnnl_memory_desc_get_size(md);
139 
140 #if DNNL_WITH_SYCL
141  bool is_cpu_sycl
142  = (DNNL_CPU_RUNTIME == DNNL_RUNTIME_SYCL && eng_kind == dnnl_cpu);
143  bool is_gpu_sycl
144  = (DNNL_GPU_RUNTIME == DNNL_RUNTIME_SYCL && eng_kind == dnnl_gpu);
145  if (is_cpu_sycl || is_gpu_sycl) {
146  void *mapped_ptr = NULL;
147  CHECK(dnnl_memory_map_data(mem, &mapped_ptr));
148  if (mapped_ptr) {
149  for (size_t i = 0; i < bytes; ++i) {
150  ((char *)mapped_ptr)[i] = ((char *)handle)[i];
151  }
152  }
153  CHECK(dnnl_memory_unmap_data(mem, mapped_ptr));
154  return;
155  }
156 #endif
157 
158 #if DNNL_GPU_RUNTIME == DNNL_RUNTIME_OCL
159  if (eng_kind == dnnl_gpu) {
160  dnnl_stream_t s;
161  cl_command_queue q;
162  cl_mem m;
163 
164  CHECK(dnnl_memory_get_ocl_mem_object(mem, &m));
166  CHECK(dnnl_stream_get_ocl_command_queue(s, &q));
167 
168  cl_int ret = clEnqueueWriteBuffer(
169  q, m, CL_TRUE, 0, bytes, handle, 0, NULL, NULL);
170  if (ret != CL_SUCCESS) {
171  fprintf(stderr,
172  "clEnqueueWriteBuffer failed.\nStatus Code: "
173  "%d\n",
174  ret);
176  exit(1);
177  }
178 
180  return;
181  }
182 #endif
183 
184  if (eng_kind == dnnl_cpu) {
185  void *ptr = NULL;
186  CHECK(dnnl_memory_get_data_handle(mem, &ptr));
187  if (ptr) {
188  for (size_t i = 0; i < bytes; ++i) {
189  ((char *)handle)[i] = ((char *)ptr)[i];
190  }
191  }
192  return;
193  }
194 
195  assert(!"not expected");
196 }
197 
198 #endif
dnnl_memory
dnnl_cpu
CPU engine.
Definition: dnnl_types.h:1324
dnnl_stream_destroy
dnnl_status_t DNNL_API dnnl_stream_destroy(dnnl_stream_t stream)
Destroys an execution stream.
dnnl_engine
An opaque structure to describe an engine.
dnnl_engine_kind_t
dnnl_engine_kind_t
Kinds of engines.
Definition: dnnl_types.h:1320
dnnl_memory_get_memory_desc
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_memory_desc_get_size
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.
dnnl_stream_get_ocl_command_queue
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.
dnnl_memory_get_data_handle
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_memory_map_data
dnnl_status_t DNNL_API dnnl_memory_map_data(const_dnnl_memory_t memory, void **mapped_ptr)
For a memory, maps the data of the memory to mapped_ptr.
dnnl_gpu
GPU engine.
Definition: dnnl_types.h:1326
dnnl_memory_get_engine
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_engine_get_kind
dnnl_status_t DNNL_API dnnl_engine_get_kind(dnnl_engine_t engine, dnnl_engine_kind_t *kind)
Returns the kind of an engine.
dnnl_memory_desc_t
Memory descriptor.
Definition: dnnl_types.h:883
dnnl_memory_get_ocl_mem_object
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.
dnnl.h
dnnl_stream_default_flags
Default stream configuration.
Definition: dnnl_types.h:1626
dnnl_stream_create
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_memory_unmap_data
dnnl_status_t DNNL_API dnnl_memory_unmap_data(const_dnnl_memory_t memory, void *mapped_ptr)
For a memory, unmaps a mapped pointer to the data of the memory.
dnnl_stream
dnnl_engine_get_count
size_t DNNL_API dnnl_engine_get_count(dnnl_engine_kind_t kind)
Returns the number of engines of a particular kind.