executor.h 8.8 KB
Newer Older
H
backup  
hjchen2 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* 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. */

#pragma once

#include <algorithm>
#ifdef _OPENMP
#include <omp.h>
#endif
21 22
// #include <sys/time.h>
// #include <iostream>
H
backup  
hjchen2 已提交
23 24 25 26 27 28 29 30 31
#include "common/log.h"
#include "memory/t_malloc.h"
#include "operators/math/gemm/cpu_info.h"
#include "operators/math/gemm/gemm_kernel.h"

namespace paddle_mobile {
namespace operators {
namespace math {

32 33
static CPUInfo *info = CPUInfo::Info();

H
hjchen2 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46
int CeilDiv(const int &x, const int &y) { return (x + y - 1) / y; }
unsigned int ResetL1Cache(const unsigned int L1_size, const int thread_num,
                          const int N, const int K) {
  unsigned int L1 = L1_size;
  if (thread_num == 1) {
    if (N >= 30000 && K > 100) {
      L1 *= 4;
    } else if (N >= 10000 && K > 100) {
      L1 *= 2;
    }
  }
  return L1;
}
H
backup  
hjchen2 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

class Executor {
 public:
  Executor() : num_threads_(1) {
#ifdef _OPENMP
    num_threads_ = omp_get_max_threads();
#endif
  }
  virtual ~Executor() {}

 protected:
  int num_threads_;
};

template <typename Strategy>
class GemmExecutor : public Executor {
  typedef typename Strategy::Itype Itype;
  typedef typename Strategy::Otype Otype;

 public:
67 68 69
  GemmExecutor(const bool transA, const bool transB, const int M, const int N,
               const int K)
      : Executor(), transA_(transA), transB_(transB), M_(M), N_(N), K_(K) {
H
hjchen2 已提交
70 71 72 73 74 75 76 77 78
    unsigned int L1_size = 0;
    unsigned int L2_size = 0;
    if (M_ > N_) {
      L2_size = ResetL1Cache(info->L1_cache, num_threads_, M_, K_);
      L1_size = info->L2_cache;
    } else {
      L1_size = ResetL1Cache(info->L1_cache, num_threads_, N_, K_);
      L2_size = info->L2_cache;
    }
H
backup  
hjchen2 已提交
79

H
hjchen2 已提交
80
    rhs_tile_num_ = L1_size / (K_ * sizeof(Itype));
H
backup  
hjchen2 已提交
81 82 83
    if (rhs_tile_num_ == 0) {
      rhs_tile_num_ = Strategy::out_width();
    } else {
H
hjchen2 已提交
84 85
      int n_block = CeilDiv(N_, rhs_tile_num_);
      rhs_tile_num_ = CeilDiv(N_, n_block);
H
backup  
hjchen2 已提交
86 87 88 89
      rhs_tile_num_ = CeilDiv(rhs_tile_num_, Strategy::out_width());
      rhs_tile_num_ *= Strategy::out_width();
    }

90 91
    //  lhs_tile_num_ = CeilDiv(M, Strategy::out_height()) *
    //  Strategy::out_height();
H
hjchen2 已提交
92
    lhs_tile_num_ = L2_size / (K_ * sizeof(Itype));
H
backup  
hjchen2 已提交
93 94 95
    if (lhs_tile_num_ == 0) {
      lhs_tile_num_ = Strategy::out_height();
    } else {
H
hjchen2 已提交
96 97
      int m_block = CeilDiv(M_, lhs_tile_num_);
      lhs_tile_num_ = CeilDiv(M_, m_block);
H
backup  
hjchen2 已提交
98 99 100 101 102 103 104 105
      lhs_tile_num_ = CeilDiv(lhs_tile_num_, Strategy::out_height());
      lhs_tile_num_ *= Strategy::out_height();
    }
  }

