Files
ceres-solver/internal/ceres/small_blas.h
T
yangfan 81f413b720 Optimization for custom small blas multiplication with dynamic
template parameters in C level.

- unroll for loops
- matrix access more cache coherent
- platform independant

Briefly, this commit brings 1~50% performance improvments for
most cases in small_blas_gem(m/v)_benchmark, but a small drop
for corner cases with small dimensions especially 1,2,3. Here
we list the results partially, which show decrease percentage
of executing time, compared to unoptimized version.

Platform: desktop PC (i7-7700 CPU MP8@3.60GHz + ubuntu 17.10)
(Lenovo Research Device+ Lab, <yangfan34@lenovo.com>)

Benchmark                                   Time        CPU
-----------------------------------------------------------
BM_MatrixMatrixMultiplyDynamic/1/1/1     -0.0850    -0.0851
BM_MatrixMatrixMultiplyDynamic/1/1/2     -0.1444    -0.1446
BM_MatrixMatrixMultiplyDynamic/1/1/3     -0.1934    -0.1935
BM_MatrixMatrixMultiplyDynamic/1/1/4     -0.2933    -0.2934
BM_MatrixMatrixMultiplyDynamic/1/1/8     -0.1579    -0.1580
BM_MatrixMatrixMultiplyDynamic/1/1/12    -0.1556    -0.1558
BM_MatrixMatrixMultiplyDynamic/1/1/15    -0.1598    -0.1599
BM_MatrixMatrixMultiplyDynamic/1/2/1     -0.0797    -0.0799
BM_MatrixMatrixMultiplyDynamic/1/2/2     -0.2950    -0.2951
BM_MatrixMatrixMultiplyDynamic/1/2/3     -0.1363    -0.1364
BM_MatrixMatrixMultiplyDynamic/1/2/4     -0.2435    -0.2437
BM_MatrixMatrixMultiplyDynamic/1/2/8     -0.2299    -0.2300
BM_MatrixMatrixMultiplyDynamic/1/2/12    -0.2441    -0.2442
BM_MatrixMatrixMultiplyDynamic/1/2/15    -0.1671    -0.1673
BM_MatrixMatrixMultiplyDynamic/1/3/1     -0.0774    -0.0775
BM_MatrixMatrixMultiplyDynamic/1/3/2     -0.2761    -0.2762
BM_MatrixMatrixMultiplyDynamic/1/3/3     -0.0840    -0.0841
BM_MatrixMatrixMultiplyDynamic/1/3/4     -0.2027    -0.2028
BM_MatrixMatrixMultiplyDynamic/1/3/8     -0.2481    -0.2482
BM_MatrixMatrixMultiplyDynamic/1/3/12    -0.2629    -0.2630
BM_MatrixMatrixMultiplyDynamic/1/3/15    -0.1958    -0.1959
BM_MatrixMatrixMultiplyDynamic/1/4/1     -0.1260    -0.1261
BM_MatrixMatrixMultiplyDynamic/1/4/2     -0.1834    -0.1835
BM_MatrixMatrixMultiplyDynamic/1/4/3     -0.1379    -0.1380
BM_MatrixMatrixMultiplyDynamic/1/4/4     -0.2636    -0.2637
BM_MatrixMatrixMultiplyDynamic/1/4/8     -0.2838    -0.2839
BM_MatrixMatrixMultiplyDynamic/1/4/12    -0.3320    -0.3321
BM_MatrixMatrixMultiplyDynamic/1/4/15    -0.2464    -0.2465
BM_MatrixMatrixMultiplyDynamic/1/8/1     -0.0766    -0.0767
BM_MatrixMatrixMultiplyDynamic/1/8/2     -0.1713    -0.1714
BM_MatrixMatrixMultiplyDynamic/1/8/3     -0.1158    -0.1159
BM_MatrixMatrixMultiplyDynamic/1/8/4     -0.3205    -0.3206
BM_MatrixMatrixMultiplyDynamic/1/8/8     -0.3514    -0.3515
BM_MatrixMatrixMultiplyDynamic/1/8/12    -0.3658    -0.3658
BM_MatrixMatrixMultiplyDynamic/1/8/15    -0.3187    -0.3188
BM_MatrixMatrixMultiplyDynamic/1/12/1    -0.0424    -0.0425
BM_MatrixMatrixMultiplyDynamic/1/12/2    -0.1800    -0.1800
BM_MatrixMatrixMultiplyDynamic/1/12/3    -0.1457    -0.1457
BM_MatrixMatrixMultiplyDynamic/1/12/4    -0.3768    -0.3769
BM_MatrixMatrixMultiplyDynamic/1/12/8    -0.4072    -0.4073
BM_MatrixMatrixMultiplyDynamic/1/12/12   -0.4391    -0.4392
BM_MatrixMatrixMultiplyDynamic/1/12/15   -0.3383    -0.3383
BM_MatrixMatrixMultiplyDynamic/1/15/1    -0.0442    -0.0443
BM_MatrixMatrixMultiplyDynamic/1/15/2    -0.2378    -0.2379
BM_MatrixMatrixMultiplyDynamic/1/15/3    -0.1553    -0.1554
BM_MatrixMatrixMultiplyDynamic/1/15/4    -0.3954    -0.3955
BM_MatrixMatrixMultiplyDynamic/1/15/8    -0.4334    -0.4335
BM_MatrixMatrixMultiplyDynamic/1/15/12   -0.4175    -0.4175
BM_MatrixMatrixMultiplyDynamic/1/15/15   -0.3242    -0.3243

