-
Notifications
You must be signed in to change notification settings - Fork 707
/
ad_drivers.cc
2186 lines (1886 loc) · 81.3 KB
/
ad_drivers.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
// ------------------------------------------------------------------------
//
// SPDX-License-Identifier: LGPL-2.1-or-later
// Copyright (C) 2019 - 2023 by the deal.II authors
//
// This file is part of the deal.II library.
//
// Part of the source code is dual licensed under Apache-2.0 WITH
// LLVM-exception OR LGPL-2.1-or-later. Detailed license information
// governing the source code and code contributions can be found in
// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
//
// ------------------------------------------------------------------------
#include <deal.II/base/config.h>
#if defined(DEAL_II_WITH_ADOLC) || defined(DEAL_II_TRILINOS_WITH_SACADO)
# include <deal.II/base/exceptions.h>
# include <deal.II/base/types.h>
# include <deal.II/base/utilities.h>
# include <deal.II/differentiation/ad/ad_drivers.h>
# include <deal.II/differentiation/ad/ad_number_traits.h>
# include <deal.II/differentiation/ad/ad_number_types.h>
# include <deal.II/differentiation/ad/adolc_number_types.h>
# include <deal.II/differentiation/ad/sacado_number_types.h>
# include <deal.II/lac/full_matrix.h>
# include <deal.II/lac/vector.h>
# ifdef DEAL_II_WITH_ADOLC
# include <adolc/adolc_fatalerror.h>
# include <adolc/drivers/drivers.h>
# include <adolc/taping.h>
# endif // DEAL_II_WITH_ADOLC
# include <vector>
DEAL_II_NAMESPACE_OPEN
namespace Differentiation
{
namespace AD
{
// ------------- TapedDrivers -------------
template <typename ADNumberType, typename ScalarType, typename T>
bool
TapedDrivers<ADNumberType, ScalarType, T>::is_recording() const
{
AssertThrow(false, ExcRequiresADNumberSpecialization());
return false;
}
template <typename ADNumberType, typename ScalarType, typename T>
typename Types<ADNumberType>::tape_index
TapedDrivers<ADNumberType, ScalarType, T>::active_tape_index() const
{
AssertThrow(false, ExcRequiresADNumberSpecialization());
return Numbers<ADNumberType>::invalid_tape_index;
}
template <typename ADNumberType, typename ScalarType, typename T>
bool
TapedDrivers<ADNumberType, ScalarType, T>::is_registered_tape(
const typename Types<ADNumberType>::tape_index) const
{
AssertThrow(false, ExcRequiresADNumberSpecialization());
return false;
}
template <typename ADNumberType, typename ScalarType, typename T>
bool
TapedDrivers<ADNumberType, ScalarType, T>::keep_independent_values() const
{
AssertThrow(false, ExcRequiresADNumberSpecialization());
return false;
}
template <typename ADNumberType, typename ScalarType, typename T>
void
TapedDrivers<ADNumberType, ScalarType, T>::set_tape_buffer_sizes(
const typename Types<ADNumberType>::tape_buffer_sizes,
const typename Types<ADNumberType>::tape_buffer_sizes,
const typename Types<ADNumberType>::tape_buffer_sizes,
const typename Types<ADNumberType>::tape_buffer_sizes)
{
AssertThrow(false, ExcRequiresADNumberSpecialization());
}
template <typename ADNumberType, typename ScalarType, typename T>
void
TapedDrivers<ADNumberType, ScalarType, T>::start_taping(
const typename Types<ADNumberType>::tape_index,
const bool)
{
AssertThrow(false, ExcRequiresADNumberSpecialization());
}
template <typename ADNumberType, typename ScalarType, typename T>
void
TapedDrivers<ADNumberType, ScalarType, T>::stop_taping(
const typename Types<ADNumberType>::tape_index,
const bool)
{
AssertThrow(false, ExcRequiresADNumberSpecialization());
}
template <typename ADNumberType, typename ScalarType, typename T>
std::vector<typename Types<ADNumberType>::tape_index>
TapedDrivers<ADNumberType, ScalarType, T>::get_registered_tape_indices()
const
{
AssertThrow(false, ExcRequiresADNumberSpecialization());
return std::vector<typename Types<ADNumberType>::tape_index>();
}
template <typename ADNumberType, typename ScalarType, typename T>
void
TapedDrivers<ADNumberType, ScalarType, T>::activate_tape(
const typename Types<ADNumberType>::tape_index)
{
AssertThrow(false, ExcRequiresADNumberSpecialization());
}
template <typename ADNumberType, typename ScalarType, typename T>
bool
TapedDrivers<ADNumberType, ScalarType, T>::requires_retaping(
const typename Types<ADNumberType>::tape_index) const
{
AssertThrow(false, ExcRequiresADNumberSpecialization());
return false;
}
template <typename ADNumberType, typename ScalarType, typename T>
bool
TapedDrivers<ADNumberType, ScalarType, T>::last_action_requires_retaping()
const
{
AssertThrow(false, ExcRequiresADNumberSpecialization());
return false;
}
template <typename ADNumberType, typename ScalarType, typename T>
void
TapedDrivers<ADNumberType, ScalarType, T>::remove_tape(
const typename Types<ADNumberType>::tape_index)
{
AssertThrow(false, ExcRequiresADNumberSpecialization());
}
template <typename ADNumberType, typename ScalarType, typename T>
void
TapedDrivers<ADNumberType, ScalarType, T>::reset(const bool)
{
AssertThrow(false, ExcRequiresADNumberSpecialization());
}
template <typename ADNumberType, typename ScalarType, typename T>
void
TapedDrivers<ADNumberType, ScalarType, T>::print(std::ostream &) const
{
AssertThrow(false, ExcRequiresADNumberSpecialization());
}
template <typename ADNumberType, typename ScalarType, typename T>
void
TapedDrivers<ADNumberType, ScalarType, T>::print_tape_stats(
const typename Types<ADNumberType>::tape_index,
std::ostream &) const
{
AssertThrow(false, ExcRequiresADNumberSpecialization());
}
template <typename ADNumberType, typename ScalarType, typename T>
ScalarType
TapedDrivers<ADNumberType, ScalarType, T>::value(
const typename Types<ADNumberType>::tape_index,
const std::vector<ScalarType> &) const
{
AssertThrow(false, ExcRequiresADNumberSpecialization());
return ScalarType(0.0);
}
template <typename ADNumberType, typename ScalarType, typename T>
void
TapedDrivers<ADNumberType, ScalarType, T>::gradient(
const typename Types<ADNumberType>::tape_index,
const std::vector<ScalarType> &,
Vector<ScalarType> &) const
{
AssertThrow(false, ExcRequiresADNumberSpecialization());
}
template <typename ADNumberType, typename ScalarType, typename T>
void
TapedDrivers<ADNumberType, ScalarType, T>::hessian(
const typename Types<ADNumberType>::tape_index,
const std::vector<ScalarType> &,
FullMatrix<ScalarType> &) const
{
AssertThrow(false, ExcRequiresADNumberSpecialization());
}
template <typename ADNumberType, typename ScalarType, typename T>
void
TapedDrivers<ADNumberType, ScalarType, T>::values(
const typename Types<ADNumberType>::tape_index,
const unsigned int,
const std::vector<ScalarType> &,
Vector<ScalarType> &) const
{
AssertThrow(false, ExcRequiresADNumberSpecialization());
}
template <typename ADNumberType, typename ScalarType, typename T>
void
TapedDrivers<ADNumberType, ScalarType, T>::jacobian(
const typename Types<ADNumberType>::tape_index,
const unsigned int,
const std::vector<ScalarType> &,
FullMatrix<ScalarType> &) const
{
AssertThrow(false, ExcRequiresADNumberSpecialization());
}
# ifdef DEAL_II_WITH_ADOLC
# ifndef DOXYGEN
// Specialization for taped ADOL-C auto-differentiable numbers.
template <typename ADNumberType>
TapedDrivers<ADNumberType,
double,
std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
NumberTypes::adolc_taped>>::TapedDrivers()
: active_tape(Numbers<ADNumberType>::invalid_tape_index)
, keep_values(true)
, is_recording_flag(false)
, use_stored_taped_buffer_sizes(false)
, obufsize(0u)
, lbufsize(0u)
, vbufsize(0u)
, tbufsize(0u)
{}
# endif
template <typename ADNumberType>
bool
TapedDrivers<ADNumberType,
double,
std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
NumberTypes::adolc_taped>>::is_recording()
const
{
return is_recording_flag;
}
template <typename ADNumberType>
typename Types<ADNumberType>::tape_index
TapedDrivers<
ADNumberType,
double,
std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
NumberTypes::adolc_taped>>::active_tape_index() const
{
return active_tape;
}
template <typename ADNumberType>
bool
TapedDrivers<ADNumberType,
double,
std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
NumberTypes::adolc_taped>>::
keep_independent_values() const
{
return keep_values;
}
template <typename ADNumberType>
bool
TapedDrivers<ADNumberType,
double,
std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
NumberTypes::adolc_taped>>::
is_registered_tape(
const typename Types<ADNumberType>::tape_index tape_index) const
{
// Sigh... This is a mess :-/
// The most succinct way to get this piece of information, would be to
// use the getTapeInfos() function, but would come at the expense of
// creating an inactive tape data within ADOL-C's global store. For
// hints as to why this is the way it is, see the getTapeInfos()
// function in
// https://gitlab.com/adol-c/adol-c/blob/master/ADOL-C/src/tape_handling.cpp
// An alternative solution would be to manually access the tape data;
// see the removeTape() function in
// https://gitlab.com/adol-c/adol-c/blob/master/ADOL-C/src/tape_handling.cpp
// with the consideration of the #defines in
// https://gitlab.com/adol-c/adol-c/blob/master/ADOL-C/src/taping_p.h
// as to how this would be performed.
// Doing things "manually" (the second way) without creating the
// additional data object would be a lot more work...
//
// Both of the above solutions would be possible IF ADOL-C exposed
// this data object or a method to access it to the outside world.
// But they don't :-(
// Instead, what we'll have to do is get the statistics for this tape
// and make our own determination as to whether or not this tape exists.
// This effectively executes the first solution, with even more
// overhead! If the tape is in existence, we can assume that it should a
// non-zero number of dependent and independent variables. Those are
// stored in the zeroth and first entries of the statistics vector.
//
// But, oh wait... Surprise! This will trigger an error if the tape
// doesn't exist at all! So lets first check their tape info cache to
// see if the tape REALLY exists (i.e. has been touched, even if nothing
// has been written to it) before trying to access it. It'll only take
// an O(n) search, but at this point who really cares about efficiency?
//
// It has been suggested in
// https://gitlab.com/adol-c/adol-c/issues/11
// that a simply try-catch block around ::tapestats is the solution that
// we want here. Unfortunately this results in unwanted pollution of
// the terminal, of the form
// ADOL-C error: reading integer tape number 4!
// >>> File or directory not found! <<<
// , every time a query is made about a non-existent tape.
// So either way we have to guard that check with something more
// conservative so that we don't output useless messages for our users.
const std::vector<typename Types<ADNumberType>::tape_index>
registered_tape_indices = get_registered_tape_indices();
const auto it = std::find(registered_tape_indices.begin(),
registered_tape_indices.end(),
tape_index);
if (it == registered_tape_indices.end())
return false;
// See https://gitlab.com/adol-c/adol-c/issues/11#note_108341333
try
{
std::vector<std::size_t> counts(STAT_SIZE);
::tapestats(tape_index, counts.data());
return true;
}
catch (const ::FatalError &exc)
{
return false;
}
}
template <typename ADNumberType>
void
TapedDrivers<ADNumberType,
double,
std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
NumberTypes::adolc_taped>>::
set_tape_buffer_sizes(
const typename Types<ADNumberType>::tape_buffer_sizes in_obufsize,
const typename Types<ADNumberType>::tape_buffer_sizes in_lbufsize,
const typename Types<ADNumberType>::tape_buffer_sizes in_vbufsize,
const typename Types<ADNumberType>::tape_buffer_sizes in_tbufsize)
{
// When valid for the chosen AD number type, these values will be used
// the next time start_recording_operations() is called.
obufsize = in_obufsize;
lbufsize = in_lbufsize;
vbufsize = in_vbufsize;
tbufsize = in_tbufsize;
use_stored_taped_buffer_sizes = true;
}
template <typename ADNumberType>
void
TapedDrivers<ADNumberType,
double,
std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
NumberTypes::adolc_taped>>::
start_taping(const typename Types<ADNumberType>::tape_index tape_index,
const bool keep_independent_values)
{
if (use_stored_taped_buffer_sizes)
trace_on(tape_index,
keep_independent_values,
obufsize,
lbufsize,
vbufsize,
tbufsize);
else
trace_on(tape_index, keep_independent_values);
// Set some other flags to their indicated / required values
keep_values = keep_independent_values;
is_recording_flag = true;
}
template <typename ADNumberType>
void
TapedDrivers<ADNumberType,
double,
std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
NumberTypes::adolc_taped>>::
stop_taping(
const typename Types<ADNumberType>::tape_index active_tape_index,
const bool write_tapes_to_file)
{
if (write_tapes_to_file)
trace_off(active_tape_index); // Slow
else
trace_off(); // Fast(er)
// Now that we've turned tracing off, we've definitely
// stopped all tape recording.
is_recording_flag = false;
// If the keep_values flag is set, then we expect the user to use this
// tape immediately after recording it. There is therefore no need to
// invalidate it. However, there is now also no way to double-check
// that the newly recorded tape is indeed the active tape.
if (keep_independent_values() == false)
active_tape = Numbers<ADNumberType>::invalid_tape_index;
}
template <typename ADNumberType>
std::vector<typename Types<ADNumberType>::tape_index>
TapedDrivers<ADNumberType,
double,
std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
NumberTypes::adolc_taped>>::
get_registered_tape_indices() const
{
// We've chosen to use unsigned shorts for the tape
// index type (a safety precaution) so we need to
// perform a conversion between ADOL-C's native tape
// index type and that chosen by us.
std::vector<short> registered_tape_indices_s;
cachedTraceTags(registered_tape_indices_s);
return std::vector<typename Types<ADNumberType>::tape_index>(
registered_tape_indices_s.begin(), registered_tape_indices_s.end());
}
template <typename ADNumberType>
void
TapedDrivers<ADNumberType,
double,
std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
NumberTypes::adolc_taped>>::
activate_tape(const typename Types<ADNumberType>::tape_index tape_index)
{
active_tape = tape_index;
}
template <typename ADNumberType>
bool
TapedDrivers<ADNumberType,
double,
std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
NumberTypes::adolc_taped>>::
requires_retaping(
const typename Types<ADNumberType>::tape_index tape_index) const
{
Assert(
is_registered_tape(tape_index) == true,
ExcMessage(
"Cannot ask for the status of a tape that has not yet been recorded and used."));
const auto it_status_tape = status.find(tape_index);
// This tape's status has not been found in the map. This could be
// because a non-existent tape_index has been used as an argument, or
// because the tape exists but has not been used (in a way that
// initiated a status update). For example, on can create a tape in one
// section of code and then query the status of all existing tapes in a
// completely different section of code that knows nothing about the
// first tape. There is no prerequisite that the first tape is ever
// used, and it can therefore not have a status. So, in this case
// there's not much we can do other than to report that the tape does
// not require retaping.
if (it_status_tape == status.end())
return false;
const auto status_tape = it_status_tape->second;
// See ADOL-C manual section 1.7 and comments in last paragraph of
// section 3.1
Assert(
status_tape < 4 && status_tape >= -2,
ExcMessage(
"The tape status is not within the range specified within the ADOL-C documentation."));
return (status_tape < 0);
}
template <typename ADNumberType>
bool
TapedDrivers<ADNumberType,
double,
std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
NumberTypes::adolc_taped>>::
last_action_requires_retaping() const
{
return requires_retaping(active_tape);
}
template <typename ADNumberType>
void
TapedDrivers<ADNumberType,
double,
std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
NumberTypes::adolc_taped>>::
remove_tape(const typename Types<ADNumberType>::tape_index tape_index)
{
Assert(is_registered_tape(tape_index),
ExcMessage(
"This tape does not exist, and therefore cannot be cleared."));
removeTape(tape_index, TapeRemovalType::ADOLC_REMOVE_COMPLETELY);
}
template <typename ADNumberType>
void
TapedDrivers<ADNumberType,
double,
std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
NumberTypes::adolc_taped>>::
reset(const bool clear_registered_tapes)
{
active_tape = Numbers<ADNumberType>::invalid_tape_index;
is_recording_flag = false;
status.clear();
if (clear_registered_tapes)
{
const std::vector<typename Types<ADNumberType>::tape_index>
registered_tape_indices = get_registered_tape_indices();
for (const auto &tape_index : registered_tape_indices)
remove_tape(tape_index);
}
}
template <typename ADNumberType>
void
TapedDrivers<ADNumberType,
double,
std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
NumberTypes::adolc_taped>>::print(std::ostream
&stream)
const
{
const std::vector<typename Types<ADNumberType>::tape_index>
registered_tape_indices = get_registered_tape_indices();
stream << "Registered tapes and their status: ";
auto it_registered_tape = registered_tape_indices.begin();
for (unsigned int i = 0; i < registered_tape_indices.size();
++i, ++it_registered_tape)
{
const auto tape_index = *it_registered_tape;
const auto it_status_tape = status.find(tape_index);
Assert(it_status_tape != status.end(),
ExcMessage(
"This tape's status has not been found in the map."));
const auto status_tape = it_status_tape->second;
stream << tape_index << "->" << status_tape
<< (i < (registered_tape_indices.size() - 1) ? "," : "");
}
stream << '\n';
stream << "Keep values? " << keep_independent_values() << '\n';
stream << "Use stored tape buffer sizes? "
<< use_stored_taped_buffer_sizes << '\n';
}
template <typename ADNumberType>
void
TapedDrivers<ADNumberType,
double,
std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
NumberTypes::adolc_taped>>::
print_tape_stats(
const typename Types<ADNumberType>::tape_index tape_index,
std::ostream &stream) const
{
// See ADOL-C manual section 2.1
// and adolc/taping.h
std::vector<std::size_t> counts(STAT_SIZE);
::tapestats(tape_index, counts.data());
Assert(counts.size() >= 18, ExcInternalError());
stream
<< "Tape index: " << tape_index << '\n'
<< "Number of independent variables: " << counts[0] << '\n'
<< "Number of dependent variables: " << counts[1] << '\n'
<< "Max number of live, active variables: " << counts[2] << '\n'
<< "Size of taylor stack (number of overwrites): " << counts[3] << '\n'
<< "Operations buffer size: " << counts[4] << '\n'
<< "Total number of recorded operations: " << counts[5] << '\n'
<< "Operations file written or not: " << counts[6] << '\n'
<< "Overall number of locations: " << counts[7] << '\n'
<< "Locations file written or not: " << counts[8] << '\n'
<< "Overall number of values: " << counts[9] << '\n'
<< "Values file written or not: " << counts[10] << '\n'
<< "Locations buffer size: " << counts[11] << '\n'
<< "Values buffer size: " << counts[12] << '\n'
<< "Taylor buffer size: " << counts[13] << '\n'
<< "Number of eq_*_prod for sparsity pattern: " << counts[14] << '\n'
<< "Use of 'min_op', deferred to 'abs_op' for piecewise calculations: "
<< counts[15] << '\n'
<< "Number of 'abs' calls that can switch branch: " << counts[16]
<< '\n'
<< "Number of parameters (doubles) interchangeable without retaping: "
<< counts[17] << '\n'
<< std::flush;
}
template <typename ADNumberType>
typename TapedDrivers<
ADNumberType,
double,
std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
NumberTypes::adolc_taped>>::scalar_type
TapedDrivers<ADNumberType,
double,
std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
NumberTypes::adolc_taped>>::
value(const typename Types<ADNumberType>::tape_index active_tape_index,
const std::vector<scalar_type> &independent_variables) const
{
Assert(is_registered_tape(active_tape_index),
ExcMessage("This tape has not yet been recorded."));
scalar_type value = 0.0;
status[active_tape_index] =
::function(active_tape_index,
1, // Only one dependent variable
independent_variables.size(),
const_cast<double *>(independent_variables.data()),
&value);
return value;
}
template <typename ADNumberType>
void
TapedDrivers<ADNumberType,
double,
std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
NumberTypes::adolc_taped>>::
gradient(const typename Types<ADNumberType>::tape_index active_tape_index,
const std::vector<scalar_type> &independent_variables,
Vector<scalar_type> &gradient) const
{
Assert(AD::ADNumberTraits<ADNumberType>::n_supported_derivative_levels >=
1,
ExcSupportedDerivativeLevels(
AD::ADNumberTraits<ADNumberType>::n_supported_derivative_levels,
1));
Assert(gradient.size() == independent_variables.size(),
ExcDimensionMismatch(gradient.size(),
independent_variables.size()));
Assert(is_registered_tape(active_tape_index),
ExcMessage("This tape has not yet been recorded."));
// Note: ADOL-C's ::gradient function expects a *double as the last
// parameter. Here we take advantage of the fact that the data in the
// Vector class is aligned (e.g. stored as an Array)
status[active_tape_index] =
::gradient(active_tape_index,
independent_variables.size(),
const_cast<scalar_type *>(independent_variables.data()),
gradient.data());
}
template <typename ADNumberType>
void
TapedDrivers<ADNumberType,
double,
std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
NumberTypes::adolc_taped>>::
hessian(const typename Types<ADNumberType>::tape_index active_tape_index,
const std::vector<scalar_type> &independent_variables,
FullMatrix<scalar_type> &hessian) const
{
Assert(AD::ADNumberTraits<ADNumberType>::n_supported_derivative_levels >=
2,
ExcSupportedDerivativeLevels(
AD::ADNumberTraits<ADNumberType>::n_supported_derivative_levels,
2));
Assert(hessian.m() == independent_variables.size(),
ExcDimensionMismatch(hessian.m(), independent_variables.size()));
Assert(hessian.n() == independent_variables.size(),
ExcDimensionMismatch(hessian.n(), independent_variables.size()));
Assert(is_registered_tape(active_tape_index),
ExcMessage("This tape has not yet been recorded."));
const unsigned int n_independent_variables = independent_variables.size();
std::vector<scalar_type *> H(n_independent_variables);
for (unsigned int i = 0; i < n_independent_variables; ++i)
H[i] = &hessian[i][0];
status[active_tape_index] =
::hessian(active_tape_index,
n_independent_variables,
const_cast<scalar_type *>(independent_variables.data()),
H.data());
// ADOL-C builds only the lower-triangular part of the
// symmetric Hessian, so we should copy the relevant
// entries into the upper triangular part.
for (unsigned int i = 0; i < n_independent_variables; ++i)
for (unsigned int j = 0; j < i; ++j)
hessian[j][i] = hessian[i][j]; // Symmetry
}
template <typename ADNumberType>
void
TapedDrivers<ADNumberType,
double,
std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
NumberTypes::adolc_taped>>::
values(const typename Types<ADNumberType>::tape_index active_tape_index,
const unsigned int n_dependent_variables,
const std::vector<scalar_type> &independent_variables,
Vector<scalar_type> &values) const
{
Assert(values.size() == n_dependent_variables,
ExcDimensionMismatch(values.size(), n_dependent_variables));
Assert(is_registered_tape(active_tape_index),
ExcMessage("This tape has not yet been recorded."));
// Note: ADOL-C's ::function function expects a *double as the last
// parameter. Here we take advantage of the fact that the data in the
// Vector class is aligned (e.g. stored as an Array)
status[active_tape_index] =
::function(active_tape_index,
n_dependent_variables,
independent_variables.size(),
const_cast<scalar_type *>(independent_variables.data()),
values.data());
}
template <typename ADNumberType>
void
TapedDrivers<ADNumberType,
double,
std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
NumberTypes::adolc_taped>>::
jacobian(const typename Types<ADNumberType>::tape_index active_tape_index,
const unsigned int n_dependent_variables,
const std::vector<scalar_type> &independent_variables,
FullMatrix<scalar_type> &jacobian) const
{
Assert(AD::ADNumberTraits<ADNumberType>::n_supported_derivative_levels >=
1,
ExcSupportedDerivativeLevels(
AD::ADNumberTraits<ADNumberType>::n_supported_derivative_levels,
1));
Assert(jacobian.m() == n_dependent_variables,
ExcDimensionMismatch(jacobian.m(), n_dependent_variables));
Assert(jacobian.n() == independent_variables.size(),
ExcDimensionMismatch(jacobian.n(), independent_variables.size()));
Assert(is_registered_tape(active_tape_index),
ExcMessage("This tape has not yet been recorded."));
std::vector<scalar_type *> J(n_dependent_variables);
for (unsigned int i = 0; i < n_dependent_variables; ++i)
J[i] = &jacobian[i][0];
status[active_tape_index] = ::jacobian(active_tape_index,
n_dependent_variables,
independent_variables.size(),
independent_variables.data(),
J.data());
}
# else
// Specialization for taped ADOL-C auto-differentiable numbers.
template <typename ADNumberType>
bool
TapedDrivers<ADNumberType,
double,
std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
NumberTypes::adolc_taped>>::is_recording()
const
{
AssertThrow(false, ExcRequiresADOLC());
return false;
}
template <typename ADNumberType>
typename Types<ADNumberType>::tape_index
TapedDrivers<
ADNumberType,
double,
std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
NumberTypes::adolc_taped>>::active_tape_index() const
{
AssertThrow(false, ExcRequiresADOLC());
return Numbers<ADNumberType>::invalid_tape_index;
}
template <typename ADNumberType>
bool
TapedDrivers<ADNumberType,
double,
std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
NumberTypes::adolc_taped>>::
keep_independent_values() const
{
AssertThrow(false, ExcRequiresADOLC());
return false;
}
template <typename ADNumberType>
bool
TapedDrivers<ADNumberType,
double,
std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
NumberTypes::adolc_taped>>::
is_registered_tape(const typename Types<ADNumberType>::tape_index) const
{
AssertThrow(false, ExcRequiresADOLC());
return false;
}
template <typename ADNumberType>
void
TapedDrivers<ADNumberType,
double,
std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
NumberTypes::adolc_taped>>::
set_tape_buffer_sizes(
const typename Types<ADNumberType>::tape_buffer_sizes,
const typename Types<ADNumberType>::tape_buffer_sizes,
const typename Types<ADNumberType>::tape_buffer_sizes,
const typename Types<ADNumberType>::tape_buffer_sizes)
{
AssertThrow(false, ExcRequiresADOLC());
}
template <typename ADNumberType>
void
TapedDrivers<ADNumberType,
double,
std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
NumberTypes::adolc_taped>>::
start_taping(const typename Types<ADNumberType>::tape_index, const bool)
{
AssertThrow(false, ExcRequiresADOLC());
}
template <typename ADNumberType>
void
TapedDrivers<ADNumberType,
double,
std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
NumberTypes::adolc_taped>>::
stop_taping(const typename Types<ADNumberType>::tape_index, const bool)
{
AssertThrow(false, ExcRequiresADOLC());
}
template <typename ADNumberType>
std::vector<typename Types<ADNumberType>::tape_index>
TapedDrivers<ADNumberType,
double,
std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
NumberTypes::adolc_taped>>::
get_registered_tape_indices() const
{
AssertThrow(false, ExcRequiresADOLC());
return std::vector<typename Types<ADNumberType>::tape_index>();
}
template <typename ADNumberType>
void
TapedDrivers<ADNumberType,
double,
std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
NumberTypes::adolc_taped>>::
activate_tape(const typename Types<ADNumberType>::tape_index)
{
AssertThrow(false, ExcRequiresADOLC());
}
template <typename ADNumberType>
bool
TapedDrivers<ADNumberType,
double,
std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
NumberTypes::adolc_taped>>::
requires_retaping(const typename Types<ADNumberType>::tape_index) const
{
AssertThrow(false, ExcRequiresADOLC());
return false;
}
template <typename ADNumberType>
bool
TapedDrivers<ADNumberType,
double,
std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
NumberTypes::adolc_taped>>::
last_action_requires_retaping() const
{
AssertThrow(false, ExcRequiresADOLC());
return false;
}
template <typename ADNumberType>
void
TapedDrivers<ADNumberType,
double,
std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
NumberTypes::adolc_taped>>::
remove_tape(const typename Types<ADNumberType>::tape_index)
{
AssertThrow(false, ExcRequiresADOLC());
}
template <typename ADNumberType>
void
TapedDrivers<ADNumberType,
double,
std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
NumberTypes::adolc_taped>>::reset(const bool)
{
AssertThrow(false, ExcRequiresADOLC());
}
template <typename ADNumberType>
void
TapedDrivers<ADNumberType,
double,
std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
NumberTypes::adolc_taped>>::print(std::ostream
&) const
{
AssertThrow(false, ExcRequiresADOLC());
}