oneAPI Deep Neural Network Library (oneDNN)
Performance library for Deep Learning
2.1.3
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 
50 
52 namespace ocl_interop {
53 
60 inline engine make_engine(cl_device_id device, cl_context context) {
61  dnnl_engine_t c_engine;
63  dnnl_ocl_interop_engine_create(&c_engine, device, context),
64  "could not create an engine");
65  return engine(c_engine);
66 }
67 
72 inline cl_context get_context(const engine &aengine) {
73  cl_context context = nullptr;
75  dnnl_ocl_interop_engine_get_context(aengine.get(), &context),
76  "could not get an OpenCL context from an engine");
77  return context;
78 }
79 
84 inline cl_device_id get_device(const engine &aengine) {
85  cl_device_id device = nullptr;
87  "could not get an OpenCL device from an engine");
88  return device;
89 }
90 
96 inline stream make_stream(const engine &aengine, cl_command_queue queue) {
97  dnnl_stream_t c_stream;
99  dnnl_ocl_interop_stream_create(&c_stream, aengine.get(), queue),
100  "could not create a stream");
101  return stream(c_stream);
102 }
103 
108 inline cl_command_queue get_command_queue(const stream &astream) {
109  cl_command_queue queue = nullptr;
112  "could not get an OpenCL command queue from a stream");
113  return queue;
114 }
115 
120 inline cl_mem get_mem_object(const memory &amemory) {
121  cl_mem mem_object;
123  dnnl_ocl_interop_memory_get_mem_object(amemory.get(), &mem_object),
124  "could not get OpenCL buffer object from a memory object");
125  return mem_object;
126 }
127 
136 inline void set_mem_object(memory &amemory, cl_mem mem_object) {
138  dnnl_ocl_interop_memory_set_mem_object(amemory.get(), mem_object),
139  "could not set OpenCL buffer object from a memory object");
140 }
141 
142 } // namespace ocl_interop
143 
145 
147 
148 } // namespace dnnl
149 
151 
152 #endif
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_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_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_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_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_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_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.
cl_command_queue get_command_queue(const stream &astream)
Returns OpenCL queue object associated with the execution stream.
Definition: dnnl_ocl.hpp:108
cl_context get_context(const engine &aengine)
Returns OpenCL context associated with the engine.
Definition: dnnl_ocl.hpp:72
cl_device_id get_device(const engine &aengine)
Returns OpenCL device associated with the engine.
Definition: dnnl_ocl.hpp:84
engine make_engine(cl_device_id device, cl_context context)
Constructs an engine from OpenCL device and context objects.
Definition: dnnl_ocl.hpp:60
void set_mem_object(memory &amemory, cl_mem mem_object)
Sets the OpenCL memory object associated with the memory object.
Definition: dnnl_ocl.hpp:136
cl_mem get_mem_object(const memory &amemory)
Returns the OpenCL memory object associated with the memory object.
Definition: dnnl_ocl.hpp:120
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:96
oneDNN namespace
Definition: dnnl.hpp:74
C++ API.
An execution engine.
Definition: dnnl.hpp:869
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
T get(bool allow_empty=false) const
Returns the underlying C API handle.
Definition: dnnl.hpp:185
Memory object.
Definition: dnnl.hpp:1108
An execution stream.
Definition: dnnl.hpp:985
An opaque structure to describe an engine.
An opaque structure to describe an execution stream.