Skip to content

Commit b018a8e

Browse files
committed
PWR083: Add benchmark
1 parent b63bc81 commit b018a8e

File tree

5 files changed

+94
-0
lines changed

5 files changed

+94
-0
lines changed

Benchmark/src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ endif()
2121
add_benchmark(PWR079)
2222
add_benchmark(PWR080)
2323
add_benchmark(PWR081)
24+
add_benchmark(PWR083)

Benchmark/src/PWR083.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include "Benchmark.h"
2+
3+
// Forward-declare the functions to benchmark
4+
extern "C" {
5+
void update_simulation_state_by_fixed_amount(const int *n, double *state);
6+
void update_simulation_state_by_fixed_amount_improved(const int *n,
7+
double *state);
8+
}
9+
10+
constexpr int N = 1024 * 1024;
11+
12+
#if OCB_ENABLE_Fortran
13+
14+
static void FortranExampleBench(benchmark::State &state) {
15+
auto array = OpenCatalog::CreateRandomVector<double>(N);
16+
17+
for (auto _ : state) {
18+
update_simulation_state_by_fixed_amount(&N, array.data());
19+
benchmark::DoNotOptimize(array);
20+
}
21+
}
22+
23+
static void FortranImprovedBench(benchmark::State &state) {
24+
auto array = OpenCatalog::CreateRandomVector<double>(N);
25+
26+
for (auto _ : state) {
27+
update_simulation_state_by_fixed_amount_improved(&N, array.data());
28+
benchmark::DoNotOptimize(array);
29+
}
30+
}
31+
32+
// The improved benchmark prevents the runtime bug, while not incurring in any
33+
// performance penalty
34+
OC_BENCHMARK("PWR083 Fortran Example", FortranExampleBench);
35+
OC_BENCHMARK("PWR083 Fortran Improved", FortranImprovedBench);
36+
37+
#endif
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
! PWR083: Match dummy and actual argument types in procedure calls
2+
3+
! NOT-PWR068: External procedure used to demonstrate how compilers can't catch
4+
! the type mismatch
5+
! NOT-PWR070: Explicit-shape arrays used for C-interoperability
6+
subroutine update_simulation_state_by_fixed_amount(n, state) bind(c)
7+
use iso_c_binding, only: c_double, c_int
8+
use iso_fortran_env, only: real32
9+
implicit none
10+
11+
external :: update_simulation_state
12+
13+
integer(kind=c_int), intent(in) :: n
14+
real(kind=c_double), dimension(n), intent(inout) :: state
15+
16+
real(kind=real32) :: numberSteps
17+
18+
numberSteps = 100
19+
call update_simulation_state(numberSteps, n, state)
20+
end subroutine update_simulation_state_by_fixed_amount
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
! PWR083: Match dummy and actual argument types in procedure calls
2+
3+
! NOT-PWR070: Explicit-shape arrays used for C-interoperability
4+
pure subroutine update_simulation_state(numberSteps, n, state)
5+
use iso_c_binding, only: c_double, c_int
6+
implicit none
7+
8+
integer, intent(in) :: numberSteps
9+
integer(kind=c_int), intent(in) :: n
10+
real(kind=c_double), dimension(n), intent(inout) :: state
11+
12+
integer(kind=c_int) :: i
13+
14+
do i = 1, n
15+
state(i) = state(i) + numberSteps * 9.81_c_double
16+
end do
17+
end subroutine update_simulation_state
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
! PWR083: Match dummy and actual argument types in procedure calls
2+
3+
! NOT-PWR068: External procedure used to demonstrate how compilers can't catch
4+
! the type mismatch
5+
! NOT-PWR070: Explicit-shape arrays used for C-interoperability
6+
subroutine update_simulation_state_by_fixed_amount_improved(n, state) bind(c)
7+
use iso_c_binding, only: c_double, c_int
8+
implicit none
9+
10+
external :: update_simulation_state
11+
12+
integer(kind=c_int), intent(in) :: n
13+
real(kind=c_double), dimension(n), intent(inout) :: state
14+
15+
integer :: numberSteps
16+
17+
numberSteps = 100
18+
call update_simulation_state(numberSteps, n, state)
19+
end subroutine update_simulation_state_by_fixed_amount_improved

0 commit comments

Comments
 (0)