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 h(x) = x - 0.2 f(g(3)) = %f h(g(3)) = %f
""" 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")
#! /usr/bin/env python import numpy, tuning_setups, batchOpenMPI, plotlib from optTune import tMOPSO from matplotlib import pyplot batchOpenMPI.begin_MPI_loop() #adjust which CEC problem to tune under, etcetera in tuning_setups.py runAlg = tuning_setups.batchOpenMPI_wrapper(tuning_setups.PSO_batch, tuning_setups.prob_ID) tuningOpt = tMOPSO( optAlg=runAlg, CPV_lb=tuning_setups.PSO_CPV_lb, CPV_ub=tuning_setups.PSO_CPV_ub, CPV_validity_checks=tuning_setups.PSO_CPV_validity_checks, OFE_budgets=tuning_setups.PSO_OFE_budgets, sampleSizes=tuning_setups.PSO_sampleSizes, #resampling size of 25 resampling_interruption_confidence=tuning_setups.PSO_alpha, gammaBudget=tuning_setups.PSO_gammaBudget, #tuning budget addtoBatch=runAlg.addtoBatch, processBatch=batchOpenMPI.processBatch) batchOpenMPI.end_MPI_loop(print_stats=True) #release workers, and print stats print(tuningOpt) OFE_budgets = [d.fv[0] for d in tuningOpt.PFA.designs] Fmin_values = [d.fv[1] for d in tuningOpt.PFA.designs] log_OFE_budgets = [d.xv[0] for d in tuningOpt.PFA.designs] N_values = [int(d.xv[1]) for d in tuningOpt.PFA.designs]
""" 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)) )
"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) batchOpenMPI.processBatch() #get the workers to calculate all the inputs res = [] #used for storing results for i in no :
import batchOpenMPI batchOpenMPI.begin_MPI_loop(print_launch_messages=False) print('First call should fail:') batchOpenMPI.showStats() print('Second call after end_MPI_loop should work') batchOpenMPI.end_MPI_loop(print_stats=True)