1/*******************************************************************************
2* Copyright 2016-2022 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#include <memory>
18
19#include "oneapi/dnnl/dnnl.h"
20
21#include "c_types_map.hpp"
22#include "engine.hpp"
23#include "memory.hpp"
24#include "nstl.hpp"
25#include "primitive.hpp"
26#include "utils.hpp"
27
28#if DNNL_CPU_RUNTIME != DNNL_RUNTIME_NONE
29#include "cpu/cpu_engine.hpp"
30#endif
31
32#if DNNL_GPU_RUNTIME == DNNL_RUNTIME_OCL
33#include "gpu/ocl/ocl_engine.hpp"
34#endif
35
36#ifdef DNNL_WITH_SYCL
37#include "sycl/sycl_engine.hpp"
38#endif
39
40namespace dnnl {
41namespace impl {
42
43static inline std::unique_ptr<engine_factory_t> get_engine_factory(
44 engine_kind_t kind, runtime_kind_t runtime_kind) {
45
46#if DNNL_CPU_RUNTIME != DNNL_RUNTIME_NONE
47 if (kind == engine_kind::cpu && is_native_runtime(runtime_kind)) {
48 return std::unique_ptr<engine_factory_t>(
49 new cpu::cpu_engine_factory_t());
50 }
51#endif
52
53#if DNNL_GPU_RUNTIME == DNNL_RUNTIME_OCL
54 if (kind == engine_kind::gpu && runtime_kind == runtime_kind::ocl) {
55 return std::unique_ptr<engine_factory_t>(
56 new gpu::ocl::ocl_engine_factory_t(kind));
57 }
58#endif
59#ifdef DNNL_WITH_SYCL
60 if (runtime_kind == runtime_kind::sycl)
61 return sycl::get_engine_factory(kind);
62#endif
63 return nullptr;
64}
65
66} // namespace impl
67} // namespace dnnl
68
69using namespace dnnl::impl::status;
70using namespace dnnl::impl::utils;
71
72using dnnl::impl::engine_kind_t;
73using dnnl::impl::engine_t;
74using dnnl::impl::status_t;
75
76size_t dnnl_engine_get_count(engine_kind_t kind) {
77 using namespace dnnl::impl;
78 auto ef = get_engine_factory(kind, get_default_runtime(kind));
79 return ef != nullptr ? ef->count() : 0;
80}
81
82status_t dnnl_engine_create(
83 engine_t **engine, engine_kind_t kind, size_t index) {
84 using namespace dnnl::impl;
85 if (engine == nullptr) return invalid_arguments;
86
87 auto ef = get_engine_factory(kind, get_default_runtime(kind));
88 if (ef == nullptr || index >= ef->count()) return invalid_arguments;
89
90 return ef->engine_create(engine, index);
91}
92
93status_t dnnl_engine_get_kind(engine_t *engine, engine_kind_t *kind) {
94 using namespace dnnl::impl;
95 if (engine == nullptr) return invalid_arguments;
96 *kind = engine->kind();
97 return success;
98}
99
100status_t dnnl_engine_destroy(engine_t *engine) {
101 if (engine != nullptr) engine->release();
102 return success;
103}
104
105// vim: et ts=4 sw=4 cindent cino+=l0,\:4,N-s
106