XGCa
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
task_group.hpp
Go to the documentation of this file.
1 #ifndef TASK_GROUP_HPP
2 #define TASK_GROUP_HPP
3 #ifdef USE_MPI
4 #include "my_mpi.hpp"
5 #endif
6 
14 class TaskGroup{
15 #ifdef USE_MPI
16  MPI_Comm comm;
17 #endif
18  int my_rank;
19  int n_ranks;
20 
21  public:
22 
27 
28 #ifdef USE_MPI
29 
34  TaskGroup(MPI_Comm comm_in)
35  : comm(comm_in)
36  {
37  MPI_Comm_size( comm, &n_ranks );
38  MPI_Comm_rank( comm, &my_rank );
39  }
40 #endif
41 
47  int get_my_rank() const{
48  return my_rank;
49  }
50 
57  int get_n_local_tasks(int n_tasks) const{
58  return ((n_tasks + n_ranks - my_rank - 1)/n_ranks);
59  }
60 
67  int get_max_n_local_tasks(int n_tasks) const{
68  return ((n_tasks + n_ranks - 1)/n_ranks);
69  }
70 
71 #ifdef USE_MPI
72  MPI_Comm get_comm() const{
73  return comm;
74  }
75 #endif
76 
84  template<typename F>
85  void execute_task(int ntasks, F func) const{
86  for(int itask=0; itask<ntasks; itask++){
87  int assigned_rank = itask%n_ranks;
88  if(my_rank==assigned_rank){
89  int i_local_task = itask/n_ranks;
90  func(itask, i_local_task);
91  }
92  }
93  }
94 
95 #ifdef USE_MPI
96 
103  template<typename F>
104  void communicate(int ntasks, F func) const{
105  for(int itask=0; itask<ntasks; itask++){
106  int i_local_task = itask/n_ranks;
107  int assigned_rank = itask%n_ranks;
108  bool is_assigned_rank = (my_rank==assigned_rank);
109  func(itask, i_local_task, assigned_rank, is_assigned_rank, comm);
110  }
111  }
112 #endif
113 };
114 
115 #endif
int my_rank
The rank of the current process within the MPI communicator.
Definition: task_group.hpp:18
void execute_task(int ntasks, F func) const
Execute the given task function for each assigned task.
Definition: task_group.hpp:85
A class that manages the distribution and execution of tasks in parallel using the MPI library...
Definition: task_group.hpp:14
int n_ranks
The total number of processes within the MPI communicator.
Definition: task_group.hpp:19
TaskGroup()
Default constructor for the TaskGroup class.
Definition: task_group.hpp:26
int get_my_rank() const
Get the rank of the current process within the MPI communicator.
Definition: task_group.hpp:47
int get_n_local_tasks(int n_tasks) const
Calculate the number of tasks assigned to the current process.
Definition: task_group.hpp:57
int get_max_n_local_tasks(int n_tasks) const
Calculate the maximum number of tasks assigned to any process.
Definition: task_group.hpp:67