File tree Expand file tree Collapse file tree 5 files changed +94
-0
lines changed
Expand file tree Collapse file tree 5 files changed +94
-0
lines changed Original file line number Diff line number Diff line change @@ -21,3 +21,4 @@ endif()
2121add_benchmark(PWR079)
2222add_benchmark(PWR080)
2323add_benchmark(PWR081)
24+ add_benchmark(PWR083)
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments