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

Add MPI pi calculation (CPU)

parent 65929158
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 100000
void main(int argc, char** argv) {
// variables used by all ranks
int rank, 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;
double mflops;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if (rank == 0) {
MPI_Comm_size(MPI_COMM_WORLD, &nproc);
gettimeofday(&start, NULL);
}
istart = NPTS * rank;
iend = NPTS * (rank + 1);
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 = %ld, total terms = %ld\n", nproc, NPTS, nproc * NPTS);
printf("time = %f, estimated MFlops = %f\n", (end_time - start_time) / 1.0e6, mflops);
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=8
#SBATCH --gres=gpu:0
#SBATCH --cpus-per-task=1
#SBATCH --ntasks-per-core=1
#SBATCH --threads-per-core=1
#SBATCH --mem=1G
#SBATCH --time 00:05:00
source ./load_modules.sh
srun ./mpi_pi_cpu
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment