Visual Servoing Platform version 3.5.0
testThread2.cpp
1/****************************************************************************
2 *
3 * ViSP, open source Visual Servoing Platform software.
4 * Copyright (C) 2005 - 2019 by Inria. All rights reserved.
5 *
6 * This software is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 * See the file LICENSE.txt at the root directory of this source
11 * distribution for additional information about the GNU GPL.
12 *
13 * For using ViSP with software that can not be combined with the GNU
14 * GPL, please contact Inria about acquiring a ViSP Professional
15 * Edition License.
16 *
17 * See http://visp.inria.fr for more information.
18 *
19 * This software was developed at:
20 * Inria Rennes - Bretagne Atlantique
21 * Campus Universitaire de Beaulieu
22 * 35042 Rennes Cedex
23 * France
24 *
25 * If you have questions regarding the use of this file, please contact
26 * Inria at visp@inria.fr
27 *
28 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30 *
31 * Description:
32 * Test threading capabilities (extended).
33 *
34 *****************************************************************************/
35
42#include <visp3/core/vpConfig.h>
43
44#if defined(VISP_HAVE_PTHREAD) || (defined(_WIN32) && !defined(WINRT_8_0))
45
46#include <iostream>
47#include <stdlib.h>
48#include <time.h>
49
50#include <visp3/core/vpColVector.h>
51#include <visp3/core/vpIoTools.h>
52#include <visp3/core/vpThread.h>
53
54namespace
55{
57class ArithmFunctor
58{
59public:
60 ArithmFunctor(const vpColVector &v1, const vpColVector &v2, unsigned int start, unsigned int end)
61 : m_add(), m_mul(), m_v1(v1), m_v2(v2), m_indexStart(start), m_indexEnd(end)
62 {
63 }
64
65 ArithmFunctor() : m_add(), m_mul(), m_v1(), m_v2(), m_indexStart(0), m_indexEnd(0) {}
66
67 void operator()() { computeImpl(); }
68
69 vpColVector getVectorAdd() const { return m_add; }
70
71 vpColVector getVectorMul() const { return m_mul; }
72
73private:
74 vpColVector m_add;
75 vpColVector m_mul;
76 vpColVector m_v1;
77 vpColVector m_v2;
78 unsigned int m_indexStart;
79 unsigned int m_indexEnd;
80
81 void computeImpl()
82 {
83 m_add.resize(m_indexEnd - m_indexStart);
84 m_mul.resize(m_indexEnd - m_indexStart);
85
86 // to simulate a long computation
87 for (int iter = 0; iter < 100; iter++) {
88 for (unsigned int i = m_indexStart, cpt = 0; i < m_indexEnd; i++, cpt++) {
89 m_add[cpt] = m_v1[i] + m_v2[i];
90 m_mul[cpt] = m_v1[i] * m_v2[i];
91 }
92 }
93 }
94};
96
98vpThread::Return arithmThread(vpThread::Args args)
99{
100 ArithmFunctor *f = static_cast<ArithmFunctor *>(args);
101 (*f)();
102 return 0;
103}
105
106void insert(vpColVector &v1, const vpColVector &v2)
107{
108 unsigned int size = v1.size();
109 v1.resize(size + v2.size(), false);
110
111 for (unsigned int i = 0, cpt = size; i < v2.size(); i++, cpt++) {
112 v1[cpt] = v2[i];
113 }
114}
115
116bool check(const vpColVector &v1, const vpColVector &v2, const vpColVector &res_add, const vpColVector &res_mul)
117{
118 double add = 0.0, mul = 0.0;
119 for (unsigned int i = 0; i < v1.size(); i++) {
120 add += v1[i] + v2[i];
121 mul += v1[i] * v2[i];
122 }
123
124 double add_th = res_add.sum();
125 double mul_th = res_mul.sum();
126
127 std::cout << "add=" << add << " ; add_th=" << add_th << std::endl;
128 std::cout << "mul=" << mul << " ; mul_th=" << mul_th << std::endl;
129
130 if (!vpMath::equal(add, add_th, std::numeric_limits<double>::epsilon())) {
131 std::cerr << "Problem: add=" << add << " ; add_th=" << add_th << std::endl;
132 return false;
133 }
134
135 if (!vpMath::equal(mul, mul_th, std::numeric_limits<double>::epsilon())) {
136 std::cerr << "Problem: mul=" << mul << " ; mul_th=" << mul_th << std::endl;
137 return false;
138 }
139
140 return true;
141}
142}
143
144int main()
145{
146 unsigned int nb_threads = 4;
147 unsigned int size = 1000007;
148 srand((unsigned int)time(NULL));
149
150 vpColVector v1(size), v2(size);
151 for (unsigned int i = 0; i < size; i++) {
152 v1[i] = rand() % 101;
153 v2[i] = rand() % 101;
154 }
155
157 std::vector<vpThread> threads(nb_threads);
158 std::vector<ArithmFunctor> functors(nb_threads);
159 unsigned int split = size / nb_threads;
160 for (unsigned int i = 0; i < nb_threads; i++) {
161 if (i < nb_threads - 1) {
162 functors[i] = ArithmFunctor(v1, v2, i * split, (i + 1) * split);
163 }
164 else {
165 functors[i] = ArithmFunctor(v1, v2, i * split, size);
166 }
167
168 std::cout << "Create thread: " << i << std::endl;
169 threads[i].create((vpThread::Fn)arithmThread, (vpThread::Args)&functors[i]);
170 }
172
174 vpColVector res_add, res_mul;
175 for (size_t i = 0; i < nb_threads; i++) {
176 std::cout << "Join thread: " << i << std::endl;
177 threads[i].join();
178
179 insert(res_add, functors[i].getVectorAdd());
180 insert(res_mul, functors[i].getVectorMul());
181 }
183
184 if (!check(v1, v2, res_add, res_mul)) {
185 return EXIT_FAILURE;
186 }
187
188 std::cout << "testThread2 is ok!" << std::endl;
189 return EXIT_SUCCESS;
190}
191
192#else
193
194#include <cstdlib>
195#include <iostream>
196
197int main()
198{
199#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
200 std::cout << "You should enable pthread usage and rebuild ViSP..." << std::endl;
201#else
202 std::cout << "Multi-threading seems not supported on this platform" << std::endl;
203#endif
204 return EXIT_SUCCESS;
205}
206#endif
unsigned int size() const
Return the number of elements of the 2D array.
Definition: vpArray2D.h:291
Implementation of column vector and the associated operations.
Definition: vpColVector.h:131
double sum() const
void resize(unsigned int i, bool flagNullify=true)
Definition: vpColVector.h:310
static bool equal(double x, double y, double s=0.001)
Definition: vpMath.h:295
void insert(const vpMatrix &A, const vpMatrix &B, vpMatrix &C, unsigned int r, unsigned int c)
Definition: vpMatrix.cpp:5500
void * Return
Definition: vpThread.h:78
void *(* Fn)(Args)
Definition: vpThread.h:79
void * Args
Definition: vpThread.h:77