コード例 #1
0
ファイル: test_quant_blas.cpp プロジェクト: naibaf7/caffe
TYPED_TEST(QuantBlasTest, TestGemvComparativeFloatQuant) {
  typedef typename TypeParam::Dtype Dtype;

  // Expect at most 5% error
  float percentile_eps = 0.05;

  std::random_device rdev;
  std::mt19937 rngen(rdev());

  // Need to test > 64 dimension
  std::uniform_int_distribution<int_tp> dimsRand(1, 256);
  std::uniform_int_distribution<int_tp> boolRand(0, 1);
  std::uniform_int_distribution<int_tp> factorRand(-25, 25);
  std::uniform_real_distribution<float> valRand(-2.0, 2.0);


  for (int_tp testIdx = 0; testIdx < 25; ++testIdx) {
    int_tp M = dimsRand(rngen);
    int_tp N = dimsRand(rngen);

    CBLAS_TRANSPOSE trans_A = boolRand(rngen) ? CblasTrans : CblasNoTrans;

    bool has_alpha = boolRand(rngen);
    bool has_beta = has_alpha ? boolRand(rngen) : true;

    bool alpha_with_quant = boolRand(rngen) && has_alpha;
    bool beta_with_quant = boolRand(rngen) && has_beta;

    float alpha_val;
    float beta_val;

    if (has_alpha) {
      alpha_val = alpha_with_quant ? valRand(rngen) : float(1.0);
    } else {
      alpha_val = 0.0;
    }

    if (has_beta) {
      beta_val = beta_with_quant ? valRand(rngen) : float(1.0);
    } else {
      beta_val = 0.0;
    }

    vector<int_tp> A_shape(4, 1);
    vector<int_tp> x_shape(4, 1);
    vector<int_tp> y_shape(4, 1);

    A_shape[2] = M;
    A_shape[3] = N;
    x_shape[3] = trans_A == CblasTrans ? M : N;
    y_shape[3] = trans_A == CblasTrans ? N : M;

    Blob<float> A(A_shape, Caffe::GetDefaultDevice());
    Blob<float> x(x_shape, Caffe::GetDefaultDevice());
    Blob<float> y(y_shape, Caffe::GetDefaultDevice());
    Blob<float> y_result(y_shape, Caffe::GetDefaultDevice());

    Blob<Dtype> A_quant(A_shape, Caffe::GetDefaultDevice());
    Blob<Dtype> x_quant(x_shape, Caffe::GetDefaultDevice());
    Blob<Dtype> y_quant(y_shape, Caffe::GetDefaultDevice());

    Blob<float> y_unquant(y_shape, Caffe::GetDefaultDevice());


    caffe_rng_gaussian(M * N, (float)0.0, (float)0.5,
                       A.mutable_cpu_data());
    caffe_rng_gaussian(trans_A == CblasTrans ? M : N,
                       (float)0.0, (float)0.5, x.mutable_cpu_data());
    caffe_rng_gaussian(trans_A == CblasTrans ? N : M,
                       (float)0.0, (float)0.5, y.mutable_cpu_data());

    caffe_copy(trans_A == CblasTrans ? N : M,
               y.cpu_data(), y_result.mutable_cpu_data());

    QuantizerParameter qpm_a;
    QuantizerParameter qpm_x;
    QuantizerParameter qpm_y;
    QuantizerParameter qpm_alpha;
    QuantizerParameter qpm_beta;
    qpm_a.set_mode(CAFFE_QUANT_OBSERVE);
    qpm_x.set_mode(CAFFE_QUANT_OBSERVE);
    qpm_y.set_mode(CAFFE_QUANT_OBSERVE);
    qpm_alpha.set_mode(CAFFE_QUANT_OBSERVE);
    qpm_beta.set_mode(CAFFE_QUANT_OBSERVE);

    Quantizer<float, Dtype> aq(qpm_a);
    Quantizer<float, Dtype> xq(qpm_x);
    Quantizer<float, Dtype> yq(qpm_y);
    Quantizer<float, Dtype> alphaq(qpm_alpha);
    Quantizer<float, Dtype> betaq(qpm_beta);

    // Normal GEMM
    caffe_gemv<float>(
                trans_A,
                M, N,
                alpha_val,
                A.cpu_data(), x.cpu_data(),
                beta_val,
                y_result.mutable_cpu_data());


    // Observe all values that will be relevant for quantization
    aq.ObserveIn_cpu(M * N, A.cpu_data());
    xq.ObserveIn_cpu(trans_A == CblasTrans ? M : N, x.cpu_data());
    yq.ObserveIn_cpu(trans_A == CblasTrans ? N : M, y.cpu_data());
    yq.ObserveIn_cpu(trans_A == CblasTrans ? N : M, y_result.cpu_data());
    alphaq.ObserveIn_cpu(1, &alpha_val);
    betaq.ObserveIn_cpu(1, &beta_val);

    // Apply observed values to the quantizer
    aq.update();
    xq.update();
    yq.update();
    alphaq.update();
    betaq.update();

    // Quantize A, B and C
    aq.Forward_cpu(M * N, A.cpu_data(), A_quant.mutable_cpu_data());
    xq.Forward_cpu(trans_A == CblasTrans ? M : N,
                   x.cpu_data(), x_quant.mutable_cpu_data());
    yq.Forward_cpu(trans_A == CblasTrans ? N : M,
                   y.cpu_data(), y_quant.mutable_cpu_data());

    Dtype alpha_val_quant = has_alpha;
    Dtype beta_val_quant = has_beta;

    // Quantize alpha
    if (alpha_with_quant) {
      alphaq.Forward_cpu(1, &alpha_val, &alpha_val_quant);
    }

    // Quantize beta
    if (beta_with_quant) {
      betaq.Forward_cpu(1, &beta_val, &beta_val_quant);
    }

    if (Caffe::mode() == Caffe::Brew::CPU) {
      caffe_gemv<Dtype>(trans_A, M, N,
                  alpha_val_quant,
                  A_quant.cpu_data(), x_quant.cpu_data(),
                  beta_val_quant,
                  y_quant.mutable_cpu_data(),
                  alpha_with_quant ? &(alphaq.out_quantizer_values()) : nullptr,
                  &(aq.out_quantizer_values()),
                  &(xq.out_quantizer_values()),
                  beta_with_quant ? &(betaq.out_quantizer_values()) : nullptr,
                  &(yq.out_quantizer_values()));
    } else {
      Caffe::GetDefaultDevice()->template gemv<Dtype>(trans_A, M, N,
                  alpha_val_quant,
                  A_quant.gpu_data(), x_quant.gpu_data(),
                  beta_val_quant,
                  y_quant.mutable_gpu_data(),
                  alpha_with_quant ? &(alphaq.out_quantizer_values()) : nullptr,
                  &(aq.out_quantizer_values()),
                  &(xq.out_quantizer_values()),
                  beta_with_quant ? &(betaq.out_quantizer_values()) : nullptr,
                  &(yq.out_quantizer_values()));
    }

    yq.Backward_cpu(trans_A == CblasTrans ? N : M,
                    y_quant.cpu_data(), y_unquant.mutable_cpu_data());

    // print_matrix(A_quant.cpu_data(), M, K);
    // print_matrix(B_quant.cpu_data(), K, N);

    // print_matrix(C_quant.cpu_data(), M, N);
    // print_matrix(C_result.cpu_data(), M, N);
    // print_matrix(C_unquant.cpu_data(), M, N);

    const QuantizerValues cqv = yq.in_quantizer_values();
    float eps = std::max(std::abs(cqv.get_max<float>()),
                         std::abs(cqv.get_min<float>())) * percentile_eps;

    for (int_tp i = 0; i < (trans_A == CblasTrans ? N : M); ++i) {
      EXPECT_NEAR(y_unquant.cpu_data()[i], y_result.cpu_data()[i], eps);
      // One error is enough to abort
      if (fabs(y_unquant.cpu_data()[i] - y_result.cpu_data()[i]) >= eps) {
        break;
      }
    }
  }
}
コード例 #2
0
ファイル: UnitTest.cpp プロジェクト: jackhui/GE
TEST(Vector, DotProduct)
{
	Vector3 v(2.0f, 4.0f, 6.0f);
	EXPECT_NEAR(56.0f, v.Dot(v), 0.01f);
}
コード例 #3
0
TEST(Stats, ByHourSimpleStatsUpdater)
{
    // Seed random() for use in simulator; --gtest_shuffle will force it to change.
    srandom((unsigned) ::testing::UnitTest::GetInstance()->random_seed());

    static_assert(2 == BHSSU::su.maxSamplesPerHour, "constant must propagate correctly");
    const uint8_t msph = BHSSU::su.maxSamplesPerHour;
    ASSERT_EQ(2, msph);

    // Reset static state to make tests re-runnable.
//    BHSSU::su.sampleStats(true, 0);
    BHSSU::ms.zapStats();
    BHSSU::occupancy.reset();
    BHSSU::ambLight.set(0, 0, false);
    BHSSU::tempC16.set(OTV0P2BASE::TemperatureC16Mock::DEFAULT_INVALID_TEMP);
    BHSSU::rh.set(0, false);

    const uint8_t unset = OTV0P2BASE::NVByHourByteStatsBase::UNSET_BYTE;

    // Set (arbitrary) initial time.
    uint8_t hourNow = ((unsigned) random()) % 24;
    BHSSU::ms._setHour(hourNow);
    // Verify that before first full update stats values unset.
    EXPECT_EQ(unset, BHSSU::ms.getByHourStatSimple(BHSSU::ms.STATS_SET_AMBLIGHT_BY_HOUR, hourNow));
    EXPECT_EQ(unset, BHSSU::ms.getByHourStatSimple(BHSSU::ms.STATS_SET_AMBLIGHT_BY_HOUR_SMOOTHED, hourNow));
    EXPECT_EQ(unset, BHSSU::ms.getByHourStatRTC(BHSSU::ms.STATS_SET_AMBLIGHT_BY_HOUR, hourNow));
    EXPECT_EQ(unset, BHSSU::ms.getByHourStatRTC(BHSSU::ms.STATS_SET_AMBLIGHT_BY_HOUR_SMOOTHED, hourNow));
    EXPECT_EQ(unset, BHSSU::ms.getByHourStatRTC(BHSSU::ms.STATS_SET_AMBLIGHT_BY_HOUR, BHSSU::ms.SPECIAL_HOUR_CURRENT_HOUR));
    EXPECT_EQ(unset, BHSSU::ms.getByHourStatRTC(BHSSU::ms.STATS_SET_AMBLIGHT_BY_HOUR_SMOOTHED, BHSSU::ms.SPECIAL_HOUR_CURRENT_HOUR));
    EXPECT_EQ(unset, BHSSU::ms.getByHourStatRTC(BHSSU::ms.STATS_SET_AMBLIGHT_BY_HOUR, BHSSU::ms.SPECIAL_HOUR_NEXT_HOUR));
    EXPECT_EQ(unset, BHSSU::ms.getByHourStatRTC(BHSSU::ms.STATS_SET_AMBLIGHT_BY_HOUR_SMOOTHED, BHSSU::ms.SPECIAL_HOUR_NEXT_HOUR));
    EXPECT_EQ(unset, BHSSU::ms.getByHourStatSimple(BHSSU::ms.STATS_SET_TEMP_BY_HOUR, hourNow));
    EXPECT_EQ(unset, BHSSU::ms.getByHourStatSimple(BHSSU::ms.STATS_SET_TEMP_BY_HOUR_SMOOTHED, hourNow));
    EXPECT_EQ(unset, BHSSU::ms.getByHourStatSimple(BHSSU::ms.STATS_SET_RHPC_BY_HOUR, hourNow));
    EXPECT_EQ(unset, BHSSU::ms.getByHourStatSimple(BHSSU::ms.STATS_SET_RHPC_BY_HOUR_SMOOTHED, hourNow));
    EXPECT_EQ(unset, BHSSU::ms.getByHourStatSimple(BHSSU::ms.STATS_SET_OCCPC_BY_HOUR, hourNow));
    EXPECT_EQ(unset, BHSSU::ms.getByHourStatSimple(BHSSU::ms.STATS_SET_OCCPC_BY_HOUR_SMOOTHED, hourNow));
    // Set initial sensor values.
    const uint8_t al0 = 254;
    BHSSU::ambLight.set(al0);
    const int16_t t0 = 18 << 4;
    const uint8_t t0c = OTV0P2BASE::compressTempC16(t0);
    ASSERT_NEAR(t0, OTV0P2BASE::expandTempC16(t0c), 1);
    BHSSU::tempC16.set(t0);
    ASSERT_EQ(18<<4, BHSSU::tempC16.get());
    const uint8_t rh0 = ((unsigned) random()) % 101;
    BHSSU::rh.set(rh0);
    BHSSU::su.sampleStats(true, hourNow);
    const uint8_t o0 = 0;
    ASSERT_EQ(o0, BHSSU::occupancy.get());
    // Verify that after first full update stats values set to specified values.
    EXPECT_EQ(al0, BHSSU::ms.getByHourStatSimple(BHSSU::ms.STATS_SET_AMBLIGHT_BY_HOUR, hourNow));
    EXPECT_EQ(al0, BHSSU::ms.getByHourStatSimple(BHSSU::ms.STATS_SET_AMBLIGHT_BY_HOUR_SMOOTHED, hourNow));
    EXPECT_EQ(al0, BHSSU::ms.getByHourStatRTC(BHSSU::ms.STATS_SET_AMBLIGHT_BY_HOUR, hourNow));
    EXPECT_EQ(al0, BHSSU::ms.getByHourStatRTC(BHSSU::ms.STATS_SET_AMBLIGHT_BY_HOUR_SMOOTHED, hourNow));
    EXPECT_EQ(al0, BHSSU::ms.getByHourStatRTC(BHSSU::ms.STATS_SET_AMBLIGHT_BY_HOUR, BHSSU::ms.SPECIAL_HOUR_CURRENT_HOUR));
    EXPECT_EQ(al0, BHSSU::ms.getByHourStatRTC(BHSSU::ms.STATS_SET_AMBLIGHT_BY_HOUR_SMOOTHED, BHSSU::ms.SPECIAL_HOUR_CURRENT_HOUR));
    EXPECT_EQ(unset, BHSSU::ms.getByHourStatRTC(BHSSU::ms.STATS_SET_AMBLIGHT_BY_HOUR, BHSSU::ms.SPECIAL_HOUR_NEXT_HOUR));
    EXPECT_EQ(unset, BHSSU::ms.getByHourStatRTC(BHSSU::ms.STATS_SET_AMBLIGHT_BY_HOUR_SMOOTHED, BHSSU::ms.SPECIAL_HOUR_NEXT_HOUR));
    EXPECT_EQ(t0c, BHSSU::ms.getByHourStatSimple(BHSSU::ms.STATS_SET_TEMP_BY_HOUR, hourNow));
    EXPECT_EQ(t0c, BHSSU::ms.getByHourStatSimple(BHSSU::ms.STATS_SET_TEMP_BY_HOUR_SMOOTHED, hourNow));
    EXPECT_EQ(rh0, BHSSU::ms.getByHourStatSimple(BHSSU::ms.STATS_SET_RHPC_BY_HOUR, hourNow));
    EXPECT_EQ(rh0, BHSSU::ms.getByHourStatSimple(BHSSU::ms.STATS_SET_RHPC_BY_HOUR_SMOOTHED, hourNow));
    EXPECT_EQ(o0, BHSSU::ms.getByHourStatSimple(BHSSU::ms.STATS_SET_OCCPC_BY_HOUR, hourNow));
    EXPECT_EQ(o0, BHSSU::ms.getByHourStatSimple(BHSSU::ms.STATS_SET_OCCPC_BY_HOUR_SMOOTHED, hourNow));

    // Nominally roll round a day and update the same slot for new sensor values.
    const uint8_t al1 = 0;
    BHSSU::ambLight.set(al1);
    const uint8_t o1 = 100;
    BHSSU::occupancy.markAsOccupied();
    ASSERT_EQ(o1, BHSSU::occupancy.get());
    const uint8_t rh1 = ((unsigned) random()) % 101;
    BHSSU::rh.set(rh1);
    // Compute expected (approximate) smoothed values.
    const uint8_t sm_al1 = OTV0P2BASE::NVByHourByteStatsBase::smoothStatsValue(al0, al1);
    EXPECT_LT(al1, sm_al1);
    EXPECT_GT(al0, sm_al1);
    const uint8_t sm_o1 = OTV0P2BASE::NVByHourByteStatsBase::smoothStatsValue(o0, o1);
    const uint8_t sm_rh1 = OTV0P2BASE::NVByHourByteStatsBase::smoothStatsValue(rh0, rh1);
    // Take single/final/full sample.
    BHSSU::su.sampleStats(true, hourNow);
    EXPECT_EQ(al1, BHSSU::ms.getByHourStatSimple(BHSSU::ms.STATS_SET_AMBLIGHT_BY_HOUR, hourNow));
    EXPECT_NEAR(sm_al1, BHSSU::ms.getByHourStatSimple(BHSSU::ms.STATS_SET_AMBLIGHT_BY_HOUR_SMOOTHED, hourNow), 1);
    EXPECT_EQ(al1, BHSSU::ms.getByHourStatRTC(BHSSU::ms.STATS_SET_AMBLIGHT_BY_HOUR, hourNow));
    EXPECT_NEAR(sm_al1, BHSSU::ms.getByHourStatRTC(BHSSU::ms.STATS_SET_AMBLIGHT_BY_HOUR_SMOOTHED, hourNow), 1);
    EXPECT_EQ(al1, BHSSU::ms.getByHourStatRTC(BHSSU::ms.STATS_SET_AMBLIGHT_BY_HOUR, BHSSU::ms.SPECIAL_HOUR_CURRENT_HOUR));
    EXPECT_NEAR(sm_al1, BHSSU::ms.getByHourStatRTC(BHSSU::ms.STATS_SET_AMBLIGHT_BY_HOUR_SMOOTHED, BHSSU::ms.SPECIAL_HOUR_CURRENT_HOUR), 1);
    EXPECT_EQ(unset, BHSSU::ms.getByHourStatRTC(BHSSU::ms.STATS_SET_AMBLIGHT_BY_HOUR, BHSSU::ms.SPECIAL_HOUR_NEXT_HOUR));
    EXPECT_EQ(unset, BHSSU::ms.getByHourStatRTC(BHSSU::ms.STATS_SET_AMBLIGHT_BY_HOUR_SMOOTHED, BHSSU::ms.SPECIAL_HOUR_NEXT_HOUR));
    EXPECT_EQ(t0c, BHSSU::ms.getByHourStatSimple(BHSSU::ms.STATS_SET_TEMP_BY_HOUR, hourNow));
    EXPECT_EQ(t0c, BHSSU::ms.getByHourStatSimple(BHSSU::ms.STATS_SET_TEMP_BY_HOUR_SMOOTHED, hourNow));
    EXPECT_EQ(rh1, BHSSU::ms.getByHourStatSimple(BHSSU::ms.STATS_SET_RHPC_BY_HOUR, hourNow));
    EXPECT_NEAR(sm_rh1, BHSSU::ms.getByHourStatSimple(BHSSU::ms.STATS_SET_RHPC_BY_HOUR_SMOOTHED, hourNow), 1);
    EXPECT_EQ(o1, BHSSU::ms.getByHourStatSimple(BHSSU::ms.STATS_SET_OCCPC_BY_HOUR, hourNow));
    EXPECT_NEAR(sm_o1, BHSSU::ms.getByHourStatSimple(BHSSU::ms.STATS_SET_OCCPC_BY_HOUR_SMOOTHED, hourNow), 1);

    // Move to next hour.
    const uint8_t nextHour = (hourNow + 1) % 24;
    BHSSU::ms._setHour(nextHour);
    // Take a couple of samples for this hour.
    BHSSU::ambLight.set(al0);
    BHSSU::rh.set(rh0);
    BHSSU::su.sampleStats(false, nextHour);
    BHSSU::ambLight.set(al1);
    BHSSU::rh.set(rh1);
    BHSSU::su.sampleStats(true, nextHour);
    // Expect to see the mean of the first and second sample as the stored value.
    // Note that this exercises sensor data that is handled differently (eg full-range AmbLight vs RH%).
    const uint8_t al01 = (al0 + al1 + 1) / 2;
    const uint8_t rh01 = (rh0 + rh1 + 1) / 2;
    EXPECT_EQ(al01, BHSSU::ms.getByHourStatSimple(BHSSU::ms.STATS_SET_AMBLIGHT_BY_HOUR, nextHour));
    EXPECT_EQ(al01, BHSSU::ms.getByHourStatSimple(BHSSU::ms.STATS_SET_AMBLIGHT_BY_HOUR_SMOOTHED, nextHour));
    EXPECT_EQ(al01, BHSSU::ms.getByHourStatRTC(BHSSU::ms.STATS_SET_AMBLIGHT_BY_HOUR, nextHour));
    EXPECT_EQ(al01, BHSSU::ms.getByHourStatRTC(BHSSU::ms.STATS_SET_AMBLIGHT_BY_HOUR_SMOOTHED, nextHour));
    EXPECT_EQ(al01, BHSSU::ms.getByHourStatRTC(BHSSU::ms.STATS_SET_AMBLIGHT_BY_HOUR, BHSSU::ms.SPECIAL_HOUR_CURRENT_HOUR));
    EXPECT_EQ(al01, BHSSU::ms.getByHourStatRTC(BHSSU::ms.STATS_SET_AMBLIGHT_BY_HOUR_SMOOTHED, BHSSU::ms.SPECIAL_HOUR_CURRENT_HOUR));
    EXPECT_EQ(unset, BHSSU::ms.getByHourStatRTC(BHSSU::ms.STATS_SET_AMBLIGHT_BY_HOUR, BHSSU::ms.SPECIAL_HOUR_NEXT_HOUR));
    EXPECT_EQ(unset, BHSSU::ms.getByHourStatRTC(BHSSU::ms.STATS_SET_AMBLIGHT_BY_HOUR_SMOOTHED, BHSSU::ms.SPECIAL_HOUR_NEXT_HOUR));
    EXPECT_EQ(t0c, BHSSU::ms.getByHourStatSimple(BHSSU::ms.STATS_SET_TEMP_BY_HOUR, nextHour));
    EXPECT_EQ(t0c, BHSSU::ms.getByHourStatSimple(BHSSU::ms.STATS_SET_TEMP_BY_HOUR_SMOOTHED, nextHour));
    EXPECT_EQ(rh01, BHSSU::ms.getByHourStatSimple(BHSSU::ms.STATS_SET_RHPC_BY_HOUR, nextHour));
    EXPECT_EQ(rh01, BHSSU::ms.getByHourStatSimple(BHSSU::ms.STATS_SET_RHPC_BY_HOUR_SMOOTHED, nextHour));
    EXPECT_EQ(rh01, BHSSU::ms.getByHourStatRTC(BHSSU::ms.STATS_SET_RHPC_BY_HOUR, nextHour));
    EXPECT_EQ(rh01, BHSSU::ms.getByHourStatRTC(BHSSU::ms.STATS_SET_RHPC_BY_HOUR_SMOOTHED, nextHour));
    EXPECT_EQ(rh01, BHSSU::ms.getByHourStatRTC(BHSSU::ms.STATS_SET_RHPC_BY_HOUR, BHSSU::ms.SPECIAL_HOUR_CURRENT_HOUR));
    EXPECT_EQ(rh01, BHSSU::ms.getByHourStatRTC(BHSSU::ms.STATS_SET_RHPC_BY_HOUR_SMOOTHED, BHSSU::ms.SPECIAL_HOUR_CURRENT_HOUR));

    // Nominally roll round a day and check the first slot's values via the RTC view.
    BHSSU::ms._setHour(hourNow);
    EXPECT_EQ(al1, BHSSU::ms.getByHourStatRTC(BHSSU::ms.STATS_SET_AMBLIGHT_BY_HOUR, BHSSU::ms.SPECIAL_HOUR_CURRENT_HOUR));
    EXPECT_NEAR(sm_al1, BHSSU::ms.getByHourStatRTC(BHSSU::ms.STATS_SET_AMBLIGHT_BY_HOUR_SMOOTHED, BHSSU::ms.SPECIAL_HOUR_CURRENT_HOUR), 2);
    EXPECT_EQ(al01, BHSSU::ms.getByHourStatRTC(BHSSU::ms.STATS_SET_AMBLIGHT_BY_HOUR, BHSSU::ms.SPECIAL_HOUR_NEXT_HOUR));
    EXPECT_EQ(al01, BHSSU::ms.getByHourStatRTC(BHSSU::ms.STATS_SET_AMBLIGHT_BY_HOUR_SMOOTHED, BHSSU::ms.SPECIAL_HOUR_NEXT_HOUR));
}
コード例 #4
0
ファイル: UnitTest.cpp プロジェクト: jackhui/GE
TEST(Vector, Length)
{
	Vector3 v(3.0f, 0.0f, 4.0f);
	EXPECT_NEAR(5.0f, v.Length(), 0.01f);
}
コード例 #5
0
ファイル: UnitTest.cpp プロジェクト: jackhui/GE
TEST(Vector, OperatorMultiplication)
{
	Vector3 v(1.0f, 1.0f, 1.0f);
	v = v * 3.0f * 3.0f;
	EXPECT_NEAR(9.0f, v.GetZ(), 0.01f);
}
コード例 #6
0
TEST_F(FPDFTextEmbeddertest, WebLinks) {
  EXPECT_TRUE(OpenDocument("weblinks.pdf"));
  FPDF_PAGE page = LoadPage(0);
  EXPECT_TRUE(page);

  FPDF_TEXTPAGE textpage = FPDFText_LoadPage(page);
  EXPECT_TRUE(textpage);

  FPDF_PAGELINK pagelink = FPDFLink_LoadWebLinks(textpage);
  EXPECT_TRUE(pagelink);

  // Page contains two HTTP-style URLs.
  EXPECT_EQ(2, FPDFLink_CountWebLinks(pagelink));

  // Only a terminating NUL required for bogus links.
  EXPECT_EQ(1, FPDFLink_GetURL(pagelink, 2, nullptr, 0));
  EXPECT_EQ(1, FPDFLink_GetURL(pagelink, 1400, nullptr, 0));
  EXPECT_EQ(1, FPDFLink_GetURL(pagelink, -1, nullptr, 0));

  // Query the number of characters required for each link (incl NUL).
  EXPECT_EQ(25, FPDFLink_GetURL(pagelink, 0, nullptr, 0));
  EXPECT_EQ(26, FPDFLink_GetURL(pagelink, 1, nullptr, 0));

  static const char expected_url[] = "http://example.com?q=foo";
  static const size_t expected_len = sizeof(expected_url);
  unsigned short fixed_buffer[128];

  // Retrieve a link with too small a buffer.  Buffer will not be
  // NUL-terminated, but must not be modified past indicated length,
  // so pre-fill with a pattern to check write bounds.
  memset(fixed_buffer, 0xbd, sizeof(fixed_buffer));
  EXPECT_EQ(1, FPDFLink_GetURL(pagelink, 0, fixed_buffer, 1));
  EXPECT_TRUE(check_unsigned_shorts(expected_url, fixed_buffer, 1));
  EXPECT_EQ(0xbdbd, fixed_buffer[1]);

  // Check buffer that doesn't have space for a terminating NUL.
  memset(fixed_buffer, 0xbd, sizeof(fixed_buffer));
  EXPECT_EQ(static_cast<int>(expected_len - 1),
            FPDFLink_GetURL(pagelink, 0, fixed_buffer, expected_len - 1));
  EXPECT_TRUE(
      check_unsigned_shorts(expected_url, fixed_buffer, expected_len - 1));
  EXPECT_EQ(0xbdbd, fixed_buffer[expected_len - 1]);

  // Retreive link with exactly-sized buffer.
  memset(fixed_buffer, 0xbd, sizeof(fixed_buffer));
  EXPECT_EQ(static_cast<int>(expected_len),
            FPDFLink_GetURL(pagelink, 0, fixed_buffer, expected_len));
  EXPECT_TRUE(check_unsigned_shorts(expected_url, fixed_buffer, expected_len));
  EXPECT_EQ(0u, fixed_buffer[expected_len - 1]);
  EXPECT_EQ(0xbdbd, fixed_buffer[expected_len]);

  // Retreive link with ample-sized-buffer.
  memset(fixed_buffer, 0xbd, sizeof(fixed_buffer));
  EXPECT_EQ(static_cast<int>(expected_len),
            FPDFLink_GetURL(pagelink, 0, fixed_buffer, 128));
  EXPECT_TRUE(check_unsigned_shorts(expected_url, fixed_buffer, expected_len));
  EXPECT_EQ(0u, fixed_buffer[expected_len - 1]);
  EXPECT_EQ(0xbdbd, fixed_buffer[expected_len]);

  // Each link rendered in a single rect in this test page.
  EXPECT_EQ(1, FPDFLink_CountRects(pagelink, 0));
  EXPECT_EQ(1, FPDFLink_CountRects(pagelink, 1));

  // Each link rendered in a single rect in this test page.
  EXPECT_EQ(0, FPDFLink_CountRects(pagelink, -1));
  EXPECT_EQ(0, FPDFLink_CountRects(pagelink, 2));
  EXPECT_EQ(0, FPDFLink_CountRects(pagelink, 10000));

  // Check boundary of valid link index with valid rect index.
  double left = 0.0;
  double right = 0.0;
  double top = 0.0;
  double bottom = 0.0;
  FPDFLink_GetRect(pagelink, 0, 0, &left, &top, &right, &bottom);
  EXPECT_NEAR(50.791, left, 0.001);
  EXPECT_NEAR(187.963, right, 0.001);
  EXPECT_NEAR(97.624, bottom, 0.001);
  EXPECT_NEAR(108.736, top, 0.001);

  // Check that valid link with invalid rect index leaves parameters unchanged.
  left = -1.0;
  right = -1.0;
  top = -1.0;
  bottom = -1.0;
  FPDFLink_GetRect(pagelink, 0, 1, &left, &top, &right, &bottom);
  EXPECT_EQ(-1.0, left);
  EXPECT_EQ(-1.0, right);
  EXPECT_EQ(-1.0, bottom);
  EXPECT_EQ(-1.0, top);

  // Check that invalid link index leaves parameters unchanged.
  left = -2.0;
  right = -2.0;
  top = -2.0;
  bottom = -2.0;
  FPDFLink_GetRect(pagelink, -1, 0, &left, &top, &right, &bottom);
  EXPECT_EQ(-2.0, left);
  EXPECT_EQ(-2.0, right);
  EXPECT_EQ(-2.0, bottom);
  EXPECT_EQ(-2.0, top);

  FPDFLink_CloseWebLinks(pagelink);
  FPDFText_ClosePage(textpage);
  UnloadPage(page);
}
コード例 #7
0
ファイル: dense_e_metric_test.cpp プロジェクト: PerryZh/stan
TEST(McmcDenseEMetric, gradients) {  
  rng_t base_rng(0);
  
  Eigen::VectorXd q = Eigen::VectorXd::Ones(11);
  
  stan::mcmc::dense_e_point z(q.size());
  z.q = q;
  z.p.setOnes();
  
  std::fstream data_stream(std::string("").c_str(), std::fstream::in);
  stan::io::dump data_var_context(data_stream);
  data_stream.close();
  

  std::stringstream model_output, metric_output;

  funnel_model_namespace::funnel_model model(data_var_context, &model_output);
  
  stan::mcmc::dense_e_metric<funnel_model_namespace::funnel_model, rng_t> metric(model, &metric_output);
  
  double epsilon = 1e-6;
  
  metric.update(z);
  
  Eigen::VectorXd g1 = metric.dtau_dq(z);
  
  for (int i = 0; i < z.q.size(); ++i) {
    
    double delta = 0;
    
    z.q(i) += epsilon;
    metric.update(z);
    delta += metric.tau(z);
    
    z.q(i) -= 2 * epsilon;
    metric.update(z);
    delta -= metric.tau(z);
    
    z.q(i) += epsilon;
    metric.update(z);
    
    delta /= 2 * epsilon;
    
    EXPECT_NEAR(delta, g1(i), epsilon);
    
  }
  
  Eigen::VectorXd g2 = metric.dtau_dp(z);
  
  for (int i = 0; i < z.q.size(); ++i) {
    
    double delta = 0;
    
    z.p(i) += epsilon;
    delta += metric.tau(z);
    
    z.p(i) -= 2 * epsilon;
    delta -= metric.tau(z);
    
    z.p(i) += epsilon;
    
    delta /= 2 * epsilon;
    
    EXPECT_NEAR(delta, g2(i), epsilon);
    
  }
  
  Eigen::VectorXd g3 = metric.dphi_dq(z);
  
  for (int i = 0; i < z.q.size(); ++i) {
    
    double delta = 0;
    
    z.q(i) += epsilon;
    metric.update(z);
    delta += metric.phi(z);
    
    z.q(i) -= 2 * epsilon;
    metric.update(z);
    delta -= metric.phi(z);
    
    z.q(i) += epsilon;
    metric.update(z);
    
    delta /= 2 * epsilon;
    
    EXPECT_NEAR(delta, g3(i), epsilon);
    
  }


  EXPECT_EQ("", model_output.str());
  EXPECT_EQ("", metric_output.str());
}
コード例 #8
0
TEST(Test2DGPUGpuNUFFTConv,GPUTest_FactorTwoTest)
{
	//oversampling ratio
	float osr = DEFAULT_OVERSAMPLING_RATIO;
	//kernel width
	int kernel_width = 3;

	//Image
	int im_width = 16;

	//Data
	int data_entries = 5;
    DType2* data = (DType2*) calloc(data_entries,sizeof(DType2)); //2* re + im
	int data_cnt = 0;
	data[data_cnt].x = 0.5f;
	data[data_cnt++].y = 0.5f;
	
	data[data_cnt].x = 0.5f;
	data[data_cnt++].y = 0.5f;
	
	data[data_cnt].x = 1;
	data[data_cnt++].y = 1;

	data[data_cnt].x = 1;
	data[data_cnt++].y = 1;

	data[data_cnt].x = 1;
	data[data_cnt++].y = 1;

	//Coords
	//Scaled between -0.5 and 0.5
	//in triplets (x,y,z)
    DType* coords = (DType*) calloc(2*data_entries,sizeof(DType));//2* x,y
	int coord_cnt = 0;
	//7.Sektor
	coords[coord_cnt++] = 0; 
	coords[coord_cnt++] = 0;
	coords[coord_cnt++] = 0;
	coords[coord_cnt++] = 0.5f; 
	coords[coord_cnt++] = 0.3f;
	
	coords[coord_cnt++] = 0;
	coords[coord_cnt++] = 0;
	coords[coord_cnt++] = 0;
	coords[coord_cnt++] = 0; 
	coords[coord_cnt++] = 0.3f;
	
	//sectors of data, count and start indices
	int sector_width = 8;
	
    gpuNUFFT::Array<DType> kSpaceData;
    kSpaceData.data = coords;
    kSpaceData.dim.length = data_entries;

	gpuNUFFT::Dimensions imgDims(im_width,im_width);

    gpuNUFFT::GpuNUFFTOperatorFactory factory; gpuNUFFT::GpuNUFFTOperator *gpuNUFFTOp = factory.createGpuNUFFTOperator(kSpaceData,kernel_width,sector_width,osr,imgDims);

	gpuNUFFT::Array<DType2> dataArray;
	dataArray.data = data;
	dataArray.dim.length = data_entries;

	gpuNUFFT::Array<CufftType> gdataArray;
	
	gdataArray = gpuNUFFTOp->performGpuNUFFTAdj(dataArray,gpuNUFFT::CONVOLUTION);
	//Output Grid
	CufftType* gdata = gdataArray.data;

	int index = get2DC2lin(5,5,im_width);
	if (DEBUG) printf("index to test %d\n",index);
	
	EXPECT_NEAR(gdata[get2DC2lin(8,8,16)].x,2.0f,epsilon);

	free(data);
	free(coords);
	free(gdata);

	delete gpuNUFFTOp;
}
コード例 #9
0
TEST(Test2DGPUGpuNUFFTConv,GPUTest_8SectorsKernel3nDataw32)
{
	//oversampling ratio
	float osr = DEFAULT_OVERSAMPLING_RATIO;
	//kernel width
	int kernel_width = 3;

	//Image
	int im_width = 32;

	//Data
	int data_entries = 5;
    DType2* data = (DType2*) calloc(data_entries,sizeof(DType2)); //2* re + im
	int data_cnt = 0;
	
	data[data_cnt].x = 0.5f;
	data[data_cnt++].y = 0;
	
	data[data_cnt].x = 0.7f;
	data[data_cnt++].y = 0;

	data[data_cnt].x = -0.2f;
	data[data_cnt++].y = 0.8f;
	
	data[data_cnt].x = -0.2f;
	data[data_cnt++].y = 0.8f;

	data[data_cnt].x = 1;
  data[data_cnt++].y = 0;


	//Coords
	//Scaled between -0.5 and 0.5
	//in triplets (x,y,z)
    DType* coords = (DType*) calloc(3*data_entries,sizeof(DType));//2* x,y
	int coord_cnt = 0;
	coords[coord_cnt++] = -0.3f; 
	coords[coord_cnt++] = -0.1f;
	coords[coord_cnt++] = 0; 
	coords[coord_cnt++] = 0;
	coords[coord_cnt++] = 0.5f; 

	coords[coord_cnt++] = 0.2f;
	coords[coord_cnt++] = 0;
	coords[coord_cnt++] = 0;
	coords[coord_cnt++] = 0;
	coords[coord_cnt++] = 0;

	//sectors of data, count and start indices
	int sector_width = 8;
	
    gpuNUFFT::Array<DType> kSpaceData;
    kSpaceData.data = coords;
    kSpaceData.dim.length = data_entries;

	gpuNUFFT::Dimensions imgDims(im_width,im_width);

    gpuNUFFT::GpuNUFFTOperatorFactory factory; gpuNUFFT::GpuNUFFTOperator *gpuNUFFTOp = factory.createGpuNUFFTOperator(kSpaceData,kernel_width,sector_width,osr,imgDims);

	gpuNUFFT::Array<DType2> dataArray;
	dataArray.data = data;
	dataArray.dim.length = data_entries;

	gpuNUFFT::Array<CufftType> gdataArray;
	
	gdataArray = gpuNUFFTOp->performGpuNUFFTAdj(dataArray,gpuNUFFT::CONVOLUTION);
	//Output Grid
	CufftType* gdata = gdataArray.data;

    //gpuNUFFT_gpu_adj(data,data_entries,1,coords,&gdata,grid_size,dims_g[1],kern,kernel_entries, kernel_width,sectors,sector_count,sector_centers,sector_width, im_width,osr,false,NULL,CONVOLUTION);

	/*for (int j=0; j<im_width; j++)
	{
		for (int i=0; i<im_width; i++)
		{
			float dpr = gdata[get2DC2lin(i,im_width-1-j,16,im_width)].x;
			float dpi = gdata[get2DC2lin(i,im_width-1-j,16,im_width)].y;

			if (abs(dpr) > 0.0f)
				printf("(%d,%d)= %.4f + %.4f i ",i,im_width-1-j,dpr,dpi);
		}
		printf("\n");
	}*/

	EXPECT_NEAR(gdata[get2DC2lin(12,16,im_width)].x,0.4289f,epsilon);
	EXPECT_NEAR(gdata[get2DC2lin(13,16,im_width)].x,0.6803f,epsilon);
	EXPECT_NEAR(gdata[get2DC2lin(14,16,im_width)].x,0.2065f,epsilon);
	EXPECT_NEAR(gdata[get2DC2lin(15,16,im_width)].x,-0.1801f,epsilon);//Re
	EXPECT_NEAR(gdata[get2DC2lin(15,16,im_width)].y,0.7206f,epsilon);//Im
	EXPECT_NEAR(gdata[get2DC2lin(16,16,im_width)].x,-0.4f,epsilon);
	EXPECT_NEAR(gdata[get2DC2lin(16,16,im_width)].y,1.6f,epsilon);
  EXPECT_NEAR(gdata[get2DC2lin(17,16,im_width)].x,-0.1801f,epsilon);//Re
	EXPECT_NEAR(gdata[get2DC2lin(17,16,im_width)].y,0.7206f,epsilon);//Im

	EXPECT_NEAR(gdata[get2DC2lin(12,15,im_width)].x,0.1932f,epsilon);
	EXPECT_NEAR(gdata[get2DC2lin(14,17,im_width)].x,0.0930f,epsilon);
	
	free(data);
	free(coords);
	free(gdata);

	delete gpuNUFFTOp;
}
コード例 #10
0
TEST(Test2DGPUGpuNUFFTConv,GPUTest_2SectorsKernel3nData)
{
	//oversampling ratio
	float osr = DEFAULT_OVERSAMPLING_RATIO;
	//kernel width
	int kernel_width = 3;

	//Image
	int im_width = 10;

	//Data
	int data_entries = 5;
    DType2* data = (DType2*) calloc(data_entries,sizeof(DType2)); //2* re + im
	int data_cnt = 0;
	data[data_cnt].x = 0.5f;
	data[data_cnt++].y = 0.5f;
	
	data[data_cnt].x = 0.7f;
	data[data_cnt++].y = 1;
	
	data[data_cnt].x = 1;
	data[data_cnt++].y = 1;

	data[data_cnt].x = 1;
	data[data_cnt++].y = 1;

	data[data_cnt].x = 1;
	data[data_cnt++].y = 1;

	//Coords
	//Scaled between -0.5 and 0.5
	//in triplets (x,y,z)
    DType* coords = (DType*) calloc(2*data_entries,sizeof(DType));//2* x,y
	int coord_cnt = 0;
	//1.Sektor
	coords[coord_cnt++] = -0.3f; //x
	coords[coord_cnt++] = -0.1f; 
	coords[coord_cnt++] = 0;    
	coords[coord_cnt++] = 0.5f; 
	coords[coord_cnt++] = 0.3f;

	coords[coord_cnt++] = 0.2f; //y
	coords[coord_cnt++] = 0;
	coords[coord_cnt++] = 0;
	coords[coord_cnt++] = 0;
	coords[coord_cnt++] = 0.3f;
	
	//sectors of data, count and start indices
	int sector_width = 5;
	
	gpuNUFFT::Array<DType> kSpaceData;
    kSpaceData.data = coords;
    kSpaceData.dim.length = data_entries;

	gpuNUFFT::Dimensions imgDims(im_width,im_width);
	
    gpuNUFFT::GpuNUFFTOperatorFactory factory; gpuNUFFT::GpuNUFFTOperator *gpuNUFFTOp = factory.createGpuNUFFTOperator(kSpaceData,kernel_width,sector_width,osr,imgDims);

	gpuNUFFT::Array<DType2> dataArray;
	dataArray.data = data;
	dataArray.dim.length = data_entries;

	gpuNUFFT::Array<CufftType> gdataArray;
	
	gdataArray = gpuNUFFTOp->performGpuNUFFTAdj(dataArray,gpuNUFFT::CONVOLUTION);
	//Output Grid
	CufftType* gdata = gdataArray.data;

	//gpuNUFFT_gpu_adj(data,data_entries,1,coords,&gdata,grid_size,dims_g[1],kern,kernel_entries, kernel_width,sectors,sector_count,sector_centers,sector_width, im_width,osr,false,NULL,CONVOLUTION);

	int index = get2DC2lin(5,5,im_width);
	if (DEBUG) printf("index to test %d\n",index);
	//EXPECT_EQ(index,2*555);
	EXPECT_NEAR(1.3152f,gdata[index].x,epsilon);
	EXPECT_NEAR(0.2432,gdata[get2DC2lin(3,6,im_width)].x,epsilon*10.0f);
	
	EXPECT_NEAR(0.2251,gdata[get2DC2lin(1,7,im_width)].x,epsilon*10.0f);
	EXPECT_NEAR(0.4502,gdata[get2DC2lin(6,5,im_width)].x,epsilon*10.0f);

	EXPECT_NEAR(1.0f,gdata[get2DC2lin(8,8,im_width)].x,epsilon*10.0f);
	EXPECT_NEAR(0.2027,gdata[get2DC2lin(9,9,im_width)].x,epsilon*10.0f);
	
	//for (int j=0; j<im_width; j++)
	//{
	//	for (int i=0; i<im_width; i++)
	//		printf("%.4f ",gdata[get2DC2lin(i,im_width-1-j,5,im_width)]);
	//	printf("\n");
	//}

	free(data);
	free(coords);
	free(gdata);
	
	delete gpuNUFFTOp;
}
コード例 #11
0
TEST(Test2DGPUGpuNUFFTConv,GPUTest_8SectorsKernel5nData)
{
	//oversampling ratio
	float osr = DEFAULT_OVERSAMPLING_RATIO;

	//kernel width
	int kernel_width = 5;

	//Image
	int im_width = 10;

	//Data
	int data_entries = 5;
    DType2* data = (DType2*) calloc(data_entries,sizeof(DType2)); //2* re + im
	int data_cnt = 0;
	data[data_cnt].x = 0.5f;
	data[data_cnt++].y = 0.5f;
	
	data[data_cnt].x = 0.7f;
	data[data_cnt++].y = 1;
	
	data[data_cnt].x = 1;
	data[data_cnt++].y = 1;

	data[data_cnt].x = 1;
	data[data_cnt++].y = 1;

	data[data_cnt].x = 1;
	data[data_cnt++].y = 1;

	//Coords
	//Scaled between -0.5 and 0.5
	//in triplets (x,y,z)
    DType* coords = (DType*) calloc(3*data_entries,sizeof(DType));//2* x,y
	int coord_cnt = 0;
	//7.Sektor
	coords[coord_cnt++] = -0.3f; 
	coords[coord_cnt++] = -0.1f;
	coords[coord_cnt++] = 0; 
	coords[coord_cnt++] = 0.5f; 
	coords[coord_cnt++] = 0.3f;

	coords[coord_cnt++] = 0.2f;
	coords[coord_cnt++] = 0;
	coords[coord_cnt++] = 0;
	coords[coord_cnt++] = 0;
	coords[coord_cnt++] = 0.3f;

	//sectors of data, count and start indices
	int sector_width = 5;

	gpuNUFFT::Array<DType> kSpaceData;
    kSpaceData.data = coords;
    kSpaceData.dim.length = data_entries;

	gpuNUFFT::Dimensions imgDims(im_width,im_width);

    gpuNUFFT::GpuNUFFTOperatorFactory factory; gpuNUFFT::GpuNUFFTOperator *gpuNUFFTOp = factory.createGpuNUFFTOperator(kSpaceData,kernel_width,sector_width,osr,imgDims);

	gpuNUFFT::Array<DType2> dataArray;
	dataArray.data = data;
	dataArray.dim.length = data_entries;

	gpuNUFFT::Array<CufftType> gdataArray;
	
	gdataArray = gpuNUFFTOp->performGpuNUFFTAdj(dataArray,gpuNUFFT::CONVOLUTION);
	//Output Grid
	CufftType* gdata = gdataArray.data;

	int index = get2DC2lin(5,5,im_width);
	if (DEBUG) printf("index to test %d\n",index);
	
	EXPECT_NEAR(1.3976f,gdata[index].x,epsilon);
	EXPECT_NEAR(0.4268f,gdata[get2DC2lin(3,6,im_width)].x,epsilon*10.0f);
	
	EXPECT_NEAR(0.0430,gdata[get2DC2lin(6,3,im_width)].x,epsilon*10.0f);
	
	EXPECT_NEAR(0.1093f,gdata[get2DC2lin(8,6,im_width)].x,epsilon*10.0f);
	
  if (DEBUG)
	  for (int j=0; j<im_width; j++)
	  {
		  for (int i=0; i<im_width; i++)
			  printf("%.4f ",gdata[get2DC2lin(i,im_width-1-j,im_width)].x);
		  printf("\n");
	  }

	free(data);
	free(coords);
	free(gdata);

	delete gpuNUFFTOp;
}
コード例 #12
0
TEST_P(ADXL362_SpiAccelerometerTest, TestAccelerometer) {
  const double EPSILON = 1 / 256.0;

  frc::SPI::Port port = GetParam();

  hal::ADXL362_SpiAccelerometer sim{port};
  frc::ADXL362 accel{port};

  EXPECT_NEAR(0, sim.GetX(), EPSILON);
  EXPECT_NEAR(0, sim.GetY(), EPSILON);
  EXPECT_NEAR(0, sim.GetZ(), EPSILON);

  EXPECT_NEAR(0, accel.GetX(), EPSILON);
  EXPECT_NEAR(0, accel.GetY(), EPSILON);
  EXPECT_NEAR(0, accel.GetZ(), EPSILON);

  sim.SetX(1.56);
  sim.SetY(-.653);
  sim.SetZ(0.11);

  EXPECT_NEAR(1.56, sim.GetX(), EPSILON);
  EXPECT_NEAR(-.653, sim.GetY(), EPSILON);
  EXPECT_NEAR(0.11, sim.GetZ(), EPSILON);

  EXPECT_NEAR(1.56, accel.GetX(), EPSILON);
  EXPECT_NEAR(-.653, accel.GetY(), EPSILON);
  EXPECT_NEAR(0.11, accel.GetZ(), EPSILON);
}
コード例 #13
0
ファイル: test_quant_blas.cpp プロジェクト: naibaf7/caffe
TYPED_TEST(QuantBlasTest, TestAxpbyComparativeFloatQuant) {
  typedef typename TypeParam::Dtype Dtype;

  // Expect at most 5% error
  float percentile_eps = 0.05;

  std::random_device rdev;
  std::mt19937 rngen(rdev());

  // Need to test > 64 dimension
  std::uniform_int_distribution<int_tp> dimsRand(1, 256);
  std::uniform_int_distribution<int_tp> boolRand(0, 1);
  std::uniform_int_distribution<int_tp> factorRand(-25, 25);
  std::uniform_real_distribution<float> valRand(-2.0, 2.0);


  for (int_tp testIdx = 0; testIdx < 25; ++testIdx) {
    int_tp N = dimsRand(rngen);

    bool has_alpha = boolRand(rngen);
    bool has_beta = has_alpha ? boolRand(rngen) : true;

    bool alpha_with_quant = boolRand(rngen) && has_alpha;
    bool beta_with_quant = boolRand(rngen) && has_beta;

    float alpha_val;
    float beta_val;

    if (has_alpha) {
      alpha_val = alpha_with_quant ? valRand(rngen) : float(1.0);
    } else {
      alpha_val = 0.0;
    }

    if (has_beta) {
      beta_val = beta_with_quant ? valRand(rngen) : float(1.0);
    } else {
      beta_val = 0.0;
    }

    vector<int_tp> x_shape(1, 1);
    vector<int_tp> y_shape(1, 1);

    x_shape[0] = N;
    y_shape[0] = N;

    Blob<float> x(x_shape, Caffe::GetDefaultDevice());
    Blob<float> y(y_shape, Caffe::GetDefaultDevice());
    Blob<float> y_result(y_shape, Caffe::GetDefaultDevice());

    Blob<Dtype> x_quant(x_shape, Caffe::GetDefaultDevice());
    Blob<Dtype> y_quant(y_shape, Caffe::GetDefaultDevice());

    Blob<float> y_unquant(y_shape, Caffe::GetDefaultDevice());


    caffe_rng_gaussian(N, (float)0.0, (float)0.5, x.mutable_cpu_data());
    caffe_rng_gaussian(N, (float)0.0, (float)0.5, y.mutable_cpu_data());

    caffe_copy(N, y.cpu_data(), y_result.mutable_cpu_data());

    QuantizerParameter qpm_x;
    QuantizerParameter qpm_y;
    QuantizerParameter qpm_alpha;
    QuantizerParameter qpm_beta;
    qpm_x.set_mode(CAFFE_QUANT_OBSERVE);
    qpm_y.set_mode(CAFFE_QUANT_OBSERVE);
    qpm_alpha.set_mode(CAFFE_QUANT_OBSERVE);
    qpm_beta.set_mode(CAFFE_QUANT_OBSERVE);

    Quantizer<float, Dtype> xq(qpm_x);
    Quantizer<float, Dtype> yq(qpm_y);
    Quantizer<float, Dtype> alphaq(qpm_alpha);
    Quantizer<float, Dtype> betaq(qpm_beta);

    // Normal GEMM
    caffe_axpby<float>(N, alpha_val, x.cpu_data(), beta_val,
                       y_result.mutable_cpu_data());


    // Observe all values that will be relevant for quantization
    xq.ObserveIn_cpu(N, x.cpu_data());
    yq.ObserveIn_cpu(N, y.cpu_data());
    yq.ObserveIn_cpu(N, y_result.cpu_data());
    alphaq.ObserveIn_cpu(1, &alpha_val);
    betaq.ObserveIn_cpu(1, &beta_val);

    // Apply observed values to the quantizer
    xq.update();
    yq.update();
    alphaq.update();
    betaq.update();

    // Quantize A, B and C
    xq.Forward_cpu(N, x.cpu_data(), x_quant.mutable_cpu_data());
    yq.Forward_cpu(N, y.cpu_data(), y_quant.mutable_cpu_data());

    Dtype alpha_val_quant = has_alpha;
    Dtype beta_val_quant = has_beta;

    // Quantize alpha
    if (alpha_with_quant) {
      alphaq.Forward_cpu(1, &alpha_val, &alpha_val_quant);
    }

    // Quantize beta
    if (beta_with_quant) {
      betaq.Forward_cpu(1, &beta_val, &beta_val_quant);
    }

    if (Caffe::mode() == Caffe::Brew::CPU) {
      // TODO: Not implemented yet
      return;
      /*caffe_axpby<Dtype>(N, alpha_val_quant, x_quant.cpu_data(),
                  beta_val_quant, y_quant.mutable_cpu_data(),
                  alpha_with_quant ? &(alphaq.out_quantizer_values()) : nullptr,
                  &(xq.out_quantizer_values()),
                  beta_with_quant ? &(betaq.out_quantizer_values()) : nullptr,
                  &(yq.out_quantizer_values()));*/
    } else {
      Caffe::GetDefaultDevice()->template axpby<Dtype>(N,
                  alpha_val_quant, x_quant.gpu_data(),
                  beta_val_quant, y_quant.mutable_gpu_data(),
                  alpha_with_quant ? &(alphaq.out_quantizer_values()) : nullptr,
                  &(xq.out_quantizer_values()),
                  beta_with_quant ? &(betaq.out_quantizer_values()) : nullptr,
                  &(yq.out_quantizer_values()));
    }

    yq.Backward_cpu(N, y_quant.cpu_data(), y_unquant.mutable_cpu_data());

    const QuantizerValues cqv = yq.in_quantizer_values();
    float eps = std::max(std::abs(cqv.get_max<float>()),
                         std::abs(cqv.get_min<float>())) * percentile_eps;

    for (int_tp i = 0; i < N; ++i) {
      EXPECT_NEAR(y_unquant.cpu_data()[i], y_result.cpu_data()[i], eps);
      // One error is enough to abort
      if (fabs(y_unquant.cpu_data()[i] - y_result.cpu_data()[i]) >= eps) {
        break;
      }
    }
  }
}
コード例 #14
0
ファイル: test_quant_blas.cpp プロジェクト: naibaf7/caffe
TYPED_TEST(QuantBlasTest, TestGemmComparativeFloatQuant) {
  typedef typename TypeParam::Dtype Dtype;

  // Expect at most 5% error
  float percentile_eps = 0.05;

  std::random_device rdev;
  std::mt19937 rngen(rdev());

  // Need to test > 64 dimension
  std::uniform_int_distribution<int_tp> dimsRand(1, 256);
  std::uniform_int_distribution<int_tp> boolRand(0, 1);
  std::uniform_int_distribution<int_tp> factorRand(-25, 25);
  std::uniform_real_distribution<float> valRand(-2.0, 2.0);


  for (int_tp testIdx = 0; testIdx < 25; ++testIdx) {
    int_tp M = dimsRand(rngen);
    int_tp N = dimsRand(rngen);
    int_tp K = dimsRand(rngen);

    CBLAS_TRANSPOSE trans_A = boolRand(rngen) ? CblasTrans : CblasNoTrans;
    CBLAS_TRANSPOSE trans_B = boolRand(rngen) ? CblasTrans : CblasNoTrans;

    bool has_alpha = boolRand(rngen);
    bool has_beta = has_alpha ? boolRand(rngen) : true;

    bool alpha_with_quant = boolRand(rngen) && has_alpha;
    bool beta_with_quant = boolRand(rngen) && has_beta;

    float alpha_val;
    float beta_val;

    if (has_alpha) {
      alpha_val = alpha_with_quant ? valRand(rngen) : float(1.0);
    } else {
      alpha_val = 0.0;
    }

    if (has_beta) {
      beta_val = beta_with_quant ? valRand(rngen) : float(1.0);
    } else {
      beta_val = 0.0;
    }

    vector<int_tp> A_shape(4, 1);
    vector<int_tp> B_shape(4, 1);
    vector<int_tp> C_shape(4, 1);

    A_shape[2] = M;
    A_shape[3] = K;
    B_shape[2] = K;
    B_shape[3] = N;
    C_shape[2] = M;
    C_shape[3] = N;

    Blob<float> A(A_shape, Caffe::GetDefaultDevice());
    Blob<float> B(B_shape, Caffe::GetDefaultDevice());
    Blob<float> C(C_shape, Caffe::GetDefaultDevice());
    Blob<float> C_result(C_shape, Caffe::GetDefaultDevice());

    Blob<Dtype> A_quant(A_shape, Caffe::GetDefaultDevice());
    Blob<Dtype> B_quant(B_shape, Caffe::GetDefaultDevice());
    Blob<Dtype> C_quant(C_shape, Caffe::GetDefaultDevice());

    Blob<float> C_unquant(C_shape, Caffe::GetDefaultDevice());


    caffe_rng_gaussian(M * K, (float)0.0, (float)0.5,
                       A.mutable_cpu_data());
    caffe_rng_gaussian(K * N, (float)0.0, (float)0.5,
                       B.mutable_cpu_data());
    caffe_rng_gaussian(M * N, (float)0.0, (float)0.5,
                       C.mutable_cpu_data());

    caffe_copy(M * N, C.cpu_data(), C_result.mutable_cpu_data());

    QuantizerParameter qpm_a;
    QuantizerParameter qpm_b;
    QuantizerParameter qpm_c;
    QuantizerParameter qpm_alpha;
    QuantizerParameter qpm_beta;
    qpm_a.set_mode(CAFFE_QUANT_OBSERVE);
    qpm_b.set_mode(CAFFE_QUANT_OBSERVE);
    qpm_c.set_mode(CAFFE_QUANT_OBSERVE);
    qpm_alpha.set_mode(CAFFE_QUANT_OBSERVE);
    qpm_beta.set_mode(CAFFE_QUANT_OBSERVE);

    Quantizer<float, Dtype> aq(qpm_a);
    Quantizer<float, Dtype> bq(qpm_b);
    Quantizer<float, Dtype> cq(qpm_c);
    Quantizer<float, Dtype> alphaq(qpm_alpha);
    Quantizer<float, Dtype> betaq(qpm_beta);

    // Normal GEMM
    caffe_gemm<float>(
                trans_A, trans_B,
                M, N, K,
                alpha_val,
                A.cpu_data(), B.cpu_data(),
                beta_val,
                C_result.mutable_cpu_data());


    // Observe all values that will be relevant for quantization
    aq.ObserveIn_cpu(M * K, A.cpu_data());
    bq.ObserveIn_cpu(K * N, B.cpu_data());
    cq.ObserveIn_cpu(M * N, C.cpu_data());
    cq.ObserveIn_cpu(M * N, C_result.cpu_data());
    alphaq.ObserveIn_cpu(1, &alpha_val);
    betaq.ObserveIn_cpu(1, &beta_val);

    // Apply observed values to the quantizer
    aq.update();
    bq.update();
    cq.update();
    alphaq.update();
    betaq.update();

    // Quantize A, B and C
    aq.Forward_cpu(M * K, A.cpu_data(), A_quant.mutable_cpu_data());
    bq.Forward_cpu(K * N, B.cpu_data(), B_quant.mutable_cpu_data());
    cq.Forward_cpu(M * N, C.cpu_data(), C_quant.mutable_cpu_data());

    Dtype alpha_val_quant = has_alpha;
    Dtype beta_val_quant = has_beta;

    // Quantize alpha
    if (alpha_with_quant) {
      alphaq.Forward_cpu(1, &alpha_val, &alpha_val_quant);
    }

    // Quantize beta
    if (beta_with_quant) {
      betaq.Forward_cpu(1, &beta_val, &beta_val_quant);
    }

    /*
    std::cout << "C max:" << cq.in_quantizer_values().max << std::endl;
    std::cout << "C min:" << cq.in_quantizer_values().min << std::endl;
    std::cout << "C zero:" << cq.in_quantizer_values().zero << std::endl;
    std::cout << "C scale:" << cq.in_quantizer_values().scale << std::endl;
    std::cout << "C max:" << cq.out_quantizer_values().max << std::endl;
    std::cout << "C min:" << cq.out_quantizer_values().min << std::endl;
    std::cout << "C zero:" << cq.out_quantizer_values().zero << std::endl;
    std::cout << "C scale:" <<  cq.out_quantizer_values().scale << std::endl;
    */

    if (Caffe::mode() == Caffe::Brew::CPU) {
      caffe_gemm<Dtype>(
                  trans_A, trans_B,
                  M, N, K,
                  alpha_val_quant,
                  A_quant.cpu_data(), B_quant.cpu_data(),
                  beta_val_quant,
                  C_quant.mutable_cpu_data(),
                  alpha_with_quant ? &(alphaq.out_quantizer_values()) : nullptr,
                  &(aq.out_quantizer_values()),
                  &(bq.out_quantizer_values()),
                  beta_with_quant ? &(betaq.out_quantizer_values()) : nullptr,
                  &(cq.out_quantizer_values()));
    } else {
      Caffe::GetDefaultDevice()->template gemm<Dtype>(trans_A, trans_B,
                  M, N, K,
                  alpha_val_quant,
                  A_quant.gpu_data(), B_quant.gpu_data(),
                  beta_val_quant,
                  C_quant.mutable_gpu_data(),
                  alpha_with_quant ? &(alphaq.out_quantizer_values()) : nullptr,
                  &(aq.out_quantizer_values()),
                  &(bq.out_quantizer_values()),
                  beta_with_quant ? &(betaq.out_quantizer_values()) : nullptr,
                  &(cq.out_quantizer_values()));
    }

    cq.Backward_cpu(M * N, C_quant.cpu_data(), C_unquant.mutable_cpu_data());

    // print_matrix(A_quant.cpu_data(), M, K);
    // print_matrix(B_quant.cpu_data(), K, N);

    // print_matrix(C_quant.cpu_data(), M, N);
    // print_matrix(C_result.cpu_data(), M, N);
    // print_matrix(C_unquant.cpu_data(), M, N);

    const QuantizerValues cqv = cq.in_quantizer_values();
    float eps = std::max(std::abs(cqv.get_max<float>()),
                         std::abs(cqv.get_min<float>())) * percentile_eps;

    for (int_tp i = 0; i < M * N; ++i) {
      EXPECT_NEAR(C_unquant.cpu_data()[i], C_result.cpu_data()[i], eps);
      // One error is enough to abort
      if (fabs(C_unquant.cpu_data()[i] - C_result.cpu_data()[i]) >= eps) {
        break;
      }
    }
  }
}
コード例 #15
0
ファイル: test_FlukaFuncs.cpp プロジェクト: ejwilson3/DAGMC
//---------------------------------------------------------------------------//
// Test distance to next surface or vertex for various directions
TEST_F(FluDAGTest, GFireGoodPropStep)
{
  // std::cout << "Calling g_fire. Start in middle of leftmost cube" << std::endl;  
  oldReg   = 2;
  point[2] = 5.0;
  // Set prepStep to something more realistic than 0.0 
  propStep = 1e38;
  
  // +z direction
  dir[2]   = 1.0;
  g_fire(oldReg, point, dir, propStep, retStep, safety, newReg);
  std::cout << "newReg is " << newReg << std::endl;
  // Start in middle of 10x10x10 cube, expect 10/2 to be dist. to next surface
  EXPECT_EQ(5.0, retStep);
  // -z direction
  dir[2] = -dir[2];
  g_fire(oldReg, point, dir, propStep, retStep, safety, newReg);
  EXPECT_EQ(5.0, retStep);
  // +y direction
  dir[2] = 0.0;
  dir[1] = 1.0;
  g_fire(oldReg, point, dir, propStep, retStep, safety, newReg);
  EXPECT_EQ(5.0, retStep);
  // -y direction
  dir[1] = -dir[1];
  g_fire(oldReg, point, dir, propStep, retStep, safety, newReg);
  EXPECT_EQ(5.0, retStep);
  // +x direction
  dir[1] = 0.0;
  dir[0] = 1.0;
  g_fire(oldReg, point, dir, propStep, retStep, safety, newReg);
  EXPECT_EQ(5.0, retStep);
  // -x direction
  dir[0] = -dir[0];
  g_fire(oldReg, point, dir, propStep, retStep, safety, newReg);
  EXPECT_EQ(5.0, retStep);

  // +++
  dir[0] = +dir_norm;
  dir[1] = +dir_norm;
  dir[2] = +dir_norm;
  g_fire(oldReg, point, dir, propStep, retStep, safety, newReg);
  EXPECT_NEAR(8.660254, retStep, 1e-6);

  // ++-
  // Not Lost Particle!
  dir[0] = +dir_norm;
  dir[1] = +dir_norm;
  dir[2] = -dir_norm;
  g_fire(oldReg, point, dir, propStep, retStep, safety, newReg);
  EXPECT_NEAR(8.660254, retStep, 1e-6);
  
  // +-+
  dir[0] = +dir_norm;
  dir[1] = -dir_norm;
  dir[2] = +dir_norm;
  g_fire(oldReg, point, dir, propStep, retStep, safety, newReg);
  EXPECT_NEAR(8.660254, retStep, 1e-6);

  // +--
  // Not Lost Particle!
  dir[0] = +dir_norm;
  dir[1] = -dir_norm;
  dir[2] = -dir_norm;
  g_fire(oldReg, point, dir, propStep, retStep, safety, newReg);
  EXPECT_NEAR(8.660254, retStep, 1e-6);

  // -++
  dir[0] = -dir_norm;
  dir[1] = +dir_norm;
  dir[2] = +dir_norm;
  g_fire(oldReg, point, dir, propStep, retStep, safety, newReg);
  EXPECT_DOUBLE_EQ(5.0/dir_norm, retStep);

  // -+-
  // Not Lost Particle!
  dir[0] = -dir_norm;
  dir[1] = +dir_norm;
  dir[2] = -dir_norm;
  g_fire(oldReg, point, dir, propStep, retStep, safety, newReg);
  EXPECT_DOUBLE_EQ(5.0/dir_norm, retStep);

  // --+
  dir[0] = -dir_norm;
  dir[1] = -dir_norm;
  dir[2] = +dir_norm;
  g_fire(oldReg, point, dir, propStep, retStep, safety, newReg);
  EXPECT_DOUBLE_EQ(5.0/dir_norm, retStep);

  // ---
  // Not Lost Particle!
  dir[0] = -dir_norm;
  dir[1] = -dir_norm;
  dir[2] = -dir_norm;
  g_fire(oldReg, point, dir, propStep, retStep, safety, newReg);
  EXPECT_DOUBLE_EQ(5.0/dir_norm, retStep);
}
コード例 #16
0
TEST(Test2DGPUGpuNUFFTConv,MatlabTest_8SK3w32)
{
	//oversampling ratio
	float osr = DEFAULT_OVERSAMPLING_RATIO;
	//kernel width
	int kernel_width = 3;

	long kernel_entries = calculateGrid3KernelSize(osr, kernel_width/2.0f);

	DType *kern = (DType*) calloc(kernel_entries,sizeof(DType));
	load1DKernel(kern,kernel_entries,kernel_width,osr);

	//Image
	int im_width = 32;

	//Data
	int data_entries = 1;
    DType2* data = (DType2*) calloc(data_entries,sizeof(DType2)); //2* re + im
	int data_cnt = 0;
	
	data[data_cnt].x = 0.0046f;
	data[data_cnt++].y = -0.0021f;

	//Coords
	//Scaled between -0.5 and 0.5
	//in triplets (x,y,z)
    DType* coords = (DType*) calloc(2*data_entries,sizeof(DType));//2* x,y
	int coord_cnt = 0;

	coords[coord_cnt++] = 0.2500f;
	coords[coord_cnt++] = -0.4330f;
	
	//sectors of data, count and start indices
	int sector_width = 8;
	
	gpuNUFFT::Array<DType> kSpaceData;
    kSpaceData.data = coords;
    kSpaceData.dim.length = data_entries;

	gpuNUFFT::Dimensions imgDims(im_width,im_width);

    gpuNUFFT::GpuNUFFTOperatorFactory factory; gpuNUFFT::GpuNUFFTOperator *gpuNUFFTOp = factory.createGpuNUFFTOperator(kSpaceData,kernel_width,sector_width,osr,imgDims);

	gpuNUFFT::Array<DType2> dataArray;
	dataArray.data = data;
	dataArray.dim.length = data_entries;

	gpuNUFFT::Array<CufftType> gdataArray;
	
	gdataArray = gpuNUFFTOp->performGpuNUFFTAdj(dataArray,gpuNUFFT::CONVOLUTION);
	//Output Grid
	CufftType* gdata = gdataArray.data;

	/*for (int j=0; j<im_width; j++)
	{
		for (int i=0; i<im_width; i++)
		{
			float dpr = gdata[get2DC2lin(i,im_width-1-j,16,im_width)];
			float dpi = gdata[get2DC2lin(i,im_width-1-j,16,im_width)+1];

			if (abs(dpr) > 0.0f)
				printf("(%d,%d)= %.4f + %.4f i ",i,im_width-1-j,dpr,dpi);
		}
		printf("\n");
	}*/

	EXPECT_NEAR(gdata[get2DC2lin(23,3,im_width)].x,0.0012f,epsilon);
	EXPECT_NEAR(gdata[get2DC2lin(23,2,im_width)].x,0.0020f,epsilon);
	EXPECT_NEAR(gdata[get2DC2lin(23,1,im_width)].x,0.0007f,epsilon);
	EXPECT_NEAR(gdata[get2DC2lin(24,3,im_width)].x,0.0026f,epsilon);//Re
	EXPECT_NEAR(gdata[get2DC2lin(24,3,im_width)].y,-0.0012f,epsilon);//Im
	EXPECT_NEAR(gdata[get2DC2lin(24,2,im_width)].x,0.0045f,epsilon);
	EXPECT_NEAR(gdata[get2DC2lin(24,1,im_width)].x,0.0016f,epsilon);
	EXPECT_NEAR(gdata[get2DC2lin(25,3,im_width)].x,0.0012f,epsilon);
	EXPECT_NEAR(gdata[get2DC2lin(25,2,im_width)].x,0.0020f,epsilon);//Re
	EXPECT_NEAR(gdata[get2DC2lin(25,2,im_width)].y,-0.0009f,epsilon);//Im
	EXPECT_NEAR(gdata[get2DC2lin(25,1,im_width)].x,0.0007f,epsilon);
	
	free(data);
	free(coords);
	free(gdata);

	delete gpuNUFFTOp;
}
コード例 #17
0
TEST(McmcHmcIntegratorsImplLeapfrog, unit_e_symplecticness) {
  rng_t base_rng(0);

  std::fstream data_stream(std::string("").c_str(), std::fstream::in);
  stan::io::dump data_var_context(data_stream);
  data_stream.close();

  std::stringstream model_output;
  std::stringstream metric_output;
  stan::interface_callbacks::writer::stream_writer writer(metric_output);
  std::stringstream error_stream;
  stan::interface_callbacks::writer::stream_writer error_writer(error_stream);

  gauss_model_namespace::gauss_model model(data_var_context, &model_output);

  stan::mcmc::impl_leapfrog<
    stan::mcmc::unit_e_metric<gauss_model_namespace::gauss_model, rng_t> >
    integrator;

  stan::mcmc::unit_e_metric<gauss_model_namespace::gauss_model, rng_t> metric(model);

  // Create a circle of points
  const int n_points = 1000;

  double pi = 3.141592653589793;
  double r = 1.5;
  double q0 = 1;
  double p0 = 0;

  std::vector<stan::mcmc::unit_e_point> z;

  for (int i = 0; i < n_points; ++i) {
    z.push_back(stan::mcmc::unit_e_point(1));

    double theta = 2 * pi * (double)i / (double)n_points;
    z.back().q(0) = r * cos(theta) + q0;
    z.back().p(0)    = r * sin(theta) + p0;
  }

  // Evolve circle
  double epsilon = 1e-3;
  size_t L = pi / epsilon;

  for (int i = 0; i < n_points; ++i)
    metric.init(z.at(i), writer, error_writer);

  for (size_t n = 0; n < L; ++n)
    for (int i = 0; i < n_points; ++i)
      integrator.evolve(z.at(i), metric, epsilon, writer, error_writer);

  // Compute area of evolved shape using divergence theorem in 2D
  double area = 0;

  for (int i = 0; i < n_points; ++i) {

    double x1 = z[i].q(0);
    double y1 = z[i].p(0);
    double x2 = z[(i + 1) % n_points].q(0);
    double y2 = z[(i + 1) % n_points].p(0);

    double x_bary = 0.5 * (x1 + x2);
    double y_bary = 0.5 * (y1 + y2);

    double x_delta = x2 - x1;
    double y_delta = y2 - y1;

    double a = sqrt( x_delta * x_delta + y_delta * y_delta);

    double x_norm = 1;
    double y_norm = - x_delta / y_delta;
    double norm = sqrt( x_norm * x_norm + y_norm * y_norm );

    a *= (x_bary * x_norm + y_bary * y_norm) / norm;
    a = a < 0 ? -a : a;

    area += a;

  }

  area *= 0.5;

  // Symplectic integrators preserve volume (area in 2D)
  EXPECT_NEAR(area, pi * r * r, 1e-2);


  EXPECT_EQ("", model_output.str());
  EXPECT_EQ("", metric_output.str());
}
コード例 #18
0
TEST(Test2DGPUGpuNUFFTConv,GPUTest_1SectorKernel5)
{
	//oversampling ratio
	float osr = DEFAULT_OVERSAMPLING_RATIO;
	//kernel width
	int kernel_width = 5;
	
	//Image
	int im_width = 10;

	//Data
	int data_entries = 1;
    DType2* data = (DType2*) calloc(data_entries,sizeof(DType2)); //2* re + im
	data[0].x = 1;
	data[0].y = 1;

	//Coords
	//Scaled between -0.5 and 0.5
	//in triplets (x,y,z)
    DType* coords = (DType*) calloc(2*data_entries,sizeof(DType));//2* x,y,z
	coords[0] = 0; //should result in 7,7 center
	coords[1] = 0;

	//sectors of data, count and start indices
	int sector_width = 5;

	gpuNUFFT::Array<DType> kSpaceData;
    kSpaceData.data = coords;
    kSpaceData.dim.length = data_entries;

	gpuNUFFT::Dimensions imgDims(im_width,im_width);
	
    gpuNUFFT::GpuNUFFTOperatorFactory factory; gpuNUFFT::GpuNUFFTOperator *gpuNUFFTOp = factory.createGpuNUFFTOperator(kSpaceData,kernel_width,sector_width,osr,imgDims);

	gpuNUFFT::Array<DType2> dataArray;
	dataArray.data = data;
	dataArray.dim.length = data_entries;

	gpuNUFFT::Array<CufftType> gdataArray;
	
	gdataArray = gpuNUFFTOp->performGpuNUFFTAdj(dataArray,gpuNUFFT::CONVOLUTION);
	//Output Grid
	CufftType* gdata = gdataArray.data;

	int index = get2DC2lin(5,5,im_width);
	if (DEBUG) printf("index to test %d\n",index);
	//EXPECT_EQ(index,2*555);
	EXPECT_NEAR(1.0f,gdata[index].x,epsilon);
	EXPECT_NEAR(0.0049,gdata[get2DC2lin(3,3,im_width)].x,epsilon*10.0f);
	EXPECT_NEAR(0.3218,gdata[get2DC2lin(4,4,im_width)].x,epsilon*10.0f);
	EXPECT_NEAR(0.5673,gdata[get2DC2lin(5,4,im_width)].x,epsilon*10.0f);

	EXPECT_NEAR(0.0697,gdata[get2DC2lin(5,7,im_width)].x,epsilon*10.0f);
	EXPECT_NEAR(0.0697,gdata[get2DC2lin(5,3,im_width)].x,epsilon*10.0f);
	
	//for (int j=0; j<im_width; j++)
	//{
	//	for (int i=0; i<im_width; i++)
	//		printf("%.4f ",gdata[get2DC2lin(i,im_width-j,5,im_width)]);
	//	printf("\n");
	//}

	free(data);
	free(coords);
	free(gdata);
	
	delete gpuNUFFTOp;
}
コード例 #19
0
TEST_F(FPDFTextEmbeddertest, Text) {
  EXPECT_TRUE(OpenDocument("hello_world.pdf"));
  FPDF_PAGE page = LoadPage(0);
  EXPECT_TRUE(page);

  FPDF_TEXTPAGE textpage = FPDFText_LoadPage(page);
  EXPECT_TRUE(textpage);

  static const char expected[] = "Hello, world!\r\nGoodbye, world!";
  unsigned short fixed_buffer[128];
  memset(fixed_buffer, 0xbd, sizeof(fixed_buffer));

  // Check includes the terminating NUL that is provided.
  int num_chars = FPDFText_GetText(textpage, 0, 128, fixed_buffer);
  ASSERT_GE(num_chars, 0);
  EXPECT_EQ(sizeof(expected), static_cast<size_t>(num_chars));
  EXPECT_TRUE(check_unsigned_shorts(expected, fixed_buffer, sizeof(expected)));

  // Count does not include the terminating NUL in the string literal.
  EXPECT_EQ(sizeof(expected) - 1,
            static_cast<size_t>(FPDFText_CountChars(textpage)));
  for (size_t i = 0; i < sizeof(expected) - 1; ++i) {
    EXPECT_EQ(static_cast<unsigned int>(expected[i]),
              FPDFText_GetUnicode(textpage, i))
        << " at " << i;
  }

  EXPECT_EQ(12.0, FPDFText_GetFontSize(textpage, 0));
  EXPECT_EQ(16.0, FPDFText_GetFontSize(textpage, 15));

  double left = 0.0;
  double right = 0.0;
  double bottom = 0.0;
  double top = 0.0;
  FPDFText_GetCharBox(textpage, 4, &left, &right, &bottom, &top);
  EXPECT_NEAR(41.071, left, 0.001);
  EXPECT_NEAR(46.243, right, 0.001);
  EXPECT_NEAR(49.844, bottom, 0.001);
  EXPECT_NEAR(55.520, top, 0.001);

  EXPECT_EQ(4, FPDFText_GetCharIndexAtPos(textpage, 42.0, 50.0, 1.0, 1.0));
  EXPECT_EQ(-1, FPDFText_GetCharIndexAtPos(textpage, 0.0, 0.0, 1.0, 1.0));
  EXPECT_EQ(-1, FPDFText_GetCharIndexAtPos(textpage, 199.0, 199.0, 1.0, 1.0));

  // Test out of range indicies.
  EXPECT_EQ(-1,
            FPDFText_GetCharIndexAtPos(textpage, 42.0, 10000000.0, 1.0, 1.0));
  EXPECT_EQ(-1, FPDFText_GetCharIndexAtPos(textpage, -1.0, 50.0, 1.0, 1.0));

  // Count does not include the terminating NUL in the string literal.
  EXPECT_EQ(2, FPDFText_CountRects(textpage, 0, sizeof(expected) - 1));

  left = 0.0;
  right = 0.0;
  bottom = 0.0;
  top = 0.0;
  FPDFText_GetRect(textpage, 1, &left, &top, &right, &bottom);
  EXPECT_NEAR(20.847, left, 0.001);
  EXPECT_NEAR(135.167, right, 0.001);
  EXPECT_NEAR(96.655, bottom, 0.001);
  EXPECT_NEAR(116.000, top, 0.001);

  // Test out of range indicies set outputs to (0.0, 0.0, 0.0, 0.0).
  left = -1.0;
  right = -1.0;
  bottom = -1.0;
  top = -1.0;
  FPDFText_GetRect(textpage, -1, &left, &top, &right, &bottom);
  EXPECT_EQ(0.0, left);
  EXPECT_EQ(0.0, right);
  EXPECT_EQ(0.0, bottom);
  EXPECT_EQ(0.0, top);

  left = -2.0;
  right = -2.0;
  bottom = -2.0;
  top = -2.0;
  FPDFText_GetRect(textpage, 2, &left, &top, &right, &bottom);
  EXPECT_EQ(0.0, left);
  EXPECT_EQ(0.0, right);
  EXPECT_EQ(0.0, bottom);
  EXPECT_EQ(0.0, top);

  EXPECT_EQ(9, FPDFText_GetBoundedText(textpage, 41.0, 56.0, 82.0, 48.0, 0, 0));

  // Extract starting at character 4 as above.
  memset(fixed_buffer, 0xbd, sizeof(fixed_buffer));
  EXPECT_EQ(1, FPDFText_GetBoundedText(textpage, 41.0, 56.0, 82.0, 48.0,
                                       fixed_buffer, 1));
  EXPECT_TRUE(check_unsigned_shorts(expected + 4, fixed_buffer, 1));
  EXPECT_EQ(0xbdbd, fixed_buffer[1]);

  memset(fixed_buffer, 0xbd, sizeof(fixed_buffer));
  EXPECT_EQ(9, FPDFText_GetBoundedText(textpage, 41.0, 56.0, 82.0, 48.0,
                                       fixed_buffer, 9));
  EXPECT_TRUE(check_unsigned_shorts(expected + 4, fixed_buffer, 9));
  EXPECT_EQ(0xbdbd, fixed_buffer[9]);

  memset(fixed_buffer, 0xbd, sizeof(fixed_buffer));
  EXPECT_EQ(10, FPDFText_GetBoundedText(textpage, 41.0, 56.0, 82.0, 48.0,
                                        fixed_buffer, 128));
  EXPECT_TRUE(check_unsigned_shorts(expected + 4, fixed_buffer, 9));
  EXPECT_EQ(0u, fixed_buffer[9]);
  EXPECT_EQ(0xbdbd, fixed_buffer[10]);

  FPDFText_ClosePage(textpage);
  UnloadPage(page);
}
コード例 #20
0
TEST(Test2DGpuNUFFTConv,Test_256x128)
{
	//oversampling ratio
	float osr = DEFAULT_OVERSAMPLING_RATIO;
	//kernel width
	int kernel_width = 3;

	//Data
	int data_entries = 1;
  DType2* data = (DType2*) calloc(data_entries,sizeof(DType2)); //2* re + im
	int data_cnt = 0;
	
	data[data_cnt].x = 1.0f;
	data[data_cnt++].y = 0.5f;

	//Coords
	//Scaled between -0.5 and 0.5
	//in triplets (x,y,z)
  DType* coords = (DType*) calloc(2*data_entries,sizeof(DType));//2* x,y
	int coord_cnt = 0;

	coords[coord_cnt++] = 0.0f;
	coords[coord_cnt++] = 0.0f;
	
	//sectors of data, count and start indices
	int sector_width = 8;
	
	gpuNUFFT::Array<DType> kSpaceData;
  kSpaceData.data = coords;
  kSpaceData.dim.length = data_entries;

	gpuNUFFT::Dimensions imgDims(256,128);

  gpuNUFFT::GpuNUFFTOperatorFactory factory; 
  gpuNUFFT::GpuNUFFTOperator *gpuNUFFTOp = factory.createGpuNUFFTOperator(kSpaceData,kernel_width,sector_width,osr,imgDims);

	gpuNUFFT::Array<DType2> dataArray;
	dataArray.data = data;
	dataArray.dim.length = data_entries;

	gpuNUFFT::Array<CufftType> gdataArray;
	
	gdataArray = gpuNUFFTOp->performGpuNUFFTAdj(dataArray,gpuNUFFT::CONVOLUTION);
	//Output Grid
	CufftType* gdata = gdataArray.data;

  if (DEBUG)
  {
	  for (int j=imgDims.height/2-10; j<imgDims.height/2+10; j++)
	  {
		  for (int i=imgDims.width/2-10; i<imgDims.width/2+10; i++)
		  {
        float dpr = gdata[computeXY2Lin(i,imgDims.height-1-j,imgDims)].x;
			  float dpi = gdata[computeXY2Lin(i,imgDims.height-1-j,imgDims)].y;
      
        printf("%.1f+%.1fi ",dpr,dpi);
		  }
		  printf("\n");
	  }
  }
  EXPECT_NEAR(1.0f,gdata[computeXY2Lin(imgDims.width/2,imgDims.height/2,imgDims)].x,epsilon);
  EXPECT_NEAR(0.5f,gdata[computeXY2Lin(imgDims.width/2,imgDims.height/2,imgDims)].y,epsilon);
  EXPECT_NEAR(0.45f,gdata[computeXY2Lin(imgDims.width/2-1,imgDims.height/2,imgDims)].x,epsilon);

  free(data);
	free(coords);
	free(gdata);

	delete gpuNUFFTOp;
}
コード例 #21
0
ファイル: SettingsTest.cpp プロジェクト: Ultimaker/CuraEngine
TEST_F(SettingsTest, AddSettingAngleDegrees)
{
    settings.add("test_setting", "4442.4");
    EXPECT_NEAR(AngleDegrees(122.4), settings.get<AngleDegrees>("test_setting"), 0.00000001) << "4320 is divisible by 360, so 4442.4 in clock arithmetic is 122.4 degrees.";
}
コード例 #22
0
 void AWorxUnitTesting::EQ( const TString& file, int line, const TString& func, uint64_t       exp, uint64_t i, uint64_t  p )  { if ((i < exp ? exp-i : i-exp) > p)   Failed(file,line,func,String128("\"") << exp << "\", \"" << i << "\" given."); EXPECT_NEAR ( exp, i, p         ); }
