41 #include <visp3/core/vpConfig.h>
43 #if defined(VISP_HAVE_PTHREAD) || (defined(_WIN32) && !defined(WINRT_8_0))
49 #include <visp3/core/vpThread.h>
50 #include <visp3/core/vpColVector.h>
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) {
61 ArithmFunctor() : m_add(), m_mul(), m_v1(), m_v2(), m_indexStart(0), m_indexEnd(0) {
81 unsigned int m_indexStart;
82 unsigned int m_indexEnd;
85 m_add.
resize(m_indexEnd - m_indexStart);
86 m_mul.resize(m_indexEnd - m_indexStart);
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];
101 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++) {
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];
123 double add_th = res_add.
sum();
124 double mul_th = res_mul.
sum();
126 std::cout <<
"add=" << add <<
" ; add_th=" << add_th << std::endl;
127 std::cout <<
"mul=" << mul <<
" ; mul_th=" << mul_th << std::endl;
129 if (!
vpMath::equal(add, add_th, std::numeric_limits<double>::epsilon())) {
130 std::cerr <<
"Problem: add=" << add <<
" ; add_th=" << add_th << std::endl;
134 if (!
vpMath::equal(mul, mul_th, std::numeric_limits<double>::epsilon())) {
135 std::cerr <<
"Problem: mul=" << mul <<
" ; mul_th=" << mul_th << std::endl;
144 unsigned int nb_threads = 4;
145 unsigned int size = 1000007;
146 srand((
unsigned int) time(NULL));
149 for (
unsigned int i = 0; i < size; i++) {
150 v1[i] = rand() % 101;
151 v2[i] = rand() % 101;
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);
162 functors[i] = ArithmFunctor(v1, v2, i*split, size);
171 for (
size_t i = 0; i < nb_threads; i++) {
174 insert(res_add, functors[i].getVectorAdd());
175 insert(res_mul, functors[i].getVectorMul());
179 if (!check(v1, v2, res_add, res_mul)) {
183 std::cout <<
"testThread2 is ok!" << std::endl;
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;
197 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.
Implementation of column vector and the associated operations.
void resize(const unsigned int i, const bool flagNullify=true)