BM_MatrixVectorMultiply/1/1              +0.1613    +0.1613
BM_MatrixVectorMultiply/1/2              +0.1715    +0.1715
BM_MatrixVectorMultiply/1/3              +0.1051    +0.1051
BM_MatrixVectorMultiply/1/4              +0.1369    +0.1369
BM_MatrixVectorMultiply/1/8              +0.1180    +0.1180
BM_MatrixVectorMultiply/1/12             +0.0869    +0.0869
BM_MatrixVectorMultiply/1/15             +0.1887    +0.1886
BM_MatrixVectorMultiply/2/1              +0.1152    +0.1152
BM_MatrixVectorMultiply/2/2              +0.1520    +0.1520
BM_MatrixVectorMultiply/2/3              +0.1867    +0.1867
BM_MatrixVectorMultiply/2/4              +0.0173    +0.0173
BM_MatrixVectorMultiply/2/8              -0.0528    -0.0528
BM_MatrixVectorMultiply/2/12             -0.0176    -0.0176
BM_MatrixVectorMultiply/2/15             -0.0753    -0.0753
BM_MatrixVectorMultiply/3/1              +0.0844    +0.0844
BM_MatrixVectorMultiply/3/2              +0.0750    +0.0750
BM_MatrixVectorMultiply/3/3              -0.0153    -0.0153
BM_MatrixVectorMultiply/3/4              +0.0060    +0.0060
BM_MatrixVectorMultiply/3/8              +0.0152    +0.0152
BM_MatrixVectorMultiply/3/12             +0.0101    +0.0101
BM_MatrixVectorMultiply/3/15             -0.0795    -0.0795
BM_MatrixVectorMultiply/4/1              -0.1425    -0.1425
BM_MatrixVectorMultiply/4/2              -0.0869    -0.0869
BM_MatrixVectorMultiply/4/3              -0.1371    -0.1371
BM_MatrixVectorMultiply/4/4              -0.0088    -0.0088
BM_MatrixVectorMultiply/4/8              -0.1049    -0.1049
BM_MatrixVectorMultiply/4/12             -0.2566    -0.2566
BM_MatrixVectorMultiply/4/15             -0.2940    -0.2940
BM_MatrixVectorMultiply/6/1              -0.1798    -0.1798
BM_MatrixVectorMultiply/6/2              -0.0627    -0.0627
BM_MatrixVectorMultiply/6/3              -0.0389    -0.0389
BM_MatrixVectorMultiply/6/4              -0.1088    -0.1088
BM_MatrixVectorMultiply/6/8              -0.1815    -0.1815
BM_MatrixVectorMultiply/6/12             -0.1650    -0.1650
BM_MatrixVectorMultiply/6/15             -0.1855    -0.1855
BM_MatrixVectorMultiply/8/1              -0.1630    -0.1630
BM_MatrixVectorMultiply/8/2              -0.1248    -0.1248
BM_MatrixVectorMultiply/8/3              -0.1911    -0.1911
BM_MatrixVectorMultiply/8/4              -0.1996    -0.1996
BM_MatrixVectorMultiply/8/8              -0.2590    -0.2590
BM_MatrixVectorMultiply/8/12             -0.3266    -0.3266
BM_MatrixVectorMultiply/8/15             -0.3999    -0.3999
BM_MatrixTransposeVectorMultiply/1/1     -0.0234    -0.0234
BM_MatrixTransposeVectorMultiply/1/2     -0.0243    -0.0243
BM_MatrixTransposeVectorMultiply/1/3     -0.1324    -0.1324
BM_MatrixTransposeVectorMultiply/1/4     -0.2635    -0.2635
BM_MatrixTransposeVectorMultiply/1/8     -0.2461    -0.2461
BM_MatrixTransposeVectorMultiply/1/12    -0.2702    -0.2702
BM_MatrixTransposeVectorMultiply/1/15    -0.2538    -0.2538
BM_MatrixTransposeVectorMultiply/2/1     -0.0170    -0.0170
BM_MatrixTransposeVectorMultiply/2/2     -0.1475    -0.1475
BM_MatrixTransposeVectorMultiply/2/3     -0.1082    -0.1082
BM_MatrixTransposeVectorMultiply/2/4     -0.2594    -0.2595
BM_MatrixTransposeVectorMultiply/2/8     -0.2710    -0.2710
BM_MatrixTransposeVectorMultiply/2/12    -0.3053    -0.3053
BM_MatrixTransposeVectorMultiply/2/15    -0.2706    -0.2706
BM_MatrixTransposeVectorMultiply/3/1     -0.0096    -0.0096
BM_MatrixTransposeVectorMultiply/3/2     -0.2885    -0.2886
BM_MatrixTransposeVectorMultiply/3/3     -0.0790    -0.0790
BM_MatrixTransposeVectorMultiply/3/4     -0.2329    -0.2330
BM_MatrixTransposeVectorMultiply/3/8     -0.2742    -0.2742
BM_MatrixTransposeVectorMultiply/3/12    -0.3177    -0.3177
BM_MatrixTransposeVectorMultiply/3/15    -0.2610    -0.2610
BM_MatrixTransposeVectorMultiply/4/1     -0.0024    -0.0024
BM_MatrixTransposeVectorMultiply/4/2     -0.1578    -0.1578
BM_MatrixTransposeVectorMultiply/4/3     -0.0918    -0.0918
BM_MatrixTransposeVectorMultiply/4/4     -0.2570    -0.2570
BM_MatrixTransposeVectorMultiply/4/8     -0.3064    -0.3064
BM_MatrixTransposeVectorMultiply/4/12    -0.3316    -0.3316
BM_MatrixTransposeVectorMultiply/4/15    -0.2794    -0.2794
BM_MatrixTransposeVectorMultiply/6/1     -0.0484    -0.0484
BM_MatrixTransposeVectorMultiply/6/2     -0.1102    -0.1102
BM_MatrixTransposeVectorMultiply/6/3     -0.1188    -0.1188
BM_MatrixTransposeVectorMultiply/6/4     -0.2967    -0.2967
BM_MatrixTransposeVectorMultiply/6/8     -0.3190    -0.3190
BM_MatrixTransposeVectorMultiply/6/12    -0.3441    -0.3441
BM_MatrixTransposeVectorMultiply/6/15    -0.2723    -0.2723
BM_MatrixTransposeVectorMultiply/8/1     -0.0397    -0.0397
BM_MatrixTransposeVectorMultiply/8/2     -0.1453    -0.1453
BM_MatrixTransposeVectorMultiply/8/3     -0.1337    -0.1337
BM_MatrixTransposeVectorMultiply/8/4     -0.3084    -0.3084
BM_MatrixTransposeVectorMultiply/8/8     -0.3444    -0.3444
BM_MatrixTransposeVectorMultiply/8/12    -0.3717    -0.3717
BM_MatrixTransposeVectorMultiply/8/15    -0.3440    -0.3440

