test_ExecViaCpu.cpp 3.3 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Z
zhangjinchao01 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15

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. */

#include <gtest/gtest.h>
Y
Yu Yang 已提交
16
#include <paddle/utils/PythonUtil.h>
Z
zhangjinchao01 已提交
17
#include <paddle/utils/Util.h>
Y
Yu Yang 已提交
18
#include <vector>
Z
zhangjinchao01 已提交
19 20 21 22 23 24 25
#include "paddle/math/SparseMatrix.h"

using namespace paddle;  // NOLINT

const int height = 10;
const int width = 16;

26 27 28 29
real f(Matrix& mat1,
       const Matrix& mat2,
       IVector& vec1,
       const IVector& vec2,
Z
zhangjinchao01 已提交
30 31 32 33 34 35 36 37 38 39 40 41
       real scalar) {
  CHECK(!mat1.useGpu());
  CHECK(!mat2.useGpu());
  CHECK(!vec1.useGpu());
  CHECK(!vec2.useGpu());
  mat1.copyFrom(mat2);
  vec1.copyFrom(vec2);

  return scalar;
}

class Functor {
W
Wu Yi 已提交
42
 public:
43 44 45 46 47
  real operator()(Matrix& mat1,
                  const Matrix& mat2,
                  IVector& vec1,
                  const IVector& vec2,
                  real scalar) {
Z
zhangjinchao01 已提交
48 49 50 51
    a_ = f(mat1, mat2, vec1, vec2, scalar);
    return a_;
  }

W
Wu Yi 已提交
52
 private:
Z
zhangjinchao01 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
  real a_;
};

template <typename F>
void testWrapper(F&& f) {
  MatrixPtr cpumat1 = Matrix::create(height, width, false, /*useGpu=*/false);
  MatrixPtr cpumat2 = Matrix::create(height, width, false, /*useGpu=*/false);

  IVectorPtr cpuvec1 = IVector::create(height, /*useGpu=*/false);
  IVectorPtr cpuvec2 = IVector::create(height, /*useGpu=*/false);

  const real scalar = 1.23456;

  MatrixPtr gpumat1 = Matrix::create(height, width, false, /*useGpu=*/true);
  MatrixPtr gpumat2 = Matrix::create(height, width, false, /*useGpu=*/true);
  IVectorPtr gpuvec1 = IVector::create(height, /*useGpu=*/true);
  IVectorPtr gpuvec2 = IVector::create(height, /*useGpu=*/true);

  cpumat2->randomizeUniform();
  cpuvec2->rand(width);
  gpumat2->copyFrom(*cpumat2);
  gpuvec2->copyFrom(*cpuvec2);

  real ret = execViaCpu(f, *gpumat1, *gpumat2, *gpuvec1, *gpuvec2, 1.23456);
  EXPECT_EQ(ret, scalar);
  cpumat1->copyFrom(*gpumat1);
  cpuvec1->copyFrom(*gpuvec1);

  for (int i = 0; i < height; ++i) {
    EXPECT_EQ(cpuvec1->getElement(i), cpuvec2->getElement(i));
    for (int j = 0; j < width; ++j) {
      EXPECT_EQ(cpumat1->getElement(i, j), cpumat2->getElement(i, j));
    }
  }
  gpumat1->resize(height, 1);
  execViaCpu2(&CpuMatrix::selectElements, *gpumat1, *gpumat2, *gpuvec1);

  cpumat1->resize(height, 1);
  cpumat1->selectElements(*cpumat2, *cpuvec1);
  for (int i = 0; i < height; ++i) {
    EXPECT_EQ(cpumat1->getElement(i, 0), gpumat1->getElement(i, 0));
  }
}

97
#ifdef PADDLE_WITH_CUDA
Z
zhangjinchao01 已提交
98 99 100 101
TEST(ExecViaCpu, test1) {
  testWrapper(f);
  testWrapper(&f);

102 103 104 105 106 107 108
  auto lambda = [](Matrix& mat1,
                   const Matrix& mat2,
                   IVector& vec1,
                   const IVector& vec2,
                   real scalar) -> real {
    return f(mat1, mat2, vec1, vec2, scalar);
  };
Z
zhangjinchao01 已提交
109 110 111 112 113 114 115 116
  LOG(INFO) << "lambda is_class=" << std::is_class<decltype(lambda)>::value
            << " is_function=" << std::is_function<decltype(lambda)>::value;
  testWrapper(lambda);

  Functor functor;
  testWrapper(functor);
}
#endif