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

commit
author Daniel Wolff
date Sat, 20 Feb 2016 18:14:24 +0100
parents
children
line wrap: on
line source
# Part of DML (Digital Music Laboratory)
# Copyright 2014-2015 Daniel Wolff, City University
 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

#!/usr/bin/python
# -*- coding: utf-8 -*-#

#
# This is a benchmark that funs calculations
# on distributed nodes and reports the
# average time the calculation needs.
# Used to determine efficitncy of virtualisation 
#

# parameters
import sys
import time
import numpy 
from IPython.parallel import Client
#import numpy as np

def approxPi(error):
    def nth_term(n):
        return 4 / (2.0 * n + 1) * (-1) ** n
    prev = nth_term(0)  # First term
    current = nth_term(0) + nth_term(1)  # First + second terms
    n = 2  # Starts at third term

    while abs(prev - current) > error:
        prev = current
        current += nth_term(n)
        n += 1

    return current

        
def main(niters = 1,hardness = 1e-7):
    # connect to client
    i = 0
    while i< 5 :
        try :
            i = i+1
            rc = Client()
            nb_core = numpy.size(rc.ids)
            lview = rc.load_balanced_view()
            lview.block = True
            dview = rc[:]
            dview.block = True
            break
        except Exception :
            print 'Client not started yet (Waiting for 5 sec...)'
            time.sleep(5)
    if i == 5 :
        return 'Cannot connect to cluster'
    time.sleep(2)


    #with dview.sync_imports():
    #    import start_benchmark

    print 'Benchmarking pi approximation on ' + str(nb_core) + ' engines.'
    ticc = time.clock()
    tic = time.time()
    result = numpy.mean(lview.map(approxPi,numpy.ones(nb_core*niters) * hardness))
    toc = time.time()-tic
    tocc = time.clock()-ticc
    print 'Result: ' + str(result)
    print 'Time used: ' + str(toc)
    print 'CPU Time passed: ' + str(nb_core) + "*" + str(toc)
    
if __name__ == "__main__":
    if len(sys.argv) >= 3:
        main(sys.argv[1],sys.argv[2])
    else:
        main()