preCICE v3.1.2
Loading...
Searching...
No Matches
mainB.cpp
Go to the documentation of this file.
5#include <mesh/Mesh.hpp>
6#include <utils/IntraComm.hpp>
7
8#include <mpi.h>
9
10#include <iostream>
11#include <vector>
12
13using namespace precice;
14
15using std::cout;
16using std::rand;
17using std::vector;
18
21{
22 int rank = utils::IntraComm::getRank();
23
24 static double data_0[] = {rand(), rand()};
25 static double data_1[] = {rand(), rand(), rand()};
26 static double data_2[] = {rand(), rand()};
27 static double *data_3;
28 static double data_4[] = {rand(), rand(), rand()};
29
30 static double *data[] = {data_0, data_1, data_2, data_3, data_4};
31 static int size[] = {sizeof(data_0) / sizeof(*data_0),
32 sizeof(data_1) / sizeof(*data_1),
33 sizeof(data_2) / sizeof(*data_2),
34 0,
35 sizeof(data_4) / sizeof(*data_4)};
36
37 return std::move(vector<double>(data[rank], data[rank] + size[rank]));
38}
39
42{
43 int rank = utils::IntraComm::getRank();
44
45 static double data_0[] = {20.0, 50.0};
46 static double data_1[] = {10.0, 30.0, 40.0};
47 static double data_2[] = {60.0, 70.0};
48 static double *data_3;
49 static double data_4[] = {80.0, 90.0, 100.0};
50
51 static double *data[] = {data_0, data_1, data_2, data_3, data_4};
52 static int size[] = {sizeof(data_0) / sizeof(*data_0),
53 sizeof(data_1) / sizeof(*data_1),
54 sizeof(data_2) / sizeof(*data_2),
55 0,
56 sizeof(data_4) / sizeof(*data_4)};
57
58 return std::move(vector<double>(data[rank], data[rank] + size[rank]));
59}
60
61bool validate(vector<double> const &data)
62{
63 bool valid = true;
64
65 vector<double> expectedData = getExpectedData();
66
67 if (data.size() != expectedData.size())
68 return false;
69
70 for (int i = 0; i < data.size(); ++i) {
71 valid &= (data[i] == expectedData[i]);
72 }
73
74 return valid;
75}
76
78{
79 for (int i = 0; i < data.size(); ++i) {
80 data[i] += utils::IntraComm::getRank() + 1;
81 }
82}
83
84int main(int argc, char **argv)
85{
86 std::cout << "Running communication dummy\n";
87
88 int provided;
89
90 MPI_Init_thread(&argc, &argv, MPI_THREAD_MULTIPLE, &provided);
91
94
95 if (utils::IntraComm::getSize() != 5) {
96 std::cout << "Please run with 5 mpi processes\n";
97 return 1;
98 }
99
100 if (utils::IntraComm::getRank() == 0) {
103 } else {
106 }
107
109 utils::Parallel::initializeMPI(NULL, NULL);
111 } else {
113 utils::Parallel::initializeMPI(NULL, NULL);
115 }
116
119
120 int rankOffset = 1;
121
123 utils::IntraComm::getCommunication()->acceptConnection(
124 "Primary", "Secondary", utils::IntraComm::getRank(), 1);
125 utils::IntraComm::getCommunication()->setRankOffset(rankOffset);
126 } else {
128 utils::IntraComm::getCommunication()->requestConnection(
129 "Primary",
130 "Secondary",
131 utils::IntraComm::getRank() - rankOffset,
132 utils::IntraComm::getSize() - rankOffset);
133 }
134
135 mesh::PtrMesh mesh(new mesh::Mesh("Mesh", 2, true));
136
138 mesh->setGlobalNumberOfVertices(10);
139
140 mesh->getVertexDistribution()[0].push_back(1);
141 mesh->getVertexDistribution()[0].push_back(4);
142
143 mesh->getVertexDistribution()[1].push_back(0);
144 mesh->getVertexDistribution()[1].push_back(2);
145 mesh->getVertexDistribution()[1].push_back(3);
146
147 // mesh->getVertexDistribution()[3].push_back(3);
148
149 mesh->getVertexDistribution()[2].push_back(5);
150 mesh->getVertexDistribution()[2].push_back(6);
151
152 mesh->getVertexDistribution()[4].push_back(7);
153 mesh->getVertexDistribution()[4].push_back(8);
154 mesh->getVertexDistribution()[4].push_back(9);
155 }
156
159
160 //std::vector<com::PtrCommunicationFactory> cfs(
161 // {com::PtrCommunicationFactory(new com::SocketCommunicationFactory),
162 // com::PtrCommunicationFactory(new com::MPIPortsCommunicationFactory)});
163
164 for (auto cf : cfs) {
166
167 c.acceptConnection("B", "A");
168
170 << "Connected!" << '\n';
171
173
174 c.receive(data.data(), data.size());
175
176 if (validate(data))
178 << "Success!" << '\n';
179 else
181 << "Failure!" << '\n';
182
183 process(data);
184
185 c.send(data.data(), data.size());
186
187 cout << "----------" << '\n';
188 }
189
191
192 MPI_Finalize();
193
194 std::cout << "Stop communication dummy\n";
195}
int MPI_Comm_rank(MPI_Comm comm, int *rank)
Definition MPI_Mock.hpp:24
static MPI_Comm MPI_COMM_WORLD
Definition MPI_Mock.hpp:13
int MPI_Comm_size(MPI_Comm comm, int *size)
Definition MPI_Mock.hpp:30
Provides connection methods for processes located in one communicator.
Point-to-point communication implementation of DistributedCommunication.
void receive(precice::span< double > itemsToReceive, int valueDimension=1) override
Receives a subset of local double values corresponding to local indices deduced from the current and ...
void acceptConnection(std::string const &acceptorName, std::string const &requesterName) override
Accepts connection from participant, which has to call requestConnection().
void send(precice::span< double const > itemsToSend, int valueDimension=1) override
Sends a subset of local double values corresponding to local indices deduced from the current and rem...
Container and creator for meshes.
Definition Mesh.hpp:39
static int getSize()
Number of ranks. This includes ranks from both participants, e.g. minimal size is 2.
Definition IntraComm.cpp:47
static Rank getRank()
Current rank.
Definition IntraComm.cpp:42
static bool isPrimary()
True if this process is running the primary rank.
Definition IntraComm.cpp:52
static bool isSecondary()
True if this process is running a secondary rank.
Definition IntraComm.cpp:57
static com::PtrCommunication & getCommunication()
Intra-participant communication.
Definition IntraComm.hpp:31
static void splitCommunicator(std::optional< int > group=std::nullopt)
Splits and creates a local MPI communicator according to groupName.
Definition Parallel.cpp:272
bool validate(vector< double > const &data)
Definition mainB.cpp:61
vector< double > getData()
Definition mainB.cpp:20
int main(int argc, char **argv)
Definition mainB.cpp:84
vector< double > getExpectedData()
Definition mainB.cpp:41
void process(vector< double > &data)
Definition mainB.cpp:77
std::shared_ptr< CommunicationFactory > PtrCommunicationFactory
std::shared_ptr< Communication > PtrCommunication
Main namespace of the precice library.
T rand(T... args)
T reset(T... args)
T size(T... args)