Skip to content
Snippets Groups Projects
Commit fdf08d82 authored by Erik Strand's avatar Erik Strand
Browse files

Write an MPI pi calculation for CPUs

parents 65929158 c15bc2a7
No related branches found
No related tags found
No related merge requests found
mpi_pi_cpu
output
mpi_pi_cpu: mpi_pi_cpu.c
mpicc $< -o $@
#!/bin/bash
module purge
module load spack git gcc/7.3.0 openmpi/3.1.4-pmi-cuda-ucx
/*
* mpipi.c
* Neil Gershenfeld 2/5/11
* Erik Strand 3/6/2021
* use MPI to evaluate pi by summation
*/
#include <stdio.h>
#include <mpi.h>
#include <sys/time.h>
// number of terms evaluated by each process
#define NPTS 250000000
void main(int argc, char** argv) {
// variables used by all ranks
int rank;
long unsigned int i, istart, iend;
double sum, pi;
// variables used only by rank 0
unsigned long int start_time, end_time;
struct timeval start, end;
int nproc;
unsigned long int total_terms;
double mflops;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if (rank == 0) {
MPI_Comm_size(MPI_COMM_WORLD, &nproc);
total_terms = nproc;
total_terms *= NPTS;
gettimeofday(&start, NULL);
}
istart = (long unsigned int)NPTS * rank + 1;
iend = istart + NPTS;
sum = 0.0;
for (i = istart; i <= iend; ++i) {
sum += 0.5 / ((i - 0.75) * (i - 0.25));
}
MPI_Reduce(&sum, &pi, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
if (rank == 0) {
gettimeofday(&end, NULL);
start_time = start.tv_sec * 1e6 + start.tv_usec;
end_time = end.tv_sec * 1e6 + end.tv_usec;
mflops = (double)nproc * NPTS * 5.0 / (end_time - start_time);
printf("processes = %d, terms per process = %ldM, total terms = %ldM\n", nproc, NPTS / 1000000, total_terms / 1000000);
printf("time = %fs, estimated GFlops = %f\n", (end_time - start_time) / 1.0e6, mflops / 1000);
printf("pi ~ %f\n", pi);
}
MPI_Finalize();
}
#!/bin/bash
#SBATCH -J mpi_pi_cpu
#SBATCH -o output/mpi_pi_cpu_%j.out
#SBATCH -e output/mpi_pi_cpu_%j.err
#SBATCH --nodes=2
#SBATCH --ntasks-per-node=40
#SBATCH --gres=gpu:0
#SBATCH --cpus-per-task=1
#SBATCH --ntasks-per-core=1
#SBATCH --threads-per-core=1
#SBATCH --mem=10G
#SBATCH --time 00:01:00
source ./load_modules.sh
srun ./mpi_pi_cpu
echo ''
echo $SLURM_NODEID
lscpu
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment