tesseract  4.00.00dev
intsimdmatrix_test.cc
Go to the documentation of this file.
1 // File: intsimdmatrix_test.cc
3 // Author: rays@google.com (Ray Smith)
4 //
5 // Copyright 2017 Google Inc. All Rights Reserved.
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
16 
17 #include "intsimdmatrix.h"
18 #include <memory>
19 #include "genericvector.h"
20 #include "include_gunit.h"
21 #include "intsimdmatrixavx2.h"
22 #include "intsimdmatrixsse.h"
23 #include "simddetect.h"
24 #include "tprintf.h"
25 
26 namespace tesseract {
27 namespace {
28 
29 class IntSimdMatrixTest : public ::testing::Test {
30  protected:
31  // Makes a random weights matrix of the given size.
32  GENERIC_2D_ARRAY<int8_t> InitRandom(int no, int ni) {
33  GENERIC_2D_ARRAY<int8_t> a(no, ni, 0);
34  for (int i = 0; i < no; ++i) {
35  for (int j = 0; j < ni; ++j) {
36  a(i, j) = static_cast<int8_t>(random_.SignedRand(MAX_INT8));
37  }
38  }
39  return a;
40  }
41  // Makes a random input vector of the given size, with rounding up.
42  std::vector<int8_t> RandomVector(int size, const IntSimdMatrix& matrix) {
43  int rounded_size = matrix.RoundInputs(size);
44  std::vector<int8_t> v(rounded_size, 0);
45  for (int i = 0; i < size; ++i) {
46  v[i] = static_cast<int8_t>(random_.SignedRand(MAX_INT8));
47  }
48  return v;
49  }
50  // Makes a random scales vector of the given size.
51  GenericVector<double> RandomScales(int size) {
52  GenericVector<double> v(size, 0.0);
53  for (int i = 0; i < size; ++i) {
54  v[i] = 1.0 + random_.SignedRand(1.0);
55  }
56  return v;
57  }
58  // Tests a range of sizes and compares the results against the base_ version.
59  void ExpectEqualResults(IntSimdMatrix* matrix) {
60  for (int num_out = 1; num_out < 130; ++num_out) {
61  for (int num_in = 1; num_in < 130; ++num_in) {
62  GENERIC_2D_ARRAY<int8_t> w = InitRandom(num_out, num_in + 1);
63  matrix->Init(w);
64  std::vector<int8_t> u = RandomVector(num_in, *matrix);
65  GenericVector<double> scales = RandomScales(num_out);
66  std::vector<double> base_result(num_out);
67  base_.MatrixDotVector(w, scales, u.data(), base_result.data());
68  std::vector<double> test_result(num_out);
69  matrix->MatrixDotVector(w, scales, u.data(), test_result.data());
70  for (int i = 0; i < num_out; ++i)
71  EXPECT_FLOAT_EQ(base_result[i], test_result[i]) << "i=" << i;
72  }
73  }
74  }
75 
76  TRand random_;
77  IntSimdMatrix base_;
78 };
79 
80 // Tests that the SSE implementation gets the same result as the vanilla.
81 TEST_F(IntSimdMatrixTest, SSE) {
83  tprintf("SSE found! Continuing...");
84  } else {
85  tprintf("No SSE found! Not Tested!");
86  return;
87  }
88  std::unique_ptr<IntSimdMatrix> matrix(new IntSimdMatrixSSE());
89  ExpectEqualResults(matrix.get());
90 }
91 
92 // Tests that the AVX2 implementation gets the same result as the vanilla.
93 TEST_F(IntSimdMatrixTest, AVX2) {
95  tprintf("AVX2 found! Continuing...");
96  } else {
97  tprintf("No AVX2 found! Not Tested!");
98  return;
99  }
100  std::unique_ptr<IntSimdMatrix> matrix(new IntSimdMatrixAVX2());
101  ExpectEqualResults(matrix.get());
102 }
103 
104 } // namespace
105 } // namespace tesseract
static bool IsSSEAvailable()
Definition: simddetect.h:38
#define MAX_INT8
Definition: host.h:60
#define tprintf(...)
Definition: tprintf.h:31
IntSimdMatrix base_
static bool IsAVX2Available()
Definition: simddetect.h:28
TRand random_