This repository was archived by the owner on May 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgpufanpy.py
More file actions
89 lines (65 loc) · 2.35 KB
/
Copy pathgpufanpy.py
File metadata and controls
89 lines (65 loc) · 2.35 KB
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
from __future__ import print_function
import os
import subprocess
class Gpufan:
PATH = "/opt/gpufanshared/"
GPUFAN_BIN = "/opt/gpufanshared/bin/centrality-gpu"
def __init__(self, cmtype, elistFrom, nodes, elistTo=None, directed=False):
""" Wrapper for gpufan-1.0
:cmtype: b (betweenness), c (closeness), e (eccentric), s (stress)
:nodes: number of vertices
:elistFrom: either and edgelist or the first part (the from entries) of
en edgelist, in the latter case you have to provide the second part
through elistTo
:directed: set to True if graph is directed
"""
self.cmtype = cmtype
self.directed = directed
self.nodes = nodes
if elistTo:
self.edgelist = zip(elistFrom, elistTo)
else:
self.edgelist = elistFrom
self.TMPGRAPH = Gpufan.PATH + "tmpGpufan.edgelist"
self.TMPRESULT = Gpufan.PATH + "tmpGpufan.output"
def writeEdgelist(self):
with open(self.TMPGRAPH, 'w') as outfile:
for item in self.edgelist:
string = "%i\t%i\n" % item
outfile.write(string)
def runGPU(self):
if self.directed:
cmd = [Gpufan.GPUFAN_BIN, "-" + self.cmtype, '-g', self.TMPGRAPH, "-d", "-o", self.TMPRESULT]
else:
cmd = [Gpufan.GPUFAN_BIN, "-" + self.cmtype, '-g', self.TMPGRAPH, "-o", self.TMPRESULT]
exitCode = subprocess.call(cmd)
print(exitCode)
self.result = self.readResults()
def readResults(self):
res = [0.0] * self.nodes
with open(self.TMPRESULT, 'r') as result:
result.readline()
while(True):
entry = result.readline().split()
if entry[0].lower() == "time:":
break
idx = int(entry[0])
value = float(entry[1])
res[idx] = value
print(" ".join(entry))
return res
def cleanUp(self):
os.remove(self.TMPGRAPH)
os.remove(self.TMPRESULT)
def runAll(self):
self.writeEdgelist()
self.runGPU()
self.cleanUp()
return self.result
def main():
eL = [4,2,4,4,5]
eR = [5,8,8,9,9]
fan = Gpufan("c", elistFrom=eL, elistTo=eR, nodes=10)
print( fan.runAll())
if __name__ == '__main__':
main()