Skip to content

Commit

Permalink
Enriched the process grid interface (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
william-dawson authored Oct 5, 2018
1 parent d1c0539 commit 44f4a7e
Show file tree
Hide file tree
Showing 6 changed files with 262 additions and 30 deletions.
16 changes: 13 additions & 3 deletions Source/C/ProcessGrid_c.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,30 @@ void ConstructGlobalProcessGrid_wrp(const int *world_comm,
const int *process_columns,
const int *process_slices,
const bool *be_verbose);
void CopyProcessGrid_wrp(const int *ih_old_grid, int* ih_new_grid);
void ConstructGlobalProcessGridabr_wrp(const int *world_comm,
const int *process_slices,
const bool *be_verbose);
void CopyProcessGrid_wrp(const int *ih_old_grid, int *ih_new_grid);
int GetGlobalMySlice_wrp();
int GetGlobalMyColumn_wrp();
int GetGlobalMyRow_wrp();
int GetGlobalNumSlices_wrp();
int GetGlobalNumColumns_wrp();
int GetGlobalNumRows_wrp();
void DestructGlobalProcessGrid_wrp();

void ConstructProcessGrid_wrp(int *ih_grid, const int *world_comm,
const int *process_rows,
const int *process_columns,
const int *process_slices,
const bool *be_verbose);
const int *process_slices);
void ConstructProcessGridabr_wrp(int *ih_grid, const int *world_comm,
const int *process_slices);
int GetMySlice_wrp(const int *ih_grid);
int GetMyColumn_wrp(const int *ih_grid);
int GetMyRow_wrp(const int *ih_grid);
int GetNumSlices_wrp(const int *ih_grid);
int GetNumColumns_wrp(const int *ih_grid);
int GetNumRows_wrp(const int *ih_grid);
void DestructProcessGrid_wrp(int *ih_grid);

#endif
52 changes: 47 additions & 5 deletions Source/CPlusPlus/ProcessGrid.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,30 @@ namespace NTPoly {

////////////////////////////////////////////////////////////////////////////////
ProcessGrid::ProcessGrid(MPI_Comm world_comm, int process_rows,
int process_columns, int process_slices,
bool be_verbose) {
int process_columns, int process_slices) {
MPI_Fint temp_comm = MPI_Comm_c2f(world_comm);
ConstructProcessGrid_wrp(ih_this, &temp_comm, &process_rows, &process_columns,
&process_slices, &be_verbose);
&process_slices);
}

////////////////////////////////////////////////////////////////////////////////
ProcessGrid::ProcessGrid(int process_rows, int process_columns,
int process_slices, bool be_verbose) {
int process_slices) {
MPI_Fint temp_comm = MPI_Comm_c2f(MPI_COMM_WORLD);
ConstructProcessGrid_wrp(ih_this, &temp_comm, &process_rows, &process_columns,
&process_slices, &be_verbose);
&process_slices);
}

////////////////////////////////////////////////////////////////////////////////
ProcessGrid::ProcessGrid(MPI_Comm world_comm, int process_slices) {
MPI_Fint temp_comm = MPI_Comm_c2f(world_comm);
ConstructProcessGridabr_wrp(ih_this, &temp_comm, &process_slices);
}

////////////////////////////////////////////////////////////////////////////////
ProcessGrid::ProcessGrid(int process_slices) {
MPI_Fint temp_comm = MPI_Comm_c2f(MPI_COMM_WORLD);
ConstructProcessGridabr_wrp(ih_this, &temp_comm, &process_slices);
}

//////////////////////////////////////////////////////////////////////////////
Expand All @@ -39,6 +50,15 @@ int ProcessGrid::GetMyColumn() { return GetMyColumn_wrp(ih_this); }
////////////////////////////////////////////////////////////////////////////////
int ProcessGrid::GetMyRow() { return GetMyRow_wrp(ih_this); }

////////////////////////////////////////////////////////////////////////////////
int ProcessGrid::GetNumSlices() { return GetNumSlices_wrp(ih_this); }

////////////////////////////////////////////////////////////////////////////////
int ProcessGrid::GetNumColumns() { return GetNumColumns_wrp(ih_this); }

////////////////////////////////////////////////////////////////////////////////
int ProcessGrid::GetNumRows() { return GetNumRows_wrp(ih_this); }

////////////////////////////////////////////////////////////////////////////////
ProcessGrid::~ProcessGrid() { DestructProcessGrid_wrp(ih_this); }

Expand All @@ -59,6 +79,19 @@ void ConstructGlobalProcessGrid(int process_rows, int process_columns,
&process_slices, &be_verbose);
}

