gemm.h 2.4 KB
Newer Older
W
wangliu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
Z
zhaojiaying01 已提交
14 15 16

#pragma once

Z
zhaojiaying01 已提交
17 18 19 20
// 矩阵取值运算宏,假设矩阵按行存储
#define A(i, j) A[(i)*lda + (j)]
#define B(i, j) B[(i)*ldb + (j)]
#define C(i, j) C[(i)*ldc + (j)]
Z
zhaojiaying01 已提交
21 22

// 分块计算的块大小,mc 与 kc 分别对应分块计算时的 m 与 k
Z
zhaojiaying01 已提交
23 24 25
#define MC 128
#define KC 128
#define NC 1024
Z
zhaojiaying01 已提交
26 27 28
#define MR 4
#define NR 4

W
wangliu 已提交
29
#define s_min(i, j) ((i) < (j) ? (i) : (j))
Z
zhaojiaying01 已提交
30 31 32 33 34

namespace paddle_mobile {
namespace operators {
namespace math {

Z
zhaojiaying01 已提交
35
// 将 A 矩阵分块复制到连续内存(ColMajor)
Z
zhaojiaying01 已提交
36 37 38
void PackMatrixA(int m, int k, int paddingM, const float *A, int lda,
                 float *buffer);

Z
zhaojiaying01 已提交
39
// 将 B 矩阵分块复制到连续内存(ColMajor)
Z
zhaojiaying01 已提交
40 41 42
void PackMatrixB(int k, int n, int paddingN, const float *B, int ldb,
                 float *buffer);

Z
zhaojiaying01 已提交
43 44 45 46 47 48 49 50
// 将 A 矩阵分块复制到连续内存(RowMajor)
void PackMatrixA_(int m, int k, int paddingM, const float *A, int lda,
                  float *buffer);

// 将 B 矩阵分块复制到连续内存(RowMajor)
void PackMatrixB_(int k, int n, int paddingN, const float *B, int ldb,
                  float *buffer);

Z
zhaojiaying01 已提交
51
// 分块矩阵乘法
52 53 54
void InnerKernel(int m, int n, int k, float alpha, const float *A, int lda,
                 const float *B, int ldb, float beta, float *C, int ldc,
                 int first_time);
Z
zhaojiaying01 已提交
55 56

// 计算一个更小的 4 * 4 的 C 矩阵分块
57 58
void AddDot4x4(int k, float alpha, const float *A, int lda, const float *B,
               int ldb, float beta, float *C, int ldc, int mc, int nc);
Z
zhaojiaying01 已提交
59 60 61 62 63 64 65 66 67 68 69 70

// 32位 float 矩阵乘法
void sgemm(int m, int n, int k, float alpha, const float *A, int lda,
           const float *B, int ldb, float beta, float *C, int ldc);

// 64位 double 矩阵乘法
void dgemm(int m, int n, int k, float alpha, const double *A, int lda,
           const double *B, int ldb, float beta, double *C, int ldc);

}  // namespace math
}  // namespace operators
}  // namespace paddle_mobile