Project
Loading...
Searching...
No Matches
testUtils.cxx
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
12#define BOOST_TEST_MODULE Test Utils
13#define BOOST_TEST_MAIN
14#define BOOST_TEST_DYN_LINK
15#include <TMath.h>
16#include <TProfile.h>
17#include <boost/test/unit_test.hpp>
18#include <iostream>
19#include <chrono>
20#include <cmath>
21#include "MathUtils/Utils.h"
22
23using namespace o2;
24
26{
27 // test MathUtils/Utils.h
28
29 { // test fastATan2()
30 int n = 1000;
31 TProfile* p = new TProfile("pFastATan2", "fastATan2(y,x)", n, -TMath::Pi(), TMath::Pi());
32 double maxDiff = 0;
33 for (int i = 0; i < n; i++) {
34 double phi0 = -TMath::Pi() + i * TMath::TwoPi() / n;
35 float x = TMath::Cos(phi0);
36 float y = TMath::Sin(phi0);
37 float phi = math_utils::fastATan2(y, x);
38 double diff = phi - phi0;
39 p->Fill(phi0, diff);
40 diff = std::fabs(diff);
41 if (diff > maxDiff) {
42 maxDiff = diff;
43 }
44 }
45 //p->Draw();
46
47 std::cout << "test fastATan2:" << std::endl;
48 std::cout << " Max inaccuracy " << maxDiff << std::endl;
49
50 // test the speed
51 std::cout << " Speed: " << std::endl;
52
53 const int M = 1000;
54 uint32_t iterations = 10000;
55 float sum = 0;
56 float vx[M], vy[M];
57 double vxd[M], vyd[M];
58
59 // set some arbitrary x, y values
60 {
61 float x = 1.e-4, y = 1.e-3, d = 1.e-5;
62 for (int i = 0; i < M; ++i, x += d, y += d) {
63 vx[i] = (i % 2) ? x : -x;
64 vy[i] = (i % 3) ? y : -y;
65 vxd[i] = vx[i];
66 vyd[i] = vy[i];
67 }
68 }
69
70 double scale = 1. / iterations / M;
71 // dry run
72 sum = 0;
73 auto begin = std::chrono::high_resolution_clock::now();
74 for (size_t i = 0; i < iterations; ++i) {
75 for (int j = 0; j < M; ++j) {
76 sum += vx[j] + vy[j];
77 }
78 }
79 auto end = std::chrono::high_resolution_clock::now();
80 auto time1 = scale * std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin).count();
81 std::cout << " dry run: time " << time1 << " ns. checksum " << sum << std::endl;
82
83 // double precision
84 double dsum = 0;
85 begin = std::chrono::high_resolution_clock::now();
86 for (size_t i = 0; i < iterations; ++i) {
87 for (int j = 0; j < M; ++j) {
88 dsum += std::atan2(vyd[j], vxd[j]);
89 }
90 }
91 end = std::chrono::high_resolution_clock::now();
92 auto time2 = scale * std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin).count();
93 std::cout << " atan2(): time " << time2 << " ns. checksum " << dsum << std::endl;
94
95 // single precision
96 sum = 0;
97 begin = std::chrono::high_resolution_clock::now();
98 for (size_t i = 0; i < iterations; ++i) {
99 for (int j = 0; j < M; ++j) {
100 sum += atan2f(vy[j], vx[j]);
101 }
102 }
103 end = std::chrono::high_resolution_clock::now();
104 auto time3 = scale * std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin).count();
105 std::cout << " atan2f(): time " << time3 << " ns. checksum " << sum << std::endl;
106
107 // fast method
108 sum = 0;
109 begin = std::chrono::high_resolution_clock::now();
110 for (size_t i = 0; i < iterations; ++i) {
111 for (int j = 0; j < M; ++j) {
112 sum += math_utils::fastATan2(vy[j], vx[j]);
113 }
114 }
115 end = std::chrono::high_resolution_clock::now();
116 auto time4 = scale * std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin).count();
117 std::cout << " fastATan2: time " << time4 << " ns. checksum " << sum << std::endl;
118 std::cout << " speed up to atan2f(): " << (time3 - time1) / (time4 - time1) << " times " << std::endl;
119
120 BOOST_CHECK(maxDiff < 1.e-3);
121
122 } // test fastATan2()
123}
General auxilliary methods.
int32_t i
uint32_t j
Definition RawData.h:0
float sum(float s, o2::dcs::DataPointValue v)
Definition dcs-ccdb.cxx:39
GLdouble n
Definition glcorearb.h:1982
GLint GLenum GLint x
Definition glcorearb.h:403
GLuint GLuint end
Definition glcorearb.h:469
GLint y
Definition glcorearb.h:270
a couple of static helper functions to create timestamp values for CCDB queries or override obsolete ...
BOOST_AUTO_TEST_CASE(Utils_test)
Definition testUtils.cxx:25
BOOST_CHECK(tree)