Skip to content

Instantly share code, notes, and snippets.

@MrSmith33
Last active June 6, 2018 11:28
Show Gist options
  • Save MrSmith33/f35c8168d265c9c414656d1c7dc0599f to your computer and use it in GitHub Desktop.
Save MrSmith33/f35c8168d265c9c414656d1c7dc0599f to your computer and use it in GitHub Desktop.
PBS
#!/bin/bash
num_steps=100
tasks_per_man=10
let num_managers=$num_steps/$tasks_per_man
int_from=-100
int_to=100
awk -v xmin="$int_from" -v xmax="$int_to" -v num_steps="$num_steps" 'BEGIN {dx=(xmax-xmin)/num_steps; for (i = xmin; i < xmax; i+=dx) print i" "i+dx }' > ranges.txt
g++ integral.cxx -oexe
# res - results for individual tasks
# out - errors
# host - results of `hostname` of each task
# host2 - results of managers cat of host
rm -rf res out host host2
mkdir res out host host2
for m in $(seq 1 $num_managers); do
mkdir "host/$m"
done
# calc separate pieces
pieces_job_id=$(qsub build_job.pbs -t 1-$num_steps)
# create managers for collecting hosts
mans=""
for m in $(seq 1 $num_managers); do
args=""
for t in $(seq 1 $tasks_per_man); do
let i=($m-1)*$tasks_per_man+$t
arg=":${pieces_job_id:0:-1}$i]"
args=$args$arg
done
man=$(qsub -W depend=afterok$args -v man_id=$m build_job_man.pbs)
#echo "man com qsub -W depend=afterok$args -v man_id=$m build_job_man.pbs"
#echo "man $man"
mans="$mans:$man"
done
# cat all host stats in ./stats
echo "mans $mans"
qsub -W depend=afterok$mans build_job_man2.pbs
# sum pieces
qsub -W depend=afterokarray:$pieces_job_id build_job_sum.pbs
#!/bin/bash
#PBS -l nodes=2:ppn=2
#PBS -l walltime=0:02:00
#PBS -N penechko_integral
#PBS -o res/ -e out/err
cd $PBS_O_WORKDIR
params=$(sed "${PBS_ARRAYID}q;d" ranges.txt)
./exe $params
let man_id=(${PBS_ARRAYID}-1)/10+1
hostname > host/$man_id/${PBS_ARRAYID}
#!/bin/bash
#PBS -l walltime=0:02:00
#PBS -N build_job_man
#PBS -o res/ -e out/err
cd $PBS_O_WORKDIR
cat host/$man_id/* > host2/$man_id
#!/bin/bash
#PBS -l walltime=0:02:00
#PBS -N build_job_man2
#PBS -o res/ -e out/err
cd $PBS_O_WORKDIR
cat host2/* | sort | uniq -c > stats
#!/bin/bash
#PBS -l walltime=0:02:00
#PBS -N penechko_sum_integral
#PBS -o res/ -e out/err
cd $PBS_O_WORKDIR
cat res/* | awk 'BEGIN{x=0}{x+=$0}END{print x}' > gt
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
// user defined function below
float f (float x){
return exp(cos(x));
}
//function to calculate a definite integral given bounds of integration (xmin/max) & bounds of function (ymin/ymax)
float integral (float (*f)(float), float xmin, float xmax, float ymin, float ymax)
{
int total = 0, inBox = 0;
for (int count=0; count < 1000000; ++count)
{
float u1 = (float)rand() / RAND_MAX;
float u2 = (float)rand() / RAND_MAX;
float xcoord = ((xmax - xmin)*u1) + xmin;
float ycoord = ((ymax - ymin)*u2) + ymin;
float val = f(xcoord);
++total;
if (val > ycoord) ++inBox;
}
float density = (float)inBox/total;
std::cout << (xmax - xmin) * (ymax - ymin) * density << std::endl;
}
int main(int argc, char* argv[])
{
if (argc < 3) {
if (argc > 1) std::cout << argv[1] << " :1 \n";
if (argc > 2) std::cout << argv[2] << " :2 \n";
std::cout << "USAGE: integral <xmin> <xmax>" << "\n";
return 1;
}
float xmin = atof(argv[1]);
float xmax = atof(argv[2]);
//integral(f,-2,2,0,4);
integral(f, xmin, xmax, 0, 4);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment