Skip to content
Snippets Groups Projects
Commit ee219234 authored by Ruben's avatar Ruben
Browse files

Addded integration

parent 34f29d5d
No related branches found
No related tags found
No related merge requests found
#include <cstdio>
#include <mpi.h>
int main()
{
MPI_Comm comm;
int size, rank;
MPI_Init(NULL, NULL); comm = MPI_COMM_WORLD;
MPI_Comm_rank(comm, &rank);
MPI_Comm_size(comm, &size);
const int totalsteps = 10000;
const int steps = totalsteps/size;
const double dx = 1./totalsteps;
int ini = rank * steps;
int fin = (rank+1) * steps;
if(fin >= totalsteps)
fin = totalsteps;
double localSum = 0.0;
for(int i=ini; i<fin; i++)
{
double x = (i-0.5) * dx;
localSum += 4.0 / (1.0 + x*x);
}
localSum = localSum * dx;
double globalSum = 0.0;
MPI_Allreduce(&localSum,&globalSum,1,MPI_DOUBLE_PRECISION,MPI_SUM,comm);
printf("%f %f\n", localSum, globalSum);
MPI_Barrier(comm);
MPI_Finalize();
return 0;
}
\ No newline at end of file
program integration
implicit none
include 'mpif.h'
integer ierr, comm, rank, size
integer i, totalsteps, steps, ini, fin
double precision dx, x, localSum, globalSum
call MPI_INIT(ierr); comm = MPI_COMM_WORLD
call MPI_COMM_RANK(comm, rank, ierr)
call MPI_COMM_SIZE(comm, size, ierr)
totalsteps = 1000000
steps = totalsteps/size
ini = rank * steps
fin = (rank+1) * steps
if(fin.ge.totalsteps) then
fin = totalsteps
endif
write(*,*) rank, ini, fin
dx = 1.d0 / totalsteps
x = -0.5d0 * dx
localSum = 0.d0
do i = ini, fin
x = (i-0.5d0)*dx
localSum = localSum + 4.d0 / (1.d0 + x*x)
enddo
localSum = localSum * dx
call MPI_ALLREDUCE(localSum,globalSum,1,MPI_DOUBLE_PRECISION,MPI_SUM,comm,ierr)
write(*,*) localSum, globalSum
call MPI_BARRIER(comm,ierr)
call MPI_FINALIZE(ierr)
end program integration
\ No newline at end of file
from mpi4py import MPI
import numpy
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
size = comm.Get_size()
totalsteps = 1000000
steps = totalsteps/size
ini = rank * steps
fin = (rank+1) * steps
if fin > totalsteps:
fin = totalsteps
print(rank, ini, fin)
dx = 1./totalsteps
x = -0.5*dx
localSum = 0.
for i in range(int(ini), int(fin)):
x = (i-0.5)*dx
localSum = localSum + 4./(1. + x*x)
localSum = localSum * dx
globalSum = comm.allreduce(localSum, op=MPI.SUM)
print(localSum, globalSum)
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment