-
Notifications
You must be signed in to change notification settings - Fork 3
/
bMatching.cpp
259 lines (224 loc) · 7.78 KB
/
bMatching.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// ***********************************************************************
//
// Vite: A C++ library for shared-memory graph b-Matching
// using OpenMP
//
// Arif Khan ([email protected])
// Mahantesh Halappanavar ([email protected])
// Pacific Northwest National Laboratory
//
// Alex Pothen ([email protected])
// Purdue University
//
// ***********************************************************************
//
// Copyright (2017) Battelle Memorial Institute
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
// ************************************************************************
#include "bMatching.h"
#include "mtxReader.h"
#include <stdlib.h>
#include <time.h>
#include <getopt.h>
#include <float.h>
using namespace std;
bmatching_parameters::bmatching_parameters():problemname(NULL),bFileName(NULL),b(5),verbose(false),
bipartite(false)
{}
void bmatching_parameters::usage()
{
const char *params =
"\n\n"
"Usage: %s -f <problemname> -e <bfilename> -b <bval> -a <algorithm> -v -g\n\n"
" -f problemname : file containing graph. Currently inputs .cbin files\n"
" -e bfilename : Optional input. (currently not implemented)\n"
" -b bval : constant b value if b=0 then randomly generated.\n"
" -a algorithm : Algorithm 0:unsorted 1:partial sorted mode\n"
" -t : bipartite graph \n";
" -v : verbose \n\n";
fprintf(stderr, params);
}
bool bmatching_parameters::parse(int argc, char** argv)
{
static struct option long_options[]=
{
// These options don't take extra arguments
{"verbose", no_argument, NULL, 'v'},
{"help", no_argument, NULL, 'h'},
{"bipartite", no_argument, NULL, 'g'},
// These do
{"problem", required_argument, NULL, 'f'},
{"bVar", required_argument, NULL, 'e'},
{"b", required_argument, NULL, 'b'},
{"a", required_argument, NULL, 'a'},
{NULL, no_argument, NULL, 0}
};
static const char *opt_string="vhtf:e:b:a:";
int opt, longindex;
opt=getopt_long(argc,argv,opt_string,long_options,&longindex);
while(opt != -1)
{
switch(opt)
{
case 'v': verbose=true;
break;
case 't': bipartite=true;
break;
case 'h': usage();
return false;
break;
case 'f': problemname=optarg;
if(problemname==NULL)
{
cerr<<"Problem file is not speficied"<<endl;
return false;
}
break;
case 'e': bFileName=optarg;
if(bFileName==NULL)
{
cerr<<"Problem file is not speficied"<<endl;
return false;
}
break;
case 'b': b=atoi(optarg);
if(b<0)
{
cerr<<"the b value can't be negative"<<endl;
return false;
}
break;
case 'a': algorithm=atoi(optarg);
if(algorithm<0)
{
cerr<<"the a value can't be negative"<<endl;
return false;
}
break;
}
opt=getopt_long(argc,argv,opt_string,long_options,&longindex);
}
return true;
}
int main(int argc, char** argv)
{
bmatching_parameters opts;
if(!opts.parse(argc,argv))
{
cout<<"Argument Parsing Done..!!"<<endl;
return -1;
}
double t1;
int numThreads,best;
double rt_start = omp_get_wtime();
/********* Reading the INPUT ******/
CSR G;
//G.readCSRbin(opts.problemname,0);
if(opts.bipartite==false)
G.readMtxG(opts.problemname);
else
G.readMtxB(opts.problemname);
double rt_end = omp_get_wtime();
cout<<"Graph (" << G.nVer << ", " << G.nEdge/2 <<
") Reading Done....!! took " << rt_end - rt_start <<endl;
/*********** Memory Allocation *************/
int *b=new int[G.nVer];
Node* S=new Node[G.nVer]; //Heap data structure
#pragma omp parallel
numThreads=omp_get_num_threads();
/************ Assigning bValues **************/
int avgb=0;
for(int i=0;i<G.nVer;i++)
{
if(opts.b>0)
{
int card=0;
for(int j=G.verPtr[i];j<G.verPtr[i+1];j++)
if(G.verInd[j].weight>0)
card++;
if(card>opts.b)
b[i]=opts.b;
else b[i]=card;
}
else
{
//int deg=(int)sqrt(G.verPtr[i+1]-G.verPtr[i]);
int deg=0;
for(int j=G.verPtr[i];j<G.verPtr[i+1];j++)
if(G.verInd[j].weight>0)
deg++;
deg=(int)sqrt(deg);
if(deg==0)
b[i]=0;
else
{
if(deg==1)
b[i]=1;
else
b[i]=floor(deg);
//b[i]=rand()%deg+1;
}
}
avgb+=b[i];
}
avgb=avgb/G.nVer;
if(opts.b>0)
cout<<"bValue is constant for each vertex"<<endl;
else
cout<<"bValue is randomly generated, b_avg= "<<avgb<<endl;
for(int i=0;i<G.nVer;i++)
if(b[i]>0)
S[i].heap=new Info[b[i]]; //Each heap of size b
else
S[i].heap=new Info[1]; //Each heap of size b
cout << "Input Processing Done: " << omp_get_wtime() - rt_end <<endl;
bSuitor(&G,b,S,opts.algorithm,opts.verbose);
if(opts.verbose)
{
ofstream fout;
fout.open("matching.net",ios::out);
if(fout.is_open())
{
fout<<"*Vertices "<<G.nVer<<endl;
fout<<"*arcs"<<endl;
for(int i=0;i<G.nVer;i++)
for(int j=0;j<S[i].curSize;j++)
if(i>S[i].heap[j].id)
fout<<i+1<<" "<<S[i].heap[j].id+1<<" "<<S[i].heap[j].weight<<endl;
}
fout.close();
}
for(int i=0;i<G.nVer;i++)
delete S[i].heap;
delete S;
return 0;
}