Skip to content

Commit

Permalink
grid: Avoid BIND(C) on nested subroutines to make CCE happy
Browse files Browse the repository at this point in the history
  • Loading branch information
oschuett committed Aug 29, 2020
1 parent 205154d commit 4fd25fa
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 57 deletions.
37 changes: 23 additions & 14 deletions src/grid/common/grid_library.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,25 +101,33 @@ void grid_library_gather_stats(const grid_library_stats increment) {
// \brief Prints statistics gathered by the grid library.
// \author Ole Schuett
//******************************************************************************
void grid_library_print_stats(void (*mpi_sum_func)(long *),
void (*print_func)(char *)) {
void grid_library_print_stats(void (*mpi_sum_func)(long *, int),
const int mpi_comm,
void (*print_func)(char *, int),
const int output_unit) {
if (!library_initialized) {
printf("Error: Grid library is not initialized.\n");
abort();
}
print_func("\n");
print_func("\n", output_unit);
print_func(" ----------------------------------------------------------------"
"---------------\n");
"---------------\n",
output_unit);
print_func(" - "
" -\n");
" -\n",
output_unit);
print_func(" - GRID STATISTICS "
" -\n");
" -\n",
output_unit);
print_func(" - "
" -\n");
" -\n",
output_unit);
print_func(" ----------------------------------------------------------------"
"---------------\n");
"---------------\n",
output_unit);
print_func(" COUNTER "
" VALUE\n");
" VALUE\n",
output_unit);

grid_library_stats totals;
memset(&totals, 0, sizeof(grid_library_stats));
Expand All @@ -129,18 +137,19 @@ void grid_library_print_stats(void (*mpi_sum_func)(long *),
}

char buffer[100];
mpi_sum_func(&totals.ref_collocate_ortho);
mpi_sum_func(&totals.ref_collocate_ortho, mpi_comm);
snprintf(buffer, sizeof(buffer), " %-58s %20li\n", "ref_collocate_ortho",
totals.ref_collocate_ortho);
print_func(buffer);
print_func(buffer, output_unit);

mpi_sum_func(&totals.ref_collocate_general);
mpi_sum_func(&totals.ref_collocate_general, mpi_comm);
snprintf(buffer, sizeof(buffer), " %-58s %20li\n", "ref_collocate_general",
totals.ref_collocate_general);
print_func(buffer);
print_func(buffer, output_unit);

print_func(" ----------------------------------------------------------------"
"---------------\n");
"---------------\n",
output_unit);
}

// EOF
4 changes: 2 additions & 2 deletions src/grid/common/grid_library.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ grid_library_config grid_library_get_config();
// \brief Prints statistics gathered by the grid library.
// \author Ole Schuett
//******************************************************************************
void grid_library_print_stats(void (*mpi_sum_func)(long *),
void (*print_func)(char *));
void grid_library_print_stats(void (*mpi_sum_func)(long *, int), int mpi_comm,
void (*print_func)(char *, int), int output_unit);

//******************************************************************************
// \brief All exiting counters. When adding a counter also update functions
Expand Down
69 changes: 36 additions & 33 deletions src/grid/grid_api.F
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,6 @@ MODULE grid_api
TYPE(C_PTR) :: c_ptr = C_NULL_PTR
END TYPE grid_task_list_type

! Can not place this into a subroutine because fpretty keeps removing it.
INTERFACE
SUBROUTINE grid_library_print_stats_c(mpi_sum_func, print_func) &
BIND(C, name="grid_library_print_stats")
IMPORT :: C_FUNPTR
TYPE(C_FUNPTR), VALUE :: mpi_sum_func
TYPE(C_FUNPTR), VALUE :: print_func
END SUBROUTINE grid_library_print_stats_c
END INTERFACE

CONTAINS

! **************************************************************************************************
Expand Down Expand Up @@ -842,50 +832,63 @@ END SUBROUTINE grid_library_set_config
!> \author Ole Schuett
! **************************************************************************************************
SUBROUTINE grid_library_print_stats(mpi_comm, output_unit)
INTEGER, INTENT(IN) :: mpi_comm, output_unit
INTEGER, INTENT(IN) :: mpi_comm, output_unit

! Interface is defined above, because fpretty kept removing it from here.
INTERFACE
SUBROUTINE grid_library_print_stats_c(mpi_sum_func, mpi_comm, print_func, output_unit) &
BIND(C, name="grid_library_print_stats")
IMPORT :: C_FUNPTR, C_INT
TYPE(C_FUNPTR), VALUE :: mpi_sum_func
INTEGER(KIND=C_INT), VALUE :: mpi_comm
TYPE(C_FUNPTR), VALUE :: print_func
INTEGER(KIND=C_INT), VALUE :: output_unit
END SUBROUTINE grid_library_print_stats_c
END INTERFACE

