Visual Servoing Platform version 3.6.0
Loading...
Searching...
No Matches
testThread2.cpp
1/****************************************************************************
2 *
3 * ViSP, open source Visual Servoing Platform software.
4 * Copyright (C) 2005 - 2023 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 https://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 vpArithmFunctor
58{
59public:
60 vpArithmFunctor(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 vpArithmFunctor() : m_add(), m_mul(), m_v1(), m_v2(), m_indexStart(0), m_indexEnd(0) { }
65
66 void operator()() { computeImpl(); }
67
68 vpColVector getVectorAdd() const { return m_add; }
69
70 vpColVector getVectorMul() const { return m_mul; }
71
72private:
73 vpColVector m_add;
74 vpColVector m_mul;
75 vpColVector m_v1;
76 vpColVector m_v2;
77 unsigned int m_indexStart;
78 unsigned int m_indexEnd;
79
80 void computeImpl()
81 {
82 m_add.resize(m_indexEnd - m_indexStart);
83 m_mul.resize(m_indexEnd - m_indexStart);
84
85 // to simulate a long computation
86 for (int iter = 0; iter < 100; iter++) {
87 for (unsigned int i = m_indexStart, cpt = 0; i < m_indexEnd; i++, cpt++) {
88 m_add[cpt] = m_v1[i] + m_v2[i];
89 m_mul[cpt] = m_v1[i] * m_v2[i];
90 }
91 }
92 }
93};
95
97vpThread::Return arithmThread(vpThread::Args args)
98{
99 vpArithmFunctor *f = static_cast<vpArithmFunctor *>(args);
100 (*f)();
101 return 0;
102}
104
105void insert(vpColVector &v1, const vpColVector &v2)
106{
107 unsigned int size = v1.size();
108 v1.resize(size + v2.size(), false);
109
110 for (unsigned int i = 0, cpt = size; i < v2.size(); i++, cpt++) {
111 v1[cpt] = v2[i];
112 }
113}
114
115bool check(const vpColVector &v1, const vpColVector &v2, const vpColVector &res_add, const vpColVector &res_mul)
116{
117 double add = 0.0, mul = 0.0;
118 for (unsigned int i = 0; i < v1.size(); i++) {
119 add += v1[i] + v2[i];
120 mul += v1[i] * v2[i];
121 }
122
123 double add_th = res_add.sum();
124 double mul_th = res_mul.sum();
125
126 std::cout << "add=" << add << " ; add_th=" << add_th << std::endl;
127 std::cout << "mul=" << mul << " ; mul_th=" << mul_th << std::endl;
128
129 if (!vpMath::equal(add, add_th, std::numeric_limits<double>::epsilon())) {
130 std::cerr << "Problem: add=" << add << " ; add_th=" << add_th << std::endl;
131 return false;
132 }
133
134 if (!vpMath::equal(mul, mul_th, std::numeric_limits<double>::epsilon())) {
135 std::cerr << "Problem: mul=" << mul << " ; mul_th=" << mul_th << std::endl;
136 return false;
137 }
138
139 return true;
140}
141} // namespace
142
143int main()
144{
145 unsigned int nb_threads = 4;
146 unsigned int size = 1000007;
147 srand((unsigned int)time(NULL));
148
149 vpColVector v1(size), v2(size);
150 for (unsigned int i = 0; i < size; i++) {
151 v1[i] = rand() % 101;
152 v2[i] = rand() % 101;
153 }
154
156 std::vector<vpThread> threads(nb_threads);
157 std::vector<vpArithmFunctor> functors(nb_threads);
158 unsigned int split = size / nb_threads;
159 for (unsigned int i = 0; i < nb_threads; i++) {
160 if (i < nb_threads - 1) {
161 functors[i] = vpArithmFunctor(v1, v2, i * split, (i + 1) * split);
162 }
163 else {
164 functors[i] = vpArithmFunctor(v1, v2, i * split, size);
165 }
166
167 std::cout << "Create thread: " << i << std::endl;
168 threads[i].create((vpThread::Fn)arithmThread, (vpThread::Args)&functors[i]);
169 }
171
173 vpColVector res_add, res_mul;
174 for (size_t i = 0; i < nb_threads; i++) {
175 std::cout << "Join thread: " << i << std::endl;
176 threads[i].join();
177
178 insert(res_add, functors[i].getVectorAdd());
179 insert(res_mul, functors[i].getVectorMul());
180 }
182
183 if (!check(v1, v2, res_add, res_mul)) {
184 return EXIT_FAILURE;
185 }
186
187 std::cout << "testThread2 is ok!" << std::endl;
188 return EXIT_SUCCESS;
189}
190
191#else
192
193#include <cstdlib>
194#include <iostream>
195
196int main()
197{
198#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
199 std::cout << "You should enable pthread usage and rebuild ViSP..." << std::endl;
200#else
201 std::cout << "Multi-threading seems not supported on this platform" << std::endl;
202#endif
203 return EXIT_SUCCESS;
204}
205#endif
unsigned int size() const
Return the number of elements of the 2D array.
Definition vpArray2D.h:292
Implementation of column vector and the associated operations.
double sum() const
void resize(unsigned int i, bool flagNullify=true)
static bool equal(double x, double y, double threshold=0.001)
Definition vpMath.h:369
void *(*) Fn(Args)
Definition vpThread.h:74
void * Args
Definition vpThread.h:72
void * Return
Definition vpThread.h:73