Skip to content
Snippets Groups Projects
Commit a1ba171c authored by Jason R Wilson's avatar Jason R Wilson
Browse files

files for lecture 21

parent 195fd256
Branches
No related merge requests found
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mpi.h>
int main(int argc, char *argv[]) {
MPI_Init (&argc, &argv);
// MPI_COMM_WORLD is the default communicator that contains all ranks
int rank, size;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
char node_name[MPI_MAX_PROCESSOR_NAME];
int node_name_len;
MPI_Get_processor_name(node_name,&node_name_len);
// make sure we are not running on a login node!
if ((strcmp(node_name,"tinkercliffs1") == 0) ||
(strcmp(node_name,"tinkercliffs2") == 0)) {
printf ("error : running on login node %s!\n",node_name);
return 1;
}
// get the number of rounds from command line
if (argc < 3) {
printf ("Command usage : %s %s\n",argv[0],"rounds","seed");
return 1;
}
int rounds = atoi(argv[1]);
int seed = atoi(argv[2]);
srandom(seed+rank);
// start the timer
double start_time, end_time;
start_time = MPI_Wtime();
// random sum
int total_sum = 0;
for (int round = 0;round < rounds;round++) {
int round_sum = random() % 5;
int number = round_sum;
MPI_Reduce(&number,&round_sum,1,MPI_INT,MPI_SUM,0,MPI_COMM_WORLD);
total_sum += round_sum;
}
// stop the timer
end_time = MPI_Wtime();
// print results
if (rank == 0) {
printf ("elapsed time = %.4f seconds\n",end_time-start_time);
printf ("rounds = %d, seed = %d, sum = %d\n",rounds,seed,total_sum);
}
MPI_Finalize();
}
#!/bin/bash
#SBATCH -A cmda3634_rjh
#SBATCH -p normal_q
#SBATCH -t 5
#SBATCH --nodes=64
#SBATCH --ntasks-per-node=1
#SBATCH -o mpi_reduce.out
# Go to the directory where the job was submitted
cd $SLURM_SUBMIT_DIR
# Load the modules we need for MPI
module load matplotlib
# Build the executable
mpicc -o mpi_reduce mpi_reduce.c
# Run mpi_reduce
mpiexec -n $SLURM_NTASKS --map-by ppr:$SLURM_NTASKS_PER_NODE:node ./mpi_reduce $1 $2
[jasonwil@tinkercliffs2 L21]$ sbatch mpi_reduce.sh 500000 1234
[jasonwil@tinkercliffs2 L21]$ squeue -u jasonwil
JOBID PARTITION NAME USER ST TIME NODES NODELIST(REASON)
2300544 normal_q mpi_redu jasonwil R 0:02 64 tc[003-004,006,009-010,012-013,015-016,018,020,022,024-027,032,035,037,046,049-053,056,059,061,066-067,070-071,075,079-087,089-090,095,099-101,104,133-134,179-181,196-201,239-240,253-254]
[jasonwil@tinkercliffs2 L21]$ cat mpi_reduce.out
elapsed time = 1.1595 seconds
rounds = 500000, seed = 1234, sum = 64020814
\ No newline at end of file
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment