-
Notifications
You must be signed in to change notification settings - Fork 81
/
tmb_core.hpp
1488 lines (1374 loc) · 49.2 KB
/
tmb_core.hpp
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) 2013-2015 Kasper Kristensen
// License: GPL-2
/** \file
* \brief Interfaces to R and CppAD
*/
/*
Call to external C++ code can potentially result in exeptions that
will crash R. However, we do not want R to crash on failed memory
allocations. Therefore:
* All interface functions (those called with .Call from R) must have
TMB_TRY wrapped around CppAD/Eigen code that allocates memory.
* Special attention must be payed to parallel code, as each thread
is responsible for catching its own exceptions.
*/
#define TMB_TRY try
#define TMB_CATCH catch(std::bad_alloc& ba)
#define TMB_ERROR_BAD_ALLOC error("Memory allocation fail in function '%s'\n", \
__FUNCTION__)
/* Memory manager:
Count the number of external pointers alive.
When total number is zero it is safe to dyn.unload
the library.
*/
/** \brief TMB: SEXP type */
struct SEXP_t{
SEXP value; /**< \brief SEXP_t: data entry*/
SEXP_t(SEXP x)CSKIP({value=x;}) /**< \brief SEXP_t: assignment*/
SEXP_t()CSKIP({value=R_NilValue;}) /**< \brief SEXP_t: default constructor*/
operator SEXP()CSKIP({return value;}) /**< \brief SEXP_t:*/
};
bool operator<(SEXP_t x, SEXP_t y)CSKIP({return (size_t(x.value)<size_t(y.value));})
/** \brief Controls the life span of objects created in the C++ template (jointly R/C++)*/
struct memory_manager_struct{
int counter; /**< \brief Number of objects alive that "memory_manager_struct" has allocated */
std::map<SEXP_t,SEXP_t> alive;
/** \brief Register "list" in memory_manager_struct */
void RegisterCFinalizer(SEXP list);
/** \brief Revmoves "x" from memory_manager_struct */
void CallCFinalizer(SEXP x);
void clear();
memory_manager_struct();
};
#ifndef WITH_LIBTMB
void memory_manager_struct::RegisterCFinalizer(SEXP list){
counter++;
SEXP x=VECTOR_ELT(list,0);
alive[x]=list;
}
void memory_manager_struct::CallCFinalizer(SEXP x){
counter--;
alive.erase(x);
}
void memory_manager_struct::clear(){
std::map<SEXP_t,SEXP_t>::iterator it;
SEXP list;
for(it = alive.begin(); it != alive.end(); it++){
list=(*it).second;
SET_VECTOR_ELT(list,0,R_NilValue);
}
}
memory_manager_struct::memory_manager_struct(){
counter=0;
}
#endif
TMB_EXTERN memory_manager_struct memory_manager;
/** \brief Convert x to TMB-format for R/C++ communication
All external pointers returned from TMB should be placed in a
list container of length one. Additional information should be set
as attributes to the pointer. The memory_manager_struct above knows
how to look up the list container given the external pointer. By
setting the list element to NULL the memory_manager can trigger the
garbage collector (and thereby the finalizers) when the library is
unloaded.
*/
#ifdef WITH_LIBTMB
SEXP ptrList(SEXP x);
#else
SEXP ptrList(SEXP x)
{
SEXP ans,names;
PROTECT(ans=allocVector(VECSXP,1));
PROTECT(names=allocVector(STRSXP,1));
SET_VECTOR_ELT(ans,0,x);
SET_STRING_ELT(names,0,mkChar("ptr"));
setAttrib(ans,R_NamesSymbol,names);
memory_manager.RegisterCFinalizer(ans);
UNPROTECT(2);
return ans;
}
#endif
extern "C"{
#ifdef LIB_UNLOAD
#include <R_ext/Rdynload.h>
void LIB_UNLOAD(DllInfo *dll)
{
if(memory_manager.counter>0)Rprintf("Warning: %d external pointers will be removed\n",memory_manager.counter);
memory_manager.clear();
for(int i=0;i<1000;i++){ // 122 seems to be sufficient.
if(memory_manager.counter>0){
R_gc();
R_RunExitFinalizers();
}
}
if(memory_manager.counter>0)error("Failed to clean. Please manually clean up before unloading\n");
}
#endif
}
#ifdef _OPENMP
TMB_EXTERN bool _openmp CSKIP( =true; )
#else
TMB_EXTERN bool _openmp CSKIP( =false; )
#endif
/** \brief Call the optimize method of an ADFun object pointer. */
template<class ADFunPointer>
void optimizeTape(ADFunPointer pf){
if(!config.optimize.instantly){
/* Drop out */
return;
}
if (!config.optimize.parallel){
#ifdef _OPENMP
#pragma omp critical
#endif
{ /* Avoid multiple tape optimizations at the same time (to reduce memory) */
if(config.trace.optimize)std::cout << "Optimizing tape... ";
pf->optimize();
if(config.trace.optimize)std::cout << "Done\n";
}
}
else
{ /* Allow multiple tape optimizations at the same time */
if(config.trace.optimize)std::cout << "Optimizing tape... ";
pf->optimize();
if(config.trace.optimize)std::cout << "Done\n";
}
}
/* Helpers, to check that data and parameters are of the right types.
"RObjectTester" denotes the type of a pointer to a test function.
Examples of test functions are "isMatrix", "isArray", "isNumeric",
etc (see Rinternals.h).
*/
typedef Rboolean (*RObjectTester)(SEXP);
#ifdef WITH_LIBTMB
void RObjectTestExpectedType(SEXP x, RObjectTester expectedtype, const char *nam);
Rboolean isValidSparseMatrix(SEXP x);
Rboolean isNumericScalar(SEXP x);
#else
void RObjectTestExpectedType(SEXP x, RObjectTester expectedtype, const char *nam){
if(expectedtype != NULL){
if(!expectedtype(x)){
if(isNull(x)){
warning("Expected object. Got NULL.");
}
error("Error when reading the variable: '%s'. Please check data and parameters.",nam);
}
}
}
Rboolean isValidSparseMatrix(SEXP x){
if(!inherits(x,"dgTMatrix"))warning("Expected sparse matrix of class 'dgTMatrix'.");
return inherits(x,"dgTMatrix");
}
Rboolean isNumericScalar(SEXP x){
if(LENGTH(x)!=1){
warning("Expected scalar. Got length=%i",LENGTH(x));
return FALSE;
}
return isNumeric(x);
}
#endif
/* Macros to obtain data and parameters from R */
/** \brief Get parameter matrix from R and declare it as matrix<Type>
\ingroup macros */
#define PARAMETER_MATRIX(name) tmbutils::matrix<Type> name(objective_function::fillShape(asMatrix<Type>(objective_function::getShape(#name,&isMatrix)),#name));
/** \brief Get parameter vector from R and declare it as vector<Type>
\ingroup macros*/
#define PARAMETER_VECTOR(name) vector<Type> name(objective_function::fillShape(asVector<Type>(objective_function::getShape(#name,&isNumeric)),#name));
/** \brief Get parameter scalar from R and declare it as Type
\ingroup macros */
#define PARAMETER(name) Type name(objective_function::fillShape(asVector<Type>(objective_function::getShape(#name,&isNumericScalar)),#name)[0]);
/** \brief Get data vector from R and declare it as vector<Type>
\note If name is found in the parameter list it will be read as a
parameter vector.
\ingroup macros */
#define DATA_VECTOR(name) \
vector<Type> name; \
if (!isNull(getListElement(objective_function::parameters,#name))) { \
name = objective_function::fillShape(asVector<Type>( \
objective_function::getShape(#name,&isNumeric)),#name); \
} else { \
name = asVector<Type>(getListElement( \
objective_function::data,#name,&isNumeric)); \
}
/** \brief Get data matrix from R and declare it as matrix<Type>
\ingroup macros */
#define DATA_MATRIX(name) matrix<Type> name(asMatrix<Type>( \
getListElement(objective_function::data,#name,&isMatrix)));
/** \brief Get data scalar from R and declare it as Type
\ingroup macros */
#define DATA_SCALAR(name) Type name(asVector<Type>( \
getListElement(objective_function::data,#name,&isNumericScalar))[0]);
/** \brief Get data scalar from R and declare it as int
\ingroup macros */
#define DATA_INTEGER(name) int name(CppAD::Integer(asVector<Type>( \
getListElement(objective_function::data,#name,&isNumericScalar))[0]));
/** \brief Get data vector of type "factor" from R and declare it as a zero-based integer vector.
The following example (R code) shows what you have on the R side and what is
being received by the C++ template:
\verbatim
> x=factor(letters[4:10])
> x
[1] d e f g h i j
Levels: d e f g h i j
# The zero-based integer vector that the C++ template sees
> unclass(x) - 1
[1] 0 1 2 3 4 5 6
\endverbatim
\ingroup macros*/
#define DATA_FACTOR(name) vector<int> name(asVector<int>( \
getListElement(objective_function::data,#name,&isNumeric)));
/** \brief Get data vector of type "integer" from R and declare it vector<int>. (DATA_INTEGER is for a scalar integer) \ingroup macros*/
#define DATA_IVECTOR(name) vector<int> name(asVector<int>( \
getListElement(objective_function::data,#name,&isNumeric)));
/** \brief Get the number of levels of a data factor from R \ingroup macros */
#define NLEVELS(name) LENGTH(getAttrib(getListElement(this->data,#name),install("levels")))
/** \brief Get sparse matrix from R and declare it as Eigen::SparseMatrix<Type> \ingroup macros*/
#define DATA_SPARSE_MATRIX(name) Eigen::SparseMatrix<Type> name(tmbutils::asSparseMatrix<Type>( \
getListElement(objective_function::data,#name,&isValidSparseMatrix)));
// NOTE: REPORT() constructs new SEXP so never report in parallel!
/** \brief Report scalar, vector or array back to R without derivative information. Important: \c REPORT(name) must not be used before \c name has been assigned a value \ingroup macros */
#define REPORT(name) if(isDouble<Type>::value && this->current_parallel_region<0){ \
defineVar(install(#name),asSEXP(name),objective_function::report);}
/** \brief Report scalar, vector or array back to R with derivative information.
The result is retrieved in R via the R function \c sdreport().
Important: \c ADREPORT(name) must not be used before \c name has been assigned a value \ingroup macros*/
#define ADREPORT(name) objective_function::reportvector.push(name,#name);
#define PARALLEL_REGION if(this->parallel_region())
/** \brief Get data array from R and declare it as array<Type>
\note If name is found in the parameter list it will be read as a
parameter array.
\ingroup macros*/
#define DATA_ARRAY(name) \
tmbutils::array<Type> name; \
if (!isNull(getListElement(objective_function::parameters,#name))) { \
name = objective_function::fillShape(tmbutils::asArray<Type>( \
objective_function::getShape(#name,&isArray)),#name); \
} else { \
name = tmbutils::asArray<Type>(getListElement( \
objective_function::data,#name,&isArray)); \
}
/** \brief Get parameter array from R and declare it as array<Type> \ingroup macros */
#define PARAMETER_ARRAY(name) tmbutils::array<Type> name(objective_function::fillShape(tmbutils::asArray<Type>(objective_function::getShape(#name,&isArray)),#name));
/** \brief Get data matrix from R and declare it as matrix<int> \ingroup macros */
#define DATA_IMATRIX(name) matrix<int> name(asMatrix<int>( \
getListElement(objective_function::data,#name,&isMatrix)));
/** \brief Get data array from R and declare it as array<int> \ingroup macros */
#define DATA_IARRAY(name) tmbutils::array<int> name(tmbutils::asArray<int>( \
getListElement(objective_function::data,#name,&isArray)));
/** \brief Get data list object from R and makes it available in C++
Example (incomplete) of use:
In R:
\verbatim
data <- list()
data$object <- list(a=1:10, b=matrix(1:6,2))
obj <- MakeADFun(data,........)
\endverbatim
In C++:
\verbatim
// Corresponding list object on the C++ side
template<class Type>
struct my_list {
vector<Type> a;
matrix<Type> b;
my_list(SEXP x){ // Constructor
a = asVector<Type>(getListElement(x,"a"));
b = asMatrix<Type>(getListElement(x,"b"));
}
};
template<class Type>
Type objective_function<Type>::operator() ()
{
DATA_STRUCT(object, my_list);
REPORT(object.a); // Now you can use "a" and "b" as you like
REPORT(object.b);
return 0;
}
\endverbatim
\ingroup macros
*/
#define DATA_STRUCT(name, struct)struct<Type> name(getListElement(this->data,#name));
/* Utilities for OSA residuals */
template<class VT, class Type>
struct data_indicator : VT{
VT cdf_lower, cdf_upper;
/* Construct from observation */
data_indicator(VT obs){
VT::operator=(obs); VT::fill(Type(1.0));
cdf_lower = obs; cdf_lower.setZero();
cdf_upper = obs; cdf_upper.setZero();
}
/* Fill with parameter vector */
void fill(vector<Type> p){
int n = (*this).size();
if(p.size() >= n ) VT::operator=(p.segment(0, n));
if(p.size() >= 2*n) cdf_lower = p.segment(n, n);
if(p.size() >= 3*n) cdf_upper = p.segment(2 * n, n);
}
};
/** \brief Declare an indicator array 'name' of same shape as 'obs'.
\ingroup macros */
#define DATA_ARRAY_INDICATOR(name, obs) \
data_indicator<tmbutils::array<Type>, Type > name(obs); \
if (!isNull(getListElement(objective_function::parameters,#name))) { \
name.fill( objective_function::fillShape(asVector<Type>( \
objective_function::getShape(#name,&isNumeric)),#name) ); \
}
/** \brief Declare an indicator vector 'name' of same shape as 'obs'.
\ingroup macros */
#define DATA_VECTOR_INDICATOR(name, obs) \
data_indicator<tmbutils::vector<Type>, Type > name(obs); \
if (!isNull(getListElement(objective_function::parameters,#name))) { \
name.fill( objective_function::fillShape(asVector<Type>( \
objective_function::getShape(#name,&isNumeric)),#name) ); \
}
// kasper: Not sure used anywhere
/** \brief Get the hessian sparsity pattern of ADFun object pointer
\deprecated Kasper is not sure that this code is used anywhere?
*/
template<class Type>
matrix<int> HessianSparsityPattern(ADFun<Type> *pf){
int n=pf->Domain();
vector<bool> Px(n * n);
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
Px[ i * n + j ] = false;
Px[ i * n + i ] = true;
}
pf->ForSparseJac(n, Px);
vector<bool> Py(1); Py[0]=true;
return asMatrix(vector<int>(pf->RevSparseHes(n,Py)),n,n);
}
/** \brief Get list element named "str", or return NULL */
#ifdef WITH_LIBTMB
SEXP getListElement(SEXP list, const char *str, RObjectTester expectedtype=NULL);
#else
SEXP getListElement(SEXP list, const char *str, RObjectTester expectedtype=NULL)
{
if(config.debug.getListElement)std::cout << "getListElement: " << str << " ";
SEXP elmt = R_NilValue, names = getAttrib(list, R_NamesSymbol);
int i;
for (i = 0; i < length(list); i++)
if(strcmp(CHAR(STRING_ELT(names, i)), str) == 0)
{
elmt = VECTOR_ELT(list, i);
break;
}
if(config.debug.getListElement)std::cout << "Length: " << LENGTH(elmt) << " ";
if(config.debug.getListElement)std::cout << "\n";
RObjectTestExpectedType(elmt, expectedtype, str);
return elmt;
}
#endif
/** \brief Do nothing if we are trying to tape non AD-types */
void Independent(vector<double> x)CSKIP({})
/** \brief Used by ADREPORT */
template <class Type>
struct report_stack{
vector<const char*> names;
vector<int> namelength;
vector<Type> result;
void clear(){
names.resize(0);
namelength.resize(0);
result.resize(0);
}
/* Make space for n new items of given name */
void increase(int n, const char* name){
names.conservativeResize(names.size()+1);
names[names.size()-1]=name;
namelength.conservativeResize(namelength.size()+1);
namelength[namelength.size()-1]=n;
result.conservativeResize(result.size()+n);
}
// push scalar
void push(Type x, const char* name){
increase(1,name);
result[result.size()-1]=x;
}
// push vector or array
template<class VectorType>
void push(VectorType x, const char* name){
int n=x.size();
int oldsize=result.size();
increase(n,name);
for(int i=0;i<n;i++)result[oldsize+i]=x[i];
}
// push matrix
void push(matrix<Type> x, const char* name){
push(x.vec(),name);
}
// Cast to vector
operator vector<Type>(){
return result;
}
/* Get names (with replicates) to R */
SEXP reportnames()
{
int n=result.size();
SEXP nam;
PROTECT(nam=allocVector(STRSXP,n));
int k=0;
for(int i=0;i<names.size();i++){
for(int j=0;j<namelength[i];j++){
SET_STRING_ELT(nam,k,mkChar(names[i]));
k++;
}
}
UNPROTECT(1);
return nam;
}
EIGEN_DEFAULT_DENSE_INDEX_TYPE size(){return result.size();}
}; // report_stack
/** \brief Type definition of user-provided objective function (i.e. neg. log. like) */
template <class Type>
class objective_function
{
// private:
public:
SEXP data;
SEXP parameters;
SEXP report;
int index;
vector<Type> theta; /**< \brief Consists of unlist(parameters_)*/
vector<const char*> thetanames; /**< \brief In R notation: names(theta). Contains repeated values*/
report_stack<Type> reportvector; /**< \brief Used by "ADREPORT" */
bool reversefill; // used to find the parameter order in user template (not anymore - use pushParname instead)
vector<const char*> parnames; /**< \brief One name for each PARAMETER_ in user template */
/** \brief Called once for each occurance of PARAMETER_ */
void pushParname(const char* x){
parnames.conservativeResize(parnames.size()+1);
parnames[parnames.size()-1]=x;
}
/* ================== For parallel Hessian computation
Need three different parallel evaluation modes:
(1) *Parallel mode* where a parallel region is evaluated iff
current_parallel_region == selected_parallel_region
(2) *Serial mode* where all parallel region tests are evaluated
to TRUE so that "PARALLEL_REGION" tests are effectively removed.
A negative value of "current_parallel_region" or "selected_parallel_region"
is used to select this mode (the default).
(3) *Count region mode* where statements inside "PARALLEL_REGION{...}"
are *ignored* and "current_parallel_region" is increased by one each
time a parallel region is visited.
NOTE: The macro "PARALLEL_REGION" is supposed to be defined as
#define PARALLEL_REGION if(this->parallel_region())
where the function "parallel_region" does the book keeping.
*/
bool parallel_ignore_statements;
int current_parallel_region; /* Identifier of a code-fragment of user template */
int selected_parallel_region; /* Consider _this_ code-fragment */
int max_parallel_regions; /* Max number of parallel region identifiers,
e.g. max_parallel_regions=omp_get_max_threads();
probably best in most cases. */
bool parallel_region(){ /* Is this the selected parallel region ? */
bool ans;
if(current_parallel_region<0 || selected_parallel_region<0)return true; /* Serial mode */
ans = (selected_parallel_region==current_parallel_region) && (!parallel_ignore_statements);
current_parallel_region++;
if(max_parallel_regions>0)current_parallel_region=current_parallel_region % max_parallel_regions;
return ans;
}
/* Note: Some other functions rely on "count_parallel_regions" to run through the users code (!) */
int count_parallel_regions(){
current_parallel_region=0; /* reset counter */
selected_parallel_region=0;
parallel_ignore_statements=true; /* Do not evaluate stuff inside PARALLEL_REGION{...} */
this->operator()(); /* Run through users code */
if(max_parallel_regions>0)return max_parallel_regions;
else
return current_parallel_region;
}
void set_parallel_region(int i){ /* Select parallel region (from within openmp loop) */
current_parallel_region=0;
selected_parallel_region=i;
parallel_ignore_statements=false;
}
/* data_ and parameters_ are R-lists containing R-vectors or R-matrices.
report_ is an R-environment.
The elements of the vector "unlist(parameters_)" are filled into "theta"
which contains the default parameter-values. This happens during the
*construction* of the objective_function object.
The user defined template "objective_function::operator()" is called
from "MakeADFunObject" which tapes the operations and creates the final
ADFun-object.
*/
/** \brief Constructor which among other things gives a value to "theta" */
objective_function(SEXP data_, SEXP parameters_, SEXP report_)
{
report=report_;
data=data_;
parameters=parameters_;
/* Fill theta with the default parameters.
Pass R-matrices column major. */
theta.resize(nparms(parameters_));
index=0;
int counter=0;
SEXP obj=parameters_;
for(int i=0;i<length(obj);i++){
for(int j=0;j<length(VECTOR_ELT(obj,i));j++)
{
theta[counter++]=Type(REAL(VECTOR_ELT(obj,i))[j]);
}
}
thetanames.resize(theta.size());
for(int i=0;i<thetanames.size();i++)thetanames[i]="";
current_parallel_region=-1;
selected_parallel_region=-1;
max_parallel_regions=-1;
reversefill=false;
}
/** \brief Extract theta vector from objetive function object */
SEXP defaultpar()
{
int n=theta.size();
SEXP res;
SEXP nam;
PROTECT(res=allocVector(REALSXP,n));
PROTECT(nam=allocVector(STRSXP,n));
for(int i=0;i<n;i++){
//REAL(res)[i]=CppAD::Value(theta[i]);
REAL(res)[i]=value(theta[i]);
SET_STRING_ELT(nam,i,mkChar(thetanames[i]));
}
setAttrib(res,R_NamesSymbol,nam);
UNPROTECT(2);
return res;
}
/** \brief Extract parnames vector from objetive function object */
SEXP parNames()
{
int n=parnames.size();
SEXP nam;
PROTECT(nam=allocVector(STRSXP,n));
for(int i=0;i<n;i++){
SET_STRING_ELT(nam,i,mkChar(parnames[i]));
}
UNPROTECT(1);
return nam;
}
/* FIXME: "Value" should be "var2par" I guess
kasper: Why not use asDouble defined previously? */
/** @name Value Functions
Overloaded functions to extract the value from objects of various types;
generally wrappers for CppAD::Value(x).
@{
*/
/** Extracts the value of from TMB objects.
\param x The variable to be extracted.
\return Object of type double containing the value of the argument.
*/
double value(double x){return x;}
double value(AD<double> x){return CppAD::Value(x);}
double value(AD<AD<double> > x){return CppAD::Value(CppAD::Value(x));}
double value(AD<AD<AD<double> > > x){return CppAD::Value(CppAD::Value(CppAD::Value(x)));}
/** @} */
/** \brief Find the length of theta, i.e. in application obj=parameters */
int nparms(SEXP obj)
{
int count=0;
for(int i=0;i<length(obj);i++){
if(!isReal(VECTOR_ELT(obj,i)))error("PARAMETER COMPONENT NOT A VECTOR!");
count+=length(VECTOR_ELT(obj,i));
}
return count;
}
/* The "fill functions" are all used to populate parameter vectors,
arrays, matrices etc with the values of the parameter vector theta. */
void fill(vector<Type> &x, const char *nam)
{
pushParname(nam);
for(int i=0;i<x.size();i++){
thetanames[index]=nam;
if(reversefill)theta[index++]=x[i];else x[i]=theta[index++];
}
}
void fill(matrix<Type> &x, const char *nam)
{
pushParname(nam);
for(int j=0;j<x.cols();j++){
for(int i=0;i<x.rows();i++){
thetanames[index]=nam;
if(reversefill)theta[index++]=x(i,j);else x(i,j)=theta[index++];
}
}
}
template<class ArrayType>
void fill(ArrayType &x, const char *nam)
{
pushParname(nam);
for(int i=0;i<x.size();i++){
thetanames[index]=nam;
if(reversefill)theta[index++]=x[i];else x[i]=theta[index++];
}
}
/* Experiment: new map feature - currently arrays only */
template<class ArrayType>
void fillmap(ArrayType &x, const char *nam)
{
pushParname(nam);
SEXP elm=getListElement(parameters,nam);
int* map=INTEGER(getAttrib(elm,install("map")));
int nlevels=INTEGER(getAttrib(elm,install("nlevels")))[0];
for(int i=0;i<x.size();i++){
if(map[i]>=0){
thetanames[index+map[i]]=nam;
if(reversefill)theta[index+map[i]]=x(i);else x(i)=theta[index+map[i]];
}
}
index+=nlevels;
}
// Auto detect whether we are in "map-mode"
SEXP getShape(const char *nam, RObjectTester expectedtype=NULL){
SEXP elm=getListElement(parameters,nam);
SEXP shape=getAttrib(elm,install("shape"));
SEXP ans;
if(shape==R_NilValue)ans=elm; else ans=shape;
RObjectTestExpectedType(ans, expectedtype, nam);
return ans;
}
template<class ArrayType>
//ArrayType fillShape(ArrayType &x, const char *nam){
ArrayType fillShape(ArrayType x, const char *nam){
SEXP elm=getListElement(parameters,nam);
SEXP shape=getAttrib(elm,install("shape"));
if(shape==R_NilValue)fill(x,nam);
else fillmap(x,nam);
return x;
}
void fill(Type &x, char const *nam)
{
pushParname(nam);
thetanames[index]=nam;
if(reversefill)theta[index++]=x;else x=theta[index++];
}
Type operator() ();
Type evalUserTemplate(){
Type ans=this->operator()();
/* After evaluating the template, "index" should be equal to the length of "theta".
If not, we assume that the "epsilon method" has been requested from R, I.e.
that the un-used theta parameters are reserved for an inner product contribution
with the numbers reported via ADREPORT. */
if(index!=theta.size()){
if( index + reportvector.size() != theta.size() )
error("evalUserTemplate: Invalid parameter length.");
if( reportvector.size() > 0 ){
vector<Type> epsilon(reportvector.size());
this->fill(epsilon,"epsilon"); /* Assume theta has been sufficiently augmented */
ans += ( this->reportvector.result * epsilon ).sum();
}
}
return ans;
}
}; // objective_function
/** \brief Helper to manage parallel accumulation.
- A parallel accumulator only has two valid operators: increment
(+=) and decrement (-=). Other usage would break the assumptions
underlying the parallel accumulator.
- In particular, direct assignment is forbidden. The parallel
accumulator is automatically zero initialized.
Example:
\code
// Initialize a variable for parallel summation:
parallel_accumulator<Type> res(this);
// Only two valid methods:
res += ...;
res -= ...;
// Automatic cast to Type when exit from function
// (cast to Type is not allowed elsewhere):
return res;
\endcode
\note It is only recommended to apply the parallel accumulator for
models that are known to work in serial (debugging becomes
substantially more difficult with parallel accumulation turned
on).
*/
template<class Type>
struct parallel_accumulator{
Type result;
objective_function<Type>* obj;
parallel_accumulator(objective_function<Type>* obj_){
result=Type(0);
obj=obj_;
#ifdef _OPENMP
#include <omp.h>
obj->max_parallel_regions=omp_get_max_threads();
#endif
}
inline void operator+=(Type x){
if(obj->parallel_region())result+=x;
}
inline void operator-=(Type x){
if(obj->parallel_region())result-=x;
}
operator Type(){
return result;
}
};
#ifndef WITH_LIBTMB
/** \brief Evaluates an ADFun object from R
Template argument can be "ADFun" or an object extending
"ADFun" such as "parallelADFun".
@param f R external pointer to ADFunType
@param theta R vector of parameters
@param control R list controlling what to be returned
It is assumed that \f$f:R^n \rightarrow R^m\f$ where n and m are found from f.
The list "control" can contain the following components:
* order: mandatory integer 0,1,2, or 3 with order of derivatives to be calculated.\n
* hessiancols, hessianrows: Optional one-based integer vectors of the same length.
Used only in the case where order=2 to extract specific entries of hessian.\n
* sparsitypattern: Integer flag. Return sparsity pattern instead of numerical values?\n
* rangeweight: Optional R vector of doubles of length m. If supplied, a 1st order reverse
mode sweep is performed in this range direction.\n
* rangecomponent: Optional one-based integer (scalar) between 1 and m. Used to select a
given component of the vector f(x).
* dumpstack: Integer flag. If non zero the entire operation stack is dumped as text output
during 0-order forward sweep.
Possible output depends on "order".
* order==0: Calculate f(x) output as vector of length m.\n
* order==1: If "rangeweight" is supplied, calculate the gradient of the function
x -> inner_prod(f(x),w) from R^n->R.
Otherwise, calculate the full jacobian (of dimension m*n).\n
* order==2: If nothing further is specified, calculate the full hessian of the function
x->f(x)[rangecomponent] from R^n->R
All other usage is considered deprecated/experimental and may be removed in the future.
*/
template<class ADFunType>
SEXP EvalADFunObjectTemplate(SEXP f, SEXP theta, SEXP control)
{
if(!isNewList(control))error("'control' must be a list");
ADFunType* pf;
pf=(ADFunType*)R_ExternalPtrAddr(f);
PROTECT(theta=coerceVector(theta,REALSXP));
int n=pf->Domain();
int m=pf->Range();
if(LENGTH(theta)!=n)error("Wrong parameter length.");
// Do forwardsweep ?
int doforward=INTEGER(getListElement(control,"doforward"))[0];
//R-index -> C-index
int rangecomponent=INTEGER(getListElement(control,"rangecomponent"))[0]-1;
if(!((0<=rangecomponent)&(rangecomponent<=m-1)))
error("Wrong range component.");
int order = INTEGER(getListElement(control,"order"))[0];
if((order!=0) & (order!=1) & (order!=2) & (order!=3))
error("order can be 0, 1, 2 or 3");
int sparsitypattern=INTEGER(getListElement(control,"sparsitypattern"))[0];
int dumpstack=INTEGER(getListElement(control,"dumpstack"))[0];
SEXP hessiancols; // Hessian columns
PROTECT(hessiancols=getListElement(control,"hessiancols"));
int ncols=length(hessiancols);
SEXP hessianrows; // Hessian rows
PROTECT(hessianrows=getListElement(control,"hessianrows"));
int nrows=length(hessianrows);
if((nrows>0)&(nrows!=ncols))error("hessianrows and hessianrows must have same length");
vector<size_t> cols(ncols);
vector<size_t> cols0(ncols);
vector<size_t> rows(nrows);
if(ncols>0){
for(int i=0;i<ncols;i++){
cols[i]=INTEGER(hessiancols)[i]-1; //R-index -> C-index
cols0[i]=0;
if(nrows>0)rows[i]=INTEGER(hessianrows)[i]-1; //R-index -> C-index
}
}
vector<double> x = asVector<double>(theta);
SEXP res=R_NilValue;
SEXP rangeweight=getListElement(control,"rangeweight");
if(rangeweight!=R_NilValue){
if(LENGTH(rangeweight)!=m)error("rangeweight must have length equal to range dimension");
if(doforward)pf->Forward(0,x);
res=asSEXP(pf->Reverse(1,asVector<double>(rangeweight)));
UNPROTECT(3);
return res;
}
if(order==3){
vector<double> w(1);
w[0]=1;
if((nrows!=1) | (ncols!=1))error("For 3rd order derivatives a single hessian coordinate must be specified.");
pf->ForTwo(x,rows,cols); /* Compute forward directions */
PROTECT(res=asSEXP(asMatrix(pf->Reverse(3,w),n,3)));
}
if(order==0){
if(dumpstack)CppAD::traceforward0sweep(1);
PROTECT(res=asSEXP(pf->Forward(0,x)));
if(dumpstack)CppAD::traceforward0sweep(0);
SEXP rangenames=getAttrib(f,install("range.names"));
if(LENGTH(res)==LENGTH(rangenames)){
setAttrib(res,R_NamesSymbol,rangenames);
}
}
if(order==1){
//PROTECT(res=asSEXP(asMatrix(pf->Jacobian(x),m,n)));
if(doforward)pf->Forward(0,x);
vector<double> jac(n*m);
vector<double> u(n);
vector<double> v(m);
for(int i=0;i<m;i++) v[i] = 0.0;
for(int i=0;i<m;i++){
v[i] = 1.0; u = pf->Reverse(1,v);
v[i] = 0.0;
for(int j=0;j<n;j++) jac[i*n+j] = u[j];
}
PROTECT(res=asSEXP(asMatrix(jac,m,n)));
}
//if(order==2)res=asSEXP(pf->Hessian(x,0),1);
if(order==2){
if(ncols==0){
if(sparsitypattern){
PROTECT(res=asSEXP(HessianSparsityPattern(pf)));
} else {
PROTECT(res=asSEXP(asMatrix(pf->Hessian(x,rangecomponent),n,n)));
}
}
else if (nrows==0){
/* Fixme: the cols0 argument should be user changeable */
PROTECT(res=asSEXP(asMatrix(pf->RevTwo(x,cols0,cols),n,ncols)));
}
else PROTECT(res=asSEXP(asMatrix(pf->ForTwo(x,rows,cols),m,ncols)));
}
UNPROTECT(4);
return res;
} // EvalADFunObjectTemplate
/** \brief Garbage collect an ADFun or parallelADFun object pointer */
template <class ADFunType>
void finalize(SEXP x)
{
ADFunType* ptr=(ADFunType*)R_ExternalPtrAddr(x);
if(ptr!=NULL)delete ptr;
memory_manager.CallCFinalizer(x);
}
/** \brief Construct ADFun object */
ADFun<double>* MakeADFunObject(SEXP data, SEXP parameters,
SEXP report, SEXP control, int parallel_region=-1,
SEXP &info=R_NilValue)
{
int returnReport = INTEGER(getListElement(control,"report"))[0];
/* Create objective_function "dummy"-object */
objective_function< AD<double> > F(data,parameters,report);
F.set_parallel_region(parallel_region);
/* Create ADFun pointer.
We have the option to tape either the value returned by the
objective_function template or the vector reported using the
macro "ADREPORT" */
Independent(F.theta); // In both cases theta is the independent variable
ADFun< double >* pf;
if(!returnReport){ // Default case: no ad report - parallel run allowed
vector< AD<double> > y(1);
y[0]=F.evalUserTemplate();
pf = new ADFun< double >(F.theta,y);
} else { // ad report case
F(); // Run through user template (modifies reportvector)
pf = new ADFun< double >(F.theta,F.reportvector.result);
info=F.reportvector.reportnames(); // parallel run *not* allowed
}
return pf;
}
extern "C"
{
/** \brief Garbage collect an ADFun object pointer */
void finalizeADFun(SEXP x)
{
ADFun<double>* ptr=(ADFun<double>*)R_ExternalPtrAddr(x);
if(ptr!=NULL)delete ptr;
memory_manager.CallCFinalizer(x);
}
void finalizeparallelADFun(SEXP x)
{
parallelADFun<double>* ptr=(parallelADFun<double>*)R_ExternalPtrAddr(x);
if(ptr!=NULL)delete ptr;
memory_manager.CallCFinalizer(x);
}
/** \brief Construct ADFun object */
SEXP MakeADFunObject(SEXP data, SEXP parameters,
SEXP report, SEXP control)
{
ADFun<double>* pf = NULL;
/* Some type checking */
if(!isNewList(data))error("'data' must be a list");
if(!isNewList(parameters))error("'parameters' must be a list");
if(!isEnvironment(report))error("'report' must be an environment");
if(!isNewList(control))error("'control' must be a list");
int returnReport = INTEGER(getListElement(control,"report"))[0];
/* Get the default parameter vector (tiny overhead) */
SEXP par,res=NULL,info;
objective_function< double > F(data,parameters,report);
#ifdef _OPENMP
int n=F.count_parallel_regions(); // Evaluates user template
#else
F.count_parallel_regions(); // Evaluates user template
#endif
if(returnReport && F.reportvector.size()==0){
/* Told to report, but no ADREPORT in template: Get out quickly */
return R_NilValue;
}
PROTECT(par=F.defaultpar());
PROTECT(info=R_NilValue); // Important
if(_openmp && !returnReport){ // Parallel mode
#ifdef _OPENMP
if(config.trace.parallel)
std::cout << n << " regions found.\n";
start_parallel(); /* Start threads */
vector< ADFun<double>* > pfvec(n);
bool bad_thread_alloc = false;
#pragma omp parallel for if (config.tape.parallel)
for(int i=0;i<n;i++){
TMB_TRY {
pfvec[i] = NULL;
pfvec[i] = MakeADFunObject(data, parameters, report, control, i, info);
if (config.optimize.instantly) pfvec[i]->optimize();
}
TMB_CATCH { bad_thread_alloc = true; }
}
if(bad_thread_alloc){
for(int i=0; i<n; i++) if (pfvec[i] != NULL) delete pfvec[i];
TMB_ERROR_BAD_ALLOC;
}
parallelADFun<double>* ppf=new parallelADFun<double>(pfvec);
/* Convert parallel ADFun pointer to R_ExternalPtr */
PROTECT(res=R_MakeExternalPtr((void*) ppf,mkChar("parallelADFun"),R_NilValue));
R_RegisterCFinalizer(res,finalizeparallelADFun);