forked from ChristophKirst/SimKernel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsim_mpi.cpp
105 lines (80 loc) · 2.87 KB
/
sim_mpi.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/***********************************************************************
sim_mpi.cpp - MPI - routines used by Sim / SimControll
Christoph Kirst
Max Planck Institue for Dynamics and Self-Organisation
HU Berlin, BCCN Göttingen & Berlin (2008)
************************************************************************/
#include "sim_mpi.h"
#include "mpi.h"
void sim_send_signal(int sig, int it, int to)
{
SIM_MPI_DEBUG("sim_send_signal sig=" << sig, "it=" <<it <<" to="<< to)
int comm[2];
comm[0] = sig; comm[1] = it;
MPI::COMM_WORLD.Send(comm, 2, MPI::INT, to, SIM_MPI_TAG);
};
void sim_recv_signal(int& sig, int&it, int& from)
{
SIM_MPI_DEBUG("sim_recv_signal from=", from)
int comm[2];
MPI::Status status;
MPI::COMM_WORLD.Recv(comm, 2, MPI::INT, from, SIM_MPI_TAG, status);
sig = comm[0];
it = comm[1];
from = status.Get_source();
SIM_MPI_DEBUG("sim_recv_signal recvied: sig="<<sig<<" it="<<it, "from="<<from)
};
void sim_send_message(std::string msg, int to)
{
SIM_MPI_DEBUG("sim_send_message to=" << to, "msg="<< msg.c_str())
int length = msg.length();
MPI::COMM_WORLD.Send(&length, 1, MPI::INT, to, SIM_MPI_TAG);
MPI::COMM_WORLD.Send(msg.c_str(), length, MPI::CHAR, to, SIM_MPI_TAG);
};
void sim_recv_message(std::string& msg, int& from)
{
SIM_MPI_DEBUG("sim_recv_message from=", from)
MPI::Status status;
int length;
MPI::COMM_WORLD.Recv(&length, 1, MPI::INT, from, SIM_MPI_TAG, status);
SIM_MPI_DEBUG("sim_recv_message length="<<length, " from="<<(status.Get_source()))
char m[length];
MPI::COMM_WORLD.Recv(m, length, MPI::CHAR, from, SIM_MPI_TAG, status);
msg = std::string(m, length);
from = status.Get_source();
SIM_MPI_DEBUG("sim_recv_message recieved: from="<<from, "msg="<<msg)
};
void sim_send_init(int niter, int to)
{
SIM_MPI_DEBUG("sim_send_init to="<<to, "niter="<<niter)
MPI::COMM_WORLD.Send(&niter, 1, MPI::INT, to, SIM_MPI_TAG);
};
void sim_recv_init(int& niter, int from)
{
MPI::Status status;
MPI::COMM_WORLD.Recv(&niter, 1, MPI::INT, from, SIM_MPI_TAG, status);
SIM_MPI_DEBUG("sim_recv_init from="<<from, "niter="<<niter)
};
void sim_send_do(int iter, int to)
{
SIM_MPI_DEBUG("sim_send_do to="<<to, "iter="<<iter)
MPI::COMM_WORLD.Send(&iter, 1, MPI::INT, to, SIM_MPI_TAG);
};
void sim_recv_do(int& iter, int from)
{
MPI::Status status;
MPI::COMM_WORLD.Recv(&iter, 1, MPI::INT, from, SIM_MPI_TAG, status);
SIM_MPI_DEBUG("sim_recv_do from="<<from, "iter="<<iter)
};
void sim_send_finalize(int rank, int to)
{
SIM_MPI_DEBUG("sim_send_finalize to="<<to, "rank="<<rank)
MPI::COMM_WORLD.Send(&rank, 1, MPI::INT, to, SIM_MPI_TAG);
};
void sim_recv_finalize(int& rank, int from)
{
MPI::Status status;
MPI::COMM_WORLD.Recv(&rank, 1, MPI::INT, from, SIM_MPI_TAG, status);
SIM_MPI_DEBUG("sim_recv_finalize from="<<from, "rank="<<rank)
};