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 <assert.h> |
18 | |
19 | #include "common/memory.hpp" |
20 | #include "common/type_helpers.hpp" |
21 | |
22 | #include "cpu/cpu_engine.hpp" |
23 | #include "cpu/cpu_memory_storage.hpp" |
24 | #include "cpu/cpu_stream.hpp" |
25 | |
26 | namespace dnnl { |
27 | namespace impl { |
28 | namespace cpu { |
29 | |
30 | status_t cpu_engine_t::create_memory_storage( |
31 | memory_storage_t **storage, unsigned flags, size_t size, void *handle) { |
32 | auto _storage = new cpu_memory_storage_t(this); |
33 | if (_storage == nullptr) return status::out_of_memory; |
34 | status_t status = _storage->init(flags, size, handle); |
35 | if (status != status::success) { |
36 | delete _storage; |
37 | return status; |
38 | } |
39 | *storage = _storage; |
40 | return status::success; |
41 | } |
42 | |
43 | status_t cpu_engine_t::create_stream(stream_t **stream, unsigned flags) { |
44 | return safe_ptr_assign(*stream, new cpu_stream_t(this, flags)); |
45 | } |
46 | |
47 | #if DNNL_CPU_RUNTIME == DNNL_RUNTIME_THREADPOOL |
48 | status_t cpu_engine_t::create_stream(stream_t **stream, |
49 | dnnl::threadpool_interop::threadpool_iface *threadpool) { |
50 | return safe_ptr_assign<stream_t>( |
51 | *stream, new cpu_stream_t(this, threadpool)); |
52 | } |
53 | #endif |
54 | |
55 | engine_t *get_service_engine() { |
56 | static std::unique_ptr<engine_t, engine_deleter_t> cpu_engine; |
57 | static std::once_flag initialized; |
58 | std::call_once(initialized, [&]() { |
59 | engine_t *cpu_engine_ptr; |
60 | cpu::cpu_engine_factory_t f; |
61 | auto status = f.engine_create(&cpu_engine_ptr, 0); |
62 | assert(status == status::success); |
63 | MAYBE_UNUSED(status); |
64 | cpu_engine.reset(cpu_engine_ptr); |
65 | }); |
66 | return cpu_engine.get(); |
67 | } |
68 | |
69 | } // namespace cpu |
70 | } // namespace impl |
71 | } // namespace dnnl |
72 | |
73 | // vim: et ts=4 sw=4 cindent cino+=l0,\:4,N-s |
74 | |