OPAL (Object Oriented Parallel Accelerator Library) 2024.2
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 <cstddef>
25#include <cstring>
26#include <sstream>
27
28#include <mpi.h>
29
30#include "Util/Types.h"
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)
80
82enum State_t {IDLE = 0, RUNNING = 1};
83
85void serialize(Param_t params, std::ostringstream &os);
86void serialize(reqVarContainer_t params, std::ostringstream &os);
87
90void deserialize(const char *buffer, std::size_t buf_size, Param_t &params);
91void deserialize(const char *buffer, std::size_t buf_size, reqVarContainer_t &params);
92
94void MPI_Bcast_params(Param_t &params, std::size_t root, MPI_Comm comm);
95
97void MPI_Bcast_reqvars(reqVarContainer_t reqvars, std::size_t root, MPI_Comm comm);
98
99
100//FIXME: test
101template<class Data_t>
102void MPI_Send_serialized(Data_t data, std::size_t pid, MPI_Comm comm) {
103
104 std::ostringstream os;
105 serialize(data, os);
106 std::size_t buf_size = os.str().length();
107
108 MPI_Send(&buf_size, 1, MPI_LONG, pid,
110
111 char *buffer = new char[buf_size];
112 std::memcpy(buffer, os.str().c_str(), buf_size);
113
114 MPI_Send(buffer, buf_size, MPI_CHAR, pid,
116
117 delete[] buffer;
118}
119
120template<class Data_t>
121void MPI_Recv_serialized(Data_t &data, std::size_t pid, MPI_Comm comm) {
122
123 MPI_Status status;
124 std::size_t buf_size = 0;
125 MPI_Recv(&buf_size, 1, MPI_LONG, pid,
126 MPI_EXCHANGE_SERIALIZED_DATA_TAG, comm, &status);
127
128 char *buffer = new char[buf_size];
129 MPI_Recv(buffer, buf_size, MPI_CHAR, pid,
130 MPI_EXCHANGE_SERIALIZED_DATA_TAG, comm, &status);
131
132 deserialize(buffer, buf_size, data);
133 delete[] buffer;
134}
135
136
144void MPI_Send_params(Param_t params, std::size_t pid, MPI_Comm comm);
145
156std::pair<std::size_t*, char*> MPI_ISend_params(Param_t params, std::size_t pid,
157 MPI_Comm comm, MPI_Request *req);
158
166void MPI_Recv_params(Param_t &params, std::size_t pid, MPI_Comm comm);
167
175void MPI_Send_reqvars(reqVarContainer_t reqvars, std::size_t pid, MPI_Comm comm);
176
185void MPI_Recv_reqvars(reqVarContainer_t &reqvars, std::size_t pid, MPI_Comm comm);
186
187#endif
std::map< std::string, reqVarInfo_t > reqVarContainer_t
Definition Types.h:96
namedVariableCollection_t Param_t
Definition Types.h:52
#define MPI_OPT_JOB_FINISHED_TAG
pilot tells optimizer that results are ready to collect
Definition MPIHelper.h:46
#define MPI_EXCHANGE_SOL_STATE_RES_SIZE_TAG
Definition MPIHelper.h:60
#define MPI_WORKER_FINISHED_TAG
notify pilot that work has been finished and results are ready to collect
Definition MPIHelper.h:35
void MPI_Send_reqvars(reqVarContainer_t reqvars, std::size_t pid, MPI_Comm comm)
std::pair< std::size_t *, char * > MPI_ISend_params(Param_t params, std::size_t pid, MPI_Comm comm, MPI_Request *req)
#define MPI_EXCHANGE_SOL_STATE_TAG
Definition MPIHelper.h:58
void MPI_Bcast_reqvars(reqVarContainer_t reqvars, std::size_t root, MPI_Comm comm)
broadcast requested variables to all entities in comm
void serialize(Param_t params, std::ostringstream &os)
serializes params to a text stream, replaces boost::serialization
Definition MPIHelper.cpp:46
#define MPI_OPT_CONVERGED_TAG
optimizer notifies pilot that optimization has converged (EXIT)
Definition MPIHelper.h:48
#define MPI_WORKER_STATUSUPDATE_TAG
notify pilot about worker status
Definition MPIHelper.h:33
void MPI_Recv_serialized(Data_t &data, std::size_t pid, MPI_Comm comm)
Definition MPIHelper.h:121
#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
void MPI_Send_params(Param_t params, std::size_t pid, MPI_Comm comm)
void MPI_Send_serialized(Data_t data, std::size_t pid, MPI_Comm comm)
Definition MPIHelper.h:102
State_t
Worker state is either idle or running.
Definition MPIHelper.h:82
@ RUNNING
Definition MPIHelper.h:82
@ IDLE
Definition MPIHelper.h:82
void MPI_Recv_reqvars(reqVarContainer_t &reqvars, std::size_t pid, MPI_Comm comm)
void MPI_Recv_params(Param_t &params, std::size_t pid, MPI_Comm comm)
void deserialize(const char *buffer, std::size_t buf_size, Param_t &params)
Definition MPIHelper.cpp:68
void MPI_Bcast_params(Param_t &params, std::size_t root, MPI_Comm comm)
broadcast params to all entities in comm