  void operator()(const float alpha, const Itype *A, const int lda,
                  const Itype *B, const int ldb, const float beta, Otype *C,
                  const int ldc) {
106 107
    //  struct timeval tv_begin, tv_end;
    //  gettimeofday(&tv_begin,NULL);
H
hjchen2 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120
    if (M_ > N_) {
      int nblock = CeilDiv(N_, Strategy::out_width()) * Strategy::out_width();
      lhs_worksize_ = sizeof(Itype) * lhs_tile_num_ * K_;
      rhs_worksize_ = sizeof(Itype) * K_ * nblock * num_threads_;
      out_worksize_ = sizeof(Otype) * lhs_tile_num_ * nblock * num_threads_;
      ldc_ = nblock;
    } else {
      int mblock = CeilDiv(M_, Strategy::out_height()) * Strategy::out_height();
      lhs_worksize_ = sizeof(Itype) * mblock * K_;
      rhs_worksize_ = sizeof(Itype) * K_ * rhs_tile_num_ * num_threads_;
      out_worksize_ = sizeof(Otype) * mblock * rhs_tile_num_ * num_threads_;
      ldc_ = rhs_tile_num_;
    }
H
backup  
hjchen2 已提交
121 122 123 124 125 126 127 128

    lhs_workspace_ =
        static_cast<Itype *>(paddle_mobile::memory::Alloc(lhs_worksize_));
    rhs_workspace_ =
        static_cast<Itype *>(paddle_mobile::memory::Alloc(rhs_worksize_));
    out_workspace_ =
        static_cast<Otype *>(paddle_mobile::memory::Alloc(out_worksize_));

H
hjchen2 已提交
129 130 131
    //  std::cout << "M: " << M_ << ", N: " << N_ << ", K: " << K_ << std::endl;
    //  std::cout << "lhs_block: " << CeilDiv(M_, lhs_tile_num_) << ", "
    //            << "rhs_block: " << CeilDiv(N_, rhs_tile_num_) << std::endl;
H
backup  
hjchen2 已提交
132

H
hjchen2 已提交
133 134
    if (M_ > N_) {
      strategy_.pack_rhs(K_, N_, B, ldb, rhs_workspace_, true);
H
backup  
hjchen2 已提交
135

H
hjchen2 已提交
136 137 138
      #pragma omp parallel for if (M_ > 128)
      for (int lhs_block = 0; lhs_block < M_; lhs_block += lhs_tile_num_) {
        int lhs_range = std::min(M_ - lhs_block, lhs_tile_num_);
H
backup  
hjchen2 已提交
139
#ifdef _OPENMP
H
hjchen2 已提交
140
        int thread_id = omp_get_thread_num();
H
backup  
hjchen2 已提交
141
#else
H
hjchen2 已提交
142
        int thread_id = 0;
H
backup  
hjchen2 已提交
143
#endif
H
hjchen2 已提交
144 145 146 147 148 149 150 151
        float *local_A = lhs_workspace_ + lhs_tile_num_ * K_ * thread_id;
        float *local_C = out_workspace_ + lhs_tile_num_ * ldc_ * thread_id;
        // load lhs into lhs_workspace
        strategy_.pack_lhs(lhs_range, K_, A + lhs_block * lda, lda, local_A,
                           false);
        for (int rhs_block = 0; rhs_block < N_; rhs_block += rhs_tile_num_) {
          int rhs_range = std::min(N_ - rhs_block, rhs_tile_num_);
          float *local_B = rhs_workspace_ + K_ * rhs_block;
H
backup  
hjchen2 已提交
152 153
          for (int rhs_tile = 0; rhs_tile < rhs_range;
               rhs_tile += Strategy::out_width()) {
H
hjchen2 已提交
154 155 156 157 158 159 160 161
            for (int lhs_tile = 0; lhs_tile < lhs_range;
                 lhs_tile += Strategy::out_height()) {
              int offset = lhs_tile * ldc_ + rhs_block + rhs_tile;
              strategy_.kernel(local_A + lhs_tile * K_, local_B + rhs_tile * K_,
                               K_, local_C + offset, ldc_);
            }
          }
        }
162 163
        strategy_.write(lhs_range, N_, alpha, local_C, ldc_, beta,
                        C + lhs_block * ldc, ldc);
H
hjchen2 已提交
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
      }
    } else {
      strategy_.pack_lhs(M_, K_, A, lda, lhs_workspace_, true);

      #pragma omp parallel for if (N_ > 128)
      for (int rhs_block = 0; rhs_block < N_; rhs_block += rhs_tile_num_) {
        int rhs_range = std::min(N_ - rhs_block, rhs_tile_num_);
#ifdef _OPENMP
        int thread_id = omp_get_thread_num();
#else
        int thread_id = 0;
#endif
        float *local_B = rhs_workspace_ + K_ * rhs_tile_num_ * thread_id;
        float *local_C = out_workspace_ + lhs_tile_num_ * ldc_ * thread_id;
        // load rhs into rhs_workspace
        strategy_.pack_rhs(K_, rhs_range, B + rhs_block, ldb, local_B, false);
        for (int lhs_block = 0; lhs_block < M_; lhs_block += lhs_tile_num_) {
          int lhs_range = std::min(M_ - lhs_block, lhs_tile_num_);
          float *local_A = lhs_workspace_ + lhs_block * K_;
          for (int lhs_tile = 0; lhs_tile < lhs_range;
               lhs_tile += Strategy::out_height()) {
            for (int rhs_tile = 0; rhs_tile < rhs_range;
                 rhs_tile += Strategy::out_width()) {
              int offset = (lhs_block + lhs_tile) * ldc_ + rhs_tile;
              strategy_.kernel(local_A + lhs_tile * K_, local_B + rhs_tile * K_,
                               K_, local_C + offset, ldc_);
            }
H
backup  
hjchen2 已提交
191 192
          }
        }
193 194
        strategy_.write(M_, rhs_range, alpha, local_C, ldc_, beta,
                        C + rhs_block, ldc);
H
backup  
hjchen2 已提交
195 196 197 198 199 200 201
      }
    }

    paddle_mobile::memory::Free(lhs_workspace_);
    paddle_mobile::memory::Free(rhs_workspace_);
    paddle_mobile::memory::Free(out_workspace_);

202 203 204 205 206 207
    //  gettimeofday(&tv_end,NULL);
    //  float elapsed = (tv_end.tv_sec - tv_begin.tv_sec) * 1000.f +
    //                  (tv_end.tv_usec - tv_begin.tv_usec) / 1000.f;
    //  std::cout << "elapsed: " << elapsed << "ms, speed: "
    //            << (M_ * N_ * K_ / 1000.f / 1000.f) / elapsed
    //            << " gflops" << std::endl;
H
backup  
hjchen2 已提交
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
  }

