preCICE v3.1.2
Loading...
Searching...
No Matches
mainA.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::vector;
17
20{
21 int rank = utils::IntraComm::getRank();
22
23 static double data_0[] = {10.0, 20.0, 40.0, 80.0};
24 static double data_1[] = {30.0, 50.0, 60.0, 90.0};
25 static double data_2[] = {70.0, 100.0};
26
27 static double *data[] = {data_0, data_1, data_2};
28 static int size[] = {sizeof(data_0) / sizeof(*data_0),
29 sizeof(data_1) / sizeof(*data_1),
30 sizeof(data_2) / sizeof(*data_2)};
31
32 return std::move(vector<double>(data[rank], data[rank] + size[rank]));
33}
34
37{
38 int rank = utils::IntraComm::getRank();
39
40 static double data_0[] = {10.0 + 2, 20.0 + 1, 40.0 + 2, 80.0 + 5};
41 static double data_1[] = {30.0 + 2, 50.0 + 1, 60.0 + 3, 90.0 + 5};
42 static double data_2[] = {70.0 + 3, 100.0 + 5};
43
44 static double *data[] = {data_0, data_1, data_2};
45 static int size[] = {sizeof(data_0) / sizeof(*data_0),
46 sizeof(data_1) / sizeof(*data_1),
47 sizeof(data_2) / sizeof(*data_2)};
48
49 return std::move(vector<double>(data[rank], data[rank] + size[rank]));
50}
51
52bool validate(vector<double> const &data)
53{
54 bool valid = true;
55
56 vector<double> expectedData = getExpectedData();
57
58 if (data.size() != expectedData.size())
59 return false;
60
61 for (int i = 0; i < data.size(); ++i) {
62 valid &= (data[i] == expectedData[i]);
63 }
64
65 return valid;
66}
67
68int main(int argc, char **argv)
69{
70 std::cout << "Running communication dummy\n";
71
72 int provided;
73
74 MPI_Init_thread(&argc, &argv, MPI_THREAD_MULTIPLE, &provided);
75
78
79 if (utils::IntraComm::getSize() != 3) {
80 std::cout << "Please run with 3 mpi processes\n";
81 return 1;
82 }
83
84 if (utils::IntraComm::getRank() == 0) {
87 } else {
90 }
91
93 utils::Parallel::initializeMPI(NULL, NULL);
95 } else {
97 utils::Parallel::initializeMPI(NULL, NULL);
99 }
100
103
104 int rankOffset = 1;
105
107 utils::IntraComm::getCommunication()->acceptConnection(
108 "Primary", "Secondary", utils::IntraComm::getRank(), 1);
109 utils::IntraComm::getCommunication()->setRankOffset(rankOffset);
110 } else {
112 utils::IntraComm::getCommunication()->requestConnection(
113 "Primary",
114 "Secondary",
115 utils::IntraComm::getRank() - rankOffset,
116 utils::IntraComm::getSize() - rankOffset);
117 }
118
119 mesh::PtrMesh mesh(new mesh::Mesh("Mesh", 2, true));
120
122 mesh->setGlobalNumberOfVertices(10);
123
124 mesh->getVertexDistribution()[0].push_back(0);
125 mesh->getVertexDistribution()[0].push_back(1);
126 mesh->getVertexDistribution()[0].push_back(3);
127 mesh->getVertexDistribution()[0].push_back(7);
128
129 mesh->getVertexDistribution()[1].push_back(2);
130 mesh->getVertexDistribution()[1].push_back(4);
131 mesh->getVertexDistribution()[1].push_back(5);
132 mesh->getVertexDistribution()[1].push_back(8);
133
134 mesh->getVertexDistribution()[2].push_back(6);
135 mesh->getVertexDistribution()[2].push_back(9);
136 }
137
140
141 //std::vector<com::PtrCommunicationFactory> cfs(
142 // {com::PtrCommunicationFactory(new com::SocketCommunicationFactory),
143 // com::PtrCommunicationFactory(new com::MPIPortsCommunicationFactory)});
144
145 for (auto cf : cfs) {
147
148 c.requestConnection("B", "A");
149
151 << "Connected!\n";
152
154
155 c.send(data.data(), data.size());
156
157 c.receive(data.data(), data.size());
158
159 if (validate(data))
161 << "Success!\n";
162 else
164 << "Failure!\n";
165
166 cout << "----------\n";
167 }
168
170
171 MPI_Finalize();
172
173 std::cout << "Stop communication dummy\n";
174}
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 requestConnection(std::string const &acceptorName, std::string const &requesterName) override
Requests connection from participant, which has to call acceptConnection().
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 mainA.cpp:52
vector< double > getData()
Definition mainA.cpp:19
int main(int argc, char **argv)
Definition mainA.cpp:68
vector< double > getExpectedData()
Definition mainA.cpp:36
std::shared_ptr< CommunicationFactory > PtrCommunicationFactory
std::shared_ptr< Communication > PtrCommunication
Main namespace of the precice library.
T reset(T... args)
T size(T... args)