test_gemm.cpp 2.2 KB
Newer Older
Z
zhaojiaying01 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* 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 已提交
15
#include <iostream>
Z
zhaojiaying01 已提交
16
#include "../test_helper.h"
Z
zhaojiaying01 已提交
17
#include "common/log.h"
Z
zhaojiaying01 已提交
18
#include "memory/t_malloc.h"
Z
zhaojiaying01 已提交
19
#include "operators/math/gemm.h"
Z
zhaojiaying01 已提交
20

Z
zhaojiaying01 已提交
21 22 23 24
#define a(i, j) a[(i)*lda + (j)]
#define b(i, j) b[(i)*ldb + (j)]
#define c1(i, j) c1[(i)*ldc + (j)]

25 26 27
#define m 62
#define n 63
#define k 74
Z
zhaojiaying01 已提交
28 29

int main() {
Z
zhaojiaying01 已提交
30 31 32
  int lda = k;
  int ldb = n;
  int ldc = n;
Z
zhaojiaying01 已提交
33

Z
zhaojiaying01 已提交
34 35 36 37 38 39 40 41 42
  float *a =
      static_cast<float *>(paddle_mobile::memory::Alloc(sizeof(float) * m * k));
  float *b =
      static_cast<float *>(paddle_mobile::memory::Alloc(sizeof(float) * k * n));
  float *c =
      static_cast<float *>(paddle_mobile::memory::Alloc(sizeof(float) * m * n));
  float *c1 =
      static_cast<float *>(paddle_mobile::memory::Alloc(sizeof(float) * m * n));

Z
zhaojiaying01 已提交
43 44 45 46 47 48
  for (int i = 0; i < m * k; ++i) {
    a[i] = 2;
  }
  for (int i = 0; i < k * n; ++i) {
    b[i] = 2;
  }
49 50 51 52
  for (int i = 0; i < m * n; ++i) {
    c[i] = 2;
    c1[i] = 2;
  }
Z
zhaojiaying01 已提交
53

Z
zhaojiaying01 已提交
54
  auto time1 = time();
55 56
  paddle_mobile::operators::math::sgemm(m, n, k, 0.9, a, lda, b, ldb, 0.3, c,
                                        ldc);
Z
zhaojiaying01 已提交
57 58
  auto time2 = time();
  DLOG << "gemm cost :" << time_diff(time1, time2) << "ms\n";
Z
zhaojiaying01 已提交
59 60 61 62 63 64 65 66
  for (int i = 0; i < m * n; ++i) {
    std::cout << c[i] << " | ";
    if (i % n == (n - 1)) {
      std::cout << std::endl;
    }
  }
  for (int j = 0; j < n; ++j) {
    for (int i = 0; i < m; ++i) {
67
      c1(i, j) *= 0.3;
Z
zhaojiaying01 已提交
68
      for (int p = 0; p < k; ++p) {
69
        c1(i, j) += 0.9 * a(i, p) * b(p, j);
Z
zhaojiaying01 已提交
70 71 72 73 74 75 76 77 78 79 80 81
      }
    }
  }
  std::cout << "正确结果对比:" << std::endl;
  for (int i = 0; i < m * n; ++i) {
    std::cout << c1[i] << " | ";
    if (i % n == (n - 1)) {
      std::cout << std::endl;
    }
  }
  return 0;
}