Parallel Plane Sweep  0.1
Shared memory multithreaded version of the plane sweep algorithm
main.cpp
Go to the documentation of this file.
1 /*
2  * The MIT License (MIT)
3  * Copyright (c) <2016> <Mark McKenney>
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6  *
7  * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
10  * */
11 
12 
13 #include <iostream>
14 #include <vector>
15 #include <string>
16 #include <sstream>
17 #include <cstdlib>
18 #include <algorithm>
19 #include "parPlaneSweep.h"
20 #include "d2hex.h"
21 #include <fstream>
22 using namespace std;
23 
30 void tokenizeString(const std::string& str,
31  std::vector<string>& tokens,
32  const string& delimiters );
33 
34 
53 int main( int argc, char * argv[] )
54 {
55 
56  // read the argument regions
57  std::string inputFileName1, inputFileName2;
58  vector< halfsegment >v1, v2, result;
59  int minStrips, maxStrips;
60  if( argc != 5 )
61  {
62  std::cerr << "usage: exe [input file name 1] [input file name 2] [min strips][max strips]" << std::endl;
63  exit( -1 );
64  }
65  {
66  std::stringstream ss1;
67  ss1 << argv[1];
68  ss1 >> inputFileName1;
69  }
70  {
71  std::stringstream ss1;
72  ss1 << argv[2];
73  ss1 >> inputFileName2;
74  }
75  {
76  std::stringstream ss1;
77  ss1 << argv[3];
78  ss1 >> minStrips;
79  }
80  {
81  std::stringstream ss1;
82  ss1 << argv[4];
83  ss1 >> maxStrips;
84  }
85 
86  ifstream inFileStrm1;
87  inFileStrm1.open( argv[1] );
88  if( ! inFileStrm1 )
89  {
90  cerr << "Error: could not open file: " << argv[1] << endl;
91  exit( -1 );
92  }
93  ifstream inFileStrm2;
94  inFileStrm2.open( argv[2] );
95  if( ! inFileStrm2 )
96  {
97  cerr << "Error: could not open file: " << argv[2] << endl;
98  exit( -1 );
99  }
100 
101  cerr << "Reading files: " << argv[1] << ", " <<argv[2] << endl;
102  vector<string> splitLine;
103  string line(" ");
104  double x, y;
105  int la, lb;
106  int i = 0;
107  while(inFileStrm1.good() )
108  {
109  getline(inFileStrm1, line);
110  if( inFileStrm1.good() )
111  {
112  if( line.size() == 0 || line[0] == '#' )
113  continue;
114  splitLine.clear();
115  string delim(" \t" );
116  tokenizeString( line, splitLine, delim );
117  v1.push_back( halfsegment() );
118  v1[i].dx = doubleHexConverter::hex2d(splitLine[0]);
119  v1[i].dy = doubleHexConverter::hex2d(splitLine[1]);
120  v1[i].sx = doubleHexConverter::hex2d(splitLine[2]);
121  v1[i].sy = doubleHexConverter::hex2d(splitLine[3]);
122  stringstream ss1( splitLine[4] );
123  ss1 >> la;
124  stringstream ss2( splitLine[5] );
125  ss2 >> lb;
126  v1[i].la = la;
127  v1[i].ola = la;
128  v1[i].lb = lb;
129  v1[i].olb = lb;
130  v1[i].regionID = 2;
131  i++;
132  v1.push_back( v1[i-1].getBrother() );
133  i++;
134  }
135  }
136  cerr <<"file 1 finished reading"<<endl;
137  i = 0;
138  while(inFileStrm2.good() )
139  {
140  getline(inFileStrm2, line);
141  if( inFileStrm2.good() )
142  {
143  if( line.size() == 0 || line[0] == '#' )
144  continue;
145  splitLine.clear();
146  string delim(" \t" );
147  tokenizeString( line, splitLine, delim );
148  v2.push_back( halfsegment() );
149  v2[i].dx = doubleHexConverter::hex2d(splitLine[0]);
150  v2[i].dy = doubleHexConverter::hex2d(splitLine[1]);
151  v2[i].sx = doubleHexConverter::hex2d(splitLine[2]);
152  v2[i].sy = doubleHexConverter::hex2d(splitLine[3]);
153  stringstream ss1( splitLine[4] );
154  ss1 >> la;
155  stringstream ss2( splitLine[5] );
156  ss2 >> lb;
157  v2[i].la = la;
158  v2[i].ola = la;
159  v2[i].lb = lb;
160  v2[i].olb = lb;
161  v2[i].regionID = 3;
162  i++;
163  v2.push_back( v2[i-1].getBrother() );
164  i++;
165  }
166  }
167  cerr <<"file 2 finished reading"<<endl;
168 
169 
170  std::sort( v1.begin(), v1.end() );
171  std::sort( v2.begin(), v2.end() );
172  if( minStrips < 1 ) {
173  minStrips = 1;
174  }
175  for( int i = minStrips; i <= maxStrips; i= (i==1)? 2: i*2 ){
176  // start the timer
177  cout << "TTT num strips: " << i << endl;
178  if( i == 1 ){
179  overlayPlaneSweep( &(v1[0]), v1.size(), &(v2[0]), v2.size(), result );
180  }
181  else {
182  parallelOverlay( v1, v2, result, i);
183  }
184  cout << "num segs: " << result.size()/2<<endl;
185 
186  }
187 
188 }
189 
190 
191 void tokenizeString(const std::string& str, std::vector<string>& tokens, const string& delimiters )
192 {
193  std::string::size_type lastPos = str.find_first_not_of(delimiters, 0); // Skip delimiters at beginning.
194  std::string::size_type pos = str.find_first_of(delimiters, lastPos); // Find first "non-delimiter".
195 
196  while (std::string::npos != pos || std::string::npos != lastPos)
197  {
198  tokens.push_back(str.substr(lastPos, pos - lastPos)); // Found a token, add it to the vector.
199  lastPos = str.find_first_not_of(delimiters, pos); // Skip delimiters. Note the "not_of"
200  pos = str.find_first_of(delimiters, lastPos); // Find next "non-delimiter"
201  }
202 }
203 
holds a single halfsegment, along with labeling information and information indicating if the opposin...
Definition: parPlaneSweep.h:76
void parallelOverlay(vector< halfsegment > &r1, vector< halfsegment > &r2, vector< halfsegment > &result, int numStrips, int numWorkerThreads)
void tokenizeString(const std::string &str, std::vector< string > &tokens, const string &delimiters)
Definition: main.cpp:212
void overlayPlaneSweep(const halfsegment r1[], int r1Size, const halfsegment r2[], int r2Size, vector< halfsegment > &result)
static double hex2d(const string &hexer)
Definition: d2hex.h:42
int main(int argc, char *argv[])
Definition: main.cpp:75