Daniel@0
|
1 # Part of DML (Digital Music Laboratory)
|
Daniel@0
|
2 # Copyright 2014-2015 Daniel Wolff, City University
|
Daniel@0
|
3
|
Daniel@0
|
4 # This program is free software; you can redistribute it and/or
|
Daniel@0
|
5 # modify it under the terms of the GNU General Public License
|
Daniel@0
|
6 # as published by the Free Software Foundation; either version 2
|
Daniel@0
|
7 # of the License, or (at your option) any later version.
|
Daniel@0
|
8 #
|
Daniel@0
|
9 # This program is distributed in the hope that it will be useful,
|
Daniel@0
|
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Daniel@0
|
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Daniel@0
|
12 # GNU General Public License for more details.
|
Daniel@0
|
13 #
|
Daniel@0
|
14 # You should have received a copy of the GNU General Public
|
Daniel@0
|
15 # License along with this library; if not, write to the Free Software
|
Daniel@0
|
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
Daniel@0
|
17
|
Daniel@0
|
18 #!/usr/bin/python
|
Daniel@0
|
19 # -*- coding: utf-8 -*-#
|
Daniel@0
|
20
|
Daniel@0
|
21 #
|
Daniel@0
|
22 # This is a benchmark that funs calculations
|
Daniel@0
|
23 # on distributed nodes and reports the
|
Daniel@0
|
24 # average time the calculation needs.
|
Daniel@0
|
25 # Used to determine efficitncy of virtualisation
|
Daniel@0
|
26 #
|
Daniel@0
|
27
|
Daniel@0
|
28 # parameters
|
Daniel@0
|
29 import sys
|
Daniel@0
|
30 import time
|
Daniel@0
|
31 import numpy
|
Daniel@0
|
32 from IPython.parallel import Client
|
Daniel@0
|
33 #import numpy as np
|
Daniel@0
|
34
|
Daniel@0
|
35 def approxPi(error):
|
Daniel@0
|
36 def nth_term(n):
|
Daniel@0
|
37 return 4 / (2.0 * n + 1) * (-1) ** n
|
Daniel@0
|
38 prev = nth_term(0) # First term
|
Daniel@0
|
39 current = nth_term(0) + nth_term(1) # First + second terms
|
Daniel@0
|
40 n = 2 # Starts at third term
|
Daniel@0
|
41
|
Daniel@0
|
42 while abs(prev - current) > error:
|
Daniel@0
|
43 prev = current
|
Daniel@0
|
44 current += nth_term(n)
|
Daniel@0
|
45 n += 1
|
Daniel@0
|
46
|
Daniel@0
|
47 return current
|
Daniel@0
|
48
|
Daniel@0
|
49
|
Daniel@0
|
50 def main(niters = 1,hardness = 1e-7):
|
Daniel@0
|
51 # connect to client
|
Daniel@0
|
52 i = 0
|
Daniel@0
|
53 while i< 5 :
|
Daniel@0
|
54 try :
|
Daniel@0
|
55 i = i+1
|
Daniel@0
|
56 rc = Client()
|
Daniel@0
|
57 nb_core = numpy.size(rc.ids)
|
Daniel@0
|
58 lview = rc.load_balanced_view()
|
Daniel@0
|
59 lview.block = True
|
Daniel@0
|
60 dview = rc[:]
|
Daniel@0
|
61 dview.block = True
|
Daniel@0
|
62 break
|
Daniel@0
|
63 except Exception :
|
Daniel@0
|
64 print 'Client not started yet (Waiting for 5 sec...)'
|
Daniel@0
|
65 time.sleep(5)
|
Daniel@0
|
66 if i == 5 :
|
Daniel@0
|
67 return 'Cannot connect to cluster'
|
Daniel@0
|
68 time.sleep(2)
|
Daniel@0
|
69
|
Daniel@0
|
70
|
Daniel@0
|
71 #with dview.sync_imports():
|
Daniel@0
|
72 # import start_benchmark
|
Daniel@0
|
73
|
Daniel@0
|
74 print 'Benchmarking pi approximation on ' + str(nb_core) + ' engines.'
|
Daniel@0
|
75 ticc = time.clock()
|
Daniel@0
|
76 tic = time.time()
|
Daniel@0
|
77 result = numpy.mean(lview.map(approxPi,numpy.ones(nb_core*niters) * hardness))
|
Daniel@0
|
78 toc = time.time()-tic
|
Daniel@0
|
79 tocc = time.clock()-ticc
|
Daniel@0
|
80 print 'Result: ' + str(result)
|
Daniel@0
|
81 print 'Time used: ' + str(toc)
|
Daniel@0
|
82 print 'CPU Time passed: ' + str(nb_core) + "*" + str(toc)
|
Daniel@0
|
83
|
Daniel@0
|
84 if __name__ == "__main__":
|
Daniel@0
|
85 if len(sys.argv) >= 3:
|
Daniel@0
|
86 main(sys.argv[1],sys.argv[2])
|
Daniel@0
|
87 else:
|
Daniel@0
|
88 main()
|
Daniel@0
|
89
|
Daniel@0
|
90
|