📄 convex_hull_bench.cpp
字号:
/* Copyright 2005-2007 Intel Corporation. All Rights Reserved. This file is part of Threading Building Blocks. Threading Building Blocks is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License version 2 as published by the Free Software Foundation. Threading Building Blocks is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Threading Building Blocks; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA As a special exception, you may use this file as part of a free software library without restriction. Specifically, if other files instantiate templates or use macros or inline functions from this file, or you compile this file and link it with other files to produce an executable, this file does not by itself cause the resulting executable to be covered by the GNU General Public License. This exception does not however invalidate any other reasons why the executable file might be covered by the GNU General Public License.*/#include "convex_hull.h"typedef util::point<double> point_t;#define USETBB 1#define USECONCVEC 1#define INIT_ONCE 1#if !USETBB // Serial implementation of Quick Hull algorithmtypedef std::vector< point_t > pointVec_t;// C++ style serial codeclass FillRNDPointsVector : public std::unary_function<point_t&, void> { unsigned int rseed; size_t count;public: FillRNDPointsVector() : rseed(1), count(0) {} void operator()(point_t& p) { p = util::GenerateRNDPoint<double>(count, rseed); }};void initialize(pointVec_t &points) { points.clear(); points.resize(cfg::MAXPOINTS); std::for_each(points.begin(), points.end(), FillRNDPointsVector());}class FindXExtremum : public std::unary_function<const point_t&, void> {public: typedef enum { minX, maxX } extremumType; FindXExtremum(const point_t& frstPoint, extremumType exType_) : extrXPoint(frstPoint), exType(exType_) {} void operator()(const point_t& p) { if(closerToExtremum(p)) extrXPoint = p; } operator point_t () { return extrXPoint; }private: const extremumType exType; point_t extrXPoint; bool closerToExtremum(const point_t &p) const { switch(exType) { case minX: return p.x<extrXPoint.x; break; case maxX: return p.x>extrXPoint.x; break; } }};template <FindXExtremum::extremumType type>point_t extremum(const pointVec_t &points) { assert(!points.empty()); return std::for_each(points.begin(), points.end(), FindXExtremum(points[0], type));}class SplitByCP : public std::unary_function<const point_t&, void> { pointVec_t &reducedSet; point_t p1, p2; point_t farPoint; double howFar;public: SplitByCP( point_t _p1, point_t _p2, pointVec_t &_reducedSet) : p1(_p1), p2(_p2), reducedSet(_reducedSet), howFar(0), farPoint(p1) {} void operator()(const point_t& p) { double cp; if( (p != p1) && (p != p2) ) { cp = util::cross_product(p1, p2, p); if(cp>0) { reducedSet.push_back(p); if(cp>howFar) { farPoint = p; howFar = cp; } } } } operator point_t (){ return farPoint; }};point_t divide(const pointVec_t &P, pointVec_t &P_reduced, const point_t &p1, const point_t &p2) { SplitByCP splitByCP(p1, p2, P_reduced); point_t farPoint = std::for_each(P.begin(), P.end(), splitByCP); if(util::VERBOSE) { std::stringstream ss; ss << P.size() << " nodes in bucket"<< ", " << "dividing by: [ " << p1 << ", " << p2 << " ], " << "farthest node: " << farPoint; util::OUTPUT.push_back(ss.str()); } return farPoint;}void divide_and_conquer(const pointVec_t &P, pointVec_t &H, point_t p1, point_t p2) { if (P.size()<2) { H.push_back(p1); H.insert(H.end(), P.begin(), P.end()); } else { pointVec_t P_reduced; pointVec_t H1, H2; point_t p_far; p_far = divide(P, P_reduced, p1, p2); divide_and_conquer(P_reduced, H1, p1, p_far); divide_and_conquer(P_reduced, H2, p_far, p2); H.insert(H.end(), H1.begin(), H1.end()); H.insert(H.end(), H2.begin(), H2.end()); }}void quickhull(const pointVec_t &points, pointVec_t &hull) { hull.clear(); point_t p_maxx = extremum<FindXExtremum::maxX>(points); point_t p_minx = extremum<FindXExtremum::minX>(points); pointVec_t H; divide_and_conquer(points, hull, p_maxx, p_minx); divide_and_conquer(points, H, p_minx, p_maxx); hull.insert(hull.end(), H.begin(), H.end());}int main(int argc, char* argv[]) { util::ParseInputArgs(argc, argv); pointVec_t points; pointVec_t hull; util::my_time_t tm_init, tm_start, tm_end; std::cout << "Starting serial version of QUICK HULL algorithm" << std::endl; tm_init = util::gettime(); initialize(points); tm_start = util::gettime(); quickhull(points, hull); tm_end = util::gettime(); util::WriteResults(1, util::time_diff(tm_init, tm_start), util::time_diff(tm_start, tm_end));}#else // USETBB - parallel version of Quick Hull algorithm#include "tbb/task_scheduler_init.h"#include "tbb/parallel_for.h"#include "tbb/parallel_reduce.h"#include "tbb/blocked_range.h"typedef tbb::blocked_range<size_t> range_t;#if USECONCVEC#include "tbb/concurrent_vector.h"typedef tbb::concurrent_vector<point_t> pointVec_t;void appendVector(const point_t* src, size_t srcSize, pointVec_t& dest) { std::copy(src, src + srcSize, dest.begin() + dest.grow_by(srcSize));}void appendVector(const pointVec_t& src, pointVec_t& dest) { std::copy(src.begin(), src.end(), dest.begin() + dest.grow_by(src.size()));}#else // USE STD::VECTOR - include spin_mutex.h and lock vector operations#include "tbb/spin_mutex.h"typedef tbb::spin_mutex mutex_t;typedef std::vector<point_t> pointVec_t;void appendVector(mutex_t& insertMutex, const pointVec_t& src, pointVec_t& dest) { mutex_t::scoped_lock lock(insertMutex); dest.insert(dest.end(), src.begin(), src.end());}void appendVector(mutex_t& insertMutex, const point_t* src, size_t srcSize, pointVec_t& dest) { mutex_t::scoped_lock lock(insertMutex); dest.insert(dest.end(), src, src + srcSize);}#endif // USECONCVECclass FillRNDPointsVector { pointVec_t &points; mutable unsigned int rseed;public: static const size_t grainSize = cfg::GENERATE_GS;#if !USECONCVEC static mutex_t pushBackMutex;#endif // USECONCVEC FillRNDPointsVector(pointVec_t& _points) : points(_points), rseed(1) {} FillRNDPointsVector(const FillRNDPointsVector& other) : points(other.points), rseed(other.rseed+1) {} void operator()(const range_t& range) const { const size_t i_end = range.end(); size_t count = 0; for(size_t i = range.begin(); i != i_end; ++i) {#if USECONCVEC points.push_back(util::GenerateRNDPoint<double>(count, rseed));#else // Locked push_back to a not thread-safe STD::VECTOR { mutex_t::scoped_lock lock(pushBackMutex); points.push_back(util::GenerateRNDPoint<double>(count, rseed)); }#endif // USECONCVEC } }};class FillRNDPointsVector_buf { pointVec_t &points; mutable unsigned int rseed;public: static const size_t grainSize = cfg::GENERATE_GS;#if !USECONCVEC static mutex_t insertMutex;#endif // USECONCVEC FillRNDPointsVector_buf(pointVec_t& _points) : points(_points), rseed(1) {} FillRNDPointsVector_buf(const FillRNDPointsVector_buf& other) : points(other.points), rseed(other.rseed+1) {} void operator()(const range_t& range) const { const size_t i_end = range.end(); size_t count = 0, j = 0; point_t tmp_vec[grainSize]; for(size_t i=range.begin(); i!=i_end; ++i) { tmp_vec[j++] = util::GenerateRNDPoint<double>(count, rseed); }#if USECONCVEC appendVector(tmp_vec, j, points);#else // USE STD::VECTOR appendVector(insertMutex, tmp_vec, j, points);#endif // USECONCVEC } };#if !USECONCVECmutex_t FillRNDPointsVector::pushBackMutex = mutex_t();mutex_t FillRNDPointsVector_buf::insertMutex = mutex_t();#endiftemplate<typename BodyType>void initialize(pointVec_t &points) { points.clear(); tbb::parallel_for(range_t(0, cfg::MAXPOINTS, BodyType::grainSize), BodyType(points));}class FindXExtremum {public: typedef enum { minX, maxX } extremumType; static const size_t grainSize = cfg::FINDEXT_GS;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -