Ice Sheet System Model  4.18
Code documentation
Functions
commops.h File Reference
#include "../../../shared/Numerics/types.h"
#include "../../../shared/io/Comm/IssmComm.h"
#include "../../mpi/issmmpi.h"

Go to the source code of this file.

Functions

int DetermineLocalSize (int global_size, ISSM_MPI_Comm comm)
 
int * DetermineRowRankFromLocalSize (int global_size, int localsize, ISSM_MPI_Comm comm)
 
void GetOwnershipBoundariesFromRange (int *plower_row, int *pupper_row, int range, ISSM_MPI_Comm comm)
 
int DetermineGlobalSize (int local_size, ISSM_MPI_Comm comm)
 

Function Documentation

◆ DetermineLocalSize()

int DetermineLocalSize ( int  global_size,
ISSM_MPI_Comm  comm 
)

Definition at line 9 of file DetermineLocalSize.cpp.

9  {
10 
11  /*output: */
12  int local_size;
13 
14  /*intermediary: */
15  int i;
16  int row_rest;
17  int* num_local_rows=NULL;
18 
19  /*from MPI: */
20  int num_procs;
21  int my_rank;
22 
23  /*recover my_rank*/
24  ISSM_MPI_Comm_rank(comm,&my_rank);
25  ISSM_MPI_Comm_size(comm,&num_procs);
26 
27  /* TODO replace the following with ->
28 
29  local_size=global_size/num_procs; // integer division
30  if (global_size%num_procs>my_rank) local_size++; // distribute the remainder
31  return local_size;
32 
33  <- to here */
34 
35  /*We are not bound by any library, just use what seems most logical*/
36  num_local_rows=xNew<int>(num_procs);
37 
38  for (i=0;i<num_procs;i++){
39  /*Here, we use floor. We under distribute rows. The rows
40  left are then redistributed, therefore resulting in a
41  more even distribution.*/
42  num_local_rows[i]=(int)floor((double)global_size/(double)num_procs);
43  }
44 
45  /*There may be some rows left. Distribute evenly.*/
46  row_rest=global_size - num_procs*(int)floor((double)global_size/(double)num_procs);
47  for (i=0;i<row_rest;i++){
48  num_local_rows[i]++;
49  }
50  local_size=num_local_rows[my_rank];
51 
52  /*free ressources: */
53  xDelete<int>(num_local_rows);
54 
55  /*return size: */
56  return local_size;
57 
58 }

◆ DetermineRowRankFromLocalSize()

int* DetermineRowRankFromLocalSize ( int  global_size,
int  localsize,
ISSM_MPI_Comm  comm 
)

Definition at line 12 of file DetermineRowRankFromLocalSize.cpp.

12  {
13 
14  /*intermediary: */
15  int i,j;
16  int my_rank=0;
17  int num_procs=0;
18  int lower_row,upper_row;
19 
20  /*output: */
21  int* RowRank=NULL;
22 
23  ISSM_MPI_Comm_rank(comm,&my_rank);
24  ISSM_MPI_Comm_size(comm,&num_procs);
25 
26  /*allocate: */
27  RowRank=xNew<int>(global_size);
28 
29  /*Gather all local_size values into alllocalsizes, for all cpus*/
30  int* alllocalsizes=xNew<int>(num_procs);
31  ISSM_MPI_Allgather(&localsize,1,ISSM_MPI_INT,alllocalsizes,1,ISSM_MPI_INT,comm);
32 
33  /*From all localsizes, get lower row and upper row*/
34  lower_row=0;
35  upper_row=lower_row+alllocalsizes[0];
36  for(j=lower_row;j<upper_row;j++)RowRank[j]=0;
37 
38  for(i=1;i<num_procs;i++){
39  lower_row=lower_row+alllocalsizes[i-1];
40  upper_row=upper_row+alllocalsizes[i];
41  for(j=lower_row;j<upper_row;j++)RowRank[j]=i;
42  }
43  xDelete<int>(alllocalsizes);
44 
45  return RowRank;
46 }

◆ GetOwnershipBoundariesFromRange()

void GetOwnershipBoundariesFromRange ( int *  plower_row,
int *  pupper_row,
int  range,
ISSM_MPI_Comm  comm 
)

Definition at line 16 of file GetOwnershipBoundariesFromRange.cpp.

16  {
17 
18  /*externals :*/
19  int my_rank;
20  int num_procs;
21 
22  /*recover my_rank and num_procs:*/
23  ISSM_MPI_Comm_size(comm,&num_procs);
24  ISSM_MPI_Comm_rank(comm,&my_rank);
25 
26  /*output: */
27  int lower_row,upper_row;
28 
29  /*Gather all range values into allranges, for all nodes*/
30  int* allranges=xNew<int>(num_procs);
31  ISSM_MPI_Allgather(&range,1,ISSM_MPI_INT,allranges,1,ISSM_MPI_INT,comm);
32 
33  /*From all ranges, get lower row and upper row*/
34  lower_row=0;
35  upper_row=lower_row+allranges[0];
36  for(int i=1;i<=my_rank;i++){
37  lower_row=lower_row+allranges[i-1];
38  upper_row=upper_row+allranges[i];
39  }
40 
41  /*Assign output*/
42  *plower_row=lower_row;
43  *pupper_row=upper_row;
44  xDelete<int>(allranges);
45 }

◆ DetermineGlobalSize()

int DetermineGlobalSize ( int  local_size,
ISSM_MPI_Comm  comm 
)

Definition at line 10 of file DetermineGlobalSize.cpp.

10  {
11 
12  /*output: */
13  int global_size;
14 
15  ISSM_MPI_Reduce(&local_size, &global_size, 1, ISSM_MPI_INT, ISSM_MPI_SUM, 0, comm);
16  ISSM_MPI_Bcast(&global_size,1,ISSM_MPI_INT,0,comm);
17 
18  return global_size;
19 
20 }
ISSM_MPI_SUM
#define ISSM_MPI_SUM
Definition: issmmpi.h:134
ISSM_MPI_Comm_rank
int ISSM_MPI_Comm_rank(ISSM_MPI_Comm comm, int *rank)
Definition: issmmpi.cpp:198
ISSM_MPI_INT
#define ISSM_MPI_INT
Definition: issmmpi.h:127
ISSM_MPI_Bcast
int ISSM_MPI_Bcast(void *buffer, int count, ISSM_MPI_Datatype datatype, int root, ISSM_MPI_Comm comm)
Definition: issmmpi.cpp:162
ISSM_MPI_Allgather
int ISSM_MPI_Allgather(void *sendbuf, int sendcount, ISSM_MPI_Datatype sendtype, void *recvbuf, int recvcount, ISSM_MPI_Datatype recvtype, ISSM_MPI_Comm comm)
Definition: issmmpi.cpp:45
ISSM_MPI_Reduce
int ISSM_MPI_Reduce(void *sendbuf, void *recvbuf, int count, ISSM_MPI_Datatype datatype, ISSM_MPI_Op op, int root, ISSM_MPI_Comm comm)
Definition: issmmpi.cpp:373
ISSM_MPI_Comm_size
int ISSM_MPI_Comm_size(ISSM_MPI_Comm comm, int *size)
Definition: issmmpi.cpp:209