////////////////////////////////////////////////////////////////////////////////
void ConstructGlobalProcessGrid(MPI_Comm world_comm, int process_slices,
bool be_verbose) {
MPI_Fint temp_comm = MPI_Comm_c2f(world_comm);
ConstructGlobalProcessGridabr_wrp(&temp_comm, &process_slices, &be_verbose);
}

////////////////////////////////////////////////////////////////////////////////
void ConstructGlobalProcessGrid(int process_slices, bool be_verbose) {
MPI_Fint temp_comm = MPI_Comm_c2f(MPI_COMM_WORLD);
ConstructGlobalProcessGridabr_wrp(&temp_comm, &process_slices, &be_verbose);
}

////////////////////////////////////////////////////////////////////////////////
int GetGlobalMySlice() { return GetGlobalMySlice_wrp(); }

Expand All @@ -68,6 +101,15 @@ int GetGlobalMyColumn() { return GetGlobalMyColumn_wrp(); }
////////////////////////////////////////////////////////////////////////////////
int GetGlobalMyRow() { return GetGlobalMyRow_wrp(); }

////////////////////////////////////////////////////////////////////////////////
int GetGlobalNumSlices() { return GetGlobalNumSlices_wrp(); }

////////////////////////////////////////////////////////////////////////////////
int GetGlobalNumColumns() { return GetGlobalNumColumns_wrp(); }

////////////////////////////////////////////////////////////////////////////////
int GetGlobalNumRows() { return GetGlobalNumRows_wrp(); }

