/
mpi.cc
1258 lines (1013 loc) · 38.7 KB
/
mpi.cc
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ---------------------------------------------------------------------
//
// Copyright (C) 2005 - 2021 by the deal.II authors
//
// This file is part of the deal.II library.
//
// The deal.II library is free software; you can use it, redistribute
// it, and/or modify it under the terms of the GNU Lesser General
// Public License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// The full text of the license can be found in the file LICENSE.md at
// the top level directory of deal.II.
//
// ---------------------------------------------------------------------
#include <deal.II/base/exceptions.h>
#include <deal.II/base/index_set.h>
#include <deal.II/base/mpi.h>
#include <deal.II/base/mpi.templates.h>
#include <deal.II/base/mpi_compute_index_owner_internal.h>
#include <deal.II/base/mpi_tags.h>
#include <deal.II/base/multithread_info.h>
#include <deal.II/base/utilities.h>
#include <deal.II/lac/la_parallel_block_vector.h>
#include <deal.II/lac/la_parallel_vector.h>
#include <deal.II/lac/vector_memory.h>
#include <iostream>
#include <numeric>
#include <set>
#include <vector>
#ifdef DEAL_II_WITH_TRILINOS
# ifdef DEAL_II_WITH_MPI
# include <deal.II/lac/trilinos_parallel_block_vector.h>
# include <deal.II/lac/trilinos_vector.h>
# include <Epetra_MpiComm.h>
# endif
#endif
#ifdef DEAL_II_WITH_PETSC
# include <deal.II/lac/petsc_block_vector.h>
# include <deal.II/lac/petsc_vector.h>
# include <petscsys.h>
#endif
#ifdef DEAL_II_WITH_SLEPC
# include <deal.II/lac/slepc_solver.h>
# include <slepcsys.h>
#endif
#ifdef DEAL_II_WITH_P4EST
# include <p4est_bits.h>
#endif
#ifdef DEAL_II_TRILINOS_WITH_ZOLTAN
# include <zoltan_cpp.h>
#endif
DEAL_II_NAMESPACE_OPEN
namespace Utilities
{
IndexSet
create_evenly_distributed_partitioning(const unsigned int my_partition_id,
const unsigned int n_partitions,
const IndexSet::size_type total_size)
{
const unsigned int remain = total_size % n_partitions;
const IndexSet::size_type min_size = total_size / n_partitions;
const IndexSet::size_type begin =
min_size * my_partition_id + std::min(my_partition_id, remain);
const IndexSet::size_type end =
min_size * (my_partition_id + 1) + std::min(my_partition_id + 1, remain);
IndexSet result(total_size);
result.add_range(begin, end);
return result;
}
namespace MPI
{
MinMaxAvg
min_max_avg(const double my_value, const MPI_Comm &mpi_communicator)
{
MinMaxAvg result;
min_max_avg(ArrayView<const double>(my_value),
ArrayView<MinMaxAvg>(result),
mpi_communicator);
return result;
}
std::vector<MinMaxAvg>
min_max_avg(const std::vector<double> &my_values,
const MPI_Comm & mpi_communicator)
{
std::vector<MinMaxAvg> results(my_values.size());
min_max_avg(my_values, results, mpi_communicator);
return results;
}
#ifdef DEAL_II_WITH_MPI
unsigned int
n_mpi_processes(const MPI_Comm &mpi_communicator)
{
int n_jobs = 1;
const int ierr = MPI_Comm_size(mpi_communicator, &n_jobs);
AssertThrowMPI(ierr);
return n_jobs;
}
unsigned int
this_mpi_process(const MPI_Comm &mpi_communicator)
{
int rank = 0;
const int ierr = MPI_Comm_rank(mpi_communicator, &rank);
AssertThrowMPI(ierr);
return rank;
}
const std::vector<unsigned int>
mpi_processes_within_communicator(const MPI_Comm &comm_large,
const MPI_Comm &comm_small)
{
if (Utilities::MPI::job_supports_mpi() == false)
return std::vector<unsigned int>{0};
const unsigned int rank = Utilities::MPI::this_mpi_process(comm_large);
const unsigned int size = Utilities::MPI::n_mpi_processes(comm_small);
std::vector<unsigned int> ranks(size);
const int ierr = MPI_Allgather(
&rank, 1, MPI_UNSIGNED, ranks.data(), 1, MPI_UNSIGNED, comm_small);
AssertThrowMPI(ierr);
return ranks;
}
MPI_Comm
duplicate_communicator(const MPI_Comm &mpi_communicator)
{
MPI_Comm new_communicator;
const int ierr = MPI_Comm_dup(mpi_communicator, &new_communicator);
AssertThrowMPI(ierr);
return new_communicator;
}
void
free_communicator(MPI_Comm &mpi_communicator)
{
// MPI_Comm_free will set the argument to MPI_COMM_NULL automatically.
const int ierr = MPI_Comm_free(&mpi_communicator);
AssertThrowMPI(ierr);
}
int
create_group(const MPI_Comm & comm,
const MPI_Group &group,
const int tag,
MPI_Comm * new_comm)
{
# if DEAL_II_MPI_VERSION_GTE(3, 0)
return MPI_Comm_create_group(comm, group, tag, new_comm);
# else
int rank;
int ierr = MPI_Comm_rank(comm, &rank);
AssertThrowMPI(ierr);
int grp_rank;
ierr = MPI_Group_rank(group, &grp_rank);
AssertThrowMPI(ierr);
if (grp_rank == MPI_UNDEFINED)
{
*new_comm = MPI_COMM_NULL;
return MPI_SUCCESS;
}
int grp_size;
ierr = MPI_Group_size(group, &grp_size);
AssertThrowMPI(ierr);
ierr = MPI_Comm_dup(MPI_COMM_SELF, new_comm);
AssertThrowMPI(ierr);
MPI_Group parent_grp;
ierr = MPI_Comm_group(comm, &parent_grp);
AssertThrowMPI(ierr);
std::vector<int> pids(grp_size);
std::vector<int> grp_pids(grp_size);
std::iota(grp_pids.begin(), grp_pids.end(), 0);
ierr = MPI_Group_translate_ranks(
group, grp_size, grp_pids.data(), parent_grp, pids.data());
AssertThrowMPI(ierr);
ierr = MPI_Group_free(&parent_grp);
AssertThrowMPI(ierr);
MPI_Comm comm_old = *new_comm;
MPI_Comm ic;
for (int merge_sz = 1; merge_sz < grp_size; merge_sz *= 2)
{
const int gid = grp_rank / merge_sz;
comm_old = *new_comm;
if (gid % 2 == 0)
{
if ((gid + 1) * merge_sz < grp_size)
{
ierr = (MPI_Intercomm_create(
*new_comm, 0, comm, pids[(gid + 1) * merge_sz], tag, &ic));
AssertThrowMPI(ierr);
ierr = MPI_Intercomm_merge(ic, 0 /* LOW */, new_comm);
AssertThrowMPI(ierr);
}
}
else
{
ierr = MPI_Intercomm_create(
*new_comm, 0, comm, pids[(gid - 1) * merge_sz], tag, &ic);
AssertThrowMPI(ierr);
ierr = MPI_Intercomm_merge(ic, 1 /* HIGH */, new_comm);
AssertThrowMPI(ierr);
}
if (*new_comm != comm_old)
{
ierr = MPI_Comm_free(&ic);
AssertThrowMPI(ierr);
ierr = MPI_Comm_free(&comm_old);
AssertThrowMPI(ierr);
}
}
return MPI_SUCCESS;
# endif
}
std::vector<IndexSet>
create_ascending_partitioning(const MPI_Comm & comm,
const IndexSet::size_type locally_owned_size)
{
const unsigned int n_proc = n_mpi_processes(comm);
const std::vector<IndexSet::size_type> sizes =
all_gather(comm, locally_owned_size);
const auto total_size =
std::accumulate(sizes.begin(), sizes.end(), IndexSet::size_type(0));
std::vector<IndexSet> res(n_proc, IndexSet(total_size));
IndexSet::size_type begin = 0;
for (unsigned int i = 0; i < n_proc; ++i)
{
res[i].add_range(begin, begin + sizes[i]);
begin = begin + sizes[i];
}
return res;
}
IndexSet
create_evenly_distributed_partitioning(const MPI_Comm & comm,
const IndexSet::size_type total_size)
{
const unsigned int this_proc = this_mpi_process(comm);
const unsigned int n_proc = n_mpi_processes(comm);
return Utilities::create_evenly_distributed_partitioning(this_proc,
n_proc,
total_size);
}
/**
* A re-implementation of compute_point_to_point_communication_pattern
* using a ConsensusAlgorithm.
*/
class ConsensusAlgorithmsProcessTargets
: public ConsensusAlgorithms::Process<unsigned int, unsigned int>
{
public:
ConsensusAlgorithmsProcessTargets(const std::vector<unsigned int> &target)
: target(target)
{}
using T1 = unsigned int;
using T2 = unsigned int;
virtual void
answer_request(const unsigned int other_rank,
const std::vector<T1> &,
std::vector<T2> &) override
{
this->sources.push_back(other_rank);
}
/**
* Simply return the user-provided list.
*
* @return List of processes this process wants to send requests to.
*/
virtual std::vector<unsigned int>
compute_targets() override
{
return target;
}
/**
* The result of the consensus algorithm.
* @return Sorted list of ranks of processes wanting to send a request to
* this process.
*/
std::vector<unsigned int>
get_result()
{
std::sort(sources.begin(), sources.end());
return sources;
}
private:
/**
* List of processes this process wants to send requests to.
*/
const std::vector<unsigned int> ⌖
/**
* List of ranks of processes wanting to send a request to this process.
*/
std::vector<unsigned int> sources;
};
std::vector<unsigned int>
compute_point_to_point_communication_pattern(
const MPI_Comm & mpi_comm,
const std::vector<unsigned int> &destinations)
{
const unsigned int myid = Utilities::MPI::this_mpi_process(mpi_comm);
const unsigned int n_procs = Utilities::MPI::n_mpi_processes(mpi_comm);
(void)myid;
(void)n_procs;
for (const unsigned int destination : destinations)
{
(void)destination;
AssertIndexRange(destination, n_procs);
}
# if DEAL_II_MPI_VERSION_GTE(3, 0)
ConsensusAlgorithmsProcessTargets process(destinations);
ConsensusAlgorithms::NBX<ConsensusAlgorithmsProcessTargets::T1,
ConsensusAlgorithmsProcessTargets::T2>
consensus_algorithm(process, mpi_comm);
consensus_algorithm.run();
return process.get_result();
# elif DEAL_II_MPI_VERSION_GTE(2, 2)
static CollectiveMutex mutex;
CollectiveMutex::ScopedLock lock(mutex, mpi_comm);
const int mpi_tag =
internal::Tags::compute_point_to_point_communication_pattern;
// Calculate the number of messages to send to each process
std::vector<unsigned int> dest_vector(n_procs);
for (const auto &el : destinations)
++dest_vector[el];
// Find how many processes will send to this one
// by reducing with sum and then scattering the
// results over all processes
unsigned int n_recv_from;
const int ierr = MPI_Reduce_scatter_block(
dest_vector.data(), &n_recv_from, 1, MPI_UNSIGNED, MPI_SUM, mpi_comm);
AssertThrowMPI(ierr);
// Send myid to every process in `destinations` vector...
std::vector<MPI_Request> send_requests(destinations.size());
for (const auto &el : destinations)
{
const int ierr =
MPI_Isend(&myid,
1,
MPI_UNSIGNED,
el,
mpi_tag,
mpi_comm,
send_requests.data() + (&el - destinations.data()));
AssertThrowMPI(ierr);
}
// Receive `n_recv_from` times from the processes
// who communicate with this one. Store the obtained id's
// in the resulting vector
std::vector<unsigned int> origins(n_recv_from);
for (auto &el : origins)
{
const int ierr = MPI_Recv(&el,
1,
MPI_UNSIGNED,
MPI_ANY_SOURCE,
mpi_tag,
mpi_comm,
MPI_STATUS_IGNORE);
AssertThrowMPI(ierr);
}
if (destinations.size() > 0)
{
const int ierr = MPI_Waitall(destinations.size(),
send_requests.data(),
MPI_STATUSES_IGNORE);
AssertThrowMPI(ierr);
}
return origins;
# else
// let all processors communicate the maximal number of destinations
// they have
const unsigned int max_n_destinations =
Utilities::MPI::max(destinations.size(), mpi_comm);
if (max_n_destinations == 0)
// all processes have nothing to send/receive:
return std::vector<unsigned int>();
// now that we know the number of data packets every processor wants to
// send, set up a buffer with the maximal size and copy our destinations
// in there, padded with -1's
std::vector<unsigned int> my_destinations(max_n_destinations,
numbers::invalid_unsigned_int);
std::copy(destinations.begin(),
destinations.end(),
my_destinations.begin());
// now exchange these (we could communicate less data if we used
// MPI_Allgatherv, but we'd have to communicate my_n_destinations to all
// processors in this case, which is more expensive than the reduction
// operation above in MPI_Allreduce)
std::vector<unsigned int> all_destinations(max_n_destinations * n_procs);
const int ierr = MPI_Allgather(my_destinations.data(),
max_n_destinations,
MPI_UNSIGNED,
all_destinations.data(),
max_n_destinations,
MPI_UNSIGNED,
mpi_comm);
AssertThrowMPI(ierr);
// now we know who is going to communicate with whom. collect who is
// going to communicate with us!
std::vector<unsigned int> origins;
for (unsigned int i = 0; i < n_procs; ++i)
for (unsigned int j = 0; j < max_n_destinations; ++j)
if (all_destinations[i * max_n_destinations + j] == myid)
origins.push_back(i);
else if (all_destinations[i * max_n_destinations + j] ==
numbers::invalid_unsigned_int)
break;
return origins;
# endif
}
unsigned int
compute_n_point_to_point_communications(
const MPI_Comm & mpi_comm,
const std::vector<unsigned int> &destinations)
{
const unsigned int n_procs = Utilities::MPI::n_mpi_processes(mpi_comm);
for (const unsigned int destination : destinations)
{
(void)destination;
AssertIndexRange(destination, n_procs);
Assert(destination != Utilities::MPI::this_mpi_process(mpi_comm),
ExcMessage(
"There is no point in communicating with ourselves."));
}
// Calculate the number of messages to send to each process
std::vector<unsigned int> dest_vector(n_procs);
for (const auto &el : destinations)
++dest_vector[el];
# if DEAL_II_MPI_VERSION_GTE(2, 2)
// Find out how many processes will send to this one
// MPI_Reduce_scatter(_block) does exactly this
unsigned int n_recv_from = 0;
const int ierr = MPI_Reduce_scatter_block(
dest_vector.data(), &n_recv_from, 1, MPI_UNSIGNED, MPI_SUM, mpi_comm);
AssertThrowMPI(ierr);
return n_recv_from;
# else
// Find out how many processes will send to this one
// by reducing with sum and then scattering the
// results over all processes
std::vector<unsigned int> buffer(dest_vector.size());
unsigned int n_recv_from = 0;
int ierr = MPI_Reduce(dest_vector.data(),
buffer.data(),
dest_vector.size(),
MPI_UNSIGNED,
MPI_SUM,
0,
mpi_comm);
AssertThrowMPI(ierr);
ierr = MPI_Scatter(buffer.data(),
1,
MPI_UNSIGNED,
&n_recv_from,
1,
MPI_UNSIGNED,
0,
mpi_comm);
AssertThrowMPI(ierr);
return n_recv_from;
# endif
}
namespace
{
// custom MIP_Op for calculate_collective_mpi_min_max_avg
void
max_reduce(const void *in_lhs_,
void * inout_rhs_,
int * len,
MPI_Datatype *)
{
const MinMaxAvg *in_lhs = static_cast<const MinMaxAvg *>(in_lhs_);
MinMaxAvg * inout_rhs = static_cast<MinMaxAvg *>(inout_rhs_);
for (int i = 0; i < *len; i++)
{
inout_rhs[i].sum += in_lhs[i].sum;
if (inout_rhs[i].min > in_lhs[i].min)
{
inout_rhs[i].min = in_lhs[i].min;
inout_rhs[i].min_index = in_lhs[i].min_index;
}
else if (inout_rhs[i].min == in_lhs[i].min)
{
// choose lower cpu index when tied to make operator commutative
if (inout_rhs[i].min_index > in_lhs[i].min_index)
inout_rhs[i].min_index = in_lhs[i].min_index;
}
if (inout_rhs[i].max < in_lhs[i].max)
{
inout_rhs[i].max = in_lhs[i].max;
inout_rhs[i].max_index = in_lhs[i].max_index;
}
else if (inout_rhs[i].max == in_lhs[i].max)
{
// choose lower cpu index when tied to make operator commutative
if (inout_rhs[i].max_index > in_lhs[i].max_index)
inout_rhs[i].max_index = in_lhs[i].max_index;
}
}
}
} // namespace
void
min_max_avg(const ArrayView<const double> &my_values,
const ArrayView<MinMaxAvg> & result,
const MPI_Comm & mpi_communicator)
{
// If MPI was not started, we have a serial computation and cannot run
// the other MPI commands
if (job_supports_mpi() == false ||
Utilities::MPI::n_mpi_processes(mpi_communicator) <= 1)
{
for (unsigned int i = 0; i < my_values.size(); i++)
{
result[i].sum = my_values[i];
result[i].avg = my_values[i];
result[i].min = my_values[i];
result[i].max = my_values[i];
result[i].min_index = 0;
result[i].max_index = 0;
}
return;
}
/*
* A custom MPI datatype handle describing the memory layout of the
* MinMaxAvg struct. Initialized on first pass control reaches the
* static variable. So hopefully not initialized too early.
*/
static MPI_Datatype type = []() {
MPI_Datatype type;
int lengths[] = {3, 2, 1};
MPI_Aint displacements[] = {0,
offsetof(MinMaxAvg, min_index),
offsetof(MinMaxAvg, avg)};
MPI_Datatype types[] = {MPI_DOUBLE, MPI_INT, MPI_DOUBLE};
int ierr =
MPI_Type_create_struct(3, lengths, displacements, types, &type);
AssertThrowMPI(ierr);
ierr = MPI_Type_commit(&type);
AssertThrowMPI(ierr);
/* Ensure that we free the allocated datatype again at the end of
* the program run just before we call MPI_Finalize():*/
MPI_InitFinalize::signals.at_mpi_finalize.connect([type]() mutable {
int ierr = MPI_Type_free(&type);
AssertThrowMPI(ierr);
});
return type;
}();
/*
* A custom MPI op handle for our max_reduce function.
* Initialized on first pass control reaches the static variable. So
* hopefully not initialized too early.
*/
static MPI_Op op = []() {
MPI_Op op;
int ierr =
MPI_Op_create(reinterpret_cast<MPI_User_function *>(&max_reduce),
true,
&op);
AssertThrowMPI(ierr);
/* Ensure that we free the allocated op again at the end of the
* program run just before we call MPI_Finalize():*/
MPI_InitFinalize::signals.at_mpi_finalize.connect([op]() mutable {
int ierr = MPI_Op_free(&op);
AssertThrowMPI(ierr);
});
return op;
}();
AssertDimension(Utilities::MPI::min(my_values.size(), mpi_communicator),
Utilities::MPI::max(my_values.size(), mpi_communicator));
AssertDimension(my_values.size(), result.size());
// To avoid uninitialized values on some MPI implementations, provide
// result with a default value already...
MinMaxAvg dummy = {0.,
std::numeric_limits<double>::max(),
-std::numeric_limits<double>::max(),
0,
0,
0.};
for (auto &i : result)
i = dummy;
const unsigned int my_id =
dealii::Utilities::MPI::this_mpi_process(mpi_communicator);
const unsigned int numproc =
dealii::Utilities::MPI::n_mpi_processes(mpi_communicator);
std::vector<MinMaxAvg> in(my_values.size());
for (unsigned int i = 0; i < my_values.size(); i++)
{
in[i].sum = in[i].min = in[i].max = my_values[i];
in[i].min_index = in[i].max_index = my_id;
}
int ierr = MPI_Allreduce(
in.data(), result.data(), my_values.size(), type, op, mpi_communicator);
AssertThrowMPI(ierr);
for (auto &r : result)
r.avg = r.sum / numproc;
}
#else
unsigned int
n_mpi_processes(const MPI_Comm &)
{
return 1;
}
unsigned int
this_mpi_process(const MPI_Comm &)
{
return 0;
}
const std::vector<unsigned int>
mpi_processes_within_communicator(const MPI_Comm &, const MPI_Comm &)
{
return std::vector<unsigned int>{0};
}
std::vector<IndexSet>
create_ascending_partitioning(const MPI_Comm & /*comm*/,
const IndexSet::size_type locally_owned_size)
{
return std::vector<IndexSet>(1, complete_index_set(locally_owned_size));
}
IndexSet
create_evenly_distributed_partitioning(const MPI_Comm & /*comm*/,
const IndexSet::size_type total_size)
{
return complete_index_set(total_size);
}
MPI_Comm
duplicate_communicator(const MPI_Comm &mpi_communicator)
{
return mpi_communicator;
}
void
free_communicator(MPI_Comm & /*mpi_communicator*/)
{}
void
min_max_avg(const ArrayView<const double> &my_values,
const ArrayView<MinMaxAvg> & result,
const MPI_Comm &)
{
AssertDimension(my_values.size(), result.size());
for (unsigned int i = 0; i < my_values.size(); i++)
{
result[i].sum = my_values[i];
result[i].avg = my_values[i];
result[i].min = my_values[i];
result[i].max = my_values[i];
result[i].min_index = 0;
result[i].max_index = 0;
}
}
#endif
/* Force initialization of static struct: */
MPI_InitFinalize::Signals MPI_InitFinalize::signals =
MPI_InitFinalize::Signals();
MPI_InitFinalize::MPI_InitFinalize(int & argc,
char **& argv,
const unsigned int max_num_threads)
{
static bool constructor_has_already_run = false;
(void)constructor_has_already_run;
Assert(constructor_has_already_run == false,
ExcMessage("You can only create a single object of this class "
"in a program since it initializes the MPI system."));
int ierr = 0;
#ifdef DEAL_II_WITH_MPI
// if we have PETSc, we will initialize it and let it handle MPI.
// Otherwise, we will do it.
int MPI_has_been_started = 0;
ierr = MPI_Initialized(&MPI_has_been_started);
AssertThrowMPI(ierr);
AssertThrow(MPI_has_been_started == 0,
ExcMessage("MPI error. You can only start MPI once!"));
int provided;
// this works like ierr = MPI_Init (&argc, &argv); but tells MPI that
// we might use several threads but never call two MPI functions at the
// same time. For an explanation see on why we do this see
// http://www.open-mpi.org/community/lists/users/2010/03/12244.php
int wanted = MPI_THREAD_SERIALIZED;
ierr = MPI_Init_thread(&argc, &argv, wanted, &provided);
AssertThrowMPI(ierr);
// disable for now because at least some implementations always return
// MPI_THREAD_SINGLE.
// Assert(max_num_threads==1 || provided != MPI_THREAD_SINGLE,
// ExcMessage("MPI reports that we are not allowed to use multiple
// threads."));
#else
// make sure the compiler doesn't warn about these variables
(void)argc;
(void)argv;
(void)ierr;
#endif
// we are allowed to call MPI_Init ourselves and PETScInitialize will
// detect this. This allows us to use MPI_Init_thread instead.
#ifdef DEAL_II_WITH_PETSC
# ifdef DEAL_II_WITH_SLEPC
// Initialize SLEPc (with PETSc):
ierr = SlepcInitialize(&argc, &argv, nullptr, nullptr);
AssertThrow(ierr == 0, SLEPcWrappers::SolverBase::ExcSLEPcError(ierr));
# else
// or just initialize PETSc alone:
ierr = PetscInitialize(&argc, &argv, nullptr, nullptr);
AssertThrow(ierr == 0, ExcPETScError(ierr));
# endif
// Disable PETSc exception handling. This just prints a large wall
// of text that is not particularly helpful for what we do:
PetscPopSignalHandler();
#endif
// Initialize zoltan
#ifdef DEAL_II_TRILINOS_WITH_ZOLTAN
float version;
Zoltan_Initialize(argc, argv, &version);
#endif
#ifdef DEAL_II_WITH_P4EST
// Initialize p4est and libsc components
# if DEAL_II_P4EST_VERSION_GTE(2, 0, 0, 0)
# else
// This feature is broken in version 2.0.0 for calls to
// MPI_Comm_create_group (see cburstedde/p4est#30).
// Disabling it leads to more verbose p4est error messages
// which should be fine.
sc_init(MPI_COMM_WORLD, 0, 0, nullptr, SC_LP_SILENT);
# endif
p4est_init(nullptr, SC_LP_SILENT);
#endif
constructor_has_already_run = true;
// Now also see how many threads we'd like to run
if (max_num_threads != numbers::invalid_unsigned_int)
{
// set maximum number of threads (also respecting the environment
// variable that the called function evaluates) based on what the
// user asked
MultithreadInfo::set_thread_limit(max_num_threads);
}
else
// user wants automatic choice
{
#ifdef DEAL_II_WITH_MPI
// we need to figure out how many MPI processes there are on the
// current node, as well as how many CPU cores we have. for the
// first task, check what get_hostname() returns and then do an
// allgather so each processor gets the answer
//
// in calculating the length of the string, don't forget the
// terminating \0 on C-style strings
const std::string hostname = Utilities::System::get_hostname();
const unsigned int max_hostname_size =
Utilities::MPI::max(hostname.size() + 1, MPI_COMM_WORLD);
std::vector<char> hostname_array(max_hostname_size);
std::copy(hostname.c_str(),
hostname.c_str() + hostname.size() + 1,
hostname_array.begin());
std::vector<char> all_hostnames(max_hostname_size *
MPI::n_mpi_processes(MPI_COMM_WORLD));
const int ierr = MPI_Allgather(hostname_array.data(),
max_hostname_size,
MPI_CHAR,
all_hostnames.data(),
max_hostname_size,
MPI_CHAR,
MPI_COMM_WORLD);
AssertThrowMPI(ierr);
// search how often our own hostname appears and the how-manyth
// instance the current process represents
unsigned int n_local_processes = 0;
unsigned int nth_process_on_host = 0;
for (unsigned int i = 0; i < MPI::n_mpi_processes(MPI_COMM_WORLD);
++i)
if (std::string(all_hostnames.data() + i * max_hostname_size) ==
hostname)
{
++n_local_processes;
if (i <= MPI::this_mpi_process(MPI_COMM_WORLD))
++nth_process_on_host;
}
Assert(nth_process_on_host > 0, ExcInternalError());
// compute how many cores each process gets. if the number does not
// divide evenly, then we get one more core if we are among the
// first few processes
//
// if the number would be zero, round up to one since every process
// needs to have at least one thread
const unsigned int n_threads =
std::max(MultithreadInfo::n_cores() / n_local_processes +
(nth_process_on_host <=
MultithreadInfo::n_cores() % n_local_processes ?
1 :
0),
1U);
#else
const unsigned int n_threads = MultithreadInfo::n_cores();
#endif
// finally set this number of threads
MultithreadInfo::set_thread_limit(n_threads);
}
// As a final step call the at_mpi_init() signal handler.
signals.at_mpi_init();
}
void
MPI_InitFinalize::register_request(MPI_Request &request)
{
// insert if it is not in the set already:
requests.insert(&request);
}
void
MPI_InitFinalize::unregister_request(MPI_Request &request)
{
Assert(
requests.find(&request) != requests.end(),
ExcMessage(
"You tried to call unregister_request() with an invalid request."));
requests.erase(&request);
}
std::set<MPI_Request *> MPI_InitFinalize::requests;
MPI_InitFinalize::~MPI_InitFinalize()
{
// First, call the at_mpi_finalize() signal handler.
signals.at_mpi_finalize();
// make memory pool release all PETSc/Trilinos/MPI-based vectors that
// are no longer used at this point. this is relevant because the static
// object destructors run for these vectors at the end of the program
// would run after MPI_Finalize is called, leading to errors