Commit 4bd846e1 authored by Ciarán Ó Rourke's avatar Ciarán Ó Rourke
Browse files

Merge branch '6-mpi-non-blocking-and-collective-demos' into 'master'

Resolve "MPI: non-blocking and collective demos"

Closes #6

See merge request training/sohpc-training-2021!6
parents bf302f59 6ef7b571
#!/bin/bash
#SBATCH --partition=CourseDevQ
#SBATCH --reservation=SOHPCCourse
#SBATCH --nodes=1
#SBATCH --time=00:10:00
#SBATCH --account=course
module load gcc openmpi/gcc/4.0.5
mpicc $1 -o prog
cd $SLURM_SUBMIT_DIR
# Run MPI code
mpirun -n 2 ./prog
module purge
rm ./prog
...@@ -52,6 +52,12 @@ void iterate(double *u, double *u_prev, const int nx, const int ny, ...@@ -52,6 +52,12 @@ void iterate(double *u, double *u_prev, const int nx, const int ny,
req_prev = req; req_prev = req;
req = temp_req; req = temp_req;
} }
/* free the MPI_Request objects */
for (int i = 0; i < 4; i++) {
MPI_Request_free(&req[i]);
MPI_Request_free(&req_prev[i]);
}
} }
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
......
#!/bin/bash
#SBATCH --partition=CourseDevQ
#SBATCH --reservation=SOHPCCourse
#SBATCH --nodes=1
#SBATCH --time=00:10:00
#SBATCH --account=course
module load gcc openmpi/gcc/4.0.5
mpicc $1 -o prog
cd $SLURM_SUBMIT_DIR
# Run MPI code
mpirun -n 40 ./prog
module purge
rm ./prog
#include <stdio.h>
#include <mpi.h>
int main(int argc, char *argv[]) {
MPI_Init(&argc, &argv);
int rank, size;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
int ranks[size];
double before_time = MPI_Wtime();
/* gather ranks at root */
MPI_Gather(&rank, 1, MPI_INT, ranks, 1, MPI_INT, 0, MPI_COMM_WORLD);
/* print ranks */
if (rank == 0) {
printf("Transfer took %lfs\n", MPI_Wtime() - before_time);
printf("The MPI_COMM_WORLD ranks are:\n");
for (int i = 0; i < size; i++) {
printf("%d ", ranks[i]);
}
printf("\n");
}
MPI_Finalize();
return 0;
}
#include <stdio.h>
#include <mpi.h>
int main(int argc, char *argv[]) {
MPI_Init(&argc, &argv);
int rank, size;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
int rank_sum = 0;
double before_time = MPI_Wtime();
/* collect ranks at root and add to running sum */
MPI_Reduce(&rank, &rank_sum, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
/* print ranks */
if (rank == 0) {
printf("Sum took %lfs\n", MPI_Wtime() - before_time);
printf("The sum of MPI_COMM_WORLD ranks is %d\n", rank_sum);
}
MPI_Finalize();
return 0;
}
#include <stdio.h>
#include <mpi.h>
int main(int argc, char *argv[]) {
MPI_Init(&argc, &argv);
int rank, size;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
int ranks[size];
double before_time = MPI_Wtime();
/* gather ranks at root */
if (rank > 0) {
MPI_Send(&rank, 1, MPI_INT, 0, rank, MPI_COMM_WORLD);
} else {
ranks[0] = rank;
for (int i = 1; i < size; i++) {
MPI_Recv(&ranks[i], 1, MPI_INT, i, i, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
}
}
/* print ranks */
if (rank == 0) {
printf("Transfer took %lfs\n", MPI_Wtime() - before_time);
printf("The MPI_COMM_WORLD ranks are:\n");
for (int i = 0; i < size; i++) {
printf("%d ", ranks[i]);
}
printf("\n");
}
MPI_Finalize();
return 0;
}
#include <stdio.h>
#include <mpi.h>
int main(int argc, char *argv[]) {
MPI_Init(&argc, &argv);
int rank, size;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
int rank_sum = 0;
double before_time = MPI_Wtime();
/* collect ranks at root and add to running sum */
if (rank > 0) {
MPI_Send(&rank, 1, MPI_INT, 0, rank, MPI_COMM_WORLD);
} else {
int recv_rank;
for (int i = 1; i < size; i++) {
MPI_Recv(&recv_rank, 1, MPI_INT, i, i, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
rank_sum += recv_rank;
}
}
/* print ranks */
if (rank == 0) {
printf("Sum took %lfs\n", MPI_Wtime() - before_time);
printf("The sum of MPI_COMM_WORLD ranks is %d\n", rank_sum);
}
MPI_Finalize();
return 0;
}
Supports Markdown
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