1 | /******************************************************************************* |
2 | * Copyright 2019-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 CPU_X64_GEMM_GEMM_THREADING_HPP |
18 | #define CPU_X64_GEMM_GEMM_THREADING_HPP |
19 | |
20 | #include <cstdint> |
21 | |
22 | #include "common/c_types_map.hpp" |
23 | |
24 | #include "cpu/x64/gemm/gemm_partition.hpp" |
25 | |
26 | namespace dnnl { |
27 | namespace impl { |
28 | namespace cpu { |
29 | namespace x64 { |
30 | |
31 | enum class partition_type { row_1d, col_1d, col_major_2d, mnk_3d }; |
32 | |
33 | enum class copy_type { |
34 | nonshared, |
35 | shared_a, |
36 | no_copy, |
37 | }; |
38 | |
39 | struct gemm_slice_t { |
40 | dim_t off_m, off_n, off_k; |
41 | dim_t m, n, k; |
42 | int ithr_m, ithr_n, ithr_k; |
43 | }; |
44 | |
45 | struct gemm_threading_t { |
46 | gemm_threading_t() {}; |
47 | |
48 | int nthrs_m, nthrs_n, nthrs_k; |
49 | dim_t block_m, block_n, block_k; // Blocking sizes (-1 = default) |
50 | dim_t thread_m, thread_n, thread_k; // Thread matrix sizes (-1 = default) |
51 | partition_type partition; |
52 | copy_type copy; |
53 | |
54 | int nthrs() const { return nthrs_m * nthrs_n * nthrs_k; } |
55 | |
56 | friend bool operator==( |
57 | const gemm_threading_t &t1, const gemm_threading_t &t2) { |
58 | return (t1.nthrs_m == t2.nthrs_m && t1.nthrs_n == t2.nthrs_n |
59 | && t1.nthrs_k == t2.nthrs_k && t1.partition == t2.partition |
60 | && t1.copy == t2.copy); |
61 | } |
62 | |
63 | friend bool operator!=( |
64 | const gemm_threading_t &t1, const gemm_threading_t &t2) { |
65 | return !(t1 == t2); |
66 | } |
67 | |
68 | gemm_slice_t get_thread_slice(int ithr, dim_t m, dim_t n, dim_t k) const { |
69 | |
70 | dim_t off_m = 0, off_n = 0, off_k = 0; |
71 | dim_t size_m = m, size_n = n, size_k = k; |
72 | int ithr_m = 0, ithr_n = 0, ithr_k = 0; |
73 | |
74 | switch (partition) { |
75 | case partition_type::row_1d: |
76 | ithr_m = ithr; |
77 | partition_1d(ithr, nthrs(), m, off_m, size_m); |
78 | break; |
79 | |
80 | case partition_type::col_1d: |
81 | ithr_n = ithr; |
82 | partition_1d(ithr, nthrs(), n, off_n, size_n); |
83 | break; |
84 | |
85 | case partition_type::col_major_2d: { |
86 | int nthr_eff = nthrs(); |
87 | ithr_m = ithr % nthrs_m; |
88 | ithr_n = ithr / nthrs_m; |
89 | |
90 | partition_2d(ithr, &nthr_eff, ithr_m, ithr_n, nthrs_m, nthrs_n, |
91 | m, n, off_m, size_m, off_n, size_n); |
92 | break; |
93 | } |
94 | |
95 | case partition_type::mnk_3d: { |
96 | assert(thread_m > 0 && thread_n > 0 && thread_k > 0); |
97 | ithr_m = ithr % nthrs_m; |
98 | ithr_n = (ithr / nthrs_m) % nthrs_n; |
99 | ithr_k = (ithr / nthrs_m) / nthrs_n; |
100 | |
101 | off_m = ithr_m * thread_m; |
102 | off_n = ithr_n * thread_n; |
103 | off_k = ithr_k * thread_k; |
104 | |
105 | size_m = nstl::min(thread_m, m - off_m); |
106 | size_n = nstl::min(thread_n, n - off_n); |
107 | size_k = nstl::min(thread_k, k - off_k); |
108 | break; |
109 | } |
110 | } |
111 | |
112 | return {off_m, off_n, off_k, size_m, size_n, size_k, ithr_m, ithr_n, |
113 | ithr_k}; |
114 | } |
115 | |
116 | int thr_k_stride() const { return nthrs_m * nthrs_n; } |
117 | }; |
118 | |
119 | } // namespace x64 |
120 | } // namespace cpu |
121 | } // namespace impl |
122 | } // namespace dnnl |
123 | |
124 | #endif |
125 | |