File: perfColVectorOperations.cpp

package info (click to toggle)
visp 3.6.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 119,296 kB
  • sloc: cpp: 500,914; ansic: 52,904; xml: 22,642; python: 7,365; java: 4,247; sh: 482; makefile: 237; objc: 145
file content (394 lines) | stat: -rw-r--r-- 11,263 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/****************************************************************************
 *
 * ViSP, open source Visual Servoing Platform software.
 * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
 *
 * This software is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * See the file LICENSE.txt at the root directory of this source
 * distribution for additional information about the GNU GPL.
 *
 * For using ViSP with software that can not be combined with the GNU
 * GPL, please contact Inria about acquiring a ViSP Professional
 * Edition License.
 *
 * See https://visp.inria.fr for more information.
 *
 * This software was developed at:
 * Inria Rennes - Bretagne Atlantique
 * Campus Universitaire de Beaulieu
 * 35042 Rennes Cedex
 * France
 *
 * If you have questions regarding the use of this file, please contact
 * Inria at visp@inria.fr
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 * Description:
 * Benchmark column vector operations.
 *
*****************************************************************************/

#include <visp3/core/vpConfig.h>

#ifdef VISP_HAVE_CATCH2
#define CATCH_CONFIG_ENABLE_BENCHMARKING
#define CATCH_CONFIG_RUNNER
#include <catch.hpp>

#include <visp3/core/vpColVector.h>

namespace
{
static bool g_runBenchmark = false;
static const std::vector<unsigned int> g_sizes = { 23, 127, 233, 419, 1153, 2749 };

double getRandomValues(double min, double max) { return (max - min) * ((double)rand() / (double)RAND_MAX) + min; }

vpColVector generateRandomVector(unsigned int rows, double min = -100, double max = 100)
{
  vpColVector v(rows);

  for (unsigned int i = 0; i < v.getRows(); i++) {
    v[i] = getRandomValues(min, max);
  }

  return v;
}

double stddev(const std::vector<double> &vec)
{
  double sum = std::accumulate(vec.begin(), vec.end(), 0.0);
  double mean = sum / vec.size();

  std::vector<double> diff(vec.size());
  std::transform(vec.begin(), vec.end(), diff.begin(), [mean](double x) {
    return x - mean; });
  double sq_sum = std::inner_product(diff.begin(), diff.end(), diff.begin(), 0.0);
  return std::sqrt(sq_sum / vec.size());
}

double computeRegularSum(const vpColVector &v)
{
  double sum = 0.0;

  for (unsigned int i = 0; i < v.getRows(); i++) {
    sum += v[i];
  }

  return sum;
}

double computeRegularSumSquare(const vpColVector &v)
{
  double sum_square = 0.0;

  for (unsigned int i = 0; i < v.getRows(); i++) {
    sum_square += v[i] * v[i];
  }

  return sum_square;
}

double computeRegularStdev(const vpColVector &v)
{
  double mean_value = computeRegularSum(v) / v.getRows();
  double sum_squared_diff = 0.0;

  for (unsigned int i = 0; i < v.size(); i++) {
    sum_squared_diff += (v[i] - mean_value) * (v[i] - mean_value);
  }

  double divisor = (double)v.size();

  return std::sqrt(sum_squared_diff / divisor);
}

std::vector<double> computeHadamard(const std::vector<double> &v1, const std::vector<double> &v2)
{
  std::vector<double> result;
  std::transform(v1.begin(), v1.end(), v2.begin(), std::back_inserter(result), std::multiplies<double>());
  return result;
}

bool almostEqual(const vpColVector &v1, const vpColVector &v2, double tol = 1e-9)
{
  if (v1.getRows() != v2.getRows()) {
    return false;
  }

  for (unsigned int i = 0; i < v1.getRows(); i++) {
    if (!vpMath::equal(v1[i], v2[i], tol)) {
      return false;
    }
  }

  return true;
}
} // namespace

TEST_CASE("Benchmark vpColVector::sum()", "[benchmark]")
{
  // Sanity checks
  {
    const double val = 11;
    vpColVector v(1, val);
    CHECK(v.sum() == Approx(val).epsilon(std::numeric_limits<double>::epsilon()));
  }
  {
    const unsigned int size = 11;
    std::vector<double> vec(size);
    vpColVector v(size);
    for (size_t i = 0; i < 11; i++) {
      vec[i] = 2. * i;
      v[static_cast<unsigned int>(i)] = vec[i];
    }
    CHECK(v.sum() ==
          Approx(std::accumulate(vec.begin(), vec.end(), 0.0)).epsilon(std::numeric_limits<double>::epsilon()));
  }

  if (g_runBenchmark) {
    for (auto size : g_sizes) {
      vpColVector v = generateRandomVector(size);
      std::vector<double> vec = v.toStdVector();

      std::ostringstream oss;
      oss << "Benchmark vpColVector::sum() with size: " << size << " (ViSP)";
      double vp_sum = 0;
      BENCHMARK(oss.str().c_str())
      {
        vp_sum = v.sum();
        return vp_sum;
      };

      oss.str("");
      oss << "Benchmark std::accumulate() with size: " << size << " (C++)";
      double std_sum = 0;
      BENCHMARK(oss.str().c_str())
      {
        std_sum = std::accumulate(vec.begin(), vec.end(), 0.0);
        return std_sum;
      };
      CHECK(vp_sum == Approx(std_sum));

      oss.str("");
      oss << "Benchmark naive sum() with size: " << size;
      double naive_sum = 0;
      BENCHMARK(oss.str().c_str())
      {
        naive_sum = computeRegularSum(v);
        return naive_sum;
      };
      CHECK(naive_sum == Approx(std_sum));
    }
  }
}

TEST_CASE("Benchmark vpColVector::sumSquare()", "[benchmark]")
{
  // Sanity checks
  {
    const double val = 11;
    vpColVector v(1, val);
    CHECK(v.sumSquare() == Approx(val * val).epsilon(std::numeric_limits<double>::epsilon()));
  }
  {
    const unsigned int size = 11;
    std::vector<double> vec(size);
    vpColVector v(size);
    for (size_t i = 0; i < 11; i++) {
      vec[i] = 2. * i;
      v[static_cast<unsigned int>(i)] = vec[i];
    }
    CHECK(v.sumSquare() == Approx(std::inner_product(vec.begin(), vec.end(), vec.begin(), 0.0))
                               .epsilon(std::numeric_limits<double>::epsilon()));
  }

  if (g_runBenchmark) {
    for (auto size : g_sizes) {
      vpColVector v = generateRandomVector(size);
      std::vector<double> vec = v.toStdVector();

      std::ostringstream oss;
      oss << "Benchmark vpColVector::sumSquare() with size: " << size << " (ViSP)";
      double vp_sq_sum = 0;
      BENCHMARK(oss.str().c_str())
      {
        vp_sq_sum = v.sumSquare();
        return vp_sq_sum;
      };

      oss.str("");
      oss << "Benchmark std::inner_product with size: " << size << " (C++)";
      double std_sq_sum = 0;
      BENCHMARK(oss.str().c_str())
      {
        std_sq_sum = std::inner_product(vec.begin(), vec.end(), vec.begin(), 0.0);
        return std_sq_sum;
      };
      CHECK(vp_sq_sum == Approx(std_sq_sum));

      oss.str("");
      oss << "Benchmark naive sumSquare() with size: " << size;
      double naive_sq_sum = 0;
      BENCHMARK(oss.str().c_str())
      {
        naive_sq_sum = computeRegularSumSquare(v);
        return naive_sq_sum;
      };
      CHECK(naive_sq_sum == Approx(std_sq_sum));
    }
  }
}