! Since Fortran units and mpi groups can't be used from C, we pass function pointers instead.
CALL grid_library_print_stats_c(mpi_sum_func=C_FUNLOC(mpi_sum_func), &
print_func=C_FUNLOC(print_func))
mpi_comm=mpi_comm, &
print_func=C_FUNLOC(print_func), &
output_unit=output_unit)
CONTAINS
END SUBROUTINE grid_library_print_stats
! **************************************************************************************************
!> \brief Closure to wrap a Fortran mpi communicator.
!> \brief Callback to run mpi_sum on a Fortran MPI communicator.
!> \param number ...
!> \param mpi_comm ...
!> \author Ole Schuett
! **************************************************************************************************
SUBROUTINE mpi_sum_func(number) BIND(C)
SUBROUTINE mpi_sum_func(number, mpi_comm) BIND(C)
INTEGER(KIND=C_LONG), INTENT(INOUT) :: number
INTEGER(KIND=C_INT), INTENT(IN), VALUE :: mpi_comm
CALL mp_sum(number, mpi_comm)
END SUBROUTINE mpi_sum_func
CALL mp_sum(number, mpi_comm)
END SUBROUTINE mpi_sum_func
! **************************************************************************************************
!> \brief Closure to wrap a Fortran output unit.
!> \brief Callback to write to a Fortran output unit.
!> \param message ...
!> \param output_unit ...
!> \author Ole Schuett
! **************************************************************************************************
SUBROUTINE print_func(message) BIND(C)
SUBROUTINE print_func(message, output_unit) BIND(C)
CHARACTER(LEN=1, KIND=C_CHAR), INTENT(IN) :: message(*)
INTEGER(KIND=C_INT), INTENT(IN), VALUE :: output_unit
CHARACTER(LEN=1000) :: buffer
INTEGER :: i
IF (output_unit > 0) THEN
! Convert C char array into Fortran string.
buffer = ""
DO i = 1, LEN(buffer)
IF (message(i) == C_NULL_CHAR) EXIT
buffer(i:i) = message(i)
END DO
! Print the message.
WRITE (output_unit, FMT="(A)", ADVANCE="NO") buffer(1:i - 1)
END IF
END SUBROUTINE print_func
END SUBROUTINE grid_library_print_stats
IF (output_unit > 0) THEN
! Convert C char array into Fortran string.
buffer = ""
DO i = 1, LEN(buffer)
IF (message(i) == C_NULL_CHAR) EXIT
buffer(i:i) = message(i)
END DO
! Print the message.
WRITE (output_unit, FMT="(A)", ADVANCE="NO") buffer(1:i - 1)
END IF
END SUBROUTINE print_func
END MODULE grid_api
12 changes: 8 additions & 4 deletions src/grid/grid_collocate_miniapp.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@
#include "common/grid_library.h"
#include "grid_collocate_replay.h"

void mpi_sum_func(long *number) {
*number += 0; // Nothing todo without MPI, pretend argument is used anyways.
void mpi_sum_func(long *number, int mpi_comm) {
*number += 0; // Nothing todo without MPI, pretend arguments are used anyways.
mpi_comm += 0;
}

void print_func(char *message) { printf("%s", message); }
void print_func(char *message, int output_unit) {
output_unit += 0; // Pretent argument is used.
printf("%s", message);
}

//******************************************************************************
// \brief Stand-alone miniapp for running .task files.
Expand Down Expand Up @@ -56,7 +60,7 @@ int main(int argc, char *argv[]) {
const double max_diff =
grid_collocate_replay(argv[iarg++], cycles, batch, cycles_per_block);

grid_library_print_stats(&mpi_sum_func, &print_func);
grid_library_print_stats(&mpi_sum_func, 0, &print_func, 0);
grid_library_finalize();

if (max_diff > 1e-12 * cycles) {
Expand Down
12 changes: 8 additions & 4 deletions src/grid/grid_collocate_unittest.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@
#include "common/grid_library.h"
#include "grid_collocate_replay.h"

void mpi_sum_func(long *number) {
*number += 0; // Nothing todo without MPI, pretend argument is used anyways.
void mpi_sum_func(long *number, int mpi_comm) {
*number += 0; // Nothing todo without MPI, pretend arguments are used anyways.
mpi_comm += 0;
}

void print_func(char *message) { printf("%s", message); }
void print_func(char *message, int output_unit) {
output_unit += 0; // Pretent argument is used.
printf("%s", message);
}

//******************************************************************************
// \brief Unit test for the grid collocate code.
Expand Down Expand Up @@ -66,7 +70,7 @@ int main(int argc, char *argv[]) {
errors += run_test(argv[1], "collocate_general_subpatch16.task");
errors += run_test(argv[1], "collocate_ortho_non_periodic.task");

grid_library_print_stats(&mpi_sum_func, &print_func);
grid_library_print_stats(&mpi_sum_func, 0, &print_func, 0);
grid_library_finalize();

if (errors == 0) {
Expand Down

0 comments on commit 4fd25fa

Please sign in to comment.