RockyML  0.0.1
A High-Performance Scientific Computing Framework
communication.h
1 /*
2  Copyright (C) 2022 Amirabbas Asadi , All Rights Reserved
3  distributed under Apache-2.0 license
4 */
5 #ifndef ROCKY_ZAGROS_COMM_STRATEGY
6 #define ROCKY_ZAGROS_COMM_STRATEGY
7 
8 #include <rocky/zagros/strategies/strategy.h>
9 #include<nlohmann/json.hpp>
10 #include<cpr/cpr.h>
11 
12 
13 
14 namespace rocky{
15 namespace zagros{
20 template<typename T_e, int T_dim>
21 class comm_strategy: public basic_strategy<T_e, T_dim>{
22 public:
23  virtual void apply() = 0;
24 };
25 
26 
31 template<typename T_e, int T_dim>
32 class mpi_strategy: public comm_strategy<T_e, T_dim>{
33 protected:
34  // number of MPI processes
35  int mpi_num_procs_;
36  // MPI rank
37  int mpi_rank_;
38 public:
47  MPI_Comm_size(MPI_COMM_WORLD, &mpi_num_procs_);
48  MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank_);
49  }
50 
51  int mpi_num_procs() const{
52  return mpi_num_procs_;
53  }
54  int mpi_rank() const{
55  return mpi_rank_;
56  }
57 
58 };
59 
64 template<typename T_e, int T_dim>
65 class sync_broadcast_best: public mpi_strategy<T_e, T_dim>{
66 protected:
67  basic_scontainer<T_e, T_dim>* cluster_best_container_;
68 public:
69  void set_target_container(basic_scontainer<T_e, T_dim>* container){
70  this->set_target_container = container;
71  }
73  this->cluster_best_container_ = container;
74  this->fetch_mpi_info();
75  }
77  this->cluster_best_container_ = nullptr;
78  this->fetch_mpi_info();
79  }
80  virtual void apply(){
81  // identify the process
82  int rank = this->mpi_rank();
83  // ask everyone in the cluster to find the min value
84  struct {
85  T_e cluster_best_min;
86  int rank;
87  } data_out, result;
88 
89  data_out.cluster_best_min = cluster_best_container_->values[0];
90  data_out.rank = rank;
91 
92  if constexpr(std::is_same<T_e, double>::value){
93  MPI_Allreduce(&data_out, &result, 1, MPI_DOUBLE_INT, MPI_MINLOC, MPI_COMM_WORLD);
94  MPI_Bcast(cluster_best_container_->particle(0), T_dim, MPI_DOUBLE, result.rank, MPI_COMM_WORLD);
95  }
96  if constexpr(std::is_same<T_e, float>::value){
97  MPI_Allreduce(&data_out, &result, 1, MPI_FLOAT_INT, MPI_MINLOC, MPI_COMM_WORLD);
98  MPI_Bcast(cluster_best_container_->particle(0), T_dim, MPI_FLOAT, result.rank, MPI_COMM_WORLD);
99  }
100  cluster_best_container_->values[0] = result.cluster_best_min;
101  }
102 };
107 template<typename T_e, int T_dim>
108 class sync_bcd_mask: public mpi_strategy<T_e, T_dim>{
109 protected:
110  int* bcd_mask_;
111 public:
112  sync_bcd_mask(int* bcd_mask){
113  this->bcd_mask_ = bcd_mask;
114  this->fetch_mpi_info();
115  }
116  virtual void apply(){
117  MPI_Bcast(this->bcd_mask_, T_dim, MPI_INT, 0, MPI_COMM_WORLD);
118  }
119 };
120 
121 };
122 };
123 #endif
rocky::zagros::mpi_strategy
base class for all strategies who need MPI communication
Definition: communication.h:32
rocky::zagros::sync_broadcast_best
A Communication strategy for broadcasting best solution.
Definition: communication.h:65
rocky::zagros::comm_strategy
Interface for communication strategies.
Definition: communication.h:21
rocky::zagros::basic_scontainer
a data container representing a scontainer
Definition: scontainer.h:31
rocky::zagros::sync_bcd_mask
A Communication strategy for broadcasting bcd mask.
Definition: communication.h:108
rocky::zagros::basic_strategy
Interface for all strategies.
Definition: strategy.h:31
rocky::zagros::basic_scontainer::particle
T_e * particle(int p)
get the starting address of a specific particle
Definition: scontainer.h:71
rocky::zagros::mpi_strategy::fetch_mpi_info
void fetch_mpi_info()
receive MPI information including
Definition: communication.h:46