2 Copyright (c) 2005-2019 Intel Corporation
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
17 #ifndef __CONVEX_HULL_H__
18 #define __CONVEX_HULL_H__
31 #include "tbb/tick_count.h"
32 #include "tbb/task_scheduler_init.h"
33 #include "../../common/utility/utility.h"
34 #include "../../common/utility/fast_random.h"
39 // convex hull problem user set parameters
40 long numberOfPoints = 5000000; // problem size
41 utility::thread_number_range threads(tbb::task_scheduler_init::default_num_threads);
43 // convex hull grain sizes for 3 subproblems. Be sure 16*GS < 512Kb
44 const size_t generateGrainSize = 25000;
45 const size_t findExtremumGrainSize = 25000;
46 const size_t divideGrainSize = 25000;
52 vector<string> OUTPUT;
54 // utility functionality
55 void ParseInputArgs(int argc, char* argv[]) {
56 utility::parse_cli_arguments(
58 utility::cli_argument_pack()
59 //"-h" option for displaying help is present implicitly
60 .positional_arg(cfg::threads,"n-of-threads",utility::thread_number_range_desc)
61 .positional_arg(cfg::numberOfPoints,"n-of-points","number of points")
62 .arg(silent,"silent","no output except elapsed time")
63 .arg(verbose,"verbose","turns verbose ON")
65 //disabling verbose if silent is specified
66 if (silent) verbose = false;;
73 //According to subparagraph 4 of paragraph 12.6.2 "Initializing bases and members" [class.base.init]
74 //of ANSI-ISO-IEC C++ 2003 standard, POD members will _not_ be initialized if they are not mentioned
75 //in the base-member initializer list.
77 //For more details why this needed please see comment in FillRNDPointsVector_buf
79 point(T _x, T _y) : x(_x), y(_y) {}
82 std::ostream& operator<< (std::ostream& o, point<double> const& p) {
83 return o << "(" << p.x << "," << p.y << ")";
87 static const size_t max_rand = USHRT_MAX;
88 utility::FastRandom my_fast_random;
89 rng (size_t seed):my_fast_random(seed) {}
90 unsigned short operator()(){return my_fast_random.get();}
91 unsigned short operator()(size_t& seed){return my_fast_random.get(seed);}
95 template < typename T ,typename rng_functor_type>
96 point<T> GenerateRNDPoint(size_t& count, rng_functor_type random, size_t rand_max) {
97 /* generates random points on 2D plane so that the cluster
98 is somewhat circle shaped */
99 const size_t maxsize=500;
100 T x = random()*2.0/(double)rand_max - 1;
101 T y = random()*2.0/(double)rand_max - 1;
106 if (random()/(double)rand_max > 0.5)
108 if (random()/(double)rand_max > 0.5)
118 x = (x+1)*0.5*maxsize;
119 y = (y+1)*0.5*maxsize;
121 return point<T>(x,y);
124 template <typename Index>
128 edge(Index _p1, Index _p2) : start(_p1), end(_p2) {};
131 template <typename T>
132 ostream& operator <<(ostream& _ostr, point<T> _p) {
133 return _ostr << '(' << _p.x << ',' << _p.y << ')';
136 template <typename T>
137 istream& operator >>(istream& _istr, point<T> _p) {
138 return _istr >> _p.x >> _p.y;
141 template <typename T>
142 bool operator ==(point<T> p1, point<T> p2) {
143 return (p1.x == p2.x && p1.y == p2.y);
146 template <typename T>
147 bool operator !=(point<T> p1, point<T> p2) {
151 template <typename T>
152 double cross_product(const point<T>& start, const point<T>& end1, const point<T>& end2) {
153 return ((end1.x-start.x)*(end2.y-start.y)-(end2.x-start.x)*(end1.y-start.y));
156 // Timing functions are based on TBB to always obtain wall-clock time
157 typedef tbb::tick_count my_time_t;
159 my_time_t gettime() {
160 return tbb::tick_count::now();
163 double time_diff(my_time_t start, my_time_t end) {
164 return (end-start).seconds();
167 void WriteResults(int nthreads, double initTime, double calcTime) {
169 cout << " Step by step hull construction:" << endl;
170 for(size_t i = 0; i < OUTPUT.size(); ++i)
171 cout << OUTPUT[i] << endl;
175 << " Number of nodes:" << cfg::numberOfPoints
176 << " Number of threads:" << nthreads
177 << " Initialization time:" << setw(10) << setprecision(3) << initTime
178 << " Calculation time:" << setw(10) << setprecision(3) << calcTime
184 #endif // __CONVEX_HULL_H__