Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Test threading capabilities (extended).
32  *
33  *****************************************************************************/
34 
41 #include <visp3/core/vpConfig.h>
42 
43 #if defined(VISP_HAVE_PTHREAD) || (defined(_WIN32) && !defined(WINRT_8_0))
44 
45 #include <iostream>
46 #include <stdlib.h>
47 #include <time.h>
48 
49 #include <visp3/core/vpThread.h>
50 #include <visp3/core/vpColVector.h>
51 
52 
53 namespace {
55  class ArithmFunctor {
56  public:
57  ArithmFunctor(const vpColVector &v1, const vpColVector &v2, const unsigned int start, const unsigned int end) :
58  m_add(), m_mul(), m_v1(v1), m_v2(v2), m_indexStart(start), m_indexEnd(end) {
59  }
60 
61  ArithmFunctor() : m_add(), m_mul(), m_v1(), m_v2(), m_indexStart(0), m_indexEnd(0) {
62  }
63 
64  void operator()() {
65  computeImpl();
66  }
67 
68  vpColVector getVectorAdd() const {
69  return m_add;
70  }
71 
72  vpColVector getVectorMul() const {
73  return m_mul;
74  }
75 
76  private:
77  vpColVector m_add;
78  vpColVector m_mul;
79  vpColVector m_v1;
80  vpColVector m_v2;
81  unsigned int m_indexStart;
82  unsigned int m_indexEnd;
83 
84  void computeImpl() {
85  m_add.resize(m_indexEnd - m_indexStart);
86  m_mul.resize(m_indexEnd - m_indexStart);
87 
88  //to simulate a long computation
89  for (int iter = 0; iter < 100; iter++) {
90  for (unsigned int i = m_indexStart, cpt = 0; i < m_indexEnd; i++, cpt++) {
91  m_add[cpt] = m_v1[i] + m_v2[i];
92  m_mul[cpt] = m_v1[i] * m_v2[i];
93  }
94  }
95  }
96  };
98 
100  vpThread::Return arithmThread(vpThread::Args args) {
101  ArithmFunctor* f = static_cast<ArithmFunctor*>(args);
102  (*f)();
103  return 0;
104  }
106 
107  void insert(vpColVector &v1, const vpColVector &v2) {
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  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 }
142 
143 int main() {
144  unsigned int nb_threads = 4;
145  unsigned int size = 1000007;
146  srand((unsigned int) time(NULL));
147 
148  vpColVector v1(size), v2(size);
149  for (unsigned int i = 0; i < size; i++) {
150  v1[i] = rand() % 101;
151  v2[i] = rand() % 101;
152  }
153 
155  std::vector<vpThread> threads(nb_threads);
156  std::vector<ArithmFunctor> functors(nb_threads);
157  unsigned int split = size / nb_threads;
158  for (unsigned int i = 0; i < nb_threads; i++) {
159  if (i < nb_threads-1) {
160  functors[i] = ArithmFunctor(v1, v2, i*split, (i+1)*split);
161  } else {
162  functors[i] = ArithmFunctor(v1, v2, i*split, size);
163  }
164 
165  threads[i].create((vpThread::Fn) arithmThread, (vpThread::Args) &functors[i]);
166  }
168 
170  vpColVector res_add, res_mul;
171  for (size_t i = 0; i < nb_threads; i++) {
172  threads[i].join();
173 
174  insert(res_add, functors[i].getVectorAdd());
175  insert(res_mul, functors[i].getVectorMul());
176  }
178 
179  if (!check(v1, v2, res_add, res_mul)) {
180  return EXIT_FAILURE;
181  }
182 
183  std::cout << "testThread2 is ok!" << std::endl;
184  return EXIT_SUCCESS;
185 }
186 
187 #else
188 
189 #include <iostream>
190 #include <cstdlib>
191 
192 int main()
193 {
194 # if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
195  std::cout << "You should enable pthread usage and rebuild ViSP..." << std::endl;
196 # else
197  std::cout << "Multi-threading seems not supported on this platform" << std::endl;
198 # endif
199  return EXIT_SUCCESS;
200 }
201 #endif
void * Return
Definition: vpThread.h:36
static bool equal(double x, double y, double s=0.001)
Definition: vpMath.h:306
unsigned int size() const
Return the number of elements of the 2D array.
Definition: vpArray2D.h:156
void *(* Fn)(Args)
Definition: vpThread.h:37
void * Args
Definition: vpThread.h:35
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
double sum() const
void resize(const unsigned int i, const bool flagNullify=true)
Definition: vpColVector.h:225