b’pi_demo_threads.tar.gz’
CC=g++
CFLAGS= -O3 -pthread -std=c++11
DEPS =
OBJ = pi_demo_threads.o
%.o: %.cpp $(DEPS)
$(CC) -c -o $@ $< $(CFLAGS)
pi_demo_threads: $(OBJ)
$(CC) -o $@ $^ $(CFLAGS)
.PHONY: clean
clean:
rm -f $(OBJ) pi_demo_threads
pi_demo_threads.cpp
pi_demo_threads.cpp/**************************************************************
* PI calculation using std::threads
*
* CMPSC 450
* MR. Seisler
*
* This requires gcc/7.3.1 (to support c++11 calls)
*************************************************************/
#include
#include
#include
//static long num_steps = 1000000000;
//double step;
#define NUMSTEPS 1000000000
#define NUMTHREADS 40
// Create a structure that will hold the execution parameters for each thread
struct MYPARAM{
int i_start;
int i_stop;
double d_step;
double d_result;
};
// Isolate the loop that we intend to execute as a thread.
void calc_part_pi(struct MYPARAM *p_params)
{
double x, d_sum = 0.0;
for (int i = p_params->i_start; i < p_params->i_stop; i++)
{
x = ((double)i+0.5) * p_params->d_step;
d_sum = d_sum + 4.0 / (1.0 + x*x);
}
p_params->d_result = d_sum;
}
int main(void)
{
double pi;
// create an array of thread containers
std::thread t[NUMTHREADS];
// create an array of thread parameters (one for each thread)
struct MYPARAM *p_params = new struct MYPARAM[NUMTHREADS];
// setup thread parameter structures
for (int i = 0; i < NUMTHREADS; i++)
{
p_params[i].i_start = i * (NUMSTEPS/NUMTHREADS);
p_params[i].i_stop = (i + 1) * (NUMSTEPS/NUMTHREADS);
p_params[i].d_step = 1.0 / (double) NUMSTEPS;
p_params[i].d_result = 0.0;
}
// fire off threads
for (int i = 0; i < NUMTHREADS; i++)
{
t[i] = std::thread(calc_part_pi, &p_params[i]);
}
// join the threads
for (int i = 0; i < NUMTHREADS; i++)
t[i].join();
// collect the results
pi = 0.0;
for (int i = 0; i < NUMTHREADS; i++)
pi += p_params[i].d_result;
pi /= (double) NUMSTEPS;
printf("PI = %0.9f\n", pi);
delete[] p_params;
return 0;
}
#!/bin/bash
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
# sampleACI-B.pbs
#
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
# Overview
#
# The PBS directives can be used in either the submission script in the
# form listed here (#PBS -X Y) or along with the submission (qsub -X Y)
# with no change.
#
# This job will be submitted on ACI-B using the command:
# qsub sampleACI-B.pbs
#
# You can check on the job using the command:
# qstat -u
#
# Note that here #PBS is a directive; a # followed by anything else is
# a comment.
#
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
# The PBS directives
#-#-# The Allocation being submitted against
#PBS -A open
# You can submit against your allocation if more time is available. See the onboarding document for information regarding how many processors you are able to request at a time for hte size of your allocation. This is similar to the -q queuename on the legacy machines. If no allocation is listed, your job will be placed on the open queue until your open limits have been reached. In order to ensure your job goes to the open queue, please use the -A open directive.
#-#-# Name of the job
#PBS -N piDemo
# This is the name given to the job. It is used for the name of the output and error files and is visibile when you use qstat to check the status of your job.
#-#-# Amount of wall time
#PBS -l walltime=00:02:00
# The time required is in HH:MM:SS format. The wall time is the amount of actual time the job runs and isn’t related to computational time (the actual time times the number of cores being used.)
#-#-# Number of processors, their nodal spread and the type of node
#PBS -l nodes=1:ppn=20
# This is the the amount of processors we ask for. Note the different ways of asking for 4 processors: Putting them all on one node can boost the performance of a job as the communication is on one node while allowing the scheduler to spread them among various processors may shorten the queue wait time. Note that if you are on one node only, you can use the pbs directive -l npcus=X.
#-#-# Memory Request
#PBS -l pmem=1gb
# We ask for 1 GB of RAM per task (pmem). Also available are mem (total memory), vmem (virtual memory) and pvmem (virtual memory per task). The mem option should only be used on single node jobs.
#-#-# Combine the stdout and stderr into one file
#PBS -j oe
export DAPL_DBG_TYPE=”0″
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
# Prepare for, compile and run the job
#-#-# Echo
echo “#-#-#Job started on `hostname` at `date` ”
echo This job runs on the following processors:
echo `cat $PBS_NODEFILE`
# We output the job start time and the processor names to the output file. This can be helpful for debugging if something goes wrong.
#-#-# Modules
module purge
module load gcc/7.3.1
# We load the modules required. Note that loading the same modules as were used when the code was compiled is required for proper execution. We include the purge, but would comment it out if the -v directive is used with qsub to pass along the environment variables. Please note that there is a default module that would be loaded for gcc but it is better to used the actual module rather than the default in case the default changes.
ulimit -s 10240
#-#-# Directory
echo “Current directory is `pwd`”
cd $PBS_O_WORKDIR
echo “Current directory is `pwd`”
# The directory you are put in to start with is your home directory. You can change to the directory directly (cd /storage/home/…) or change to the directory you submitted from using the PBS_O_WORKDIR environment variable.
#-#-# Compile
cd ~/cmps450/demos/pi_demos/F19
make
# We compile the code within the submission script here, but this is not required. You can compile with a previous job (or on ACI-I) and just run code within jobs. Be sure the modules used whdn compiling and running are the same.
#-#-# Echo
echo “#-#-#Compilation completed and execution started at `date`”
# Output the time here for possible debugging purposes.
#-#-# Run
echo “A”
time ./pi_demo_threads
#-#-# Echo
echo “#-#-#Job Ended at `date`”
# Output the time here for possible debugging purposes.
Makefile
pi_demo_threads.cpp
sampleACI-B.pbs