OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
 
Loading...
Searching...
No Matches
main.cpp
1#include "Grid/grid_dist_id.hpp"
2#include "data_type/aggregate.hpp"
3#include "Decomposition/CartDecomposition.hpp"
4#include "VCluster/VCluster.hpp"
5
27int main(int argc, char* argv[])
28{
43
44 openfpm_init(&argc,&argv);
45
47
63
64 Vcluster<> & v_cl = create_vcluster();
65 long int N_prc = v_cl.getProcessingUnits();
66
68
91
92 long int id = v_cl.getProcessUnitID();
93
94 v_cl.max(id);
95 v_cl.execute();
96 if (v_cl.getProcessUnitID() == 0)
97 std::cout << "Maximum processor rank: " << id << "\n";
98
100
116
117 size_t id2 = v_cl.getProcessUnitID();
118
119 v_cl.sum(id2);
120 v_cl.execute();
121 if (v_cl.getProcessUnitID() == 0)
122 std::cout << "Sum of all processors rank: " << id2 << "\n";
123
125
139
140 long int id3 = v_cl.getProcessUnitID();
142
143 v_cl.allGather(id3,v);
144 v_cl.execute();
145
146 if (v_cl.getProcessUnitID() == 0)
147 {
148 std::cout << "Collected ids: ";
149 for(size_t i = 0 ; i < v.size() ; i++)
150 std::cout << " " << v.get(i) << " ";
151
152 std::cout << "\n";
153 }
154
156
173
174 // Create 2 messages with and hello message inside
175 std::stringstream ss_message_1;
176 std::stringstream ss_message_2;
177 ss_message_1 << "Hello from " << std::setw(8) << v_cl.getProcessUnitID() << "\n";
178 ss_message_2 << "Hello from " << std::setw(8) << v_cl.getProcessUnitID() << "\n";
179 std::string message_1 = ss_message_1.str();
180 std::string message_2 = ss_message_2.str();
181 size_t msg_size = message_1.size();
182
183 // Processor 0 send to processors 1,2 , 1 to 2,1, 2 to 0,1
184
185 // send the message
186 v_cl.send(((id3+1)%N_prc + N_prc)%N_prc,0,message_1.c_str(),msg_size);
187 v_cl.send(((id3+2)%N_prc + N_prc)%N_prc,0,message_2.c_str(),msg_size);
188
189 // create the receiving buffer
191 v_one.resize(msg_size);
192 openfpm::vector<char> v_two(msg_size);
193 v_two.resize(msg_size);
194
195 // Processor 0 receive from 1,2 ...
196
197 v_cl.recv(((id3-1)%N_prc + N_prc)%N_prc,0,(void *)v_one.getPointer(),msg_size);
198 v_cl.recv(((id3-2)%N_prc + N_prc)%N_prc,0,(void *)v_two.getPointer(),msg_size);
199 v_cl.execute();
200
201 // Processor 0 print the received message
202 if (v_cl.getProcessUnitID() == 0)
203 {
204 for (size_t i = 0 ; i < msg_size ; i++)
205 std::cout << v_one.get(i);
206
207 for (size_t i = 0 ; i < msg_size ; i++)
208 std::cout << v_two.get(i);
209 }
210
212
227
228 // Get the rank of the processor and put this rank in one variable
229 id = v_cl.getProcessUnitID();
230 id2 = v_cl.getProcessUnitID();
231 id3 = v_cl.getProcessUnitID();
232 v.clear();
233
234 // convert the string into a vector
235 openfpm::vector<char> message_1_v(msg_size);
236 openfpm::vector<char> message_2_v(msg_size);
237
238 for (size_t i = 0 ; i < msg_size ; i++)
239 message_1_v.get(i) = message_1[i];
240
241 for (size_t i = 0 ; i < msg_size ; i++)
242 message_2_v.get(i) = message_2[i];
243
244 // Calculate the maximin across all the rank
245 v_cl.max(id);
246 // Calculate the sum across all the rank
247 v_cl.sum(id2);
248 // all processor send one number, all processor receive all numbers
249 v_cl.allGather(id3,v);
250
251 // in the case of vector we have special functions that avoid to specify the size
252 v_cl.send(((id+1)%N_prc + N_prc)%N_prc,0,message_1_v);
253 v_cl.send(((id+2)%N_prc + N_prc)%N_prc,0,message_2_v);
254 v_cl.recv(((id-1)%N_prc + N_prc)%N_prc,0,v_one);
255 v_cl.recv(((id-2)%N_prc + N_prc)%N_prc,0,v_two);
256 v_cl.execute();
257
258 // Only processor one print the received data
259 if (v_cl.getProcessUnitID() == 1)
260 {
261 std::cout << "Maximum processor rank: " << id << "\n";
262 std::cout << "Sum of all processors rank: " << id << "\n";
263
264 std::cout << "Collected ids: ";
265 for(size_t i = 0 ; i < v.size() ; i++)
266 std::cout << " " << v.get(i) << " ";
267
268 std::cout << "\n";
269
270 for (size_t i = 0 ; i < msg_size ; i++)
271 std::cout << v_one.get(i);
272
273 for (size_t i = 0 ; i < msg_size ; i++)
274 std::cout << v_two.get(i);
275 }
276
278
291
292 openfpm_finalize();
293
295
304}
void execute()
Execute all the requests.
void sum(T &num)
Sum the numbers across all processors and get the result.
size_t getProcessUnitID()
Get the process unit id.
size_t getProcessingUnits()
Get the total number of processors.
bool recv(size_t proc, size_t tag, void *v, size_t sz)
Recv data from a processor.
bool allGather(T &send, openfpm::vector< T, Mem, gr > &v)
Gather the data from all processors.
void max(T &num)
Get the maximum number across all processors (or reduction with infinity norm)
bool send(size_t proc, size_t tag, const void *mem, size_t sz)
Send data to a processor.
Implementation of VCluster class.
Definition VCluster.hpp:59
Implementation of 1-D std::vector like structure.