aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorWojtek Kosior <kwojtus@protonmail.com>2019-06-21 14:10:35 +0200
committerWojtek Kosior <kwojtus@protonmail.com>2019-06-21 14:10:35 +0200
commit6a6f16735de786c41a73f668385ef049accf9e41 (patch)
tree095e24c90f49e43d15ec9a9f9006aeed367f8947 /src
parent00e4d345ac3a40e7d1d18df7cf751833cd608612 (diff)
downloadfortran-assignment3-6a6f16735de786c41a73f668385ef049accf9e41.tar.gz
fortran-assignment3-6a6f16735de786c41a73f668385ef049accf9e41.zip
parallelize newton-cotes (untestedgit add quadratures.f90)
Diffstat (limited to 'src')
-rw-r--r--src/quadratures.f9038
1 files changed, 31 insertions, 7 deletions
diff --git a/src/quadratures.f90 b/src/quadratures.f90
index 9be7517..2eac963 100644
--- a/src/quadratures.f90
+++ b/src/quadratures.f90
@@ -47,12 +47,12 @@ CONTAINS
integer(kind=4), intent(in) :: p
real(kind=8) :: val, subinterval_width, qbeg, qend
-
+ real(kind=8), allocatable :: partval[:]
+
procedure(quadrature), pointer :: quad
- integer(kind=8) :: i
+ integer(kind=8) :: min_i, max_i, i, subintervals_per_thread
+ integer(kind=4) :: im
- subinterval_width = (iend - ibeg) / subintervals
-
SELECT CASE (p)
CASE (:-1)
STOP "negative interpolationg polynomial order passed"
@@ -66,14 +66,38 @@ CONTAINS
STOP "Newton-Cotes quadratures only implemented for order < 3"
END SELECT
+ if (this_image() == 1) allocate(partval[*])
+
+ subintervals_per_thread = &
+ (subintervals + num_images() - 1) / num_images()
+
+ min_i = subintervals_per_thread * (this_image() - 1) + 1
+ max_i = min(subintervals, subintervals_per_thread * this_image())
+
+ subinterval_width = (iend - ibeg) / subintervals
+
! compute integral using quadrature pointed by quad
- val = 0
+ partval = 0
- DO i = 1, subintervals
+ DO i = min_i, max_i
qend = ibeg + i * subinterval_width
qbeg = ibeg + (i - 1) * subinterval_width
- val = val + quad(qbeg, qend, fun)
+ partval = partval + quad(qbeg, qend, fun)
END DO
+
+ IF (this_image() == 1 .and. num_images() > 1) THEN
+ sync images([(im, im = 2, num_images())])
+ partval = partval + sum([(partval[im], im = 2, num_images())])
+ sync images([(im, im = 2, num_images())])
+ END IF
+
+ IF (this_image() /= 1) THEN
+ sync images(1)
+ sync images(1)
+ END IF
+
+ val = partval[1]
+
END FUNCTION newton_cotes
FUNCTION rectangle(qbeg, qend, fun) result(val)