OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
OPAL
MPIHelper.h
Go to the documentation of this file.
1 //
2 // Global functions MPIHelper
3 //
4 // Copyright (c) 2010 - 2013, Yves Ineichen, ETH Zürich
5 // All rights reserved
6 //
7 // Implemented as part of the PhD thesis
8 // "Toward massively parallel multi-objective optimization with application to
9 // particle accelerators" (https://doi.org/10.3929/ethz-a-009792359)
10 //
11 // This file is part of OPAL.
12 //
13 // OPAL is free software: you can redistribute it and/or modify
14 // it under the terms of the GNU General Public License as published by
15 // the Free Software Foundation, either version 3 of the License, or
16 // (at your option) any later version.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with OPAL. If not, see <https://www.gnu.org/licenses/>.
20 //
21 #ifndef __MPIHELPER_H__
22 #define __MPIHELPER_H__
23 
24 #include <vector>
25 #include <map>
26 #include <sstream>
27 #include <mpi.h>
28 
29 #include "Util/Types.h"
30 
31 
33 #define MPI_WORKER_STATUSUPDATE_TAG 0x11
35 #define MPI_WORKER_FINISHED_TAG 0x12
37 #define MPI_WORKER_FINISHED_ACK_TAG 0x13
38 
40 #define MPI_COWORKER_NEW_JOB_TAG 0x14
41 
42 
44 #define MPI_OPT_NEW_JOB_TAG 0x21
46 #define MPI_OPT_JOB_FINISHED_TAG 0x22
48 #define MPI_OPT_CONVERGED_TAG 0x23
49 
50 
52 #define MPI_WORK_JOBID_TAG 0x41
54 #define MPI_WORK_SIZE_TAG 0x42
56 #define MPI_WORK_SIZE_PARAMS 0x43
57 
58 #define MPI_EXCHANGE_SOL_STATE_TAG 0x51
59 #define MPI_EXCHANGE_SOL_STATE_DATA_TAG 0x52
60 #define MPI_EXCHANGE_SOL_STATE_RES_SIZE_TAG 0x53
61 #define MPI_EXCHANGE_SOL_STATE_RES_TAG 0x54
62 
64 #define MPI_STOP_TAG 0x91
65 
67 #define MPI_EXCHANGE_SERIALIZED_DATA_TAG 0x99
68 
69 
70 //FIXME: double information (use static vars or enums)
71 enum MPITag_t {
79 };
80 
82 enum State_t {IDLE = 0, RUNNING = 1};
83 
85 void serialize(Param_t params, std::ostringstream &os);
86 void serialize(reqVarContainer_t params, std::ostringstream &os);
87 
89 void deserialize(char *buffer, Param_t &params);
90 void deserialize(char *buffer, reqVarContainer_t &params);
91 
93 void MPI_Bcast_params(Param_t &params, size_t root, MPI_Comm comm);
94 
96 void MPI_Bcast_reqvars(reqVarContainer_t reqvars, size_t root, MPI_Comm comm);
97 
98 
99 //FIXME: test
100 template<class Data_t>
101 void MPI_Send_serialized(Data_t data, size_t pid, MPI_Comm comm) {
102 
103  std::ostringstream os;
104  serialize(data, os);
105  size_t buf_size = os.str().length();
106 
107  MPI_Send(&buf_size, 1, MPI_LONG, pid,
109 
110  char *buffer = new char[buf_size];
111  memcpy(buffer, os.str().c_str(), buf_size);
112 
113  MPI_Send(buffer, buf_size, MPI_CHAR, pid,
115 
116  delete[] buffer;
117 }
118 
119 template<class Data_t>
120 void MPI_Recv_serialized(Data_t &data, size_t pid, MPI_Comm comm) {
121 
122  MPI_Status status;
123  size_t buf_size = 0;
124  MPI_Recv(&buf_size, 1, MPI_LONG, pid,
125  MPI_EXCHANGE_SERIALIZED_DATA_TAG, comm, &status);
126 
127  char *buffer = new char[buf_size];
128  MPI_Recv(buffer, buf_size, MPI_CHAR, pid,
129  MPI_EXCHANGE_SERIALIZED_DATA_TAG, comm, &status);
130 
131  deserialize(buffer, data);
132  delete[] buffer;
133 }
134 
135 
136 
144 void MPI_Send_params(Param_t params, size_t pid, MPI_Comm comm);
145 
156 std::pair<size_t*, char*> MPI_ISend_params(Param_t params, size_t pid,
157  MPI_Comm comm, MPI_Request *req);
158 
166 void MPI_Recv_params(Param_t &params, size_t pid, MPI_Comm comm);
167 
175 void MPI_Send_reqvars(reqVarContainer_t reqvars, size_t pid, MPI_Comm comm);
176 
185 void MPI_Recv_reqvars(reqVarContainer_t &reqvars, size_t pid, MPI_Comm comm);
186 
187 #endif
void MPI_Send_reqvars(reqVarContainer_t reqvars, size_t pid, MPI_Comm comm)
Definition: MPIHelper.cpp:146
#define MPI_OPT_JOB_FINISHED_TAG
pilot tells optimizer that results are ready to collect
Definition: MPIHelper.h:46
void MPI_Bcast_params(Param_t &params, size_t root, MPI_Comm comm)
broadcast params to all entities in comm
Definition: MPIHelper.cpp:60
#define MPI_EXCHANGE_SOL_STATE_RES_SIZE_TAG
Definition: MPIHelper.h:60
void MPI_Send_params(Param_t params, size_t pid, MPI_Comm comm)
Definition: MPIHelper.cpp:87
void MPI_Bcast_reqvars(reqVarContainer_t reqvars, size_t root, MPI_Comm comm)
broadcast requested variables to all entities in comm
#define MPI_WORKER_FINISHED_TAG
notify pilot that work has been finished and results are ready to collect
Definition: MPIHelper.h:35
void MPI_Recv_serialized(Data_t &data, size_t pid, MPI_Comm comm)
Definition: MPIHelper.h:120
void MPI_Recv_reqvars(reqVarContainer_t &reqvars, size_t pid, MPI_Comm comm)
Definition: MPIHelper.cpp:165
#define MPI_EXCHANGE_SOL_STATE_TAG
Definition: MPIHelper.h:58
void serialize(Param_t params, std::ostringstream &os)
serializes params using Boost archive
Definition: MPIHelper.cpp:32
#define MPI_OPT_CONVERGED_TAG
optimizer notifies pilot that optimization has converged (EXIT)
Definition: MPIHelper.h:48
void MPI_Send_serialized(Data_t data, size_t pid, MPI_Comm comm)
Definition: MPIHelper.h:101
#define MPI_WORKER_STATUSUPDATE_TAG
notify pilot about worker status
Definition: MPIHelper.h:33
#define MPI_OPT_NEW_JOB_TAG
optimizer sends new job to pilot
Definition: MPIHelper.h:44
#define MPI_EXCHANGE_SERIALIZED_DATA_TAG
tag for exchanging serialized data
Definition: MPIHelper.h:67
MPITag_t
Definition: MPIHelper.h:71
@ REQUEST_FINISHED
Definition: MPIHelper.h:76
@ WORKER_FINISHED_TAG
Definition: MPIHelper.h:72
@ EXCHANGE_SOL_STATE_RES_SIZE_TAG
Definition: MPIHelper.h:78
@ OPT_CONVERGED_TAG
Definition: MPIHelper.h:74
@ OPT_NEW_JOB_TAG
Definition: MPIHelper.h:73
@ WORKER_STATUSUPDATE_TAG
Definition: MPIHelper.h:75
@ EXCHANGE_SOL_STATE_TAG
Definition: MPIHelper.h:77
std::pair< size_t *, char * > MPI_ISend_params(Param_t params, size_t pid, MPI_Comm comm, MPI_Request *req)
Definition: MPIHelper.cpp:105
State_t
Worker state is either idle or running.
Definition: MPIHelper.h:82
@ RUNNING
Definition: MPIHelper.h:82
@ IDLE
Definition: MPIHelper.h:82
void deserialize(char *buffer, Param_t &params)
deserializes params using Boost archive
Definition: MPIHelper.cpp:44
void MPI_Recv_params(Param_t &params, size_t pid, MPI_Comm comm)
Definition: MPIHelper.cpp:128
std::map< std::string, reqVarInfo_t > reqVarContainer_t
Definition: Types.h:79
namedVariableCollection_t Param_t
Definition: Types.h:48