comparison ipcluster/benchmark/start_benchmark.py @ 0:e34cf1b6fe09 tip

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