Skip to content

Commit e46a055

Browse files
Vedant2005goyalvgvassilev
authored andcommitted
This PR adds reverse mode pullback for the vector for basic std::vector initializations. Example:- std::vector<int> v(size);
1 parent b633a92 commit e46a055

2 files changed

Lines changed: 51 additions & 0 deletions

File tree

include/clad/Differentiator/STLBuiltins.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,21 @@ void constructor_pullback(const ::std::array<T, N>& arr,
687687
for (size_t i = 0; i < N; ++i)
688688
(*d_arr)[i] += (*d_this)[i];
689689
}
690+
template <typename T>
691+
void constructor_pullback(typename ::std::vector<T>::size_type count,
692+
::std::vector<T>* d_this,
693+
typename ::std::vector<T>::size_type* d_count) {
694+
d_this->clear();
695+
}
690696

697+
template <typename T>
698+
void constructor_pullback(
699+
typename ::std::vector<T>::size_type count,
700+
const typename ::std::vector<T>::allocator_type& alloc,
701+
::std::vector<T>* d_this, typename ::std::vector<T>::size_type* d_count,
702+
typename ::std::vector<T>::allocator_type* d_alloc) {
703+
d_this->clear();
704+
}
691705
// tuple forward mode
692706

693707
template <typename... Args1, typename... Args2>

test/Features/VectorPullback.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// RUN: %cladclang -std=c++17 -O0 -I%S/../../include/ %s -o %t
2+
// RUN: %t | %filecheck_exec %s
3+
4+
#include "clad/Differentiator/Differentiator.h"
5+
#include "clad/Differentiator/STLBuiltins.h"
6+
#include <vector>
7+
#include <memory>
8+
#include <iostream>
9+
10+
double f(double x) {
11+
std::vector<double> v(5);
12+
return x * x;
13+
}
14+
15+
double f1(double x) {
16+
std::allocator<double> alloc;
17+
std::vector<double> v(5, alloc);
18+
return x * x * x;
19+
}
20+
21+
int main() {
22+
23+
auto df = clad::gradient(f);
24+
auto df1 = clad::gradient(f1);
25+
26+
double dx1 = 0.0;
27+
df.execute(3.0, &dx1);
28+
std::cout << "Diff result: " << dx1 << "\n";
29+
//CHECK-EXEC: Diff result: 6
30+
31+
double dx2 = 0.0;
32+
df1.execute(2.0, &dx2);
33+
std::cout << "Diff result: " << dx2 << "\n";
34+
//CHECK-EXEC: Diff result: 12
35+
36+
return 0;
37+
}

0 commit comments

Comments
 (0)