set verbose off function void write_GP_code (const string usercode) outfile "GP_script.inp" --quiet --write print "/* Script created by gretl_parallel */" print "# standard setup code" print "scalar i = $mpirank + 1" printf "string outname = sprintf(\"GP_output_%%d.txt\", i)\n" print "outfile @outname --write" printf "string bunfile = sprintf(\"GP_bundle_%%d.xml\", i)\n" print "bundle mybundle = bread(bunfile)" print "scalar mybundle.stillworking = 1" print "bwrite(mybundle, bunfile)" print "# code supplied by the user" print usercode print "# standard closing code" printf "printf \"Calc with arg set %%d complete\\n\", i\n" print "mybundle.stillworking = 0" print "bwrite(mybundle, bunfile)" print "outfile --close" outfile --close end function function void display_GP_output (int N, bool debugging) print "gretl_parallel: MPI finished" loop i=1..N -q printf "output for arg set %d:\n", i string fname = sprintf("GP_output_%d.txt", i) printf "%s", readfile(fname) if !debugging remove(fname) endif endloop end function function bundles create_GP_array (int N, bool debugging) print "gretl_parallel: assembling bundles for return" bundles B = array(N) loop i=1..N -q string fname = sprintf("GP_bundle_%d.xml", i) B[i] = bread(fname) if !debugging remove(fname) endif endloop return B end function function bundles gretl_parallel (const string usercode, bundles bargs, bool debugging[0]) N = nelem(bargs) if !$sysinfo.mpi funcerr "No MPI support present, aborting" elif N > $sysinfo.nproc funcerr "Too many arg bundles, aborting" endif # write out bundles containing the argument sets loop i=1..N -q bwrite(bargs[i], sprintf("GP_bundle_%d.xml", i)) endloop # write out script for exec via MPI write_GP_code(usercode) print "Finished writing bundles and MPI script" print "Invoking MPI (this could take a while)" flush mpi run GP_script.inp end mpi --np=N # present the output from all workers display_GP_output(N, debugging) # assemble the return array bundles bbout = create_GP_array(N, debugging) if !debugging remove("GP_script.inp") endif return bbout end function ### sample caller script ### # prepare some code with a bundle parameter string code = "" code += sprintf("matrix m = {3}\n") code += sprintf("scalar num = mybundle.num\n") code += sprintf("m += num\n") N = 2 bundles bs = array(N) bundle b1 = null b1.num = 1 bundle b2 = null b2.num = 2 bs[1] = b1 bs[2] = b2 bundles bbresult = gretl_parallel(code, bs, 1) print "main script: eval the bundles we got" loop i=1..N -q eval bbresult[i] endloop