""" simple dependency example, """ import batchOpenMPI #defining function def f_org(x) : return x ** 2 def g_org(x) : return x + 1 def h_org(x) : return x - 0.2 # creating batch functions f = batchOpenMPI.batchFunction(f_org) g = batchOpenMPI.batchFunction(g_org) h = batchOpenMPI.batchFunction(h_org) batchOpenMPI.begin_MPI_loop() # building processing que bi = g.addtoBatch(3,2) print(f.addtoBatch(bi)) print(h.addtoBatch(bi)) batchOpenMPI.processBatch() #get the workers to calculate all the inputs # now actuall code print ( """ f(x) = x ** 2 g(x) = x + 1
def _DE_batch(x): return DE(tfun_id=x[0], Np=int(x[1]), Cr=x[2], F=x[3], evals=x[4], randomSeed=x[5], tfun_d=prob_D, DE_x=0, DE_y=1, printLevel=0) DE_batch = batchOpenMPI.batchFunction(_DE_batch) def DE_CPV_validity_checks(CPV_array, OFE_budget): 'check tuning constraints' N, Cr, F = CPV_array if OFE_budget < N: return False, 'OFE_budget < N' if N < 5: return False, 'N < 5' if Cr < 0 or Cr > 1: return False, 'Cr not in [0,1]' if F < 0: return False, 'F < 0' return True, ""
def __init__(self, batchFun, prob_id): self.batchFun = batchFun self.prob_id = prob_id def prep_input( self, CPV_tuple, OFE_budgets, randomSeed): return [ self.prob_id ] + CPV_tuple.tolist() + [ max(OFE_budgets), randomSeed ] def __call__(self, CPV_tuple, OFE_budgets, randomSeed): F, OFEs_made = self.batchFun( self.prep_input( CPV_tuple, OFE_budgets, randomSeed)) F = F - cec_tp_mins[self.prob_id] return get_F_vals_at_specified_OFE_budgets(F, OFEs_made, OFE_budgets) def addtoBatch(self, CPV_tuple, OFE_budgets, randomSeed): self.batchFun.addtoBatch(self.prep_input( CPV_tuple, OFE_budgets, randomSeed)) def _DE_batch(x): return DE(tfun_id=x[0], Np=int(x[1]), Cr=x[2], F=x[3], evals=x[4], randomSeed=x[5], tfun_d=prob_D, DE_x=0, DE_y=1, printLevel=0) DE_batch = batchOpenMPI.batchFunction( _DE_batch ) def DE_CPV_validity_checks(CPV_array, OFE_budget): 'check tuning constraints' N, Cr, F = CPV_array if OFE_budget < N : return False, 'OFE_budget < N' if N < 5: return False, 'N < 5' if Cr < 0 or Cr > 1 : return False, 'Cr not in [0,1]' if F < 0: return False, 'F < 0' return True, "" DE_CPV_lb = numpy.array([ 5, 0.0, 0.0 ]) #lower initilization bound
""" example of how2use batchOpenMPI, with lastProcessBatch=True to avoid process being tied down unessarcyilly """ import batchOpenMPI, time def randomPause_base(pause) : time.sleep(pause) randomPause = batchOpenMPI.batchFunction(randomPause_base) batchOpenMPI.begin_MPI_loop() print('lastProcessBatch=True example/test') pauses = [1,3] for pause in pauses : randomPause.addtoBatch(pause) batchOpenMPI.processBatch(lastProcessBatch=True) for pause in pauses: randomPause(pause) batchOpenMPI.end_MPI_loop(print_stats=True) print('The first worker should have exited before the last worker, and spent less time processing')
""" example with 'calls_expected' in addtoBatch used """ import batchOpenMPI def f_mult(x) : return x*2.0 f = batchOpenMPI.batchFunction(f_mult) #creating function wrapper batchOpenMPI.begin_MPI_loop() # both the workers and the master process run the same code up until here f.addtoBatch(4,calls_expected=4) batchOpenMPI.processBatch() #get the workers to calculate all the inputs res = [f(4),f(4),f(4)] print(res) #another test f.addtoBatch(1) batchOpenMPI.processBatch() #get the workers to calculate all the inputs res = f(1), f(1) batchOpenMPI.end_MPI_loop(print_stats=True) #releases workers print("*** jobs executed by workers should be 2 ,(5 calls made),jobs uncollected should = 1, jobs_master=1")
""" example with multiRef """ import batchOpenMPI def f_mult(x) : return x*2.0 f = batchOpenMPI.batchFunction(f_mult,multiRef=True) #creating function wrapper batchOpenMPI.begin_MPI_loop(print_launch_messages=False) # both the workers and the master process run the same code up until here no = range(10) + range(10) # creates [0,1,2,3,4,5,6,7,8,9] x 2 for i in no :# adding all f_inv input and queing them for parallel processing f.addtoBatch(i) batchOpenMPI.processBatch() #get the workers to calculate all the inputs res = [] #used for storing results for i in no : res.append(f(i)) print(res) batchOpenMPI.end_MPI_loop(print_stats=True) #releases workers print("*** jobs executed by workers should be %i, out of the total of %i" % (len(no)/2,len(no)) )
""" example of how2use batchOpenMPI, simple example """ import batchOpenMPI def f_inv_org(x) : "function returns the inverse of a number" return 1.0/x f_inv = batchOpenMPI.batchFunction(f_inv_org, permissible_exceptions = [ZeroDivisionError]) #creating function wrapper batchOpenMPI.begin_MPI_loop() # both the workers and the master process run the same code up until here print('this example script prints in the inverse of the integers from 0 to 9') no = range(10) # creates [0,1,2,3,4,5,6,7,8,9] for i in no :# adding all f_inv input and queing them for parallel processing f_inv.addtoBatch(i) batchOpenMPI.processBatch() #get the workers to calculate all the inputs res = [] #used for storing results for i in no : try : res.append(f_inv(i)) except ZeroDivisionError : print("couldn't inverse " + str(i) + " due to 0 division error") print(res) batchOpenMPI.end_MPI_loop(print_stats=True) #release workers, and print stats
import batchOpenMPI, os def ex_prog(x) : "simulates an external program that writes its output to file" f = file('results.txt','w') f.write(str(x ** 2)) f.close() def fun_org(x) : ex_prog(x) f = file('results.txt','r') res = float(f.readline().strip()) f.close() return res fun = batchOpenMPI.batchFunction(fun_org) #creating function wrapper batchOpenMPI.WorkingDir_base = 'workspace' #giving each worker a directory to workin. batchOpenMPI.begin_MPI_loop() #split workers and master no = range(10) #creating inputs print("""f(x) = x ** 2 the results will be written to file, and as the result file name will be the same. each process will be given its own workspace using batchOpenMPI.WorkingDir_base""") print("\ninputs :" + str(no)) for i in no :# adding all f_inv input and queing them for parallel processing fun.addtoBatch(i)
""" simple dependency example, where an input depency raises an error """ import batchOpenMPI #defining function def f_org(x) : return x ** 2 def g_org(x) : return 1.0/x + 1 def h_org(x) : return x - 0.2 # creating batch functions f = batchOpenMPI.batchFunction(f_org) g = batchOpenMPI.batchFunction(g_org,True,permissible_exceptions=[ZeroDivisionError]) h = batchOpenMPI.batchFunction(h_org) batchOpenMPI.begin_MPI_loop() # building processing que print(f.addtoBatch(g.addtoBatch(3))) print(h.addtoBatch(g.addtoBatch(3))) print(f.addtoBatch(g.addtoBatch(0))) batchOpenMPI.processBatch() #get the workers to calculate all the inputs # now actuall code try : res3 = str(f(g(0))) except ZeroDivisionError,msg : res3 = 'no solution'
""" batchOpenMPI.drop_previous_batch_results example """ import batchOpenMPI #defining function def f1_org(x) : return 4.0 / (x-1) / (x-3) def f2_org(x) : return x + 1 # creating batch functions f1 = batchOpenMPI.batchFunction(f1_org, permissible_exceptions = [ZeroDivisionError] ) f2 = batchOpenMPI.batchFunction(f2_org) batchOpenMPI.begin_MPI_loop() batchOpenMPI.drop_previous_batch_results = True # building processing que inputs = range(10) print(""" y(x) = f1(x) + f2(x) where f1(x) = 4 / (x-1) / (x-3) f2(x) = x + 1"") inputs: """ + str(inputs)) print(""" as f1 will fail at 1 and 3. the results from f2 for these inputs will be uncollected.