////////////////////////////////////////////////////////////////////////////////
void DestructGlobalProcessGrid() { DestructGlobalProcessGrid_wrp(); }
} // namespace NTPoly
38 changes: 33 additions & 5 deletions Source/CPlusPlus/ProcessGrid.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,21 @@ class ProcessGrid {
//! \param[in] process_rows number of grid rows.
//! \param[in] process_columns number of grid columns.
//! \param[in] process_slices number of grid slices.
//! \param[in] be_verbose verbosity flag.
ProcessGrid(MPI_Comm world_comm, int process_rows, int process_columns,
int process_slices, bool be_verbose = false);
int process_slices);
//! Construct the process grid from comm world
//! \param[in] process_rows number of grid rows.
//! \param[in] process_columns number of grid columns.
//! \param[in] process_slices number of grid slices.
//! \param[in] be_verbose verbosity flag.
ProcessGrid(int process_rows, int process_columns, int process_slices,
bool be_verbose = false);
ProcessGrid(int process_rows, int process_columns, int process_slices);
//! Construct the process grid.
//! \param[in] world_comm a communicator that every process in the grid is
//! a part of.
//! \param[in] process_rows number of grid rows.
ProcessGrid(MPI_Comm world_comm, int process_slices);
//! Construct the process grid from comm world
//! \param[in] process_slices number of grid slices.
ProcessGrid(int process_slices);
//! Copy constructor.
//!\param old_grid to copy from.
ProcessGrid(const ProcessGrid &old_grid);
Expand All @@ -36,6 +41,12 @@ class ProcessGrid {
int GetMyColumn();
//! Get the row of the current process.
int GetMyRow();
//! Get the number of slices in this grid.
int GetNumSlices();
//! Get the number of columns in this grid.
int GetNumColumns();
//! Get the number of rows in this grid.
int GetNumRows();

public:
//! Standard destructor
Expand Down Expand Up @@ -65,12 +76,29 @@ void ConstructGlobalProcessGrid(MPI_Comm world_comm, int process_rows,
//! \param[in] be_verbose verbosity flag.
void ConstructGlobalProcessGrid(int process_rows, int process_columns,
int process_slices, bool be_verbose = false);
//! Construct the global process grid.
//! \param[in] world_comm a communicator that every process in the grid is
//! a part of.
//! \param[in] process_slices number of grid slices.
//! \param[in] be_verbose verbosity flag.
void ConstructGlobalProcessGrid(MPI_Comm world_comm, int process_slices,
bool be_verbose = false);
//! Construct the global process grid from comm world
//! \param[in] process_slices number of grid slices.
//! \param[in] be_verbose verbosity flag.
void ConstructGlobalProcessGrid(int process_slices, bool be_verbose = false);
//! Get the slice of the current process.
int GetGlobalMySlice();
//! Get the column of the current process.
int GetGlobalMyColumn();
//! Get the row of the current process.
int GetGlobalMyRow();
//! Get the number of process slices.
int GetGlobalNumSlices();
//! Get the number of process columns.
int GetGlobalNumColumns();
//! Get the number of process rows.
int GetGlobalNumRows();
//! Standard destructor
void DestructGlobalProcessGrid();
} // namespace NTPoly
Expand Down
78 changes: 63 additions & 15 deletions Source/Fortran/ProcessGridModule.F90
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ MODULE ProcessGridModule
MODULE PROCEDURE ConstructProcessGrid_full
MODULE PROCEDURE ConstructProcessGrid_onlyslice
END INTERFACE
INTERFACE ConstructNewProcessGrid
MODULE PROCEDURE ConstructNewProcessGrid_full
MODULE PROCEDURE ConstructNewProcessGrid_onlyslice
END INTERFACE
CONTAINS !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!> Setup the default process grid.
SUBROUTINE ConstructProcessGrid_full(world_comm_, process_rows_, &
Expand Down Expand Up @@ -132,9 +136,7 @@ SUBROUTINE ConstructProcessGrid_onlyslice(world_comm_, process_slices_in, &
!! Local Data
LOGICAL(kind=c_bool) :: be_verbose
INTEGER :: process_rows, process_columns, process_slices
INTEGER :: slice_size, size_search
INTEGER :: total_processors
INTEGER :: II
INTEGER :: ierr

!! Total processors
Expand All @@ -153,25 +155,16 @@ SUBROUTINE ConstructProcessGrid_onlyslice(world_comm_, process_slices_in, &
END IF

!! Create a 3D grid
process_rows = 1
process_columns = 1
slice_size = total_processors/process_slices
size_search = FLOOR(SQRT(REAL(slice_size)))
DO II=size_search,1,-1
IF (MOD(slice_size,II) .EQ. 0) THEN
process_rows = II
process_columns = slice_size/II
EXIT
END IF
END DO
CALL ComputeGridSize(total_processors, process_slices, process_rows, &
& process_columns)

!! Now call the full setup
CALL ConstructProcessGrid(world_comm_, process_rows, process_columns, &
& process_slices, be_verbose)
END SUBROUTINE ConstructProcessGrid_onlyslice
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!> Construct a process grid.
SUBROUTINE ConstructNewProcessGrid(grid, world_comm_, process_rows_, &
SUBROUTINE ConstructNewProcessGrid_full(grid, world_comm_, process_rows_, &
& process_columns_, process_slices_)
!> The grid to construct
TYPE(ProcessGrid_t), INTENT(INOUT) :: grid
Expand Down Expand Up @@ -289,7 +282,33 @@ SUBROUTINE ConstructNewProcessGrid(grid, world_comm_, process_rows_, &
& grid%global_rank, grid%blocked_row_comm(II), ierr)
END DO

END SUBROUTINE ConstructNewProcessGrid
END SUBROUTINE ConstructNewProcessGrid_full
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!> Setup a process grid specifying only the slices
SUBROUTINE ConstructNewProcessGrid_onlyslice(grid, world_comm_, &
& process_slices)
!> The grid to construct
TYPE(ProcessGrid_t), INTENT(INOUT) :: grid
!> A communicator that every process in the grid is a part of.
INTEGER(kind=c_int), INTENT(IN) :: world_comm_
!> The number of grid slices.
INTEGER(kind=c_int), INTENT(IN) :: process_slices
!! Local Data
INTEGER :: process_rows, process_columns
INTEGER :: total_processors
INTEGER :: ierr

!! Total processors
CALL MPI_COMM_SIZE(world_comm_, total_processors, ierr)

!! Create a 3D grid
CALL ComputeGridSize(total_processors, process_slices, process_rows, &
& process_columns)

!! Now call the full setup
CALL ConstructNewProcessGrid(grid, world_comm_, process_rows, &
& process_columns, process_slices)
END SUBROUTINE ConstructNewProcessGrid_onlyslice
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!> Copy a process grid.
!! Note that this makes a complete and independent copy of the process grid.
Expand Down Expand Up @@ -563,5 +582,34 @@ FUNCTION GetMyRow(grid) RESULT(return_val)
return_val = global_grid%my_row
END IF
END FUNCTION GetMyRow
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!> Sometimes we only want to specify a process grid's number of slices
!! and then automatically compute the right number of rows and columns.
SUBROUTINE ComputeGridSize(total_processors, set_slices, rows, columns)
!> Total processors in the grid
INTEGER, INTENT(IN) :: total_processors
!> Desired number of slices
INTEGER, INTENT(IN) :: set_slices
!> Computed number of rows
INTEGER, INTENT(OUT) :: rows
!> Computed number of columns
INTEGER, INTENT(OUT) :: columns
!! Local variables
INTEGER :: slice_size, size_search
INTEGER :: II

rows = 1
columns = 1
slice_size = total_processors/set_slices
size_search = FLOOR(SQRT(REAL(slice_size)))
DO II=size_search,1,-1
IF (MOD(slice_size,II) .EQ. 0) THEN
rows = II
columns = slice_size/II
EXIT
END IF
END DO

END SUBROUTINE ComputeGridSize
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
END MODULE ProcessGridModule
Loading

0 comments on commit 44f4a7e

Please sign in to comment.