File: testThread2.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 (205 lines) | stat: -rw-r--r-- 5,813 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
/****************************************************************************
 *
 * 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:
 * Test threading capabilities (extended).
 *
*****************************************************************************/

/*!
  \example testThread2.cpp

  \brief Test threading capabilities (extended).
*/

#include <visp3/core/vpConfig.h>

#if defined(VISP_HAVE_PTHREAD) || (defined(_WIN32) && !defined(WINRT_8_0))

#include <iostream>
#include <stdlib.h>
#include <time.h>

#include <visp3/core/vpColVector.h>
#include <visp3/core/vpIoTools.h>
#include <visp3/core/vpThread.h>

namespace
{
//! [functor-thread-example declaration]
class vpArithmFunctor
{
public:
  vpArithmFunctor(const vpColVector &v1, const vpColVector &v2, unsigned int start, unsigned int end)
    : m_add(), m_mul(), m_v1(v1), m_v2(v2), m_indexStart(start), m_indexEnd(end)
  { }

  vpArithmFunctor() : m_add(), m_mul(), m_v1(), m_v2(), m_indexStart(0), m_indexEnd(0) { }

  void operator()() { computeImpl(); }

  vpColVector getVectorAdd() const { return m_add; }

  vpColVector getVectorMul() const { return m_mul; }

private:
  vpColVector m_add;
  vpColVector m_mul;
  vpColVector m_v1;
  vpColVector m_v2;
  unsigned int m_indexStart;
  unsigned int m_indexEnd;

  void computeImpl()
  {
    m_add.resize(m_indexEnd - m_indexStart);
    m_mul.resize(m_indexEnd - m_indexStart);

    // to simulate a long computation
    for (int iter = 0; iter < 100; iter++) {
      for (unsigned int i = m_indexStart, cpt = 0; i < m_indexEnd; i++, cpt++) {
        m_add[cpt] = m_v1[i] + m_v2[i];
        m_mul[cpt] = m_v1[i] * m_v2[i];
      }
    }
  }
};
//! [functor-thread-example declaration]

//! [functor-thread-example threadFunction]
vpThread::Return arithmThread(vpThread::Args args)
{
  vpArithmFunctor *f = static_cast<vpArithmFunctor *>(args);
  (*f)();
  return 0;
}
//! [functor-thread-example threadFunction]

void insert(vpColVector &v1, const vpColVector &v2)
{
  unsigned int size = v1.size();
  v1.resize(size + v2.size(), false);

  for (unsigned int i = 0, cpt = size; i < v2.size(); i++, cpt++) {
    v1[cpt] = v2[i];
  }
}

bool check(const vpColVector &v1, const vpColVector &v2, const vpColVector &res_add, const vpColVector &res_mul)
{
  double add = 0.0, mul = 0.0;
  for (unsigned int i = 0; i < v1.size(); i++) {
    add += v1[i] + v2[i];
    mul += v1[i] * v2[i];
  }

  double add_th = res_add.sum();
  double mul_th = res_mul.sum();

  std::cout << "add=" << add << " ; add_th=" << add_th << std::endl;
  std::cout << "mul=" << mul << " ; mul_th=" << mul_th << std::endl;

  if (!vpMath::equal(add, add_th, std::numeric_limits<double>::epsilon())) {
    std::cerr << "Problem: add=" << add << " ; add_th=" << add_th << std::endl;
    return false;
  }

  if (!vpMath::equal(mul, mul_th, std::numeric_limits<double>::epsilon())) {
    std::cerr << "Problem: mul=" << mul << " ; mul_th=" << mul_th << std::endl;
    return false;
  }

  return true;
}
} // namespace

int main()
{
  unsigned int nb_threads = 4;
  unsigned int size = 1000007;
  srand((unsigned int)time(NULL));

  vpColVector v1(size), v2(size);
  for (unsigned int i = 0; i < size; i++) {
    v1[i] = rand() % 101;
    v2[i] = rand() % 101;
  }

  //! [functor-thread-example threadCreation]
  std::vector<vpThread> threads(nb_threads);
  std::vector<vpArithmFunctor> functors(nb_threads);
  unsigned int split = size / nb_threads;
  for (unsigned int i = 0; i < nb_threads; i++) {
    if (i < nb_threads - 1) {
      functors[i] = vpArithmFunctor(v1, v2, i * split, (i + 1) * split);
    }
    else {
      functors[i] = vpArithmFunctor(v1, v2, i * split, size);
    }

    std::cout << "Create thread: " << i << std::endl;
    threads[i].create((vpThread::Fn)arithmThread, (vpThread::Args)&functors[i]);
  }
  //! [functor-thread-example threadCreation]

  //! [functor-thread-example getResults]
  vpColVector res_add, res_mul;
  for (size_t i = 0; i < nb_threads; i++) {
    std::cout << "Join thread: " << i << std::endl;
    threads[i].join();

    insert(res_add, functors[i].getVectorAdd());
    insert(res_mul, functors[i].getVectorMul());
  }
  //! [functor-thread-example getResults]

  if (!check(v1, v2, res_add, res_mul)) {
    return EXIT_FAILURE;
  }

  std::cout << "testThread2 is ok!" << std::endl;
  return EXIT_SUCCESS;
}

#else

#include <cstdlib>
#include <iostream>

int main()
{
#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
  std::cout << "You should enable pthread usage and rebuild ViSP..." << std::endl;
#else
  std::cout << "Multi-threading seems not supported on this platform" << std::endl;
#endif
  return EXIT_SUCCESS;
}
#endif