test_gemm.cpp 1.8 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 16
#include <iostream>
#include "common/log.h"
Z
zhaojiaying01 已提交
17
#include "operators/math/gemm.h"
Z
zhaojiaying01 已提交
18

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

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

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

32 33 34 35
  float a[62 * 74];
  float b[74 * 63];
  float c[62 * 63] = {0};
  float c1[62 * 63] = {0};
Z
zhaojiaying01 已提交
36 37 38 39 40 41
  for (int i = 0; i < m * k; ++i) {
    a[i] = 2;
  }
  for (int i = 0; i < k * n; ++i) {
    b[i] = 2;
  }
42 43 44 45
  for (int i = 0; i < m * n; ++i) {
    c[i] = 2;
    c1[i] = 2;
  }
Z
zhaojiaying01 已提交
46

47 48
  paddle_mobile::operators::math::sgemm(m, n, k, 0.9, a, lda, b, ldb, 0.3, c,
                                        ldc);
Z
zhaojiaying01 已提交
49 50 51 52 53 54 55 56
  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) {
57
      c1(i, j) *= 0.3;
Z
zhaojiaying01 已提交
58
      for (int p = 0; p < k; ++p) {
59
        c1(i, j) += 0.9 * a(i, p) * b(p, j);
Z
zhaojiaying01 已提交
60 61 62 63 64 65 66 67 68 69 70 71
      }
    }
  }
  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;
}