Visible to Intel only — GUID: GUID-EF3CD36F-9441-43BB-999C-25AAEC368B2B
Abs
AbsBackward
Add
AvgPool
AvgPoolBackward
BatchNormForwardTraining
BatchNormInference
BatchNormTrainingBackward
BiasAdd
BiasAddBackward
Clamp
ClampBackward
Concat
Convolution
ConvolutionBackwardData
ConvolutionBackwardWeights
ConvTranspose
ConvTransposeBackwardData
ConvTransposeBackwardWeights
Dequantize
Divide
DynamicDequantize
DynamicQuantize
Elu
EluBackward
End
Exp
GELU
GELUBackward
HardSigmoid
HardSigmoidBackward
HardSwish
HardSwishBackward
Interpolate
InterpolateBackward
LayerNorm
LayerNormBackward
LeakyReLU
Log
LogSoftmax
LogSoftmaxBackward
MatMul
Maximum
MaxPool
MaxPoolBackward
Minimum
Mish
MishBackward
Multiply
Pow
PReLU
PReLUBackward
Quantize
Reciprocal
ReduceL1
ReduceL2
ReduceMax
ReduceMean
ReduceMin
ReduceProd
ReduceSum
ReLU
ReLUBackward
Reorder
Round
Select
Sigmoid
SigmoidBackward
SoftMax
SoftMaxBackward
SoftPlus
SoftPlusBackward
Sqrt
SqrtBackward
Square
SquaredDifference
StaticReshape
StaticTranspose
Subtract
Tanh
TanhBackward
TypeCast
Wildcard
enum dnnl_alg_kind_t
enum dnnl_normalization_flags_t
enum dnnl_primitive_kind_t
enum dnnl_prop_kind_t
enum dnnl_query_t
enum dnnl::normalization_flags
enum dnnl::query
struct dnnl_exec_arg_t
struct dnnl_primitive
struct dnnl_primitive_desc
struct dnnl::primitive
struct dnnl::primitive_desc
struct dnnl::primitive_desc_base
enum dnnl_rnn_direction_t
enum dnnl_rnn_flags_t
enum dnnl::rnn_direction
enum dnnl::rnn_flags
struct dnnl::augru_backward
struct dnnl::augru_forward
struct dnnl::gru_backward
struct dnnl::gru_forward
struct dnnl::lbr_augru_backward
struct dnnl::lbr_augru_forward
struct dnnl::lbr_gru_backward
struct dnnl::lbr_gru_forward
struct dnnl::lstm_backward
struct dnnl::lstm_forward
struct dnnl::rnn_primitive_desc_base
struct dnnl::vanilla_rnn_backward
struct dnnl::vanilla_rnn_forward
Visible to Intel only — GUID: GUID-EF3CD36F-9441-43BB-999C-25AAEC368B2B
gpu_opencl_interop cpp
This C++ API example demonstrates programming for Intel(R) Processor Graphics with OpenCL* extensions API in oneDNN. Annotated version: Getting started on GPU with OpenCL extensions API
This C++ API example demonstrates programming for Intel(R) Processor Graphics with OpenCL* extensions API in oneDNN. Annotated version: Getting started on GPU with OpenCL extensions API
/*******************************************************************************
* Copyright 2019-2022 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.
*******************************************************************************/
// [Prologue]
#include <iostream>
#include <numeric>
#include <stdexcept>
#include <CL/cl.h>
#include "oneapi/dnnl/dnnl.hpp"
#include "oneapi/dnnl/dnnl_ocl.hpp"
#include "example_utils.hpp"
using namespace dnnl;
using namespace std;
// [Prologue]
#define OCL_CHECK(x) \
do { \
cl_int s = (x); \
if (s != CL_SUCCESS) { \
std::cout << "[" << __FILE__ << ":" << __LINE__ << "] '" << #x \
<< "' failed (status code: " << s << ")." << std::endl; \
exit(1); \
} \
} while (0)
cl_kernel create_init_opencl_kernel(
cl_context ocl_ctx, const char *kernel_name, const char *ocl_code) {
cl_int err;
const char *sources[] = {ocl_code};
cl_program ocl_program
= clCreateProgramWithSource(ocl_ctx, 1, sources, nullptr, &err);
OCL_CHECK(err);
OCL_CHECK(
clBuildProgram(ocl_program, 0, nullptr, nullptr, nullptr, nullptr));
cl_kernel ocl_kernel = clCreateKernel(ocl_program, kernel_name, &err);
OCL_CHECK(err);
OCL_CHECK(clReleaseProgram(ocl_program));
return ocl_kernel;
}
void gpu_opencl_interop_tutorial() {
// [Initialize engine]
engine eng(engine::kind::gpu, 0);
// [Initialize engine]
// [Initialize stream]
dnnl::stream strm(eng);
// [Initialize stream]
// [memory alloc]
memory::dims tz_dims = {2, 3, 4, 5};
const size_t N = std::accumulate(tz_dims.begin(), tz_dims.end(), (size_t)1,
std::multiplies<size_t>());
memory::desc mem_d(
tz_dims, memory::data_type::f32, memory::format_tag::nchw);
memory mem(mem_d, eng);
// [memory alloc]
// [ocl kernel]
const char *ocl_code
= "__kernel void init(__global float *data) {"
" int id = get_global_id(0);"
" data[id] = (id % 2) ? -id : id;"
"}";
// [ocl kernel]
// [oclkernel create]
const char *kernel_name = "init";
cl_kernel ocl_init_kernel = create_init_opencl_kernel(
ocl_interop::get_context(eng), kernel_name, ocl_code);
// [oclkernel create]
// [oclexecution]
cl_mem ocl_buf = ocl_interop::get_mem_object(mem);
OCL_CHECK(clSetKernelArg(ocl_init_kernel, 0, sizeof(ocl_buf), &ocl_buf));
cl_command_queue ocl_queue = ocl_interop::get_command_queue(strm);
OCL_CHECK(clEnqueueNDRangeKernel(ocl_queue, ocl_init_kernel, 1, nullptr, &N,
nullptr, 0, nullptr, nullptr));
// [oclexecution]
// [relu creation]
auto relu_pd = eltwise_forward::primitive_desc(eng, prop_kind::forward,
algorithm::eltwise_relu, mem_d, mem_d, 0.0f);
auto relu = eltwise_forward(relu_pd);
// [relu creation]
// [relu exec]
relu.execute(strm, {{DNNL_ARG_SRC, mem}, {DNNL_ARG_DST, mem}});
strm.wait();
// [relu exec]
// [Check the results]
std::vector<float> mem_data(N);
read_from_dnnl_memory(mem_data.data(), mem);
for (size_t i = 0; i < N; i++) {
float expected = (i % 2) ? 0.0f : (float)i;
if (mem_data[i] != expected) {
std::cout << "Expect " << expected << " but got " << mem_data[i]
<< "." << std::endl;
throw std::logic_error("Accuracy check failed.");
}
}
// [Check the results]
OCL_CHECK(clReleaseKernel(ocl_init_kernel));
}
int main(int argc, char **argv) {
return handle_example_errors(
{engine::kind::gpu}, gpu_opencl_interop_tutorial);
}