TEST_CASE("Benchmark vpColVector::stdev()", "[benchmark]")
{
  // Sanity checks
  {
    vpColVector v(2);
    v[0] = 11;
    v[1] = 16;
    std::vector<double> vec = v.toStdVector();
    CHECK(vpColVector::stdev(v) == Approx(stddev(vec)).epsilon(std::numeric_limits<double>::epsilon()));
  }
  {
    const unsigned int size = 11;
    std::vector<double> vec(size);
    vpColVector v(size);
    for (size_t i = 0; i < 11; i++) {
      vec[i] = 2. * i;
      v[static_cast<unsigned int>(i)] = vec[i];
    }
    CHECK(vpColVector::stdev(v) == Approx(stddev(vec)).epsilon(std::numeric_limits<double>::epsilon()));
  }

  if (g_runBenchmark) {
    for (auto size : g_sizes) {
      vpColVector v = generateRandomVector(size);
      std::vector<double> vec = v.toStdVector();

      std::ostringstream oss;
      oss << "Benchmark vpColVector::stdev() with size: " << size << " (ViSP)";
      double vp_stddev = 0;
      BENCHMARK(oss.str().c_str())
      {
        vp_stddev = vpColVector::stdev(v);
        return vp_stddev;
      };

      oss.str("");
      oss << "Benchmark C++ stddev impl with size: " << size << " (C++)";
      double std_stddev = 0;
      BENCHMARK(oss.str().c_str())
      {
        std_stddev = stddev(vec);
        return std_stddev;
      };
      CHECK(vp_stddev == Approx(std_stddev));

      oss.str("");
      oss << "Benchmark naive stdev() with size: " << size;
      double naive_stddev = 0;
      BENCHMARK(oss.str().c_str())
      {
        naive_stddev = computeRegularStdev(v);
        return naive_stddev;
      };
      CHECK(naive_stddev == Approx(std_stddev));
    }
  }
}

TEST_CASE("Benchmark vpColVector::hadamard()", "[benchmark]")
{
  // Sanity checks
  {
    vpColVector v1(2), v2(2);
    v1[0] = 11;
    v1[1] = 16;
    v2[0] = 8;
    v2[1] = 23;
    vpColVector res1 = v1.hadamard(v2);
    vpColVector res2(computeHadamard(v1.toStdVector(), v2.toStdVector()));
    CHECK(almostEqual(res1, res2));
  }
  {
    const unsigned int size = 11;
    std::vector<double> vec1(size), vec2(size);
    for (size_t i = 0; i < 11; i++) {
      vec1[i] = 2. * i;
      vec2[i] = 3. * i + 5.;
    }
    vpColVector v1(vec1), v2(vec2);
    vpColVector res1 = v1.hadamard(v2);
    vpColVector res2(computeHadamard(v1.toStdVector(), v2.toStdVector()));
    CHECK(almostEqual(res1, res2));
  }

  if (g_runBenchmark) {
    for (auto size : g_sizes) {
      vpColVector v1 = generateRandomVector(size);
      vpColVector v2 = generateRandomVector(size);
      std::vector<double> vec1 = v1.toStdVector();
      std::vector<double> vec2 = v2.toStdVector();

      std::ostringstream oss;
      oss << "Benchmark vpColVector::hadamard() with size: " << size << " (ViSP)";
      vpColVector vp_res;
      BENCHMARK(oss.str().c_str())
      {
        vp_res = v1.hadamard(v2);
        return vp_res;
      };

      oss.str("");
      oss << "Benchmark C++ element wise multiplication with size: " << size << " (C++)";
      std::vector<double> std_res;
      BENCHMARK(oss.str().c_str())
      {
        std_res = computeHadamard(vec1, vec2);
        return std_res;
      };
      CHECK(almostEqual(vp_res, vpColVector(std_res)));
    }
  }
}

int main(int argc, char *argv[])
{
  // Set random seed explicitly to avoid confusion
  // See: https://en.cppreference.com/w/cpp/numeric/random/srand
  // If rand() is used before any calls to srand(), rand() behaves as if it was seeded with srand(1).
  srand(1);

  Catch::Session session; // There must be exactly one instance

  // Build a new parser on top of Catch's
  using namespace Catch::clara;
  auto cli = session.cli()         // Get Catch's composite command line parser
    | Opt(g_runBenchmark) // bind variable to a new option, with a hint string
    ["--benchmark"] // the option names it will respond to
    ("run benchmark?");   // description string for the help output

// Now pass the new composite back to Catch so it uses that
  session.cli(cli);

  // Let Catch (using Clara) parse the command line
  session.applyCommandLine(argc, argv);

  int numFailed = session.run();

  // numFailed is clamped to 255 as some unices only use the lower 8 bits.
  // This clamping has already been applied, so just return it here
  // You can also do any post run clean-up here
  return numFailed;
}
#else
#include <iostream>

int main() { return EXIT_SUCCESS; }
#endif