-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathfunction.hpp
More file actions
1318 lines (1155 loc) · 47.9 KB
/
Copy pathfunction.hpp
File metadata and controls
1318 lines (1155 loc) · 47.9 KB
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) 2022 ETH Zurich
// Copyright (C) 2022 UT-Battelle, LLC
// All rights reserved.
//
// See LICENSE for terms of usage.
// See CITATION.md for citation guidelines, if DCA++ is used for scientific publications.
//
// Author: Peter Staar (taa@zurich.ibm.com)
// Urs R. Haehner (haehneru@itp.phys.ethz.ch)
// Giovanni Balduzzi (gbalduzz@itp.phys.ethz.ch)
// Peter Doak (doakpw@ornl.gov)
//
// This class connects the function values to the domains.
//
// TODO: Remame template parameter scalartype --> ElementType.
#ifndef DCA_FUNCTION_FUNCTION_HPP
#define DCA_FUNCTION_FUNCTION_HPP
#include <algorithm> // std::copy_n
#include <cassert>
#include <cmath> // std::abs
#include <complex> // std::abs(std::complex)
#include <iostream>
#include <initializer_list>
#include <stdexcept>
#include <string>
#include <type_traits> // std::is_integral
#include <utility> // std::move, std::swap
#include <vector>
#include "dca/distribution/dist_types.hpp"
#include "dca/function/scalar_cast.hpp"
#include "dca/function/set_to_zero.hpp"
#include "dca/util/pack_operations.hpp"
#include "dca/util/integer_division.hpp"
#include "dca/util/type_utils.hpp"
#include "dca/util/type_help.hpp"
#include "dca/util/to_string.hpp"
namespace dca {
namespace func {
// dca::func::
/** The tensor class used through DCA++.
* This is a pretty complex construct but probably helps keeping track of the large number of
* different tensors over domains in the code. The memory layout is close packed so it is generally
* necessary to copy slices of functions into matrices or vectors for efficient calculation.
*
* First domain is fastest, indexes are in order of domain.
* example:
* func<double, dmn_variadic<dmn_0<4, double>, dmn_0<8, double>> a_func
* 0 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 21
*
* the following is true
* a_func(0,3) == 12
* a_func(3,0) == 3
*
* i.e. row major layout with column first indexing.
*/
template <typename scalartype, class domain, DistType DT = DistType::NONE>
class function;
template <typename scalartype, class domain, DistType DT>
class function {
static const std::string default_name_;
public:
static constexpr DistType dist = DT;
typedef scalartype this_scalar_type;
typedef domain this_domain_type;
// Default constructor
// Constructs the function with the name name.
// Postcondition: All elements are set to zero.
function(const std::string& name = default_name_);
// Distributed function. Access with multi-index operator() is not safe.
template <class Concurrency>
function(const std::string& name, const Concurrency& concurrency);
// Copy constructor
// Constructs the function with the a copy of elements and name of other.
// Precondition: The other function has been resetted, if the domain had been initialized after
// the other function's construction.
function(const function<scalartype, domain, DT>& other);
// Same as above, but with name change from name argument.
function(const function<scalartype, domain, DT>& other, const std::string& name);
template <typename Scalar2>
function(const function<Scalar2, domain, DT>& other);
// Move constructor
// Constructs the function with elements and name of other using move semantics.
// Precondition: The other function has been resetted, if the domain had been initialized after
// the other function's construction.
// Postcondition: The other function is in a non-specified state.
function(function<scalartype, domain, DT>&& other);
function(function<scalartype, domain, DT>&& other, const std::string& name)
: function(std::move(other)) {
name_ = name;
}
// Initializer list constructor
function(std::initializer_list<scalartype> init_list, const std::string& name = default_name_);
// Copy assignment operator
// Replaces the function's elements with a copy of the elements of other.
// Precondition: The other function has been resetted, if the domain had been initialized after
// the other function's construction.
// Postcondition: The function's name is unchanged.
function<scalartype, domain, DT>& operator=(const function<scalartype, domain, DT>& other);
template <typename Scalar2>
function<scalartype, domain, DT>& operator=(const function<Scalar2, domain, DT>& other);
// Move assignment operator
// Replaces the function's elements with those of other using move semantics.
// Precondition: The other function has been resetted, if the domain had been initialized after
// the other function's construction.
// Postconditions: The function's name is unchanged.
// The other function is in a non-specified state.
function<scalartype, domain, DT>& operator=(function<scalartype, domain, DT>&& other);
// Resets the function by resetting the domain object and reallocating the memory for the function
// elements.
// \todo These odd semantics need serious justification.
// Postcondition: All elements are set to zero.
template <class Concurrency>
void reset(const Concurrency& conc);
void reset();
const domain& get_domain() const {
return dmn;
}
const std::string& get_name() const {
return name_;
}
// TODO: Remove this method and use constructor parameter instead.
void set_name(const std::string& name) {
name_ = name;
}
std::size_t get_start() const {
return start_;
}
/** end in sense of last index not 1 past.
*/
std::size_t get_end() const {
assert(end_ > 0);
return end_ - 1;
}
std::vector<size_t> get_start_subindex() const {
return linind_2_subind(start_);
}
/** end in sense of last subindex
*/
std::vector<size_t> get_end_subindex() const {
assert(end_ > 0);
return linind_2_subind(end_ - 1);
}
int signature() const {
return Nb_sbdms;
}
std::size_t size() const {
return fnc_values_.size();
}
// TODO: remove as it breaks class' invariant.
void resize(std::size_t nb_elements_new) {
fnc_values_.resize(nb_elements_new);
}
// void local_resize(std::size_t nb_elements) {}
// Returns the size of the leaf domain with the given index.
// Does not return function values!
// Broken off except on rank 0.
int operator[](const int index) const {
return dmn.get_leaf_domain_sizes()[index];
}
const auto& getDomainSizes() const noexcept {
return dmn.get_leaf_domain_sizes();
}
const std::vector<scalartype>& getValues() const noexcept {
return fnc_values_;
}
// Begin and end methods for compatibility with range for loop.
auto begin() {
return fnc_values_.begin();
}
auto end() {
return fnc_values_.end();
}
auto begin() const {
return fnc_values_.begin();
}
auto end() const {
return fnc_values_.end();
}
// Returns a pointer to the function's elements.
scalartype* values() {
return fnc_values_.data();
}
const scalartype* values() const {
return fnc_values_.data();
}
scalartype* data() {
return fnc_values_.data();
}
const scalartype* data() const {
return fnc_values_.data();
}
//
// Methods for index conversion
//
// Converts the linear index to the corresponding subindices of the leaf domains.
// Pointer version
// Precondition: The size of the array pointed to by subind must be equal to the number of leaf
// domains (Nb_sbdms).
// \todo Replace pointer version with std::array to be able to check subind's size.
// \todo validate or not usage of these for distributed (across MPI) functions, I strongly suspect they are
// not ok./
void linind_2_subind(int linind, int* subind) const;
// std::vector version
void linind_2_subind(int linind, std::vector<int>& subind) const;
template <std::size_t N>
void linind_2_subind(int linind, std::array<int, N>& subind) const;
// modern RVO version
std::vector<size_t> linind_2_subind(int linind) const;
// Computes the linear index for the given subindices of the leaf domains.
// Precondition: subind stores the the subindices of all LEAF domains.
// TODO: Use std::array or std::vector to be able to check the size of subind.
void subind_2_linind(const int* subind, int& linind) const;
// using standard vector and avoiding returning argument
size_t subind_2_linind(const std::vector<int>& subind) const;
// using standard vector and avoiding returning argument
size_t branch_subind_2_linind(const std::vector<int>& subind) const;
// Computes and returns the linear index for the given subindices of the branch or leaf domains,
// depending on the size of subindices.
// Enable only if all arguments are integral to prevent subind_to_linind(int*, int) to resolve to
// subind_to_linind(int...) rather than subind_to_linind(const int* const, int).
template <typename... Ts>
std::enable_if_t<util::ifAll(std::is_integral_v<Ts>...), int> subind_2_linind(
const Ts... subindices) const {
// We need to cast all subindices to the same type for dmn_variadic.
return dmn(static_cast<int>(subindices)...);
}
// TODO: Remove this method.
template <typename T>
int subind_2_linind(const T ind) const {
static_assert(std::is_integral<T>::value, "Index ind must be an integer.");
assert(ind >= 0 && ind < size());
return ind;
}
//
// operator()
//
// TODO: Remove these two methods and use the variadic domains versions instead.
scalartype& operator()(const int* subind);
const scalartype& operator()(const int* subind) const;
template <std::size_t N>
scalartype& operator()(const std::array<int, N>& subind) {
#ifndef NDEBUG
auto linind = dmn.index_by_array(subind);
assert(linind >= 0 && linind < size());
return fnc_values_[linind];
#else
return fnc_values_[dmn.index_by_array(subind)];
#endif
}
const scalartype& operator()(const std::vector<int>& subind) const;
template <typename T, typename = typename std::enable_if<std::is_integral<T>::value, void>::type>
scalartype& operator()(const T linind) {
static_assert(std::is_integral<T>::value, "Index linind must be an integer.");
assert(linind >= 0 && linind < size());
return fnc_values_[linind];
}
template <typename T, typename = typename std::enable_if<std::is_integral<T>::value, void>::type>
const scalartype& operator()(const T linind) const {
static_assert(std::is_integral<T>::value, "Index linind must be an integer.");
assert(linind >= 0 && linind < size());
return fnc_values_[linind];
}
template <typename T, typename... Ts,
typename = typename std::enable_if<std::is_integral<T>::value, void>::type>
scalartype& operator()(const T t, const Ts... subindices) {
// We need to cast all indices to the same type for dmn_variadic.
return fnc_values_[dmn(static_cast<int>(t), static_cast<int>(subindices)...)];
}
template <typename T, typename... Ts,
typename = typename std::enable_if<std::is_integral<T>::value, void>::type>
const scalartype& operator()(const T t, const Ts... subindices) const {
return fnc_values_[dmn(static_cast<int>(t), static_cast<int>(subindices)...)];
}
void operator+=(const function<scalartype, domain, DT>& other);
void operator-=(const function<scalartype, domain, DT>& other);
void operator*=(const function<scalartype, domain, DT>& other);
void operator/=(const function<scalartype, domain, DT>& other);
void operator=(scalartype c);
void operator+=(scalartype c);
void operator-=(scalartype c);
void operator*=(scalartype c);
void operator/=(scalartype c);
// Equal-comparison opertor
// Returns true if the function's elements (fnc_values_) are equal to other's elements, false
// otherwise.
// TODO: Make the equal-comparison operator a non-member function.
bool operator==(const function<scalartype, domain, DT>& other) const;
/** slice across 1 leaf domain
* deprecated, really unsafe pointer use
*/
template <typename new_scalartype>
void slice(int sbdm_index, int* subind, new_scalartype* fnc_vals) const;
/** slice across leaf domains or branch domains based on size of subind.
* \param[in] subind subindex of the slice, index in the slice domain is ignored
*/
template <typename new_scalartype>
void slice(const int sbdm_index, std::vector<int> subind, new_scalartype* fnc_vals) const;
template <typename new_scalartype>
void slice(int sbdm_index_1, int sbdm_index_2, std::vector<int> subind,
new_scalartype* fnc_vals) const;
template <typename new_scalartype>
void slice(int sbdm_index_1, int sbdm_index_2, int* subind, new_scalartype* fnc_vals) const;
/** write a slice to a larger function.
* The thread safety of this is questionable especially if the sbdm_index > 0
* i.e. we are not writing a fastest slice.
*/
template <typename new_scalartype>
void distribute(int sbdm_index, std::vector<int> subind, const new_scalartype* fnc_vals);
template <typename new_scalartype>
void distribute(int sbdm_index, int* subind, const new_scalartype* fnc_vals);
template <typename new_scalartype>
void distribute(int sbdm_index_1, int sbdm_index_2, std::vector<int> subind,
const new_scalartype* fnc_vals);
template <typename new_scalartype>
void distribute(int sbdm_index_1, int sbdm_index_2, int* subind, const new_scalartype* fnc_vals);
//
// Methods for printing
//
// Prints the function's metadata.
void print_fingerprint(std::ostream& stream = std::cout) const;
// Prints the function's elements.
void print_elements(std::ostream& stream = std::cout) const;
//
// Methods for message passing concurrency
//
template <typename concurrency_t>
int get_buffer_size(const concurrency_t& concurrency) const;
template <class concurrency_t>
void pack(const concurrency_t& concurrency, char* buffer, int buffer_size, int& position) const;
// TODO: Make parameter buffer const correct (const char* const buffer).
template <class concurrency_t>
void unpack(const concurrency_t& concurrency, char* buffer, int buffer_size, int& position);
// Gather a function that was initialized as distributed.
// Precondition: concurrency must be the same object used during construction.
template <class Concurrency>
inline function gather(const Concurrency& concurrency) const;
private:
// For DistType::BLOCKED and DistType::LINEAR calculates the local_function_size and sets start_ and end_.
template <class Concurrency>
std::size_t calcDistribution(const Concurrency& concurrency);
std::string name_;
std::string function_type;
domain dmn; // TODO: Remove domain object?
// The subdomains (sbdmn) represent the leaf domains, not the branch domains.
int Nb_sbdms;
std::vector<scalartype> fnc_values_;
// These are the linear start and end indexes with respect to the complete function.
std::size_t start_;
std::size_t end_;
};
template <typename scalartype, class domain, DistType DT>
const std::string function<scalartype, domain, DT>::default_name_ = "no-name";
/** default constructor
*/
template <typename scalartype, class domain, DistType DT>
function<scalartype, domain, DT>::function(const std::string& name)
: name_(name),
function_type(__PRETTY_FUNCTION__),
dmn(),
Nb_sbdms(dmn.get_leaf_domain_sizes().size()) {
if constexpr (dist == DistType::BLOCKED || dist == DistType::LINEAR) {
throw std::runtime_error(
"function named constructor without concurrency reference may only be called for "
"DistType::NONE");
}
start_ = 0;
end_ = dmn.get_size();
// If the function is more than 256 megs report it.
if (end_ > 268435456) {
std::cerr << "function " << name << " allocates " << sizeof(scalartype) * end_ / 1024 / 1024
<< " MB" << '\n';
if (name_ == "no-name")
std::cerr << "large functions need names give yourself a chance.\n";
}
// will zero real or complex values
fnc_values_.resize(dmn.get_size());
}
/** copy constructor
*/
template <typename scalartype, class domain, DistType DT>
function<scalartype, domain, DT>::function(const function<scalartype, domain, DT>& other)
: name_(other.name_),
function_type(__PRETTY_FUNCTION__),
dmn(),
Nb_sbdms(dmn.get_leaf_domain_sizes().size()),
fnc_values_(other.fnc_values_) {
start_ = other.start_;
end_ = other.end_;
}
/** name change copy constructor
*/
template <typename scalartype, class domain, DistType DT>
function<scalartype, domain, DT>::function(const function<scalartype, domain, DT>& other,
const std::string& name)
: name_(name),
function_type(__PRETTY_FUNCTION__),
dmn(),
Nb_sbdms(dmn.get_leaf_domain_sizes().size()),
fnc_values_(other.fnc_values_) {
start_ = other.start_;
end_ = other.end_;
}
/** converting "copy" constructor
*/
template <typename scalartype, class domain, DistType DT>
template <typename Scalar2>
function<scalartype, domain, DT>::function(const function<Scalar2, domain, DT>& other)
: name_(other.get_name()),
function_type(__PRETTY_FUNCTION__),
dmn(),
Nb_sbdms(dmn.get_leaf_domain_sizes().size()),
fnc_values_(dmn.get_size()) {
if (size() != other.size()) {
// The other function has not been resetted after the domain was initialized.
throw std::logic_error("Copy construction from a not yet resetted function.");
}
start_ = other.get_start();
end_ = other.get_end();
std::copy(other.begin(), other.end(), begin());
}
/** move constructor */
template <typename scalartype, class domain, DistType DT>
function<scalartype, domain, DT>::function(function<scalartype, domain, DT>&& other)
: name_(std::move(other.name_)),
function_type(__PRETTY_FUNCTION__),
dmn(),
Nb_sbdms(dmn.get_leaf_domain_sizes().size()),
fnc_values_(std::move(other.fnc_values_)) {
if (dmn.get_size() != other.dmn.get_size())
// The other function has not been reset after the domain was initialized.
throw std::logic_error("Move construction from a not yet resetted function.");
start_ = other.start_;
end_ = other.end_;
}
/** initializer list constructor
* only needed for testing.
*/
template <typename scalartype, class domain, DistType DT>
function<scalartype, domain, DT>::function(std::initializer_list<scalartype> init_list,
const std::string& name)
: name_(name),
function_type(__PRETTY_FUNCTION__),
dmn(),
Nb_sbdms(dmn.get_leaf_domain_sizes().size()) {
start_ = 0;
end_ = dmn.get_size();
fnc_values_.resize(dmn.get_size());
std::copy_n(init_list.begin(), init_list.size(), fnc_values_.begin());
}
template <typename scalartype, class domain, DistType DT>
template <class Concurrency>
std::size_t function<scalartype, domain, DT>::calcDistribution(const Concurrency& concurrency) {
if constexpr (dist == DistType::NONE) {
throw std::logic_error("calcDistritribution should not be called for DistType::NONE function.");
}
else if constexpr (dist == DistType::BLOCKED) {
auto error_bad_block = [](size_t conc_size, domain& dmn) {
std::ostringstream error_message;
double block_size = (dmn.get_size() * sizeof(scalartype)) / 1024 / 1024 / 1024; // gigabytes
error_message << "Blocked concurrency is not possible. Concurrency size: " << conc_size
<< " is not blockwise divisor of function with dimensions:\n"
<< vectorToString(dmn.get_leaf_domain_sizes()) << '\n'
<< "Total Blocked Function Size: " << block_size << "GB\n";
throw std::runtime_error(error_message.str());
};
const std::size_t my_concurrency_id = concurrency.id();
const std::size_t my_concurrency_size = concurrency.number_of_processors();
std::size_t local_function_size = dca::util::ceilDiv(dmn.get_size(), my_concurrency_size);
// This is a necessary but not sufficient proof of "regular blocking"
if (local_function_size * my_concurrency_size != dmn.get_size()) {
error_bad_block(my_concurrency_size, dmn);
}
start_ = local_function_size * my_concurrency_id;
end_ = start_ + local_function_size;
bool regular_local_function_size = false;
size_t remaining_ranks = my_concurrency_size;
for (int idim = (dmn.get_Nb_leaf_domains() - 1); idim >= 0; --idim) {
if (remaining_ranks < dmn.get_subdomain_size(idim)) {
if (dmn.get_subdomain_size(idim) % remaining_ranks != 0) {
break; // i.e no regular bricked blocking
}
else {
regular_local_function_size = true;
break;
}
}
else {
if (remaining_ranks % dmn.get_subdomain_size(idim) != 0) {
break; // i.e no regular bricked blocking
}
else {
remaining_ranks /= dmn.get_subdomain_size(idim);
}
}
}
if (!regular_local_function_size) {
error_bad_block(my_concurrency_size, dmn);
}
return local_function_size;
}
else if constexpr (dist == DistType::LINEAR) {
const std::size_t my_concurrency_id = concurrency.id();
const std::size_t my_concurrency_size = concurrency.number_of_processors();
size_t local_function_size = dca::util::ceilDiv(dmn.get_size(), my_concurrency_size);
size_t residue = dmn.get_size() % my_concurrency_size;
start_ = local_function_size * my_concurrency_id;
if (residue != 0 && my_concurrency_id > residue - 1) {
start_ -= my_concurrency_id - residue;
--local_function_size;
}
end_ = start_ + local_function_size;
return local_function_size;
}
}
/** distributed function constructor
*/
template <typename scalartype, class domain, DistType DT>
template <class Concurrency>
function<scalartype, domain, DT>::function(const std::string& name, const Concurrency& concurrency)
: name_(name),
function_type(__PRETTY_FUNCTION__),
dmn(),
Nb_sbdms(dmn.get_leaf_domain_sizes().size()) {
if constexpr (dist == DistType::NONE) {
const std::size_t nb_elements = dmn.get_size();
try {
fnc_values_.resize(nb_elements);
}
catch (const std::exception& exc) {
std::cout << "exception caught on resize of blocked function: " << exc.what() << '\n';
throw(exc);
}
for (int linind = 0; linind < nb_elements; ++linind)
setToZero(fnc_values_[linind]);
start_ = 0;
end_ = dmn.get_size();
}
else if constexpr (dist == DistType::LINEAR) {
std::size_t local_function_size = calcDistribution(concurrency);
try {
fnc_values_.resize(local_function_size);
}
catch (const std::exception& exc) {
std::cout << "exception caught on resize of linear distributed function: " << exc.what()
<< '\n';
throw(exc);
}
for (int linind = 0; linind < local_function_size; ++linind)
setToZero(fnc_values_[linind]);
}
else if constexpr (dist == DistType::BLOCKED) {
std::size_t local_function_size = calcDistribution(concurrency);
// Ok this can be a blocked function so we finally resize i.e. allocate.
try {
fnc_values_.resize(local_function_size);
}
catch (const std::exception& exc) {
std::cout << "exception caught on resize of blocked function: " << exc.what() << '\n';
throw(exc);
}
for (int linind = 0; linind < local_function_size; ++linind)
setToZero(fnc_values_[linind]);
double block_size = (local_function_size * sizeof(scalartype)) / 1024 / 1024 / 1024; // gigabytes
if (concurrency.id() == 0)
std::cout << "Blocked function " << vectorToString(dmn.get_leaf_domain_sizes()) << '\n'
<< "allocated: " << block_size << "GB\n";
}
}
template <typename scalartype, class domain, DistType DT>
function<scalartype, domain, DT>& function<scalartype, domain, DT>::operator=(
const function<scalartype, domain, DT>& other) {
if (this != &other) {
if constexpr (dist == DistType::NONE) {
if (dmn.get_size() != other.dmn.get_size() || size() != other.size()) {
// Domain had not been initialized when the functions were created.
// Reset this function and check again.
reset();
if (dmn.get_size() != other.dmn.get_size() || size() != other.size())
// The other function has not been resetted after the domain was initialized.
throw std::logic_error("Copy assignment from a not yet resetted function.");
}
}
else if constexpr (dist == DistType::BLOCKED || dist == DistType::LINEAR) {
Nb_sbdms = other.dmn.get_leaf_domain_sizes().size();
start_ = other.start_;
end_ = other.end_;
fnc_values_.resize(other.size());
}
fnc_values_ = other.fnc_values_;
}
return *this;
}
template <typename Scalar, class domain, DistType DT>
template <typename Scalar2>
inline function<Scalar, domain, DT>& function<Scalar, domain, DT>::operator=(
const function<Scalar2, domain, DT>& other) {
if constexpr (std::is_same_v<decltype(*this), decltype(other)>) {
if (this != &other) {
if constexpr (dist == DistType::NONE) {
if (size() != other.size()) {
throw(std::logic_error("Function size does not match."));
}
}
else if constexpr (dist == DistType::LINEAR || dist == DistType::BLOCKED) {
Nb_sbdms = other.dmn.get_leaf_domain_sizes().size();
start_ = other.start_;
end_ = other.end_;
fnc_values_.resize(other.size());
}
fnc_values_ = other.fnc_values_;
}
}
else {
if constexpr (dist == DistType::NONE) {
if (size() != other.size()) {
throw(std::logic_error("Function size does not match."));
}
}
else if constexpr (dist == DistType::LINEAR || dist == DistType::BLOCKED) {
Nb_sbdms = other.dmn.get_leaf_domain_sizes().size();
start_ = other.start_;
end_ = other.end_;
fnc_values_.resize(other.size());
}
auto kConvert = [](auto& kvec) -> std::vector<Scalar> {
std::vector<Scalar> k_converted(kvec.size());
std::transform(kvec.begin(), kvec.end(), k_converted.begin(),
[](auto& val) -> typename decltype(k_converted)::value_type {
return static_cast<typename decltype(k_converted)::value_type>(val);
});
return k_converted;
};
fnc_values_ = kConvert(other.getValues());
}
return *this;
}
template <typename scalartype, class domain, DistType DT>
inline function<scalartype, domain, DT>& function<scalartype, domain, DT>::operator=(
function<scalartype, domain, DT>&& other) {
if (this != &other) {
if constexpr (dist == DistType::NONE) {
if (dmn.get_size() != other.dmn.get_size()) {
// Domain had not been initialized when the functions were created.
// Reset this function and check again.
reset();
if (dmn.get_size() != other.dmn.get_size())
// The other function has not been resetted after the domain was initialized.
throw std::logic_error("Move assignment from a not yet resetted function.");
}
}
else if constexpr (dist == DistType::LINEAR || dist == DistType::BLOCKED) {
Nb_sbdms = other.dmn.get_leaf_domain_sizes().size();
start_ = other.start_;
end_ = other.end_;
}
fnc_values_ = std::move(other.fnc_values_);
}
return *this;
}
template <typename scalartype, class domain, DistType DT>
void function<scalartype, domain, DT>::reset() {
if constexpr (dist != DistType::NONE)
throw std::logic_error("a distributed function must be reset with a concurrency reference");
dmn.reset();
const std::size_t nb_elements = dmn.get_size();
try {
fnc_values_.resize(nb_elements);
}
catch (const std::exception& exc) {
std::cout << "exception caught on resize of non distributed function: " << exc.what() << '\n';
throw(exc);
}
for (int linind = 0; linind < nb_elements; ++linind)
setToZero(fnc_values_[linind]);
start_ = 0;
end_ = dmn.get_size();
}
template <typename scalartype, class domain, DistType DT>
template <class Concurrency>
void function<scalartype, domain, DT>::reset(const Concurrency& concurrency) {
dmn.reset();
if constexpr (dist == DistType::NONE) {
const std::size_t nb_elements = dmn.get_size();
try {
fnc_values_.resize(nb_elements);
}
catch (const std::exception& exc) {
std::cout << "exception caught on resize of non distributed function: " << exc.what() << '\n';
throw(exc);
}
for (int linind = 0; linind < nb_elements; ++linind)
setToZero(fnc_values_[linind]);
start_ = 0;
end_ = dmn.get_size();
}
else if constexpr (dist == DistType::LINEAR) {
size_t local_function_size = calcDistribution();
try {
fnc_values_.resize(local_function_size);
}
catch (const std::exception& exc) {
std::cout << "exception caught on resize of linearly distributed function: " << exc.what()
<< '\n';
throw(exc);
}
for (int linind = 0; linind < local_function_size; ++linind)
setToZero(fnc_values_[linind]);
}
else if constexpr (dist == DistType::BLOCKED) {
size_t local_function_size = calcBlocking(concurrency);
// Ok this can be a blocked function so we finally resize i.e. allocate.
try {
fnc_values_.resize(local_function_size);
}
catch (const std::exception& exc) {
std::cout << "exception caught on resize of blocked function: " << exc.what() << '\n';
throw(exc);
}
for (int linind = 0; linind < local_function_size; ++linind)
setToZero(fnc_values_[linind]);
double block_size = (local_function_size * sizeof(scalartype)) / 1024 / 1024 / 1024; // gigabytes
if (concurrency.id == 0) {
std::cout << "Blocked function " << vectorToString(dmn.get_leaf_domain_sizes()) << '\n'
<< "allocated: " << block_size << "GB\n";
}
}
}
template <typename scalartype, class domain, DistType DT>
void function<scalartype, domain, DT>::linind_2_subind(int linind, int* subind) const {
auto& size_sbdm = dmn.get_leaf_domain_sizes();
for (size_t i = 0; i < size_sbdm.size(); ++i) {
subind[i] = linind % size_sbdm[i];
linind = (linind - subind[i]) / size_sbdm[i];
}
}
template <typename scalartype, class domain, DistType DT>
template <std::size_t N>
void function<scalartype, domain, DT>::linind_2_subind(int linind, std::array<int, N>& subind) const {
assert(N == dmn.get_Nb_branch_domains() || dmn.get_Nb_leaf_domains());
auto& size_sbdm = (N == dmn.get_Nb_branch_domains() ? dmn.get_branch_domain_sizes()
: dmn.get_leaf_domain_sizes());
for (size_t i = 0; i < size_sbdm.size(); ++i) {
subind[i] = linind % size_sbdm[i];
linind = (linind - subind[i]) / size_sbdm[i];
}
}
// TODO: Resize vector if necessary.
template <typename scalartype, class domain, DistType DT>
void function<scalartype, domain, DT>::linind_2_subind(int linind, std::vector<int>& subind) const {
auto& size_sbdm = dmn.get_leaf_domain_sizes();
assert(int(subind.size()) == Nb_sbdms);
for (size_t i = 0; i < Nb_sbdms; ++i) {
subind[i] = linind % size_sbdm[i];
linind = (linind - subind[i]) / size_sbdm[i];
}
}
template <typename scalartype, class domain, DistType DT>
std::vector<size_t> function<scalartype, domain, DT>::linind_2_subind(int linind) const {
std::vector<size_t> subind(Nb_sbdms);
auto& size_sbdm = dmn.get_leaf_domain_sizes();
if constexpr (dist == DistType::NONE) {
for (int i = 0; i < Nb_sbdms; ++i) {
subind[i] = linind % size_sbdm[i];
linind = (linind - subind[i]) / size_sbdm[i];
}
}
else if constexpr (dist == DistType::LINEAR) {
std::cout << "linind:" << linind << '\n';
throw std::runtime_error("Subindices aren't valid accessors for DistType::LINEAR");
}
else if constexpr (dist == DistType::BLOCKED) {
for (int i = 0; i < int(size_sbdm.size()); ++i) {
subind[i] = linind % size_sbdm[i];
linind = (linind - subind[i]) / size_sbdm[i];
}
}
return subind;
}
template <typename scalartype, class domain, DistType DT>
void function<scalartype, domain, DT>::subind_2_linind(const int* const subind, int& linind) const {
auto& step_sbdm = dmn.get_leaf_domain_steps();
linind = 0;
for (int i = 0; i < int(step_sbdm.size()); ++i)
linind += subind[i] * step_sbdm[i];
}
template <typename scalartype, class domain, DistType DT>
size_t function<scalartype, domain, DT>::subind_2_linind(const std::vector<int>& subind) const {
auto& step_sbdm = dmn.get_leaf_domain_steps();
assert(subind.size() == step_sbdm.size());
int linind = 0;
for (int i = 0; i < int(step_sbdm.size()); ++i)
linind += subind[i] * step_sbdm[i];
return linind;
}
template <typename scalartype, class domain, DistType DT>
size_t function<scalartype, domain, DT>::branch_subind_2_linind(const std::vector<int>& subind) const {
auto& branch_sbdm = dmn.get_branch_domain_steps();
assert(subind.size() == branch_sbdm.size());
int linind = 0;
for (int i = 0; i < int(branch_sbdm.size()); ++i)
linind += subind[i] * branch_sbdm[i];
return linind;
}
template <typename scalartype, class domain, DistType DT>
scalartype& function<scalartype, domain, DT>::operator()(const int* const subind) {
auto& step_sbdm = dmn.get_leaf_domain_steps();
int linind;
subind_2_linind(subind, linind);
assert(linind >= 0 && linind < size());
return fnc_values_[linind];
}
template <typename scalartype, class domain, DistType DT>
const scalartype& function<scalartype, domain, DT>::operator()(const int* const subind) const {
int linind;
subind_2_linind(subind, linind);
assert(linind >= 0 && linind < size());
return fnc_values_[linind];
}
template <typename scalartype, class domain, DistType DT>
const scalartype& function<scalartype, domain, DT>::operator()(const std::vector<int>& subind) const {
int linind = 0; // silence warning
if (subind.size() == Nb_sbdms) {
linind = subind_2_linind(subind);
assert(linind >= 0 && linind < size());
}
else if (subind.size() == dmn.get_Nb_branch_domains()) {
linind = branch_subind_2_linind(subind);
}
else
throw std::runtime_error("number of indicies matches neither branches or leaves");
return fnc_values_[linind];
}
template <typename scalartype, class domain, DistType DT>
void function<scalartype, domain, DT>::operator+=(const function<scalartype, domain, DT>& other) {
for (int linind = 0; linind < size(); ++linind)
fnc_values_[linind] += other(linind);
}
template <typename scalartype, class domain, DistType DT>
void function<scalartype, domain, DT>::operator-=(const function<scalartype, domain, DT>& other) {
for (int linind = 0; linind < size(); ++linind)
fnc_values_[linind] -= other(linind);
}
template <typename scalartype, class domain, DistType DT>
void function<scalartype, domain, DT>::operator*=(const function<scalartype, domain, DT>& other) {
for (int linind = 0; linind < size(); ++linind)
fnc_values_[linind] *= other(linind);
}
template <typename scalartype, class domain, DistType DT>
void function<scalartype, domain, DT>::operator/=(const function<scalartype, domain, DT>& other) {
for (int linind = 0; linind < size(); ++linind) {
assert(std::abs(other(linind)) > 1.e-16);
fnc_values_[linind] /= other(linind);
}
}
template <typename scalartype, class domain, DistType DT>
inline void function<scalartype, domain, DT>::operator=(const scalartype c) {
for (int linind = 0; linind < size(); linind++)
fnc_values_[linind] = c;
}
template <typename scalartype, class domain, DistType DT>
void function<scalartype, domain, DT>::operator+=(const scalartype c) {
for (int linind = 0; linind < size(); linind++)
fnc_values_[linind] += c;
}
template <typename scalartype, class domain, DistType DT>
void function<scalartype, domain, DT>::operator-=(const scalartype c) {
for (int linind = 0; linind < size(); linind++)
fnc_values_[linind] -= c;
}
template <typename scalartype, class domain, DistType DT>
void function<scalartype, domain, DT>::operator*=(const scalartype c) {
for (int linind = 0; linind < size(); linind++)
fnc_values_[linind] *= c;
}
template <typename scalartype, class domain, DistType DT>
void function<scalartype, domain, DT>::operator/=(const scalartype c) {
for (int linind = 0; linind < size(); linind++)
fnc_values_[linind] /= c;
}
template <typename scalartype, class domain, DistType DT>
bool function<scalartype, domain, DT>::operator==(const function<scalartype, domain, DT>& other) const {
if (size() != other.size())
// One of the function has not been resetted after the domain was initialized.
throw std::logic_error("Comparing functions of different sizes.");
for (int i = 0; i < size(); ++i)
if (other(i) != fnc_values_[i])
return false;
return true;
}
template <typename scalartype, class domain, DistType DT>
template <typename new_scalartype>
void function<scalartype, domain, DT>::slice(const int sbdm_index, std::vector<int> subind,