  virtual ~GemmExecutor() {}

 private:
  const unsigned int M_;
  const unsigned int N_;
  const unsigned int K_;
  const bool transA_;
  const bool transB_;

  unsigned int lhs_tile_num_ = 0;
  unsigned int rhs_tile_num_ = 0;
  unsigned int out_tile_num_ = 0;

  unsigned int lhs_worksize_ = 0;
  unsigned int rhs_worksize_ = 0;
  unsigned int out_worksize_ = 0;
H
hjchen2 已提交
226
  unsigned int ldc_ = 0;
H
backup  
hjchen2 已提交
227 228 229 230 231 232 233 234 235 236 237 238 239 240

  Itype *lhs_workspace_ = nullptr;
  Itype *rhs_workspace_ = nullptr;
  Otype *out_workspace_ = nullptr;

  Strategy strategy_;
};

template <typename Strategy>
class GemvExecutor : public Executor {
  typedef typename Strategy::Itype Itype;
  typedef typename Strategy::Otype Otype;

 public:
241
  GemvExecutor(const bool transA, const int M, const int N)
242
      : Executor(), M_(M), N_(N), trans_(transA) {}
H
backup  
hjchen2 已提交
243 244 245

  void operator()(const float alpha, const Itype *A, const int lda,
                  const Itype *B, const float beta, Otype *C) {
246
    strategy_.kernel(trans_, M_, N_, alpha, A, lda, B, beta, C);
H
backup  
hjchen2 已提交
247 248 249 250 251 252 253
  }

  virtual ~GemvExecutor() {}

 private:
  const unsigned int M_;
  const unsigned int N_;
254
  const bool trans_;
H
backup  
hjchen2 已提交
255 256 257 258 259 260 261

  Strategy strategy_;
};

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