42 #include <visp3/core/vpConfig.h> 44 #if defined(VISP_HAVE_PTHREAD) || (defined(_WIN32) && !defined(WINRT_8_0)) 50 #include <visp3/core/vpColVector.h> 51 #include <visp3/core/vpIoTools.h> 52 #include <visp3/core/vpThread.h> 61 : m_add(), m_mul(), m_v1(v1), m_v2(v2), m_indexStart(start), m_indexEnd(end)
65 ArithmFunctor() : m_add(), m_mul(), m_v1(), m_v2(), m_indexStart(0), m_indexEnd(0) {}
67 void operator()() { computeImpl(); }
78 unsigned int m_indexStart;
79 unsigned int m_indexEnd;
83 m_add.
resize(m_indexEnd - m_indexStart);
84 m_mul.
resize(m_indexEnd - m_indexStart);
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];
100 ArithmFunctor *f =
static_cast<ArithmFunctor *
>(args);
108 unsigned int size = v1.
size();
111 for (
unsigned int i = 0, cpt = size; i < v2.
size(); i++, cpt++) {
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];
124 double add_th = res_add.
sum();
125 double mul_th = res_mul.
sum();
127 std::cout <<
"add=" << add <<
" ; add_th=" << add_th << std::endl;
128 std::cout <<
"mul=" << mul <<
" ; mul_th=" << mul_th << std::endl;
130 if (!
vpMath::equal(add, add_th, std::numeric_limits<double>::epsilon())) {
131 std::cerr <<
"Problem: add=" << add <<
" ; add_th=" << add_th << std::endl;
135 if (!
vpMath::equal(mul, mul_th, std::numeric_limits<double>::epsilon())) {
136 std::cerr <<
"Problem: mul=" << mul <<
" ; mul_th=" << mul_th << std::endl;
146 unsigned int nb_threads = 4;
147 unsigned int size = 1000007;
148 srand((
unsigned int)time(NULL));
151 for (
unsigned int i = 0; i < size; i++) {
152 v1[i] = rand() % 101;
153 v2[i] = rand() % 101;
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);
165 functors[i] = ArithmFunctor(v1, v2, i * split, size);
168 std::cout <<
"Create thread: " << i << std::endl;
175 for (
size_t i = 0; i < nb_threads; i++) {
176 std::cout <<
"Join thread: " << i << std::endl;
179 insert(res_add, functors[i].getVectorAdd());
180 insert(res_mul, functors[i].getVectorMul());
184 if (!check(v1, v2, res_add, res_mul)) {
188 std::cout <<
"testThread2 is ok!" << std::endl;
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;
202 std::cout <<
"Multi-threading seems not supported on this platform" << std::endl;
static bool equal(double x, double y, double s=0.001)
unsigned int size() const
Return the number of elements of the 2D array.
void resize(unsigned int i, bool flagNullify=true)
Implementation of column vector and the associated operations.