Visual Servoing Platform  version 3.1.0
testThread2.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 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 
54 namespace
55 {
57 class ArithmFunctor
58 {
59 public:
60  ArithmFunctor(const vpColVector &v1, const vpColVector &v2, const unsigned int start, const 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 
73 private:
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 
98 vpThread::Return arithmThread(vpThread::Args args)
99 {
100  ArithmFunctor *f = static_cast<ArithmFunctor *>(args);
101  (*f)();
102  return 0;
103 }
105 
106 void 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 
116 bool 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 
144 int main()
145 {
146  std::string appveyor_threading = "";
147  try {
148  appveyor_threading = vpIoTools::getenv("APPVEYOR_THREADING");
149  } catch (...) {
150  }
151 
152  if (appveyor_threading == "true") {
153  unsigned int nb_threads = 4;
154  unsigned int size = 1000007;
155  srand((unsigned int)time(NULL));
156 
157  vpColVector v1(size), v2(size);
158  for (unsigned int i = 0; i < size; i++) {
159  v1[i] = rand() % 101;
160  v2[i] = rand() % 101;
161  }
162 
164  std::vector<vpThread> threads(nb_threads);
165  std::vector<ArithmFunctor> functors(nb_threads);
166  unsigned int split = size / nb_threads;
167  for (unsigned int i = 0; i < nb_threads; i++) {
168  if (i < nb_threads - 1) {
169  functors[i] = ArithmFunctor(v1, v2, i * split, (i + 1) * split);
170  } else {
171  functors[i] = ArithmFunctor(v1, v2, i * split, size);
172  }
173 
174  std::cout << "Create thread: " << i << std::endl;
175  threads[i].create((vpThread::Fn)arithmThread, (vpThread::Args)&functors[i]);
176  }
178 
180  vpColVector res_add, res_mul;
181  for (size_t i = 0; i < nb_threads; i++) {
182  std::cout << "Join thread: " << i << std::endl;
183  threads[i].join();
184 
185  insert(res_add, functors[i].getVectorAdd());
186  insert(res_mul, functors[i].getVectorMul());
187  }
189 
190  if (!check(v1, v2, res_add, res_mul)) {
191  return EXIT_FAILURE;
192  }
193  }
194 
195  std::cout << "testThread2 is ok!" << std::endl;
196  return EXIT_SUCCESS;
197 }
198 
199 #else
200 
201 #include <cstdlib>
202 #include <iostream>
203 
204 int main()
205 {
206 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
207  std::cout << "You should enable pthread usage and rebuild ViSP..." << std::endl;
208 #else
209  std::cout << "Multi-threading seems not supported on this platform" << std::endl;
210 #endif
211  return EXIT_SUCCESS;
212 }
213 #endif
void * Return
Definition: vpThread.h:41
static bool equal(double x, double y, double s=0.001)
Definition: vpMath.h:290
static std::string getenv(const char *env)
Definition: vpIoTools.cpp:262
unsigned int size() const
Return the number of elements of the 2D array.
Definition: vpArray2D.h:158
double sum() const
void *(* Fn)(Args)
Definition: vpThread.h:42
void * Args
Definition: vpThread.h:40
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
void resize(const unsigned int i, const bool flagNullify=true)
Definition: vpColVector.h:241