/* * Copyright (C) 2011-2020 Intel Corporation. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * Neither the name of Intel Corporation nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */ #ifndef EXAMPLE_UTILS_HPP #define EXAMPLE_UTILS_HPP #include #include #include #include #include "dnnl.hpp" extern "C" void printf(const char *fmt, ...); static 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 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 dnnl::engine::kind::cpu; } else if (engine_kind_str == "gpu") { // Checking if a GPU exists on the machine if (dnnl::engine::get_count(dnnl::engine::kind::gpu) == 0) { printf("Application couldn't find GPU, please run with CPU instead. Thanks!\n"); abort(); } return dnnl::engine::kind::gpu; } } // If all above fails, the example should be ran properly printf("Please run example like this: cpu|gpu"); abort(); } // 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 bytes = mem.get_desc().get_size(); if (eng.get_kind() == dnnl::engine::kind::cpu) { uint8_t *src = static_cast(mem.get_data_handle()); for (size_t i = 0; i < bytes; ++i) ((uint8_t *)handle)[i] = src[i]; } #if DNNL_GPU_RUNTIME == DNNL_RUNTIME_OCL else if (eng.get_kind() == dnnl::engine::kind::gpu) { dnnl::stream s(eng); cl_command_queue q = s.get_ocl_command_queue(); cl_mem m = mem.get_ocl_mem_object(); cl_int ret = clEnqueueReadBuffer( q, m, CL_TRUE, 0, bytes, handle, 0, NULL, NULL); if (ret != CL_SUCCESS) throw std::runtime_error("clEnqueueReadBuffer failed. Status Code: " + std::to_string(ret) + "\n"); } #endif } // 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 bytes = mem.get_desc().get_size(); if (eng.get_kind() == dnnl::engine::kind::cpu) { uint8_t *dst = static_cast(mem.get_data_handle()); for (size_t i = 0; i < bytes; ++i) dst[i] = ((uint8_t *)handle)[i]; } #if DNNL_GPU_RUNTIME == DNNL_RUNTIME_OCL else if (eng.get_kind() == dnnl::engine::kind::gpu) { dnnl::stream s(eng); cl_command_queue q = s.get_ocl_command_queue(); cl_mem m = mem.get_ocl_mem_object(); size_t bytes = mem.get_desc().get_size(); cl_int ret = clEnqueueWriteBuffer( q, m, CL_TRUE, 0, bytes, handle, 0, NULL, NULL); if (ret != CL_SUCCESS) throw std::runtime_error( "clEnqueueWriteBuffer failed. Status Code: " + std::to_string(ret) + "\n"); } #endif } #endif