oneAPI Deep Neural Network Library (oneDNN)
Performance library for Deep Learning
1.96.0
dnnl_ocl.hpp
1 /*******************************************************************************
2 * Copyright 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 ONEAPI_DNNL_DNNL_OCL_HPP
18 #define ONEAPI_DNNL_DNNL_OCL_HPP
19 
20 #include "oneapi/dnnl/dnnl.hpp"
21 
23 #include <algorithm>
24 #include <cstdlib>
25 #include <iterator>
26 #include <memory>
27 #include <string>
28 #include <vector>
29 #include <unordered_map>
30 
31 #include "oneapi/dnnl/dnnl_ocl.h"
32 
33 #include <CL/cl.h>
35 
38 
39 namespace dnnl {
40 
44 
48 
50 namespace ocl_interop {
51 
58 inline engine make_engine(cl_device_id device, cl_context context) {
59  dnnl_engine_t c_engine;
61  dnnl_ocl_interop_engine_create(&c_engine, device, context),
62  "could not create an engine");
63  return engine(c_engine);
64 }
65 
70 inline cl_context get_context(const engine &aengine) {
71  cl_context context = nullptr;
73  dnnl_ocl_interop_engine_get_context(aengine.get(), &context),
74  "could not get an OpenCL context from an engine");
75  return context;
76 }
77 
82 inline cl_device_id get_device(const engine &aengine) {
83  cl_device_id device = nullptr;
85  "could not get an OpenCL device from an engine");
86  return device;
87 }
88 
94 inline stream make_stream(const engine &aengine, cl_command_queue queue) {
95  dnnl_stream_t c_stream;
97  dnnl_ocl_interop_stream_create(&c_stream, aengine.get(), queue),
98  "could not create a stream");
99  return stream(c_stream);
100 }
101 
106 inline cl_command_queue get_command_queue(const stream &astream) {
107  cl_command_queue queue = nullptr;
110  "could not get an OpenCL command queue from a stream");
111  return queue;
112 }
113 
118 inline cl_mem get_mem_object(const memory &amemory) {
119  cl_mem mem_object;
121  dnnl_ocl_interop_memory_get_mem_object(amemory.get(), &mem_object),
122  "could not get OpenCL buffer object from a memory object");
123  return mem_object;
124 }
125 
134 inline void set_mem_object(memory &amemory, cl_mem mem_object) {
136  dnnl_ocl_interop_memory_set_mem_object(amemory.get(), mem_object),
137  "could not set OpenCL buffer object from a memory object");
138 }
139 
140 } // namespace ocl_interop
141 
143 
145 
146 } // namespace dnnl
147 
149 
150 #endif
dnnl_ocl_interop_memory_set_mem_object
dnnl_status_t DNNL_API dnnl_ocl_interop_memory_set_mem_object(dnnl_memory_t memory, cl_mem mem_object)
Sets OpenCL memory object associated with a memory object.
dnnl.hpp
dnnl::stream
An execution stream.
Definition: dnnl.hpp:975
dnnl::engine
An execution engine.
Definition: dnnl.hpp:859
dnnl_engine
An opaque structure to describe an engine.
dnnl::ocl_interop::set_mem_object
void set_mem_object(memory &amemory, cl_mem mem_object)
Sets the OpenCL memory object associated with the memory object.
Definition: dnnl_ocl.hpp:134
dnnl::ocl_interop::get_mem_object
cl_mem get_mem_object(const memory &amemory)
Returns the OpenCL memory object associated with the memory object.
Definition: dnnl_ocl.hpp:118
dnnl::ocl_interop::get_context
cl_context get_context(const engine &aengine)
Returns OpenCL context associated with the engine.
Definition: dnnl_ocl.hpp:70
dnnl_ocl_interop_engine_create
dnnl_status_t DNNL_API dnnl_ocl_interop_engine_create(dnnl_engine_t *engine, cl_device_id device, cl_context context)
Creates an engine associated with an OpenCL device and an OpenCL context.
dnnl_ocl_interop_stream_create
dnnl_status_t DNNL_API dnnl_ocl_interop_stream_create(dnnl_stream_t *stream, dnnl_engine_t engine, cl_command_queue queue)
Creates an execution stream for a given engine associated with an OpenCL command queue.
dnnl_ocl_interop_memory_get_mem_object
dnnl_status_t DNNL_API dnnl_ocl_interop_memory_get_mem_object(const_dnnl_memory_t memory, cl_mem *mem_object)
Returns an OpenCL memory object associated with a memory object.
dnnl::ocl_interop::get_command_queue
cl_command_queue get_command_queue(const stream &astream)
Returns OpenCL queue object associated with the execution stream.
Definition: dnnl_ocl.hpp:106
dnnl_ocl_interop_stream_get_command_queue
dnnl_status_t DNNL_API dnnl_ocl_interop_stream_get_command_queue(dnnl_stream_t stream, cl_command_queue *queue)
Returns the OpenCL command queue associated with an execution stream.
dnnl_ocl_interop_engine_get_context
dnnl_status_t DNNL_API dnnl_ocl_interop_engine_get_context(dnnl_engine_t engine, cl_context *context)
Returns the OpenCL context associated with an engine.
dnnl::handle::get
T get(bool allow_empty=false) const
Returns the underlying C API handle.
Definition: dnnl.hpp:185
dnnl::memory
Memory object.
Definition: dnnl.hpp:1098
dnnl::error::wrap_c_api
static void wrap_c_api(dnnl_status_t status, const char *message)
A convenience function for wrapping calls to C API functions.
Definition: dnnl.hpp:103
dnnl::ocl_interop::get_device
cl_device_id get_device(const engine &aengine)
Returns OpenCL device associated with the engine.
Definition: dnnl_ocl.hpp:82
dnnl
oneDNN namespace
Definition: dnnl.hpp:74
dnnl_stream
dnnl_ocl_interop_get_device
dnnl_status_t DNNL_API dnnl_ocl_interop_get_device(dnnl_engine_t engine, cl_device_id *device)
Returns the OpenCL device associated with an engine.
dnnl::ocl_interop::make_engine
engine make_engine(cl_device_id device, cl_context context)
Constructs an engine from OpenCL device and context objects.
Definition: dnnl_ocl.hpp:58
dnnl::ocl_interop::make_stream
stream make_stream(const engine &aengine, cl_command_queue queue)
Constructs an execution stream for the specified engine and OpenCL queue.
Definition: dnnl_ocl.hpp:94