コード例 #23
0
ファイル: UnitTest.cpp プロジェクト: jackhui/GE
TEST(Vector, Multiplication)
{
	Vector3 v(1.0f, 1.0f, 1.0f);
	v.Multiply(3.0f);
	EXPECT_NEAR(3.0f, v.GetZ(), 0.01f);
}
コード例 #24
0
 void AWorxUnitTesting::EQ( const TString& file, int line, const TString& func, double    exp, double d,   double   p )        { if ((d < exp ? exp-d : d-exp) > p)   Failed(file,line,func,String128("\"") << exp << "\", \"" << d << "\" given."); EXPECT_NEAR ( exp, d, p         ); }
コード例 #25
0
ファイル: UnitTest.cpp プロジェクト: jackhui/GE
TEST(Vector, Normalization)
{
	Vector3 v(0.0f, 3.0f, 4.0f);
	v.Normalize();
	EXPECT_NEAR(0.8f, v.GetZ(), 0.01f);
}
コード例 #26
0
TYPED_TEST(DummyDataLayerTest, TestThreeTopConstantGaussianConstant) {
  Caffe::set_mode(Caffe::CPU);
  LayerParameter param;
  DummyDataParameter* dummy_data_param = param.mutable_dummy_data_param();
  dummy_data_param->add_num(5);
  dummy_data_param->add_channels(3);
  dummy_data_param->add_height(2);
  dummy_data_param->add_width(4);
  FillerParameter* data_filler_param_a = dummy_data_param->add_data_filler();
  data_filler_param_a->set_value(7);
  FillerParameter* data_filler_param_b = dummy_data_param->add_data_filler();
  data_filler_param_b->set_type("gaussian");
  TypeParam gaussian_mean = 3.0;
  TypeParam gaussian_std = 0.01;
  data_filler_param_b->set_mean(gaussian_mean);
  data_filler_param_b->set_std(gaussian_std);
  FillerParameter* data_filler_param_c = dummy_data_param->add_data_filler();
  data_filler_param_c->set_value(9);
  DummyDataLayer<TypeParam> layer(param);
  layer.SetUp(this->blob_bottom_vec_, &this->blob_top_vec_);
  EXPECT_EQ(this->blob_top_a_->num(), 5);
  EXPECT_EQ(this->blob_top_a_->channels(), 3);
  EXPECT_EQ(this->blob_top_a_->height(), 2);
  EXPECT_EQ(this->blob_top_a_->width(), 4);
  EXPECT_EQ(this->blob_top_b_->num(), 5);
  EXPECT_EQ(this->blob_top_b_->channels(), 3);
  EXPECT_EQ(this->blob_top_b_->height(), 2);
  EXPECT_EQ(this->blob_top_b_->width(), 4);
  EXPECT_EQ(this->blob_top_c_->num(), 5);
  EXPECT_EQ(this->blob_top_c_->channels(), 3);
  EXPECT_EQ(this->blob_top_c_->height(), 2);
  EXPECT_EQ(this->blob_top_c_->width(), 4);
  for (int i = 0; i < this->blob_top_a_->count(); ++i) {
    EXPECT_EQ(7, this->blob_top_a_->cpu_data()[i]);
  }
  // Blob b uses a Gaussian filler, so SetUp should not have initialized it.
  // Blob b's data should therefore be the default Blob data value: 0.
  for (int i = 0; i < this->blob_top_b_->count(); ++i) {
    EXPECT_EQ(0, this->blob_top_b_->cpu_data()[i]);
  }
  for (int i = 0; i < this->blob_top_c_->count(); ++i) {
    EXPECT_EQ(9, this->blob_top_c_->cpu_data()[i]);
  }

  // Do a Forward pass to fill in Blob b with Gaussian data.
  layer.Forward(this->blob_bottom_vec_, &this->blob_top_vec_);
  for (int i = 0; i < this->blob_top_a_->count(); ++i) {
    EXPECT_EQ(7, this->blob_top_a_->cpu_data()[i]);
  }
  // Check that the Gaussian's data has been filled in with values within
  // 10 standard deviations of the mean. Record the first and last sample.
  // to check that they're different after the next Forward pass.
  for (int i = 0; i < this->blob_top_b_->count(); ++i) {
    EXPECT_NEAR(gaussian_mean, this->blob_top_b_->cpu_data()[i],
                gaussian_std * 10);
  }
  const TypeParam first_gaussian_sample = this->blob_top_b_->cpu_data()[0];
  const TypeParam last_gaussian_sample =
      this->blob_top_b_->cpu_data()[this->blob_top_b_->count() - 1];
  for (int i = 0; i < this->blob_top_c_->count(); ++i) {
    EXPECT_EQ(9, this->blob_top_c_->cpu_data()[i]);
  }

  // Do another Forward pass to fill in Blob b with Gaussian data again,
  // checking that we get different values.
  layer.Forward(this->blob_bottom_vec_, &this->blob_top_vec_);
  for (int i = 0; i < this->blob_top_a_->count(); ++i) {
    EXPECT_EQ(7, this->blob_top_a_->cpu_data()[i]);
  }
  for (int i = 0; i < this->blob_top_b_->count(); ++i) {
    EXPECT_NEAR(gaussian_mean, this->blob_top_b_->cpu_data()[i],
                gaussian_std * 10);
  }
  EXPECT_NE(first_gaussian_sample, this->blob_top_b_->cpu_data()[0]);
  EXPECT_NE(last_gaussian_sample,
      this->blob_top_b_->cpu_data()[this->blob_top_b_->count() - 1]);
  for (int i = 0; i < this->blob_top_c_->count(); ++i) {
    EXPECT_EQ(9, this->blob_top_c_->cpu_data()[i]);
  }
}
コード例 #27
0
ファイル: UnitTest.cpp プロジェクト: jackhui/GE
TEST(Vector, SquaredLength)
{
	Vector3 v(3.0f, 1.0f, 4.0f);
	EXPECT_NEAR(26.0f, v.LengthSquared(), 0.01f);
}
コード例 #28
0
TEST(TestSuite, CheckGenerateBoxAnchors)
{
  const int MAX_NUM_PILLARS = 12000;
  const int MAX_NUM_POINTS_PER_PILLAR = 100;
  const int GRID_X_SIZE = 432;
  const int GRID_Y_SIZE = 496;
  const int GRID_Z_SIZE = 1;
  const float PILLAR_X_SIZE = 0.16;
  const float PILLAR_Y_SIZE = 0.16;
  const float PILLAR_Z_SIZE = 4.0;
  const float MIN_X_RANGE = 0;
  const float MIN_Y_RANGE = -39.68;
  const float MIN_Z_RANGE = -3.0;
  const int NUM_INDS_FOR_SCAN = 512;
  const int NUM_BOX_CORNERS = 4;
  TestClass test_obj(MAX_NUM_PILLARS,
                     MAX_NUM_POINTS_PER_PILLAR,
                     GRID_X_SIZE,
                     GRID_Y_SIZE,
                     GRID_Z_SIZE,
                     PILLAR_X_SIZE,
                     PILLAR_Y_SIZE,
                     PILLAR_Z_SIZE,
                     MIN_X_RANGE,
                     MIN_Y_RANGE,
                     MIN_Z_RANGE,
                     NUM_INDS_FOR_SCAN,
                     NUM_BOX_CORNERS);

  const int NUM_ANCHOR = 432*0.5*496*0.5*2;

  float* anchors_px = new float[NUM_ANCHOR];
  float* anchors_py = new float[NUM_ANCHOR];
  float* anchors_pz = new float[NUM_ANCHOR];
  float* anchors_dx = new float[NUM_ANCHOR];
  float* anchors_dy = new float[NUM_ANCHOR];
  float* anchors_dz = new float[NUM_ANCHOR];
  float* anchors_ro = new float[NUM_ANCHOR];
  float* box_anchors_min_x = new float[NUM_ANCHOR];
  float* box_anchors_min_y = new float[NUM_ANCHOR];
  float* box_anchors_max_x = new float[NUM_ANCHOR];
  float* box_anchors_max_y = new float[NUM_ANCHOR];
  test_obj.generateAnchors(anchors_px, anchors_py, anchors_pz, anchors_dx, anchors_dy, anchors_dz, anchors_ro);
  test_obj.convertAnchors2BoxAnchors(anchors_px, anchors_py, anchors_dx, anchors_dy,
                                    box_anchors_min_x, box_anchors_min_y,
                                    box_anchors_max_x, box_anchors_max_y);

  EXPECT_NEAR(53.25,  box_anchors_min_x[345], 0.001);
  EXPECT_NEAR(-41.47, box_anchors_min_y[22], 0.001);
  EXPECT_NEAR(38.4,   box_anchors_max_x[1098], 0.001);
  EXPECT_NEAR(-38.4,  box_anchors_max_y[675], 0.001);


  delete[] anchors_px;
  delete[] anchors_py;
  delete[] anchors_pz;
  delete[] anchors_dx;
  delete[] anchors_dy;
  delete[] anchors_dz;
  delete[] anchors_ro;
  delete[] box_anchors_min_x;
  delete[] box_anchors_min_y;
  delete[] box_anchors_max_x;
  delete[] box_anchors_max_y;
}
コード例 #29
0
ファイル: IndoleTest.cpp プロジェクト: z1lc/MCGPU
// Descr: evident
// Implementation details: See gtest/samples for GTest syntax and usage
TEST(IndoleTest, FrontToEndIntegrationTest)
{
	string directory = get_current_dir_name();
	
	std::string mc ("MCGPU");
	std::size_t found = directory.find(mc);
	
	if (found != std::string::npos) {
		directory = directory.substr(0,found+6);
	}
	std::string MCGPU = directory;
	
	
	// Create the test config file,
	// 		because it requires full hardcoded filepath names that will change on each user's computer
	
	ofstream configFile;
	std::string configFilePath (std::string( MCGPU + "/test/unittests/Integration/IndoleTest/IndoleTest.config"));
	configFile.open( configFilePath.c_str() );
	std::stringstream cfg;
	cfg << ""
		<< "#size of periodic box (x, y, z in angstroms)\n"
		<< "35.36\n"
		<< "35.36\n"
		<< "35.36\n"
		<< "#temperature in Kelvin\n"
		<< "298.15\n"
		<< "#max translation\n"
		<< ".12\n"
		<< "#number of steps\n"
		<< "40000\n"
		<< "#number of molecues\n"
		<< "267\n"
		<< "#path to opls.par file\n"
		<< MCGPU << "resources/bossFiles/oplsaa.par\n"
		<< "#path to z matrix file\n"
		<< MCGPU << "test/unittests/Integration/IndoleTest/indole.z\n"
		<< "#path to state input\n"
		<< MCGPU << "test/unittests/Integration/IndoleTest\n"
		<< "#path to state output\n"
		<< MCGPU << "test/unittests/Integration/IndoleTest\n"
		<< "#pdb output path\n"
		<< "indoletest.pdb\n"
		<< "#cutoff distance in angstroms\n"
		<< "12.0\n"
		<< "#max rotation\n"
		<< "12.0\n"
		<< "#Random Seed Input\n"
		<< "12345\n"
		<< "#Primary Atom Index\n"
		<< "1";
	configFile << cfg.str();
	configFile.close();
	
    std::stringstream ss;    
	ss << MCGPU << "/bin/metrosim "
       << " " // don't forget a space between the path and the arguments
       << MCGPU << "/test/unittests/Integration/IndoleTest/IndoleTest.config -s --name indoleCPU -k";
	
	
    // Launch MCGPU application in serial, expect output files in /MCGPU/ directory   
	system(ss.str().c_str());
	
	double expected = 58900;
	double energyResult = -1;
	
	std::ifstream infile( std::string(MCGPU + "bin/indoleCPU.results").c_str() );
	
	for( std::string line; getline( infile, line ); )
	{
		std::string str2 ("Final-Energy");
		std::string result;
		found = line.find(str2);
		
		if (found != std::string::npos) {
			result = line.substr(15);
			energyResult  = strtod(result.c_str(), NULL);
			break;
		}
	}
	
	// Clean up
	remove(configFilePath.c_str());
	remove(  std::string(MCGPU + "/indoleCPU.pdb").c_str()  );
	remove(  std::string(MCGPU + "/indoleCPU.results").c_str()  );
	remove(  std::string(MCGPU + "/indoleCPU_40000.state").c_str()  );
	
	EXPECT_NEAR(expected, energyResult, 100 );
}
コード例 #30
0
ファイル: gtest_dylm.cpp プロジェクト: capoe/soapxx
TEST(TestFunctions, GradientYlm) {

    // SETUP
    std::vector< soap::vec > r_list;    
    r_list.push_back(soap::vec(2.,0.,0.));
    r_list.push_back(soap::vec(0.,1.,0.));
    r_list.push_back(soap::vec(0.,0.,1.));
    r_list.push_back(soap::vec(std::sqrt(0.5),0.,std::sqrt(0.5)));
    r_list.push_back(soap::vec(-0.2,0.3,0.7));
    r_list.push_back(soap::vec(-0.2,-0.4,-0.1));

    std::vector< std::pair<int,int> > lm_list;
    lm_list.push_back(std::pair<int,int>(1,0));
    lm_list.push_back(std::pair<int,int>(1,1));
    lm_list.push_back(std::pair<int,int>(2,0));
    lm_list.push_back(std::pair<int,int>(2,1));
    lm_list.push_back(std::pair<int,int>(2,-1));
    lm_list.push_back(std::pair<int,int>(2,2));
    lm_list.push_back(std::pair<int,int>(2,-2));

    std::vector<std::complex<double> > results;
    results.push_back(std::complex<double>(-1.4959138e-17, +0.0000000e+00));
    results.push_back(std::complex<double>(-0.0000000e+00, +0.0000000e+00));
    results.push_back(std::complex<double>(+2.4430126e-01, +0.0000000e+00));
    results.push_back(std::complex<double>(-1.8319660e-33, +0.0000000e+00));
    results.push_back(std::complex<double>(-2.9918275e-17, +0.0000000e+00));
    results.push_back(std::complex<double>(+4.8860251e-01, +0.0000000e+00));
    results.push_back(std::complex<double>(-0.0000000e+00, +0.0000000e+00));
    results.push_back(std::complex<double>(+0.0000000e+00, +0.0000000e+00));
    results.push_back(std::complex<double>(+0.0000000e+00, +0.0000000e+00));
    results.push_back(std::complex<double>(-2.4430126e-01, +0.0000000e+00));
    results.push_back(std::complex<double>(-0.0000000e+00, +0.0000000e+00));
    results.push_back(std::complex<double>(+2.4430126e-01, +0.0000000e+00));
    results.push_back(std::complex<double>(+1.4011873e-01, +0.0000000e+00));
    results.push_back(std::complex<double>(-2.1017810e-01, +0.0000000e+00));
    results.push_back(std::complex<double>(+1.3011025e-01, +0.0000000e+00));
    results.push_back(std::complex<double>(-1.0154458e-01, +0.0000000e+00));
    results.push_back(std::complex<double>(-2.0308916e-01, +0.0000000e+00));
    results.push_back(std::complex<double>(+1.0154458e+00, +0.0000000e+00));
    results.push_back(std::complex<double>(-6.4769779e-34, +0.0000000e+00));
    results.push_back(std::complex<double>(-0.0000000e+00, -1.7274707e-01));
    results.push_back(std::complex<double>(+1.0577708e-17, -0.0000000e+00));
    results.push_back(std::complex<double>(-3.4549415e-01, +2.1154717e-17));
    results.push_back(std::complex<double>(+2.1155415e-17, -2.5907484e-33));
    results.push_back(std::complex<double>(+1.2953528e-33, +2.1155415e-17));
    results.push_back(std::complex<double>(-3.4549415e-01, +6.9868116e-22));
    results.push_back(std::complex<double>(-6.9868116e-22, -3.4549415e-01));
    results.push_back(std::complex<double>(+0.0000000e+00, +0.0000000e+00));
    results.push_back(std::complex<double>(-1.7274707e-01, +0.0000000e+00));
    results.push_back(std::complex<double>(-0.0000000e+00, -3.4549415e-01));
    results.push_back(std::complex<double>(+1.7274707e-01, -0.0000000e+00));
    results.push_back(std::complex<double>(-4.1046975e-01, -4.2462388e-02));
    results.push_back(std::complex<double>(-4.2462388e-02, -3.7508443e-01));
    results.push_back(std::complex<double>(-9.9078905e-02, +1.4861836e-01));
    results.push_back(std::complex<double>(-6.1032432e-01, +2.8721145e-01));
    results.push_back(std::complex<double>(+2.8721145e-01, -1.7950715e-01));
    results.push_back(std::complex<double>(+7.1802861e-02, +1.4360572e-01));
    results.push_back(std::complex<double>(-3.5475869e-33, +0.0000000e+00));
    results.push_back(std::complex<double>(-0.0000000e+00, +0.0000000e+00));
    results.push_back(std::complex<double>(+5.7936491e-17, +0.0000000e+00));
    results.push_back(std::complex<double>(-4.3445409e-49, +0.0000000e+00));
    results.push_back(std::complex<double>(-7.0951738e-33, +0.0000000e+00));
    results.push_back(std::complex<double>(+1.1587298e-16, +0.0000000e+00));
    results.push_back(std::complex<double>(-0.0000000e+00, +0.0000000e+00));
    results.push_back(std::complex<double>(+0.0000000e+00, +0.0000000e+00));
    results.push_back(std::complex<double>(+0.0000000e+00, +0.0000000e+00));
    results.push_back(std::complex<double>(-6.6904654e-01, +0.0000000e+00));
    results.push_back(std::complex<double>(-0.0000000e+00, +0.0000000e+00));
    results.push_back(std::complex<double>(+6.6904654e-01, +0.0000000e+00));
    results.push_back(std::complex<double>(+4.8244079e-01, +0.0000000e+00));
    results.push_back(std::complex<double>(-7.2366119e-01, +0.0000000e+00));
    results.push_back(std::complex<double>(+4.4798074e-01, +0.0000000e+00));
    results.push_back(std::complex<double>(+8.5820834e-02, +0.0000000e+00));
    results.push_back(std::complex<double>(+1.7164167e-01, +0.0000000e+00));
    results.push_back(std::complex<double>(-8.5820834e-01, +0.0000000e+00));
    results.push_back(std::complex<double>(+2.3652473e-17, -0.0000000e+00));
    results.push_back(std::complex<double>(-0.0000000e+00, -2.3652473e-17));
    results.push_back(std::complex<double>(-3.8627420e-01, +0.0000000e+00));
    results.push_back(std::complex<double>(-4.7304947e-17, +5.7930895e-33));
    results.push_back(std::complex<double>(+5.7930895e-33, +4.7304947e-17));
    results.push_back(std::complex<double>(-4.7303384e-17, -7.7254840e-01));
    results.push_back(std::complex<double>(-7.7254840e-01, +1.5622986e-21));
    results.push_back(std::complex<double>(-1.5622986e-21, -7.7254840e-01));
    results.push_back(std::complex<double>(+0.0000000e+00, +0.0000000e+00));
    results.push_back(std::complex<double>(-3.3449648e-17, +0.0000000e+00));
    results.push_back(std::complex<double>(-0.0000000e+00, -5.4627422e-01));
    results.push_back(std::complex<double>(+3.3449648e-17, -0.0000000e+00));
    results.push_back(std::complex<double>(-7.5968600e-01, -1.6881911e-01));
    results.push_back(std::complex<double>(-1.6881911e-01, -6.1900340e-01));
    results.push_back(std::complex<double>(-1.4470209e-01, +2.1705314e-01));
    results.push_back(std::complex<double>(+2.2773536e-01, -2.8028967e-01));
    results.push_back(std::complex<double>(-2.8028967e-01, -1.9269915e-01));
    results.push_back(std::complex<double>(+6.6568797e-01, +1.3313759e+00));
    results.push_back(std::complex<double>(-2.3652473e-17, +0.0000000e+00));
    results.push_back(std::complex<double>(+0.0000000e+00, -2.3652473e-17));
    results.push_back(std::complex<double>(+3.8627420e-01, +0.0000000e+00));
    results.push_back(std::complex<double>(+4.7304947e-17, +5.7930895e-33));
    results.push_back(std::complex<double>(-5.7930895e-33, +4.7304947e-17));
    results.push_back(std::complex<double>(+4.7303384e-17, -7.7254840e-01));
    results.push_back(std::complex<double>(+7.7254840e-01, +1.5622986e-21));
    results.push_back(std::complex<double>(+1.5622986e-21, -7.7254840e-01));
    results.push_back(std::complex<double>(+0.0000000e+00, +0.0000000e+00));
    results.push_back(std::complex<double>(+3.3449648e-17, +0.0000000e+00));
    results.push_back(std::complex<double>(+0.0000000e+00, -5.4627422e-01));
    results.push_back(std::complex<double>(-3.3449648e-17, +0.0000000e+00));
    results.push_back(std::complex<double>(+7.5968600e-01, -1.6881911e-01));
    results.push_back(std::complex<double>(+1.6881911e-01, -6.1900340e-01));
    results.push_back(std::complex<double>(+1.4470209e-01, +2.1705314e-01));
    results.push_back(std::complex<double>(-2.2773536e-01, -2.8028967e-01));
    results.push_back(std::complex<double>(+2.8028967e-01, -1.9269915e-01));
    results.push_back(std::complex<double>(-6.6568797e-01, +1.3313759e+00));
    results.push_back(std::complex<double>(+1.4482963e-33, +0.0000000e+00));
    results.push_back(std::complex<double>(+0.0000000e+00, +3.8627420e-01));
    results.push_back(std::complex<double>(-2.3652473e-17, +0.0000000e+00));
    results.push_back(std::complex<double>(+9.4606768e-17, +7.7254840e-01));
    results.push_back(std::complex<double>(-8.6895864e-33, -4.7304947e-17));
    results.push_back(std::complex<double>(+4.7304947e-17, -5.7929938e-33));
    results.push_back(std::complex<double>(+0.0000000e+00, +0.0000000e+00));
    results.push_back(std::complex<double>(+0.0000000e+00, +0.0000000e+00));
    results.push_back(std::complex<double>(+0.0000000e+00, +0.0000000e+00));
    results.push_back(std::complex<double>(+2.7313711e-01, +0.0000000e+00));
    results.push_back(std::complex<double>(+0.0000000e+00, +5.4627422e-01));
    results.push_back(std::complex<double>(-2.7313711e-01, +0.0000000e+00));
    results.push_back(std::complex<double>(-2.6930668e-01, +3.2557971e-01));
    results.push_back(std::complex<double>(-3.4366747e-01, -1.7685812e-01));
    results.push_back(std::complex<double>(+7.0341296e-02, +1.6881911e-01));
    results.push_back(std::complex<double>(-1.1561949e+00, -9.1094143e-01));
    results.push_back(std::complex<double>(+6.3065176e-01, +3.8539830e-01));
    results.push_back(std::complex<double>(-2.1021725e-01, +2.8028967e-01));
    results.push_back(std::complex<double>(+1.4482963e-33, -0.0000000e+00));
    results.push_back(std::complex<double>(+0.0000000e+00, -3.8627420e-01));
    results.push_back(std::complex<double>(-2.3652473e-17, +0.0000000e+00));
    results.push_back(std::complex<double>(+9.4606768e-17, -7.7254840e-01));
    results.push_back(std::complex<double>(-8.6895864e-33, +4.7304947e-17));
    results.push_back(std::complex<double>(+4.7304947e-17, +5.7929938e-33));
    results.push_back(std::complex<double>(+0.0000000e+00, -0.0000000e+00));
    results.push_back(std::complex<double>(+0.0000000e+00, -0.0000000e+00));
    results.push_back(std::complex<double>(+0.0000000e+00, -0.0000000e+00));
    results.push_back(std::complex<double>(+2.7313711e-01, -0.0000000e+00));
    results.push_back(std::complex<double>(+0.0000000e+00, -5.4627422e-01));
    results.push_back(std::complex<double>(-2.7313711e-01, +0.0000000e+00));
    results.push_back(std::complex<double>(-2.6930668e-01, -3.2557971e-01));
    results.push_back(std::complex<double>(-3.4366747e-01, +1.7685812e-01));
    results.push_back(std::complex<double>(+7.0341296e-02, -1.6881911e-01));
    results.push_back(std::complex<double>(-1.1561949e+00, +9.1094143e-01));
    results.push_back(std::complex<double>(+6.3065176e-01, -3.8539830e-01));
    results.push_back(std::complex<double>(-2.1021725e-01, -2.8028967e-01));   
    
    // COMPUTE
    int res_idx = -1;
    for (int lm = 0; lm < lm_list.size(); ++lm) {
        int l = lm_list[lm].first;
        int m = lm_list[lm].second;

        for (int n = 0; n < r_list.size(); ++n) {
            soap::vec r = r_list[n];    
            std::vector<std::complex<double> > dylm = soap::GradSphericalYlm::eval(l, m, r);

            // VERIFY
            res_idx += 1;
            EXPECT_NEAR(dylm[0].real(), results[res_idx].real(), 1e-7);
            EXPECT_NEAR(dylm[0].imag(), results[res_idx].imag(), 1e-7);
            res_idx += 1;
            EXPECT_NEAR(dylm[1].real(), results[res_idx].real(), 1e-7);
            EXPECT_NEAR(dylm[1].imag(), results[res_idx].imag(), 1e-7);            
            res_idx += 1;
            EXPECT_NEAR(dylm[2].real(), results[res_idx].real(), 1e-7);
            EXPECT_NEAR(dylm[2].imag(), results[res_idx].imag(), 1e-7);
        }        
    }
}