Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crash on jacobian with array inputs #472

Closed
FROL256 opened this issue Jul 21, 2022 · 2 comments · Fixed by #1121 · May be fixed by #478
Closed

crash on jacobian with array inputs #472

FROL256 opened this issue Jul 21, 2022 · 2 comments · Fixed by #1121 · May be fixed by #478
Assignees
Milestone

Comments

@FROL256
Copy link

FROL256 commented Jul 21, 2022

Hello! I took an example of jacobian from main page (h). If i make the first parameter array, clad crashes :(

Input:

//void h(float a, float b, float output[]) {
//    output[0] = a * a * a;
//    output[1] = a * a * a + b * b * b;
//    output[2] = 2 * (a + b);
//}

void h(float a[3], float b, float output[]) {
    output[0] = a[0] * a[0] * a[0];
    output[1] = a[0] * a[0] * a[0] + b * b * b;
    output[2] = 2 * (a[0] + b);
}

Error:

Executing task: clang-12 -I /home/frol/PROG/clad_autodiff_examples/inst/include -x c++ -std=c++11 -g -fplugin=/home/frol/PROG/clad_autodiff_examples/inst/lib/clad.so SourceFile.cpp -o z_out -lstdc++ -lm -Xclang -plugin-arg-clad -Xclang -fgenerate-source-file -DCLAD_NO_NUM_DIFF 

clang: /usr/lib/llvm-12/include/llvm/ADT/SmallVector.h:277: T& llvm::SmallVectorTemplateCommon<T, <template-parameter-1-2> >::operator[](llvm::SmallVectorTemplateCommon<T, <template-parameter-1-2> >::size_type) [with T = const clang::ValueDecl*; <template-parameter-1-2> = void; llvm::SmallVectorTemplateCommon<T, <template-parameter-1-2> >::reference = const clang::ValueDecl*&; llvm::SmallVectorTemplateCommon<T, <template-parameter-1-2> >::size_type = long unsigned int]: Assertion `idx < size()' failed.
PLEASE submit a bug report to https://bugs.llvm.org/ and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0.      Program arguments: /usr/lib/llvm-12/bin/clang -cc1 -triple x86_64-pc-linux-gnu -emit-obj -mrelax-all --mrelax-relocations -disable-free -disable-llvm-verifier -discard-value-names -main-file-name SourceFile.cpp -mrelocation-model static -mframe-pointer=all -fmath-errno -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -fno-split-dwarf-inlining -debug-info-kind=limited -dwarf-version=4 -debugger-tuning=gdb -resource-dir /usr/lib/llvm-12/lib/clang/12.0.0 -I /home/frol/PROG/clad_autodiff_examples/inst/include -D CLAD_NO_NUM_DIFF -internal-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9 -internal-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/x86_64-linux-gnu/c++/9 -internal-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/x86_64-linux-gnu/c++/9 -internal-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/backward -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-12/lib/clang/12.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -std=c++11 -fdeprecated-macro -fdebug-compilation-dir /home/frol/PROG/clad_autodiff_examples -ferror-limit 19 -fgnuc-version=4.2.1 -fcxx-exceptions -fexceptions -fcolor-diagnostics -load /home/frol/PROG/clad_autodiff_examples/inst/lib/clad.so -plugin-arg-clad -fgenerate-source-file -faddrsig -o /tmp/SourceFile-dc6b59.o -x c++ SourceFile.cpp
1.      <eof> parser at end of file
 #0 0x00007ffa76823ef3 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/lib/x86_64-linux-gnu/libLLVM-12.so.1+0xbd8ef3)
 #1 0x00007ffa76822210 llvm::sys::RunSignalHandlers() (/lib/x86_64-linux-gnu/libLLVM-12.so.1+0xbd7210)
 #2 0x00007ffa7682455f (/lib/x86_64-linux-gnu/libLLVM-12.so.1+0xbd955f)
 #3 0x00007ffa7e059420 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x14420)
 #4 0x00007ffa7574e00b raise /build/glibc-SzIz7B/glibc-2.31/signal/../sysdeps/unix/sysv/linux/raise.c:51:1
 #5 0x00007ffa7572d859 abort /build/glibc-SzIz7B/glibc-2.31/stdlib/abort.c:81:7
 #6 0x00007ffa7572d729 get_sysdep_segment_value /build/glibc-SzIz7B/glibc-2.31/intl/loadmsgcat.c:509:8
 #7 0x00007ffa7572d729 _nl_load_domain /build/glibc-SzIz7B/glibc-2.31/intl/loadmsgcat.c:970:34
 #8 0x00007ffa7573efd6 (/lib/x86_64-linux-gnu/libc.so.6+0x33fd6)
 #9 0x00007ffa752b036f llvm::SmallVectorTemplateCommon<clang::ValueDecl const*, void>::operator[](unsigned long) (/home/frol/PROG/clad_autodiff_examples/inst/lib/clad.so+0x14936f)
#10 0x00007ffa75475141 clad::ReverseModeVisitor::VisitBinaryOperator(clang::BinaryOperator const*) (/home/frol/PROG/clad_autodiff_examples/inst/lib/clad.so+0x30e141)
#11 0x00007ffa754804e8 clang::StmtVisitorBase<llvm::make_const_ptr, clad::ReverseModeVisitor, clad::StmtDiff>::VisitBinAssign(clang::BinaryOperator const*) (/home/frol/PROG/clad_autodiff_examples/inst/lib/clad.so+0x3194e8)
#12 0x00007ffa7547bdf1 clang::StmtVisitorBase<llvm::make_const_ptr, clad::ReverseModeVisitor, clad::StmtDiff>::Visit(clang::Stmt const*) (/home/frol/PROG/clad_autodiff_examples/inst/lib/clad.so+0x314df1)
#13 0x00007ffa75469efa clad::ReverseModeVisitor::Visit(clang::Stmt const*, clang::Expr*) (/home/frol/PROG/clad_autodiff_examples/inst/lib/clad.so+0x302efa)
#14 0x00007ffa754773a7 clad::ReverseModeVisitor::DifferentiateSingleStmt(clang::Stmt const*, clang::Expr*) (/home/frol/PROG/clad_autodiff_examples/inst/lib/clad.so+0x3103a7)
#15 0x00007ffa7546de9f clad::ReverseModeVisitor::VisitCompoundStmt(clang::CompoundStmt const*) (/home/frol/PROG/clad_autodiff_examples/inst/lib/clad.so+0x306e9f)
#16 0x00007ffa7547c22e clang::StmtVisitorBase<llvm::make_const_ptr, clad::ReverseModeVisitor, clad::StmtDiff>::Visit(clang::Stmt const*) (/home/frol/PROG/clad_autodiff_examples/inst/lib/clad.so+0x31522e)
#17 0x00007ffa75469efa clad::ReverseModeVisitor::Visit(clang::Stmt const*, clang::Expr*) (/home/frol/PROG/clad_autodiff_examples/inst/lib/clad.so+0x302efa)
#18 0x00007ffa7546cd14 clad::ReverseModeVisitor::Derive(clang::FunctionDecl const*, clad::DiffRequest const&) (/home/frol/PROG/clad_autodiff_examples/inst/lib/clad.so+0x305d14)
#19 0x00007ffa75468ea4 clad::JacobianModeVisitor::Derive(clang::FunctionDecl const*, clad::DiffRequest const&) (/home/frol/PROG/clad_autodiff_examples/inst/lib/clad.so+0x301ea4)
#20 0x00007ffa7528cb2c clad::DerivativeBuilder::Derive(clad::DiffRequest const&) (/home/frol/PROG/clad_autodiff_examples/inst/lib/clad.so+0x125b2c)
#21 0x00007ffa7527da12 clad::plugin::CladPlugin::ProcessDiffRequest(clad::DiffRequest&) (.localalias) (/home/frol/PROG/clad_autodiff_examples/inst/lib/clad.so+0x116a12)
#22 0x00007ffa7527d225 clad::plugin::CladPlugin::HandleTopLevelDecl(clang::DeclGroupRef) (/home/frol/PROG/clad_autodiff_examples/inst/lib/clad.so+0x116225)
#23 0x00007ffa7d300b2c clang::MultiplexConsumer::HandleTopLevelDecl(clang::DeclGroupRef) (/lib/x86_64-linux-gnu/libclang-cpp.so.12+0x1effb2c)
#24 0x00007ffa7bd39e58 clang::ParseAST(clang::Sema&, bool, bool) (/lib/x86_64-linux-gnu/libclang-cpp.so.12+0x938e58)
#25 0x00007ffa7d2cf118 clang::FrontendAction::Execute() (/lib/x86_64-linux-gnu/libclang-cpp.so.12+0x1ece118)
#26 0x00007ffa7d25cdd1 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/lib/x86_64-linux-gnu/libclang-cpp.so.12+0x1e5bdd1)
#27 0x00007ffa7d331502 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/lib/x86_64-linux-gnu/libclang-cpp.so.12+0x1f30502)
#28 0x0000000000412782 cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/usr/lib/llvm-12/bin/clang+0x412782)
#29 0x0000000000410afe (/usr/lib/llvm-12/bin/clang+0x410afe)
#30 0x000000000041090e main (/usr/lib/llvm-12/bin/clang+0x41090e)
#31 0x00007ffa7572f083 __libc_start_main /build/glibc-SzIz7B/glibc-2.31/csu/../csu/libc-start.c:342:3
#32 0x000000000040dcbe _start (/usr/lib/llvm-12/bin/clang+0x40dcbe)
clang: error: unable to execute command: Aborted (core dumped)
clang: error: clang frontend command failed due to signal (use -v to see invocation)
Ubuntu clang version 12.0.0-3ubuntu1~20.04.5
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
clang: note: diagnostic msg: 

PS: reproduces both with current version in master and release v0.9.
Thank you very much! :)

@vgvassilev
Copy link
Owner

Hi @FROL256,

This works for me. Can you give a standalone example which crashes? I use Ubuntu clang version 12.0.1-++20210630032957+fed41342a82f-1~exp1~20210630133716.134

cat array.cpp 
#include "clad/Differentiator/Differentiator.h"

void h(float a[3], float b, float output[]) {
    output[0] = a[0] * a[0] * a[0];
    output[1] = a[0] * a[0] * a[0] + b * b * b;
    output[2] = 2 * (a[0] + b);
}


int main() {
  auto dh = clad::gradient(h);
  dh.dump();
  return 0;
}
vvassilev@vv-nuc ~/workspace/builds/clad $ /usr/lib/llvm-12/bin/clang -x c++  -std=c++11 -Xclang -add-plugin -Xclang clad -Xclang         -plugin-arg-clad -Xclang -fdump-derived-fn -Xclang         -load -Xclang /home/vvassilev/workspace/builds/clad/lib/clad.so array.cpp -I/home/vvassilev/workspace/sources/clad/test/Hessian/../../include -lstdc++ -lm
void h_grad(float a[3], float b, float output[], clad::array_ref<float> _d_a, clad::array_ref<float> _d_b, clad::array_ref<float> _d_output) {
    float _t0;
    float _t1;
    float _t2;
    float _t3;
    float _t4;
    float _t5;
    float _t6;
    float _t7;
    float _t8;
    float _t9;
    float _t10;
    float _t11;
    float _t12;
    _t2 = a[0];
    _t1 = a[0];
    _t3 = _t2 * _t1;
    _t0 = a[0];
    output[0] = _t3 * _t0;
    _t6 = a[0];
    _t5 = a[0];
    _t7 = _t6 * _t5;
    _t4 = a[0];
    _t10 = b;
    _t9 = b;
    _t11 = _t10 * _t9;
    _t8 = b;
    output[1] = _t7 * _t4 + _t11 * _t8;
    _t12 = (a[0] + b);
    output[2] = 2 * _t12;
    {
        float _r_d2 = _d_output[2];
        float _r12 = _r_d2 * _t12;
        float _r13 = 2 * _r_d2;
        _d_a[0] += _r13;
        * _d_b += _r13;
        _d_output[2] -= _r_d2;
        _d_output[2];
    }
    {
        float _r_d1 = _d_output[1];
        float _r4 = _r_d1 * _t4;
        float _r5 = _r4 * _t5;
        _d_a[0] += _r5;
        float _r6 = _t6 * _r4;
        _d_a[0] += _r6;
        float _r7 = _t7 * _r_d1;
        _d_a[0] += _r7;
        float _r8 = _r_d1 * _t8;
        float _r9 = _r8 * _t9;
        * _d_b += _r9;
        float _r10 = _t10 * _r8;
        * _d_b += _r10;
        float _r11 = _t11 * _r_d1;
        * _d_b += _r11;
        _d_output[1] -= _r_d1;
        _d_output[1];
    }
    {
        float _r_d0 = _d_output[0];
        float _r0 = _r_d0 * _t0;
        float _r1 = _r0 * _t1;
        _d_a[0] += _r1;
        float _r2 = _t2 * _r0;
        _d_a[0] += _r2;
        float _r3 = _t3 * _r_d0;
        _d_a[0] += _r3;
        _d_output[0] -= _r_d0;
        _d_output[0];
    }
}
vvassilev@vv-nuc ~/workspace/builds/clad $ ./a.out 
The code is: 
void h_grad(float a[3], float b, float output[], clad::array_ref<float> _d_a, clad::array_ref<float> _d_b, clad::array_ref<float> _d_output) {
    float _t0;
    float _t1;
    float _t2;
    float _t3;
    float _t4;
    float _t5;
    float _t6;
    float _t7;
    float _t8;
    float _t9;
    float _t10;
    float _t11;
    float _t12;
    _t2 = a[0];
    _t1 = a[0];
    _t3 = _t2 * _t1;
    _t0 = a[0];
    output[0] = _t3 * _t0;
    _t6 = a[0];
    _t5 = a[0];
    _t7 = _t6 * _t5;
    _t4 = a[0];
    _t10 = b;
    _t9 = b;
    _t11 = _t10 * _t9;
    _t8 = b;
    output[1] = _t7 * _t4 + _t11 * _t8;
    _t12 = (a[0] + b);
    output[2] = 2 * _t12;
    {
        float _r_d2 = _d_output[2];
        float _r12 = _r_d2 * _t12;
        float _r13 = 2 * _r_d2;
        _d_a[0] += _r13;
        * _d_b += _r13;
        _d_output[2] -= _r_d2;
        _d_output[2];
    }
    {
        float _r_d1 = _d_output[1];
        float _r4 = _r_d1 * _t4;
        float _r5 = _r4 * _t5;
        _d_a[0] += _r5;
        float _r6 = _t6 * _r4;
        _d_a[0] += _r6;
        float _r7 = _t7 * _r_d1;
        _d_a[0] += _r7;
        float _r8 = _r_d1 * _t8;
        float _r9 = _r8 * _t9;
        * _d_b += _r9;
        float _r10 = _t10 * _r8;
        * _d_b += _r10;
        float _r11 = _t11 * _r_d1;
        * _d_b += _r11;
        _d_output[1] -= _r_d1;
        _d_output[1];
    }
    {
        float _r_d0 = _d_output[0];
        float _r0 = _r_d0 * _t0;
        float _r1 = _r0 * _t1;
        _d_a[0] += _r1;
        float _r2 = _t2 * _r0;
        _d_a[0] += _r2;
        float _r3 = _t3 * _r_d0;
        _d_a[0] += _r3;
        _d_output[0] -= _r_d0;
        _d_output[0];
    }
}

@vgvassilev
Copy link
Owner

I can reproduce the crash if I change clad::gradient to clad::jacobian.

@Nirhar, can you take a look what is wrong here?

Nirhar added a commit to Nirhar/clad that referenced this issue Aug 3, 2022
…bian Mode

Now One must be able to find the Jacobian of functions with Constant Arrays in the parameter list.
For example, a function of the form:
```cpp
void func(double arr[3], double x, double y, double* output){
	output[0]=arr[2]*x*y;
	.
	.
	.
	output[n-1]=arr[0]*arr[1]*arr[2];
}
```

will generate a Jacobian of size n x 5.

Corresponding tests for the same have been written.

Closes vgvassilev#472
Nirhar added a commit to Nirhar/clad that referenced this issue Aug 12, 2022
…bian Mode

Now One must be able to find the Jacobian of functions with Constant Arrays in the parameter list.
For example, a function of the form:
```cpp
void func(double arr[3], double x, double y, double* output){
	output[0]=arr[2]*x*y;
	.
	.
	.
	output[n-1]=arr[0]*arr[1]*arr[2];
}
```

will generate a Jacobian of size n x 5.

Corresponding tests for the same have been written.

Previously this feature was not implememnted because it was necessary to know the size
of an array to correctly determine the size of the output jacobian matrix. This has now
been achieved for constant arrays, as we know the array sizes for them and hence we can
precisely locate where each jacobian entry for a array parameter is located. This is
achieved with the help of m_IndependentVarsSize, which stores the number of actual parameters
that each function parameter corresponds to.

Prior to this commit a std::map<ValueDecl, Expr> was used to map variable declarations to
their corresponding jacobian expression, so that we can lookup the latter during the reversemode
computations quickly. While this is fine for primitive variables(because each variable will only
correspond to one jacobian expression), an array declaration will correspond to multiple jacobian
expressions, depending on which index of the array is being referred to. Since the ValueDecl can
only refer to the name of the array and not name+index, we must update the map to use the name of
the array + index as the key. This can be done easily by using a string representation of the
ValueDecl name with the index as a suffix as the key of the map. Hence variables like
m_ExprVariables, m_VectorOutputString have been introduced to map array declarations, indexed by
position in array to their corresponding jacobian expressions.

Closes vgvassilev#472
Nirhar added a commit to Nirhar/clad that referenced this issue Aug 12, 2022
…bian Mode

Now One must be able to find the Jacobian of functions with Constant Arrays in the parameter list.
For example, a function of the form:
```cpp
void func(double arr[3], double x, double y, double* output){
	output[0]=arr[2]*x*y;
	.
	.
	.
	output[n-1]=arr[0]*arr[1]*arr[2];
}
```

will generate a Jacobian of size n x 5.

Corresponding tests for the same have been written.

Previously this feature was not implememnted because it was necessary to know the size
of an array to correctly determine the size of the output jacobian matrix. This has now
been achieved for constant arrays, as we know the array sizes for them and hence we can
precisely locate where each jacobian entry for a array parameter is located. This is
achieved with the help of m_IndependentVarsSize, which stores the number of actual parameters
that each function parameter corresponds to.

Prior to this commit a std::map<ValueDecl, Expr> was used to map variable declarations to
their corresponding jacobian expression, so that we can lookup the latter during the reversemode
computations quickly. While this is fine for primitive variables(because each variable will only
correspond to one jacobian expression), an array declaration will correspond to multiple jacobian
expressions, depending on which index of the array is being referred to. Since the ValueDecl can
only refer to the name of the array and not name+index, we must update the map to use the name of
the array + index as the key. This can be done easily by using a string representation of the
ValueDecl name with the index as a suffix as the key of the map. Hence variables like
m_ExprVariables, m_VectorOutputString have been introduced to map array declarations, indexed by
position in array to their corresponding jacobian expressions.

Closes vgvassilev#472
@vgvassilev vgvassilev added this to the v1.6 milestone Feb 7, 2024
@vgvassilev vgvassilev modified the milestones: v1.6, v1.7 Jul 11, 2024
@vgvassilev vgvassilev assigned PetroZarytskyi and unassigned vaithak Jul 11, 2024
@vgvassilev vgvassilev modified the milestones: v1.7, v1.8 Aug 8, 2024
PetroZarytskyi added a commit to PetroZarytskyi/clad that referenced this issue Nov 19, 2024
 Previously, jacobians were based on the non-vectorized reverse mode, which was mostly incapable of capturing multiple outputs. The implementation worked in a few particular cases. For example, it was not possible to differentiate function calls or declare variables inside the original function body.
 This PR implements jacobians using the vectorized forward mode. At the very least, this will solve the issues described above and give a way forward to solve other ones. This also means introducing features to the vectorized fwd mode will introduce the same features to jacobians.
 Let's take a look at the new signature of jacobians. Since the function to be differentiated is expected to have multiple outputs, we should expect the output in the form of array/pointer/reference parameters (just like before). And for every output parameter, we should generate a corresponding adjoint parameter for the user to acquire the results. Since there is no way to specify which parameters are used as output and which are not, adjoints are generated for all array/pointer/reference parameters. For example:

```
void f(double a, double b, double* c)
 -->
void f_jac(double a, double b, double* c, <matrix<double>* _d_c)
```

or

```
void f(double a, double b, double* c, double[7] t)
 -->
void f_jac(double a, double b, double* c, double[7] t,
 array_ref<matrix<double>> _d_c, matrix<double>* _d_t)
```

This signature is also similar to the old one. e.g.
```
df.execute(a, b, c, result); // old behavior
df.execute(a, b, c, &result); // new behavior
```
However, the behavior differs for multiple output parameters, which the old jacobians did not support.

 Note: the same functionality can be achieved by using the vectorized reverse mode, which should probably be implemented at some point. However, the old code for jacobians is unlikely to be useful for that, and there is not much point in keeping it.

Fixes vgvassilev#472, vgvassilev#1057, vgvassilev#480, vgvassilev#527
PetroZarytskyi added a commit to PetroZarytskyi/clad that referenced this issue Nov 19, 2024
 Previously, jacobians were based on the non-vectorized reverse mode, which was mostly incapable of capturing multiple outputs. The implementation worked in a few particular cases. For example, it was not possible to differentiate function calls or declare variables inside the original function body.
 This PR implements jacobians using the vectorized forward mode. At the very least, this will solve the issues described above and give a way forward to solve other ones. This also means introducing features to the vectorized fwd mode will introduce the same features to jacobians.
 Let's take a look at the new signature of jacobians. Since the function to be differentiated is expected to have multiple outputs, we should expect the output in the form of array/pointer/reference parameters (just like before). And for every output parameter, we should generate a corresponding adjoint parameter for the user to acquire the results. Since there is no way to specify which parameters are used as output and which are not, adjoints are generated for all array/pointer/reference parameters. For example:

```
void f(double a, double b, double* c)
 -->
void f_jac(double a, double b, double* c, <matrix<double>* _d_c)
```

or

```
void f(double a, double b, double* c, double[7] t)
 -->
void f_jac(double a, double b, double* c, double[7] t,
 array_ref<matrix<double>> _d_c, matrix<double>* _d_t)
```

This signature is also similar to the old one. e.g.
```
df.execute(a, b, c, result); // old behavior
df.execute(a, b, c, &result); // new behavior
```
However, the behavior differs for multiple output parameters, which the old jacobians did not support.

 Note: the same functionality can be achieved by using the vectorized reverse mode, which should probably be implemented at some point. However, the old code for jacobians is unlikely to be useful for that, and there is not much point in keeping it.

Fixes vgvassilev#472, Fixes vgvassilev#1057, Fixes vgvassilev#480, Fixes vgvassilev#527
vgvassilev pushed a commit that referenced this issue Nov 19, 2024
 Previously, jacobians were based on the non-vectorized reverse mode, which was mostly incapable of capturing multiple outputs. The implementation worked in a few particular cases. For example, it was not possible to differentiate function calls or declare variables inside the original function body.
 This PR implements jacobians using the vectorized forward mode. At the very least, this will solve the issues described above and give a way forward to solve other ones. This also means introducing features to the vectorized fwd mode will introduce the same features to jacobians.
 Let's take a look at the new signature of jacobians. Since the function to be differentiated is expected to have multiple outputs, we should expect the output in the form of array/pointer/reference parameters (just like before). And for every output parameter, we should generate a corresponding adjoint parameter for the user to acquire the results. Since there is no way to specify which parameters are used as output and which are not, adjoints are generated for all array/pointer/reference parameters. For example:

```
void f(double a, double b, double* c)
 -->
void f_jac(double a, double b, double* c, <matrix<double>* _d_c)
```

or

```
void f(double a, double b, double* c, double[7] t)
 -->
void f_jac(double a, double b, double* c, double[7] t,
 array_ref<matrix<double>> _d_c, matrix<double>* _d_t)
```

This signature is also similar to the old one. e.g.
```
df.execute(a, b, c, result); // old behavior
df.execute(a, b, c, &result); // new behavior
```
However, the behavior differs for multiple output parameters, which the old jacobians did not support.

 Note: the same functionality can be achieved by using the vectorized reverse mode, which should probably be implemented at some point. However, the old code for jacobians is unlikely to be useful for that, and there is not much point in keeping it.

Fixes #472, Fixes #1057, Fixes #480, Fixes #527
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment