-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7fc5ad5
commit b7d6e27
Showing
1 changed file
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# # Trapezoidal Integration | ||
# | ||
|
||
# Function to be integrated (from 0 to 1). The analytic result is π. | ||
|
||
f(x) = 4 * √(1 - x^2) | ||
|
||
# Integration routine | ||
|
||
"Evaluate definite integral (from `a` to `b`) by using the trapezoidal rule." | ||
function trapezoidal(a, b, n, h) | ||
y = (f(a) + f(b)) / 2.0 | ||
for i in 1:n-1 | ||
x = a + i * h | ||
y = y + f(x) | ||
end | ||
return y * h | ||
end | ||
|
||
using Base.Threads: nthreads | ||
|
||
N = 10_000 | ||
|
||
function integrate_parallel(N) | ||
# compute local integration interval etc. | ||
h = 1.0 / N | ||
a_loc = [i * n_loc * h for i in 0:nthreads()-1] | ||
b_loc = a_loc + n_loc * h | ||
|
||
result = tmapreduce(+, ...) do | ||
trapezoidal(a_loc, b_loc, n, h) | ||
end | ||
|
||
res_loc = trapezoidal(a_loc, b_loc, n_loc, h) | ||
|
||
println("π (numerical integration) ≈ ", res) | ||
end | ||
|
||
|
||
# perform local integration | ||
res_loc = trapezoidal(a_loc, b_loc, n_loc, h) | ||
|
||
# parallel reduction | ||
res = MPI.Reduce(res_loc, +, comm) | ||
|
||
# print result | ||
if 0 == rank | ||
@printf("π (numerical integration) ≈ %20.16f\\n", res) | ||
end |