• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 // compute.h: the central stage of the Gemm computation, operates
16 // on already-packed LHS and RHS blocks and calls the Gemm kernel
17 // to compute a block of the product.
18 
19 #ifndef GEMMLOWP_INTERNAL_COMPUTE_H_
20 #define GEMMLOWP_INTERNAL_COMPUTE_H_
21 
22 #include "block_params.h"
23 #include "kernel.h"
24 #include "pack.h"
25 
26 namespace gemmlowp {
27 
28 template <typename PackedLhs, typename PackedRhs, typename PackedResult>
29 class ComputeImpl {
30   typedef typename PackedLhs::KernelSideFormat KernelLhsFormat;
31   typedef typename PackedRhs::KernelSideFormat KernelRhsFormat;
32   typedef KernelFormat<KernelLhsFormat, KernelRhsFormat> Format;
33 
34   const KernelBase& kernel_;
35   const BlockParams& block_params_;
36 
37   PackedResult* const packed_result_;
38   const PackedLhs& packed_lhs_;
39   const PackedRhs& packed_rhs_;
40 
41  public:
ComputeImpl(const KernelBase & _kernel,const BlockParams & _block_params,PackedResult * _packed_result,const PackedLhs & _packed_lhs,const PackedRhs & _packed_rhs)42   ComputeImpl(const KernelBase& _kernel, const BlockParams& _block_params,
43               PackedResult* _packed_result, const PackedLhs& _packed_lhs,
44               const PackedRhs& _packed_rhs)
45       : kernel_(_kernel),
46         block_params_(_block_params),
47         packed_result_(_packed_result),
48         packed_lhs_(_packed_lhs),
49         packed_rhs_(_packed_rhs) {}
50 
Compute()51   void Compute() {
52     for (int d = 0; d < block_params_.l2_depth; d += block_params_.l1_depth) {
53       int ds = std::min(block_params_.l1_depth, block_params_.l2_depth - d);
54 
55       for (int r = 0; r < block_params_.l2_rows; r += block_params_.l1_rows) {
56         int rs = std::min(block_params_.l1_rows, block_params_.l2_rows - r);
57 
58         ComputeL1(r, rs, 0, block_params_.l2_cols, d, ds);
59       }
60     }
61   }
62 
63  private:
ComputeRun(int start_row,int start_col,int start_depth,int depth)64   void ComputeRun(int start_row, int start_col, int start_depth,
65                   int depth) GEMMLOWP_NOINLINE {
66     packed_lhs_.seek_run(start_row, start_depth);
67     packed_rhs_.seek_run(start_col, start_depth);
68     auto packed_result_block = packed_result_->Map().block(
69         start_row, start_col, Format::kRows, Format::kCols);
70     kernel_.Run(packed_result_block.data(), packed_result_block.rows_stride(),
71                 packed_result_block.cols_stride(), packed_lhs_.current_data(),
72                 packed_rhs_.current_data(), start_depth, depth);
73   }
74 
ComputeL1(int start_row,int rows,int start_col,int cols,int start_depth,int depth)75   void ComputeL1(int start_row, int rows, int start_col, int cols,
76                  int start_depth, int depth) {
77     assert(rows % Format::kRows == 0);
78     assert(cols % Format::kCols == 0);
79     assert(depth % Format::kDepth == 0);
80 
81     for (int c = 0; c < cols; c += Format::kCols) {
82       for (int r = 0; r < rows; r += Format::kRows) {
83         ComputeRun(start_row + r, start_col + c, start_depth, depth);
84       }
85     }
86   }
87 };
88 
89 template <typename PackedLhs, typename PackedRhs, typename PackedResult>
Compute(const KernelBase & kernel,const BlockParams & block_params,PackedResult * packed_result,const PackedLhs & packed_lhs,const PackedRhs & packed_rhs)90 void Compute(const KernelBase& kernel, const BlockParams& block_params,
91              PackedResult* packed_result, const PackedLhs& packed_lhs,
92              const PackedRhs& packed_rhs) {
93   ScopedProfilingLabel label("compute");
94   ComputeImpl<PackedLhs, PackedRhs, PackedResult> impl(
95       kernel, block_params, packed_result, packed_lhs, packed_rhs);
96 
97   impl.Compute();
98 }
99 
100 }  // namespace gemmlowp
101 
102 #endif  // GEMMLOWP_INTERNAL_COMPUTE_H_
103