@@ -166,10 +166,22 @@ void vpRBSilhouetteCCDTracker::extractFeatures(const vpRBFeatureTrackerInput &fr
166166 const vpHomogeneousMatrix cMo = frame.renders .cMo ;
167167 const vpHomogeneousMatrix oMc = cMo.inverse ();
168168
169+ std::vector<std::vector<vpRBSilhouetteControlPoint>> pointsPerThread;
170+ #ifdef VISP_HAVE_OPENMP
171+ pointsPerThread.resize (omp_get_num_threads ());
172+ #else
173+ pointsPerThread.resize (1 );
174+ #endif
175+
169176#ifdef VISP_HAVE_OPENMP
170177#pragma omp parallel
171178#endif
172179 {
180+ #ifdef VISP_HAVE_OPENMP
181+ unsigned int threadIdx = omp_get_thread_num ();
182+ #else
183+ unsigned int threadIdx = 0 ;
184+ #endif
173185 std::vector<vpRBSilhouetteControlPoint> localControlPoints;
174186#ifdef VISP_HAVE_OPENMP
175187#pragma omp for nowait
@@ -205,14 +217,21 @@ void vpRBSilhouetteCCDTracker::extractFeatures(const vpRBFeatureTrackerInput &fr
205217 }
206218 localControlPoints.push_back (std::move (pccd));
207219 }
208- #ifdef VISP_HAVE_OPENMP
209- #pragma omp critical
210- #endif
220+
221+
211222 {
212- m_controlPoints. insert (m_controlPoints. end (), localControlPoints. begin (), localControlPoints. end () );
223+ pointsPerThread[threadIdx] = std::move (localControlPoints );
213224 }
214225 }
226+ unsigned int numElements = 0 ;
227+ for (const std::vector<vpRBSilhouetteControlPoint> &points: pointsPerThread) {
228+ numElements += points.size ();
229+ }
215230
231+ m_controlPoints.reserve (numElements);
232+ for (const std::vector<vpRBSilhouetteControlPoint> &points: pointsPerThread) {
233+ m_controlPoints.insert (m_controlPoints.end (), points.begin (), points.end ());
234+ }
216235
217236 if (m_maxPoints > 0 && m_controlPoints.size () > m_maxPoints) {
218237 std::vector<size_t > keptIndices (m_maxPoints);
@@ -798,13 +817,28 @@ void vpRBSilhouetteCCDTracker::computeErrorAndInteractionMatrix()
798817 m_weights[i * 2 * normal_points_number * 3 + j] = weightPerPoint[i];
799818 }
800819 }
820+ std::vector<vpColVector> gradientPerThread;
821+ std::vector<vpMatrix> hessianPerThread;
822+ #ifdef VISP_HAVE_OPENMP
823+ unsigned int numThreads = omp_get_num_threads ();
824+ #else
825+ unsigned int numThreads = 1 ;
826+ #endif
827+
828+ gradientPerThread.resize (omp_get_num_threads ());
829+ hessianPerThread.resize (omp_get_num_threads ());
801830
802831 m_gradient = 0.0 ;
803832 m_hessian = 0.0 ;
804833#ifdef VISP_HAVE_OPENMP
805834#pragma omp parallel
806835#endif
807836 {
837+ #ifdef VISP_HAVE_OPENMP
838+ unsigned int threadIdx = omp_get_thread_num ();
839+ #else
840+ unsigned int threadIdx = 1 ;
841+ #endif
808842 vpColVector localGradient (m_gradient.getRows (), 0.0 );
809843 vpMatrix localHessian (m_hessian.getRows (), m_hessian.getCols (), 0.0 );
810844
@@ -826,15 +860,15 @@ void vpRBSilhouetteCCDTracker::computeErrorAndInteractionMatrix()
826860 }
827861 }
828862 }
829- #ifdef VISP_HAVE_OPENMP
830- #pragma omp critical
831- #endif
832- {
833- m_gradient += localGradient;
834- m_hessian += localHessian;
835- }
863+
864+ gradientPerThread[threadIdx] = localGradient;
865+ hessianPerThread[threadIdx] = localHessian;
836866 }
837867
868+ for (unsigned int i = 0 ; i < gradientPerThread.size (); ++i) {
869+ m_gradient += gradientPerThread[i];
870+ m_hessian += hessianPerThread[i];
871+ }
838872
839873 m_LTL = m_hessian;
840874 m_LTR = -m_gradient;
@@ -864,7 +898,6 @@ void vpRBSilhouetteCCDTracker::computeErrorAndInteractionMatrix()
864898 m_LTL = 0 ;
865899 m_LTR = 0 ;
866900
867-
868901 std::cerr << e.what () << std::endl;
869902 }
870903}
0 commit comments