Change-Id: I17de05bf94699a07eea880b92a6d08daf1f038bb
2018-04-10 14:42:23 +08:00

556 lines
19 KiB
C++

// Ceres Solver - A fast non-linear least squares minimizer
// Copyright 2015 Google Inc. All rights reserved.
// http://ceres-solver.org/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// * Neither the name of Google Inc. nor the names of its contributors may be
// used to endorse or promote products derived from this software without
// specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
// Author: sameeragarwal@google.com (Sameer Agarwal)
//
// Simple blas functions for use in the Schur Eliminator. These are
// fairly basic implementations which already yield a significant
// speedup in the eliminator performance.
#ifndef CERES_INTERNAL_SMALL_BLAS_H_
#define CERES_INTERNAL_SMALL_BLAS_H_
#include "ceres/internal/port.h"
#include "ceres/internal/eigen.h"
#include "glog/logging.h"
#include "small_blas_generic.h"
namespace ceres {
namespace internal {
// The following three macros are used to share code and reduce
// template junk across the various GEMM variants.
#define CERES_GEMM_BEGIN(name) \
template<int kRowA, int kColA, int kRowB, int kColB, int kOperation> \
inline void name(const double* A, \
const int num_row_a, \
const int num_col_a, \
const double* B, \
const int num_row_b, \
const int num_col_b, \
double* C, \
const int start_row_c, \
const int start_col_c, \
const int row_stride_c, \
const int col_stride_c)
#define CERES_GEMM_NAIVE_HEADER \
DCHECK_GT(num_row_a, 0); \
DCHECK_GT(num_col_a, 0); \
DCHECK_GT(num_row_b, 0); \
DCHECK_GT(num_col_b, 0); \
DCHECK_GE(start_row_c, 0); \
DCHECK_GE(start_col_c, 0); \
DCHECK_GT(row_stride_c, 0); \
DCHECK_GT(col_stride_c, 0); \
DCHECK((kRowA == Eigen::Dynamic) || (kRowA == num_row_a)); \
DCHECK((kColA == Eigen::Dynamic) || (kColA == num_col_a)); \
DCHECK((kRowB == Eigen::Dynamic) || (kRowB == num_row_b)); \
DCHECK((kColB == Eigen::Dynamic) || (kColB == num_col_b)); \
const int NUM_ROW_A = (kRowA != Eigen::Dynamic ? kRowA : num_row_a); \
const int NUM_COL_A = (kColA != Eigen::Dynamic ? kColA : num_col_a); \
const int NUM_ROW_B = (kRowB != Eigen::Dynamic ? kRowB : num_row_b); \
const int NUM_COL_B = (kColB != Eigen::Dynamic ? kColB : num_col_b);
#define CERES_GEMM_EIGEN_HEADER \
const typename EigenTypes<kRowA, kColA>::ConstMatrixRef \
Aref(A, num_row_a, num_col_a); \
const typename EigenTypes<kRowB, kColB>::ConstMatrixRef \
Bref(B, num_row_b, num_col_b); \
MatrixRef Cref(C, row_stride_c, col_stride_c); \
#define CERES_CALL_GEMM(name) \
name<kRowA, kColA, kRowB, kColB, kOperation>( \
A, num_row_a, num_col_a, \
B, num_row_b, num_col_b, \
C, start_row_c, start_col_c, row_stride_c, col_stride_c);
#define CERES_GEMM_STORE_SINGLE(p, index, value) \
if (kOperation > 0) { \
p[index] += value; \
} else if (kOperation < 0) { \
p[index] -= value; \
} else { \
p[index] = value; \
}
#define CERES_GEMM_STORE_PAIR(p, index, v1, v2) \
if (kOperation > 0) { \
p[index] += v1; \
p[index + 1] += v2; \
} else if (kOperation < 0) { \
p[index] -= v1; \
p[index + 1] -= v2; \
} else { \
p[index] = v1; \
p[index + 1] = v2; \
}
// For the matrix-matrix functions below, there are three variants for
// each functionality. Foo, FooNaive and FooEigen. Foo is the one to
// be called by the user. FooNaive is a basic loop based
// implementation and FooEigen uses Eigen's implementation. Foo
// chooses between FooNaive and FooEigen depending on how many of the
// template arguments are fixed at compile time. Currently, FooEigen
// is called if all matrix dimensions are compile time
// constants. FooNaive is called otherwise. This leads to the best
// performance currently.
//
// The MatrixMatrixMultiply variants compute:
//
// C op A * B;
//
// The MatrixTransposeMatrixMultiply variants compute:
//
// C op A' * B
//
// where op can be +=, -=, or =.
//
// The template parameters (kRowA, kColA, kRowB, kColB) allow
// specialization of the loop at compile time. If this information is
// not available, then Eigen::Dynamic should be used as the template
// argument.
//
// kOperation = 1 -> C += A * B
// kOperation = -1 -> C -= A * B
// kOperation = 0 -> C = A * B
//
// The functions can write into matrices C which are larger than the
// matrix A * B. This is done by specifying the true size of C via
// row_stride_c and col_stride_c, and then indicating where A * B
// should be written into by start_row_c and start_col_c.
//
// Graphically if row_stride_c = 10, col_stride_c = 12, start_row_c =
// 4 and start_col_c = 5, then if A = 3x2 and B = 2x4, we get
//
// ------------
// ------------
// ------------
// ------------
// -----xxxx---
// -----xxxx---
// -----xxxx---
// ------------
// ------------
// ------------
//
CERES_GEMM_BEGIN(MatrixMatrixMultiplyEigen) {
CERES_GEMM_EIGEN_HEADER
Eigen::Block<MatrixRef, kRowA, kColB>
block(Cref, start_row_c, start_col_c, num_row_a, num_col_b);
if (kOperation > 0) {
block.noalias() += Aref * Bref;
} else if (kOperation < 0) {
block.noalias() -= Aref * Bref;
} else {
block.noalias() = Aref * Bref;
}
}
CERES_GEMM_BEGIN(MatrixMatrixMultiplyNaive) {
CERES_GEMM_NAIVE_HEADER
DCHECK_EQ(NUM_COL_A, NUM_ROW_B);
const int NUM_ROW_C = NUM_ROW_A;
const int NUM_COL_C = NUM_COL_B;
DCHECK_LE(start_row_c + NUM_ROW_C, row_stride_c);
DCHECK_LE(start_col_c + NUM_COL_C, col_stride_c);
const int span = 4;
// Calculate the remainder part first.
// Process the last odd column if present.
if (NUM_COL_C & 1) {
int col = NUM_COL_C - 1;
const double* pa = &A[0];
for (int row = 0; row < NUM_ROW_C; ++row, pa += NUM_COL_A) {
const double* pb = &B[col];
double tmp = 0.0;
for (int k = 0; k < NUM_COL_A; ++k, pb += NUM_COL_B) {
tmp += pa[k] * pb[0];
}
const int index = (row + start_row_c) * col_stride_c + start_col_c + col;
CERES_GEMM_STORE_SINGLE(C, index, tmp);
}
// Return directly for efficiency of extremely small matrix multiply.
if (NUM_COL_C == 1) {
return;
}
}
// Process the couple columns in remainder if present.
if (NUM_COL_C & 2) {
int col = NUM_COL_C & (int)(~(span - 1)) ;
const double* pa = &A[0];
for (int row = 0; row < NUM_ROW_C; ++row, pa += NUM_COL_A) {
const double* pb = &B[col];
double tmp1 = 0.0, tmp2 = 0.0;
for (int k = 0; k < NUM_COL_A; ++k, pb += NUM_COL_B) {
double av = pa[k];
tmp1 += av * pb[0];
tmp2 += av * pb[1];
}
const int index = (row + start_row_c) * col_stride_c + start_col_c + col;
CERES_GEMM_STORE_PAIR(C, index, tmp1, tmp2);
}
// Return directly for efficiency of extremely small matrix multiply.
if (NUM_COL_C < span) {
return;
}
}
// Calculate the main part with multiples of 4.
int col_m = NUM_COL_C & (int)(~(span - 1));
for (int col = 0; col < col_m; col += span) {
for (int row = 0; row < NUM_ROW_C; ++row) {
const int index = (row + start_row_c) * col_stride_c + start_col_c + col;
MMM_mat1x4(NUM_COL_A, &A[row * NUM_COL_A],
&B[col], NUM_COL_B, &C[index], kOperation);
}
}
}
CERES_GEMM_BEGIN(MatrixMatrixMultiply) {
#ifdef CERES_NO_CUSTOM_BLAS
CERES_CALL_GEMM(MatrixMatrixMultiplyEigen)
return;
#else
if (kRowA != Eigen::Dynamic && kColA != Eigen::Dynamic &&
kRowB != Eigen::Dynamic && kColB != Eigen::Dynamic) {
CERES_CALL_GEMM(MatrixMatrixMultiplyEigen)
} else {
CERES_CALL_GEMM(MatrixMatrixMultiplyNaive)
}
#endif
}
CERES_GEMM_BEGIN(MatrixTransposeMatrixMultiplyEigen) {
CERES_GEMM_EIGEN_HEADER
Eigen::Block<MatrixRef, kColA, kColB> block(Cref,
start_row_c, start_col_c,
num_col_a, num_col_b);
if (kOperation > 0) {
block.noalias() += Aref.transpose() * Bref;
} else if (kOperation < 0) {
block.noalias() -= Aref.transpose() * Bref;
} else {
block.noalias() = Aref.transpose() * Bref;
}
}
CERES_GEMM_BEGIN(MatrixTransposeMatrixMultiplyNaive) {
CERES_GEMM_NAIVE_HEADER
DCHECK_EQ(NUM_ROW_A, NUM_ROW_B);
const int NUM_ROW_C = NUM_COL_A;
const int NUM_COL_C = NUM_COL_B;
DCHECK_LE(start_row_c + NUM_ROW_C, row_stride_c);
DCHECK_LE(start_col_c + NUM_COL_C, col_stride_c);
const int span = 4;
// Process the remainder part first.
// Process the last odd column if present.
if (NUM_COL_C & 1) {
int col = NUM_COL_C - 1;
for (int row = 0; row < NUM_ROW_C; ++row) {
const double* pa = &A[row];
const double* pb = &B[col];
double tmp = 0.0;
for (int k = 0; k < NUM_ROW_A; ++k) {
tmp += pa[0] * pb[0];
pa += NUM_COL_A;
pb += NUM_COL_B;
}
const int index = (row + start_row_c) * col_stride_c + start_col_c + col;
CERES_GEMM_STORE_SINGLE(C, index, tmp);
}
// Return directly for efficiency of extremely small matrix multiply.
if (NUM_COL_C == 1) {
return;
}
}
// Process the couple columns in remainder if present.
if (NUM_COL_C & 2) {
int col = NUM_COL_C & (int)(~(span - 1)) ;
for (int row = 0; row < NUM_ROW_C; ++row) {
const double* pa = &A[row];
const double* pb = &B[col];
double tmp1 = 0.0, tmp2 = 0.0;
for (int k = 0; k < NUM_ROW_A; ++k) {
double av = *pa;
tmp1 += av * pb[0];
tmp2 += av * pb[1];
pa += NUM_COL_A;
pb += NUM_COL_B;
}
const int index = (row + start_row_c) * col_stride_c + start_col_c + col;
CERES_GEMM_STORE_PAIR(C, index, tmp1, tmp2);
}
// Return directly for efficiency of extremely small matrix multiply.
if (NUM_COL_C < span) {
return;
}
}
// Process the main part with multiples of 4.
int col_m = NUM_COL_C & (int)(~(span - 1));
for (int col = 0; col < col_m; col += span) {
for (int row = 0; row < NUM_ROW_C; ++row) {
const int index = (row + start_row_c) * col_stride_c + start_col_c + col;
MTM_mat1x4(NUM_ROW_A, &A[row], NUM_COL_A,
&B[col], NUM_COL_B, &C[index], kOperation);
}
}
}
CERES_GEMM_BEGIN(MatrixTransposeMatrixMultiply) {
#ifdef CERES_NO_CUSTOM_BLAS
CERES_CALL_GEMM(MatrixTransposeMatrixMultiplyEigen)
return;
#else
if (kRowA != Eigen::Dynamic && kColA != Eigen::Dynamic &&
kRowB != Eigen::Dynamic && kColB != Eigen::Dynamic) {
CERES_CALL_GEMM(MatrixTransposeMatrixMultiplyEigen)
} else {
CERES_CALL_GEMM(MatrixTransposeMatrixMultiplyNaive)
}
#endif
}
// Matrix-Vector multiplication
//
// c op A * b;
//
// where op can be +=, -=, or =.
//
// The template parameters (kRowA, kColA) allow specialization of the
// loop at compile time. If this information is not available, then
// Eigen::Dynamic should be used as the template argument.
//
// kOperation = 1 -> c += A' * b
// kOperation = -1 -> c -= A' * b
// kOperation = 0 -> c = A' * b
template<int kRowA, int kColA, int kOperation>
inline void MatrixVectorMultiply(const double* A,
const int num_row_a,
const int num_col_a,
const double* b,
double* c) {
#ifdef CERES_NO_CUSTOM_BLAS
const typename EigenTypes<kRowA, kColA>::ConstMatrixRef
Aref(A, num_row_a, num_col_a);
const typename EigenTypes<kColA>::ConstVectorRef bref(b, num_col_a);
typename EigenTypes<kRowA>::VectorRef cref(c, num_row_a);
// lazyProduct works better than .noalias() for matrix-vector
// products.
if (kOperation > 0) {
cref += Aref.lazyProduct(bref);
} else if (kOperation < 0) {
cref -= Aref.lazyProduct(bref);
} else {
cref = Aref.lazyProduct(bref);
}
#else
DCHECK_GT(num_row_a, 0);
DCHECK_GT(num_col_a, 0);
DCHECK((kRowA == Eigen::Dynamic) || (kRowA == num_row_a));
DCHECK((kColA == Eigen::Dynamic) || (kColA == num_col_a));
const int NUM_ROW_A = (kRowA != Eigen::Dynamic ? kRowA : num_row_a);
const int NUM_COL_A = (kColA != Eigen::Dynamic ? kColA : num_col_a);
const int span = 4;
// Calculate the remainder part first.
// Process the last odd row if present.
if (NUM_ROW_A & 1) {
int row = NUM_ROW_A - 1;
const double* pa = &A[row * NUM_COL_A];
const double* pb = &b[0];
double tmp = 0.0;
for (int col = 0; col < NUM_COL_A; ++col) {
tmp += (*pa++) * (*pb++);
}
CERES_GEMM_STORE_SINGLE(c, row, tmp);
// Return directly for efficiency of extremely small matrix multiply.
if (NUM_ROW_A == 1) {
return;
}
}
// Process the couple rows in remainder if present.
if (NUM_ROW_A & 2) {
int row = NUM_ROW_A & (int)(~(span - 1));
const double* pa1 = &A[row * NUM_COL_A];
const double* pa2 = pa1 + NUM_COL_A;
const double* pb = &b[0];
double tmp1 = 0.0, tmp2 = 0.0;
for (int col = 0; col < NUM_COL_A; ++col) {
double bv = *pb++;
tmp1 += *(pa1++) * bv;
tmp2 += *(pa2++) * bv;
}
CERES_GEMM_STORE_PAIR(c, row, tmp1, tmp2);
// Return directly for efficiency of extremely small matrix multiply.
if (NUM_ROW_A < span) {
return;
}
}
// Calculate the main part with multiples of 4.
int row_m = NUM_ROW_A & (int)(~(span - 1));
for (int row = 0; row < row_m; row += span) {
MVM_mat4x1(NUM_COL_A, &A[row * NUM_COL_A], NUM_COL_A,
&b[0], &c[row], kOperation);
}
#endif // CERES_NO_CUSTOM_BLAS
}
// Similar to MatrixVectorMultiply, except that A is transposed, i.e.,
//
// c op A' * b;
template<int kRowA, int kColA, int kOperation>
inline void MatrixTransposeVectorMultiply(const double* A,
const int num_row_a,
const int num_col_a,
const double* b,
double* c) {
#ifdef CERES_NO_CUSTOM_BLAS
const typename EigenTypes<kRowA, kColA>::ConstMatrixRef
Aref(A, num_row_a, num_col_a);
const typename EigenTypes<kRowA>::ConstVectorRef bref(b, num_row_a);
typename EigenTypes<kColA>::VectorRef cref(c, num_col_a);
// lazyProduct works better than .noalias() for matrix-vector
// products.
if (kOperation > 0) {
cref += Aref.transpose().lazyProduct(bref);
} else if (kOperation < 0) {
cref -= Aref.transpose().lazyProduct(bref);
} else {
cref = Aref.transpose().lazyProduct(bref);
}
#else
DCHECK_GT(num_row_a, 0);
DCHECK_GT(num_col_a, 0);
DCHECK((kRowA == Eigen::Dynamic) || (kRowA == num_row_a));
DCHECK((kColA == Eigen::Dynamic) || (kColA == num_col_a));
const int NUM_ROW_A = (kRowA != Eigen::Dynamic ? kRowA : num_row_a);
const int NUM_COL_A = (kColA != Eigen::Dynamic ? kColA : num_col_a);
const int span = 4;
// Calculate the remainder part first.
// Process the last odd column if present.
if (NUM_COL_A & 1) {
int row = NUM_COL_A - 1;
const double* pa = &A[row];
const double* pb = &b[0];
double tmp = 0.0;
for (int col = 0; col < NUM_ROW_A; ++col) {
tmp += *pa * (*pb++);
pa += NUM_COL_A;
}
CERES_GEMM_STORE_SINGLE(c, row, tmp);
// Return directly for efficiency of extremely small matrix multiply.
if (NUM_COL_A == 1) {
return;
}
}
// Process the couple columns in remainder if present.
if (NUM_COL_A & 2) {
int row = NUM_COL_A & (int)(~(span - 1));
const double* pa = &A[row];
const double* pb = &b[0];
double tmp1 = 0.0, tmp2 = 0.0;
for (int col = 0; col < NUM_ROW_A; ++col) {
double bv = *pb++;
tmp1 += *(pa ) * bv;
tmp2 += *(pa + 1) * bv;
pa += NUM_COL_A;
}
CERES_GEMM_STORE_PAIR(c, row, tmp1, tmp2);
// Return directly for efficiency of extremely small matrix multiply.
if (NUM_COL_A < span) {
return;
}
}
// Calculate the main part with multiples of 4.
int row_m = NUM_COL_A & (int)(~(span - 1));
for (int row = 0; row < row_m; row += span) {
MTV_mat4x1(NUM_ROW_A, &A[row], NUM_COL_A,
&b[0], &c[row], kOperation);
}
#endif // CERES_NO_CUSTOM_BLAS
}
#undef CERES_GEMM_BEGIN
#undef CERES_GEMM_EIGEN_HEADER
#undef CERES_GEMM_NAIVE_HEADER
#undef CERES_CALL_GEMM
#undef CERES_GEMM_STORE_SINGLE
#undef CERES_GEMM_STORE_PAIR
} // namespace internal
} // namespace ceres
#endif // CERES_INTERNAL_SMALL_BLAS_H_