From ee2192344db7a81eb786770f2b93007d11aa211a Mon Sep 17 00:00:00 2001 From: Ruben <ruben.cabezon@unibas.ch> Date: Wed, 13 May 2020 08:31:41 +0200 Subject: [PATCH] Addded integration --- integration/integration.cpp | 42 ++++++++++++++++++++++++++++++++++++ integration/integration.f90 | 43 +++++++++++++++++++++++++++++++++++++ integration/integration.py | 31 ++++++++++++++++++++++++++ 3 files changed, 116 insertions(+) create mode 100644 integration/integration.cpp create mode 100644 integration/integration.f90 create mode 100644 integration/integration.py diff --git a/integration/integration.cpp b/integration/integration.cpp new file mode 100644 index 0000000..0fd2da0 --- /dev/null +++ b/integration/integration.cpp @@ -0,0 +1,42 @@ +#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 diff --git a/integration/integration.f90 b/integration/integration.f90 new file mode 100644 index 0000000..4736eee --- /dev/null +++ b/integration/integration.f90 @@ -0,0 +1,43 @@ +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 diff --git a/integration/integration.py b/integration/integration.py new file mode 100644 index 0000000..9a0b89f --- /dev/null +++ b/integration/integration.py @@ -0,0 +1,31 @@ +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 -- GitLab