Commit 03073f66 authored by Christoph Gerum's avatar Christoph Gerum

Added mpi examples

parent 23e51ccd
File added
#include <mpi.h>
#include <stdio.h>
int main(int argc, char **argv){
int rank, size;
MPI_Init (&argc, &argv); /* starts MPI */
MPI_Comm_rank (MPI_COMM_WORLD, &rank); /* get current process id */
MPI_Comm_size (MPI_COMM_WORLD, &size); /* get number of processes */
printf("Hello World process %d of %d!\n", rank, size);
MPI_Finalize();
return 0;
}
File added
#include <mpi.h>
#include <stdio.h>
static long num_steps = 100000000;
double step;
int main (int argc, char **argv)
{
int i, myid, num_procs;
double x, pi, remote_sum, sum = 0;
MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD, &myid);
MPI_Comm_size(MPI_COMM_WORLD, &num_procs);
step = 1.0/(double) num_steps;
for (i=myid; i< num_steps; i=i+num_procs) {
x = (i+0.5)*step;
sum += 4.0/(1.0+x*x);
}
if (myid == 0){
for(i = 1; i < num_procs; i++){
MPI_Status status;
MPI_Recv(&remote_sum, 1, MPI_DOUBLE, i, 0, MPI_COMM_WORLD, &status);
sum += remote_sum;
}
pi = sum * step;
printf("PI is aproximately %1.12f\n", pi);
}else{
MPI_Send(&sum, 1, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD);
}
MPI_Finalize();
}
Markdown is supported
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