1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
|
/*******************************************************************************
* Copyright 2019-2025 Intel Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
/// @file
/// Examples C++ Utility Functions
#ifndef EXAMPLE_UTILS_HPP
#define EXAMPLE_UTILS_HPP
#include <algorithm>
#include <cassert>
#include <cstring>
#include <functional>
#include <iostream>
#include <numeric>
#include <stdexcept>
#include <stdlib.h>
#include <initializer_list>
#include "dnnl.hpp"
#include "dnnl_debug.h"
#if DNNL_GPU_RUNTIME == DNNL_RUNTIME_OCL
#include "dnnl_ocl.hpp"
#elif DNNL_GPU_RUNTIME == DNNL_RUNTIME_SYCL
#include "dnnl_sycl.hpp"
#endif
#if DNNL_CPU_THREADING_RUNTIME == DNNL_RUNTIME_OMP
#ifdef _MSC_VER
#define PRAGMA_MACRo(x) __pragma(x)
#define PRAGMA_MACRO(x) PRAGMA_MACRo(x)
#else
#define PRAGMA_MACRo(x) _Pragma(#x)
#define PRAGMA_MACRO(x) PRAGMA_MACRo(x)
#endif
// MSVC doesn't support collapse clause in omp parallel
#if defined(_MSC_VER) && !defined(__clang__) && !defined(__INTEL_COMPILER)
#define collapse(x)
#endif
#define PRAGMA_OMP_PARALLEL_FOR_COLLAPSE(n) PRAGMA_MACRO(omp parallel for collapse(n))
#else // DNNL_CPU_THREADING_RUNTIME == DNNL_RUNTIME_OMP
#define PRAGMA_OMP_PARALLEL_FOR_COLLAPSE(n)
#endif
#if DNNL_CPU_THREADING_RUNTIME == DNNL_RUNTIME_TBB
#include "tbb/version.h"
#if defined(TBB_INTERFACE_VERSION) && (TBB_INTERFACE_VERSION >= 12060)
#include "tbb/global_control.h"
#define DNNL_TBB_NEED_EXPLICIT_FINALIZE
#endif
#endif
// TBB runtime may crash when it is used under CTest. This is a known TBB
// limitation that can be worked around by doing explicit finalization.
// The API to do that was introduced in 2021.6.0. When using an older TBB
// runtime the crash may still happen.
inline void finalize() {
#ifdef DNNL_TBB_NEED_EXPLICIT_FINALIZE
tbb::task_scheduler_handle handle
= tbb::task_scheduler_handle {tbb::attach {}};
tbb::finalize(handle, std::nothrow);
#endif
}
inline dnnl::engine::kind validate_engine_kind(dnnl::engine::kind akind) {
// Checking if a GPU exists on the machine
if (akind == dnnl::engine::kind::gpu) {
if (dnnl::engine::get_count(dnnl::engine::kind::gpu) == 0) {
std::cout << "Application couldn't find GPU, please run with CPU "
"instead.\n";
exit(0);
}
}
return akind;
}
// Exception class to indicate that the example uses a feature that is not
// available on the current systems. It is not treated as an error then, but
// just notifies a user.
// NOLINTNEXTLINE(readability-identifier-naming)
struct example_allows_unimplemented : public std::exception {
example_allows_unimplemented(const char *message) noexcept
: message(message) {}
const char *what() const noexcept override { return message; }
const char *message;
};
inline const char *engine_kind2str_upper(dnnl::engine::kind kind);
// Runs example function with signature void() and catches errors.
// Returns `0` on success, `1` or oneDNN error, and `2` on example error.
inline int handle_example_errors(
std::initializer_list<dnnl::engine::kind> engine_kinds,
const std::function<void()> &example) {
int exit_code = 0;
try {
example();
} catch (example_allows_unimplemented &e) {
std::cout << e.message << std::endl;
exit_code = 0;
} catch (dnnl::error &e) {
std::cout << "oneDNN error caught: " << std::endl
<< "\tStatus: " << dnnl_status2str(e.status) << std::endl
<< "\tMessage: " << e.what() << std::endl;
exit_code = 1;
} catch (std::exception &e) {
std::cout << "Error in the example: " << e.what() << "." << std::endl;
exit_code = 2;
}
std::string engine_kind_str;
for (auto it = engine_kinds.begin(); it != engine_kinds.end(); ++it) {
if (it != engine_kinds.begin()) engine_kind_str += "/";
engine_kind_str += engine_kind2str_upper(*it);
}
std::cout << "Example " << (exit_code ? "failed" : "passed") << " on "
<< engine_kind_str << "." << std::endl;
finalize();
return exit_code;
}
// Same as above, but for functions with signature
// void(dnnl::engine::kind engine_kind, int argc, char **argv).
inline int handle_example_errors(
std::function<void(dnnl::engine::kind, int, char **)> example,
dnnl::engine::kind engine_kind, int argc, char **argv) {
return handle_example_errors(
{engine_kind}, [&]() { example(engine_kind, argc, argv); });
}
// Same as above, but for functions with signature void(dnnl::engine::kind).
inline int handle_example_errors(
std::function<void(dnnl::engine::kind)> example,
dnnl::engine::kind engine_kind) {
return handle_example_errors(
{engine_kind}, [&]() { example(engine_kind); });
}
inline dnnl::engine::kind parse_engine_kind(
int argc, char **argv, int extra_args = 0) {
// Returns default engine kind, i.e. CPU, if none given
if (argc == 1) {
return validate_engine_kind(dnnl::engine::kind::cpu);
} else if (argc <= extra_args + 2) {
std::string engine_kind_str = argv[1];
// Checking the engine type, i.e. CPU or GPU
if (engine_kind_str == "cpu") {
return validate_engine_kind(dnnl::engine::kind::cpu);
} else if (engine_kind_str == "gpu") {
return validate_engine_kind(dnnl::engine::kind::gpu);
}
}
// If all above fails, the example should be ran properly
std::cout << "Inappropriate engine kind." << std::endl
<< "Please run the example like this: " << argv[0] << " [cpu|gpu]"
<< (extra_args ? " [extra arguments]" : "") << "." << std::endl;
exit(1);
}
inline const char *engine_kind2str_upper(dnnl::engine::kind kind) {
if (kind == dnnl::engine::kind::cpu) return "CPU";
if (kind == dnnl::engine::kind::gpu) return "GPU";
assert(!"not expected");
return "<Unknown engine>";
}
inline dnnl::memory::dim product(const dnnl::memory::dims &dims) {
return std::accumulate(dims.begin(), dims.end(), (dnnl::memory::dim)1,
std::multiplies<dnnl::memory::dim>());
}
// Read from memory, write to handle
inline void read_from_dnnl_memory(void *handle, dnnl::memory &mem) {
dnnl::engine eng = mem.get_engine();
size_t size = mem.get_desc().get_size();
if (!handle) throw std::runtime_error("handle is nullptr.");
#ifdef DNNL_WITH_SYCL
bool is_cpu_sycl = (DNNL_CPU_RUNTIME == DNNL_RUNTIME_SYCL
&& eng.get_kind() == dnnl::engine::kind::cpu);
bool is_gpu_sycl = (DNNL_GPU_RUNTIME == DNNL_RUNTIME_SYCL
&& eng.get_kind() == dnnl::engine::kind::gpu);
if (is_cpu_sycl || is_gpu_sycl) {
auto mkind = dnnl::sycl_interop::get_memory_kind(mem);
if (mkind == dnnl::sycl_interop::memory_kind::buffer) {
auto buffer = dnnl::sycl_interop::get_buffer<uint8_t>(mem);
auto src = buffer.get_host_access();
uint8_t *src_ptr = src.get_pointer();
if (!src_ptr)
throw std::runtime_error("get_pointer returned nullptr.");
for (size_t i = 0; i < size; ++i)
((uint8_t *)handle)[i] = src_ptr[i];
} else {
assert(mkind == dnnl::sycl_interop::memory_kind::usm);
uint8_t *src_ptr = (uint8_t *)mem.get_data_handle();
if (!src_ptr)
throw std::runtime_error("get_data_handle returned nullptr.");
if (is_cpu_sycl) {
for (size_t i = 0; i < size; ++i)
((uint8_t *)handle)[i] = src_ptr[i];
} else {
auto sycl_queue
= dnnl::sycl_interop::get_queue(dnnl::stream(eng));
sycl_queue.memcpy(handle, src_ptr, size).wait();
}
}
return;
}
#endif
#if DNNL_GPU_RUNTIME == DNNL_RUNTIME_OCL
if (eng.get_kind() == dnnl::engine::kind::gpu) {
void *mapped_ptr = mem.map_data();
if (mapped_ptr) std::memcpy(handle, mapped_ptr, size);
mem.unmap_data(mapped_ptr);
return;
}
#endif
if (eng.get_kind() == dnnl::engine::kind::cpu) {
uint8_t *src = static_cast<uint8_t *>(mem.get_data_handle());
if (!src) throw std::runtime_error("get_data_handle returned nullptr.");
for (size_t i = 0; i < size; ++i)
((uint8_t *)handle)[i] = src[i];
return;
}
assert(!"not expected");
}
// Read from handle, write to memory
inline void write_to_dnnl_memory(void *handle, dnnl::memory &mem) {
dnnl::engine eng = mem.get_engine();
size_t size = mem.get_desc().get_size();
if (!handle) throw std::runtime_error("handle is nullptr.");
#ifdef DNNL_WITH_SYCL
bool is_cpu_sycl = (DNNL_CPU_RUNTIME == DNNL_RUNTIME_SYCL
&& eng.get_kind() == dnnl::engine::kind::cpu);
bool is_gpu_sycl = (DNNL_GPU_RUNTIME == DNNL_RUNTIME_SYCL
&& eng.get_kind() == dnnl::engine::kind::gpu);
if (is_cpu_sycl || is_gpu_sycl) {
auto mkind = dnnl::sycl_interop::get_memory_kind(mem);
if (mkind == dnnl::sycl_interop::memory_kind::buffer) {
auto buffer = dnnl::sycl_interop::get_buffer<uint8_t>(mem);
auto dst = buffer.get_host_access();
uint8_t *dst_ptr = dst.get_pointer();
if (!dst_ptr)
throw std::runtime_error("get_pointer returned nullptr.");
for (size_t i = 0; i < size; ++i)
dst_ptr[i] = ((uint8_t *)handle)[i];
} else {
assert(mkind == dnnl::sycl_interop::memory_kind::usm);
uint8_t *dst_ptr = (uint8_t *)mem.get_data_handle();
if (!dst_ptr)
throw std::runtime_error("get_data_handle returned nullptr.");
if (is_cpu_sycl) {
for (size_t i = 0; i < size; ++i)
dst_ptr[i] = ((uint8_t *)handle)[i];
} else {
auto sycl_queue
= dnnl::sycl_interop::get_queue(dnnl::stream(eng));
sycl_queue.memcpy(dst_ptr, handle, size).wait();
}
}
return;
}
#endif
#if DNNL_GPU_RUNTIME == DNNL_RUNTIME_OCL
if (eng.get_kind() == dnnl::engine::kind::gpu) {
void *mapped_ptr = mem.map_data();
if (mapped_ptr) std::memcpy(mapped_ptr, handle, size);
mem.unmap_data(mapped_ptr);
return;
}
#endif
if (eng.get_kind() == dnnl::engine::kind::cpu) {
uint8_t *dst = static_cast<uint8_t *>(mem.get_data_handle());
if (!dst) throw std::runtime_error("get_data_handle returned nullptr.");
for (size_t i = 0; i < size; ++i)
dst[i] = ((uint8_t *)handle)[i];
return;
}
assert(!"not expected");
}
#endif
|