1/*******************************************************************************
2* Copyright 2020-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#ifndef GPU_GPU_RESOURCE_HPP
18#define GPU_GPU_RESOURCE_HPP
19
20#include "oneapi/dnnl/dnnl.h"
21
22#include "common/c_types_map.hpp"
23#include "common/primitive.hpp"
24#include "common/resource.hpp"
25#include "gpu/compute/compute.hpp"
26
27namespace dnnl {
28namespace impl {
29namespace gpu {
30
31struct gpu_resource_t : public resource_t {
32 using key_kernel_t = compute::kernel_t::id_t;
33 using mapped_kernel_t = compute::kernel_t;
34
35 using key_memory_t = int;
36 using mapped_memory_t = std::unique_ptr<memory_storage_t>;
37
38 gpu_resource_t() = default;
39
40 status_t add_kernel(compute::kernel_t::id_t kernel_id,
41 const compute::kernel_t &kernel) {
42 if (!kernel) return status::success;
43 assert(kernel_id_to_kernel_.count(kernel_id) == 0);
44 kernel_id_to_kernel_.emplace(kernel_id, kernel);
45 return status::success;
46 }
47
48 const compute::kernel_t &get_kernel(key_kernel_t id) const {
49 assert(kernel_id_to_kernel_.count(id));
50 const auto &kernel = kernel_id_to_kernel_.at(id);
51 assert(kernel);
52 return kernel;
53 }
54
55 void add_memory_storage(key_memory_t idx, mapped_memory_t &&m) {
56 assert(idx_to_memory_storage_.count(idx) == 0);
57 if (!m) return;
58 idx_to_memory_storage_.emplace(idx, std::move(m));
59 }
60
61 const memory_storage_t *get_memory_storage(int idx) const {
62 assert(idx_to_memory_storage_.count(idx) != 0);
63 return idx_to_memory_storage_.at(idx).get();
64 }
65
66 DNNL_DISALLOW_COPY_AND_ASSIGN(gpu_resource_t);
67
68private:
69 std::unordered_map<key_kernel_t, mapped_kernel_t> kernel_id_to_kernel_;
70 std::unordered_map<key_memory_t, mapped_memory_t> idx_to_memory_storage_;
71};
72
73} // namespace gpu
74} // namespace impl
75} // namespace dnnl
76
77#endif
78