-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
119 lines (109 loc) · 3.04 KB
/
main.py
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
'''
Created on 10 Sep 2014
@author: gustavo
'''
from reduction import Reduction
from polynomial import Polynomial
from threadc import ThreadCount
import os
import threading
import math
import sys, getopt
def recoverfile(saved, readed):
if not os.path.exists(saved):
return True, []
f = open(saved,'r')
if(not os.stat(saved).st_size==0):
pols = []
pols_done = []
for line in readed:
pol = Polynomial(line)
pols.append(pol)
for line in f:
line = line.replace("[","")
line = line.replace("]","")
spl = line.split(',')
p = ""
for i in xrange(0,len(spl)-1):
p = p + " + x^" + str(spl[i].replace(" ",""))
p = p + " + 1"
p = p.replace("+","",1)
#print p
pol_ = Polynomial(p)
pols_done.append(pol_)
pols_set = set(pols)
pols_set_done = set(pols_done)
result = pols_set - pols_set_done
return False, list(result)
else:
return True, []
def main(argv):
inputfile = ''
outputfile = ''
n_threads = 4
try:
opts, args = getopt.getopt(argv,"hi:o:t:",["ifile=","ofile=", "threads="])
except getopt.GetoptError:
print 'main.py -i <inputfile> -o <outputfile> -t <numberofthreads>'
sys.exit(2)
for opt, arg in opts:
print opt
if opt == '-h':
print 'main.py -i <inputfile> -o <outputfile> -t <numberofthreads>'
sys.exit()
elif opt in ("-i", "--ifile"):
inputfile = arg
elif opt in ("-o", "--ofile"):
outputfile = arg
elif opt in ("-t", "--threads"):
n_threads = int(arg)
try:
fi = open(inputfile,"r")
except IOError:
print 'Error to open the file'
print 'main.py -i <inputfile> -o <outputfile> -t <numberofthreads>'
sys.exit(2)
fl = open(outputfile,"a")
print 'Input file is "', inputfile
print 'Output file is "', outputfile
print "number of threds " + str(n_threads)
lock = threading.Lock()
lockScreen = threading.Lock()
files = [inputfile]
for fileName in files:
save = outputfile
f = open(fileName,'r')
read, pols = recoverfile(save, f)
if read:
for line in f:
try:
pol = Polynomial(line)
pols.append(pol)
except Exception as e:
print line
sys.exit(2)
print len(pols)
threads = []
si = int(math.ceil(float(len(pols))/float(n_threads)))
print "si + " + str(si)
if si == 0:
si = 1
i = 0
j = si
print "starting...."
for temp in range(0, n_threads):
if (j > len(pols)):
j = len(pols)
print "i = " + str(i)
print "j = " + str(j)
thread = ThreadCount(temp,lockScreen, lock, pols[i:j], save)
i = j
j += si
threads.append(thread)
for thread in threads:
thread.start()
for current in threads:
current.join()
print "Finished."
if __name__ == '__main__':
main(sys.argv[1:])