Daniel@0: # Part of DML (Digital Music Laboratory) Daniel@0: # Copyright 2014-2015 Daniel Wolff, City University Daniel@0: Daniel@0: # This program is free software; you can redistribute it and/or Daniel@0: # modify it under the terms of the GNU General Public License Daniel@0: # as published by the Free Software Foundation; either version 2 Daniel@0: # of the License, or (at your option) any later version. Daniel@0: # Daniel@0: # This program is distributed in the hope that it will be useful, Daniel@0: # but WITHOUT ANY WARRANTY; without even the implied warranty of Daniel@0: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Daniel@0: # GNU General Public License for more details. Daniel@0: # Daniel@0: # You should have received a copy of the GNU General Public Daniel@0: # License along with this library; if not, write to the Free Software Daniel@0: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Daniel@0: Daniel@0: #!/usr/bin/python Daniel@0: # -*- coding: utf-8 -*-# Daniel@0: Daniel@0: # Daniel@0: # This is a benchmark that funs calculations Daniel@0: # on distributed nodes and reports the Daniel@0: # average time the calculation needs. Daniel@0: # Used to determine efficitncy of virtualisation Daniel@0: # Daniel@0: Daniel@0: # parameters Daniel@0: import sys Daniel@0: import time Daniel@0: import numpy Daniel@0: from IPython.parallel import Client Daniel@0: #import numpy as np Daniel@0: Daniel@0: def approxPi(error): Daniel@0: def nth_term(n): Daniel@0: return 4 / (2.0 * n + 1) * (-1) ** n Daniel@0: prev = nth_term(0) # First term Daniel@0: current = nth_term(0) + nth_term(1) # First + second terms Daniel@0: n = 2 # Starts at third term Daniel@0: Daniel@0: while abs(prev - current) > error: Daniel@0: prev = current Daniel@0: current += nth_term(n) Daniel@0: n += 1 Daniel@0: Daniel@0: return current Daniel@0: Daniel@0: Daniel@0: def main(niters = 1,hardness = 1e-7): Daniel@0: # connect to client Daniel@0: i = 0 Daniel@0: while i< 5 : Daniel@0: try : Daniel@0: i = i+1 Daniel@0: rc = Client() Daniel@0: nb_core = numpy.size(rc.ids) Daniel@0: lview = rc.load_balanced_view() Daniel@0: lview.block = True Daniel@0: dview = rc[:] Daniel@0: dview.block = True Daniel@0: break Daniel@0: except Exception : Daniel@0: print 'Client not started yet (Waiting for 5 sec...)' Daniel@0: time.sleep(5) Daniel@0: if i == 5 : Daniel@0: return 'Cannot connect to cluster' Daniel@0: time.sleep(2) Daniel@0: Daniel@0: Daniel@0: #with dview.sync_imports(): Daniel@0: # import start_benchmark Daniel@0: Daniel@0: print 'Benchmarking pi approximation on ' + str(nb_core) + ' engines.' Daniel@0: ticc = time.clock() Daniel@0: tic = time.time() Daniel@0: result = numpy.mean(lview.map(approxPi,numpy.ones(nb_core*niters) * hardness)) Daniel@0: toc = time.time()-tic Daniel@0: tocc = time.clock()-ticc Daniel@0: print 'Result: ' + str(result) Daniel@0: print 'Time used: ' + str(toc) Daniel@0: print 'CPU Time passed: ' + str(nb_core) + "*" + str(toc) Daniel@0: Daniel@0: if __name__ == "__main__": Daniel@0: if len(sys.argv) >= 3: Daniel@0: main(sys.argv[1],sys.argv[2]) Daniel@0: else: Daniel@0: main() Daniel@0: Daniel@0: