Skip to content

Commit

Permalink
Fix extra lines generated when using clad::array or array_ref
Browse files Browse the repository at this point in the history
fixes #350
  • Loading branch information
vaithak authored and vgvassilev committed Mar 5, 2024
1 parent 17002f0 commit d7e5434
Show file tree
Hide file tree
Showing 13 changed files with 28 additions and 80 deletions.
17 changes: 12 additions & 5 deletions include/clad/Differentiator/Array.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@
namespace clad {
template <typename T> class array_ref;

// For MSVC, __attribute__((pure)) is not supported.
#if defined(_MSC_VER)
#define PUREFUNC
#else
#define PUREFUNC __attribute__((pure))
#endif

/// This class is not meant to be used by user. It is used by clad internally
/// only

Expand Down Expand Up @@ -102,16 +109,16 @@ template <typename T> class array {
/// Returns the size of the underlying array
CUDA_HOST_DEVICE std::size_t size() const { return m_size; }
/// Returns the ptr of the underlying array
CUDA_HOST_DEVICE T* ptr() const { return m_arr; }
CUDA_HOST_DEVICE T*& ptr_ref() { return m_arr; }
CUDA_HOST_DEVICE PUREFUNC T* ptr() const { return m_arr; }
CUDA_HOST_DEVICE PUREFUNC T*& ptr_ref() { return m_arr; }
/// Returns the reference to the location at the index of the underlying
/// array
CUDA_HOST_DEVICE T& operator[](std::ptrdiff_t i) { return m_arr[i]; }
CUDA_HOST_DEVICE const T& operator[](std::ptrdiff_t i) const {
CUDA_HOST_DEVICE PUREFUNC T& operator[](std::ptrdiff_t i) { return m_arr[i]; }
CUDA_HOST_DEVICE PUREFUNC const T& operator[](std::ptrdiff_t i) const {
return m_arr[i];
}
/// Returns the reference to the underlying array
CUDA_HOST_DEVICE T& operator*() { return *m_arr; }
CUDA_HOST_DEVICE PUREFUNC T& operator*() { return *m_arr; }

// Arithmetic overloads
/// Divides the number from every element in the array
Expand Down
6 changes: 3 additions & 3 deletions include/clad/Differentiator/ArrayRef.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ template <typename T> class array_ref {
}
/// Returns the size of the underlying array
CUDA_HOST_DEVICE std::size_t size() const { return m_size; }
CUDA_HOST_DEVICE T* ptr() const { return m_arr; }
CUDA_HOST_DEVICE T*& ptr_ref() { return m_arr; }
CUDA_HOST_DEVICE PUREFUNC T* ptr() const { return m_arr; }
CUDA_HOST_DEVICE PUREFUNC T*& ptr_ref() { return m_arr; }
/// Returns an array_ref to a part of the underlying array starting at
/// offset and having the specified size
CUDA_HOST_DEVICE array_ref<T> slice(std::size_t offset, std::size_t size) {
Expand All @@ -64,7 +64,7 @@ template <typename T> class array_ref {
return array_ref<T>(&m_arr[offset], size);
}
/// Returns the reference to the underlying array
CUDA_HOST_DEVICE T& operator*() { return *m_arr; }
CUDA_HOST_DEVICE PUREFUNC T& operator*() { return *m_arr; }

// Arithmetic overloads
/// Divides the arrays element wise
Expand Down
3 changes: 0 additions & 3 deletions test/Arrays/ArrayInputsReverseMode.C
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,6 @@ double func5(int k) {
//CHECK-NEXT: double _r_d0 = _d_arr[i];
//CHECK-NEXT: _d_arr[i] -= _r_d0;
//CHECK-NEXT: * _d_k += _r_d0;
//CHECK-NEXT: _d_arr[i];
//CHECK-NEXT: }
//CHECK-NEXT: }
//CHECK-NEXT: * _d_k += _d_n;
Expand Down Expand Up @@ -498,7 +497,6 @@ void modify(double& elem, double val) {
//CHECK-NEXT: double _r_d0 = * _d_elem;
//CHECK-NEXT: * _d_elem -= _r_d0;
//CHECK-NEXT: * _d_val += _r_d0;
//CHECK-NEXT: * _d_elem;
//CHECK-NEXT: }
//CHECK-NEXT: }

Expand Down Expand Up @@ -565,7 +563,6 @@ double sq(double& elem) {
//CHECK-NEXT: * _d_elem -= _r_d0;
//CHECK-NEXT: * _d_elem += _r_d0 * elem;
//CHECK-NEXT: * _d_elem += elem * _r_d0;
//CHECK-NEXT: * _d_elem;
//CHECK-NEXT: }
//CHECK-NEXT: }

Expand Down
6 changes: 0 additions & 6 deletions test/ErrorEstimation/Assignments.C
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ float func(float x, float y) {
//CHECK-NEXT: float _r_d1 = * _d_y;
//CHECK-NEXT: * _d_y -= _r_d1;
//CHECK-NEXT: * _d_x += _r_d1;
//CHECK-NEXT: * _d_y;
//CHECK-NEXT: }
//CHECK-NEXT: {
//CHECK-NEXT: x = _t0;
Expand All @@ -40,7 +39,6 @@ float func(float x, float y) {
//CHECK-NEXT: * _d_x += _r_d0;
//CHECK-NEXT: * _d_y += _r_d0;
//CHECK-NEXT: _delta_x += std::abs(_r_d0 * _EERepl_x1 * {{.+}});
//CHECK-NEXT: * _d_x;
//CHECK-NEXT: }
//CHECK-NEXT: _delta_x += std::abs(* _d_x * _EERepl_x0 * {{.+}});
//CHECK-NEXT: double _delta_y = 0;
Expand Down Expand Up @@ -73,7 +71,6 @@ float func2(float x, int y) {
//CHECK-NEXT: * _d_x += _r_d0 * x;
//CHECK-NEXT: * _d_x += x * _r_d0;
//CHECK-NEXT: _delta_x += std::abs(_r_d0 * _EERepl_x1 * {{.+}});
//CHECK-NEXT: * _d_x;
//CHECK-NEXT: }
//CHECK-NEXT: _delta_x += std::abs(* _d_x * _EERepl_x0 * {{.+}});
//CHECK-NEXT: _final_error += _delta_x;
Expand All @@ -96,7 +93,6 @@ float func3(int x, int y) {
//CHECK-NEXT: int _r_d0 = * _d_x;
//CHECK-NEXT: * _d_x -= _r_d0;
//CHECK-NEXT: * _d_y += _r_d0;
//CHECK-NEXT: * _d_x;
//CHECK-NEXT: }
//CHECK-NEXT: }

Expand Down Expand Up @@ -129,7 +125,6 @@ float func4(float x, float y) {
//CHECK-NEXT: _d_z += _r_d0;
//CHECK-NEXT: * _d_y += _r_d0;
//CHECK-NEXT: _delta_x += std::abs(_r_d0 * _EERepl_x1 * {{.+}});
//CHECK-NEXT: * _d_x;
//CHECK-NEXT: }
//CHECK-NEXT: * _d_y += _d_z;
//CHECK-NEXT: _delta_x += std::abs(* _d_x * _EERepl_x0 * {{.+}});
Expand Down Expand Up @@ -164,7 +159,6 @@ float func5(float x, float y) {
//CHECK-NEXT: _d_z += _r_d0;
//CHECK-NEXT: * _d_y += _r_d0;
//CHECK-NEXT: _delta_x += std::abs(_r_d0 * _EERepl_x1 * {{.+}});
//CHECK-NEXT: * _d_x;
//CHECK-NEXT: }
//CHECK-NEXT: _delta_x += std::abs(* _d_x * _EERepl_x0 * {{.+}});
//CHECK-NEXT: double _delta_y = 0;
Expand Down
5 changes: 0 additions & 5 deletions test/ErrorEstimation/BasicOps.C
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ float func(float x, float y) {
//CHECK-NEXT: _delta_y += std::abs(* _d_y * _EERepl_y1 * {{.+}});
//CHECK-NEXT: * _d_y += _r_d1;
//CHECK-NEXT: _delta_y += std::abs(_r_d1 * _EERepl_y2 * {{.+}});
//CHECK-NEXT: * _d_y;
//CHECK-NEXT: }
//CHECK-NEXT: {
//CHECK-NEXT: x = _t0;
Expand All @@ -62,7 +61,6 @@ float func(float x, float y) {
//CHECK-NEXT: * _d_x += _r_d0;
//CHECK-NEXT: * _d_y += _r_d0;
//CHECK-NEXT: _delta_x += std::abs(_r_d0 * _EERepl_x1 * {{.+}});
//CHECK-NEXT: * _d_x;
//CHECK-NEXT: }
//CHECK-NEXT: _delta_x += std::abs(* _d_x * _EERepl_x0 * {{.+}});
//CHECK-NEXT: _delta_y += std::abs(* _d_y * _EERepl_y0 * {{.+}});
Expand Down Expand Up @@ -108,7 +106,6 @@ float func2(float x, float y) {
//CHECK-NEXT: * _d_y += -_r_d0 * y;
//CHECK-NEXT: * _d_y += y * -_r_d0;
//CHECK-NEXT: _delta_x += std::abs(_r_d0 * _EERepl_x1 * {{.+}});
//CHECK-NEXT: * _d_x;
//CHECK-NEXT: }
//CHECK-NEXT: _delta_x += std::abs(* _d_x * _EERepl_x0 * {{.+}});
//CHECK-NEXT: double _delta_y = 0;
Expand Down Expand Up @@ -176,7 +173,6 @@ float func3(float x, float y) {
//CHECK-NEXT: * _d_y += -_r_d0 * y;
//CHECK-NEXT: * _d_y += y * -_r_d0;
//CHECK-NEXT: _delta_x += std::abs(_r_d0 * _EERepl_x1 * {{.+}});
//CHECK-NEXT: * _d_x;
//CHECK-NEXT: }
//CHECK-NEXT: _delta_x += std::abs(* _d_x * _EERepl_x0 * {{.+}});
//CHECK-NEXT: _delta_y += std::abs(* _d_y * _EERepl_y0 * {{.+}});
Expand Down Expand Up @@ -236,7 +232,6 @@ float func5(float x, float y) {
//CHECK-NEXT: float _r0 = _r_d0 * clad::custom_derivatives{{(::std)?}}::sin_pushforward(x, 1.F).pushforward;
//CHECK-NEXT: * _d_x += _r0;
//CHECK-NEXT: _delta_y += std::abs(_r_d0 * _EERepl_y1 * {{.+}});
//CHECK-NEXT: * _d_y;
//CHECK-NEXT: }
//CHECK-NEXT: double _delta_x = 0;
//CHECK-NEXT: _delta_x += std::abs(* _d_x * x * {{.+}});
Expand Down
27 changes: 11 additions & 16 deletions test/ErrorEstimation/ConditonalStatements.C
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,13 @@ float func(float x, float y) {
//CHECK-NEXT: * _d_y += _r_d0 * x;
//CHECK-NEXT: * _d_x += y * _r_d0;
//CHECK-NEXT: _delta_y += std::abs(_r_d0 * _EERepl_y1 * {{.+}});
//CHECK-NEXT: * _d_y;
//CHECK-NEXT: }
//CHECK-NEXT: } else {
//CHECK-NEXT: {
//CHECK-NEXT: x = _t2;
//CHECK-NEXT: float _r_d2 = * _d_x;
//CHECK-NEXT: * _d_x -= _r_d2;
//CHECK-NEXT: * _d_y += _r_d2;
//CHECK-NEXT: * _d_x;
//CHECK-NEXT: }
//CHECK-NEXT: {
//CHECK-NEXT: temp = _t1;
Expand Down Expand Up @@ -189,20 +187,17 @@ float func4(float x, float y) {
//CHECK-NEXT: float _r0 = 1 * -y / (x * x);
//CHECK-NEXT: * _d_x += _r0;
//CHECK-NEXT: }
//CHECK-NEXT: {
//CHECK-NEXT: if (_cond0) {
//CHECK-NEXT: x = _t0;
//CHECK-NEXT: float _r_d0 = * _d_x;
//CHECK-NEXT: _delta_x += std::abs(_r_d0 * _EERepl_x1 * {{.+}});
//CHECK-NEXT: } else {
//CHECK-NEXT: x = _t1;
//CHECK-NEXT: float _r_d1 = * _d_x;
//CHECK-NEXT: * _d_x -= _r_d1;
//CHECK-NEXT: * _d_x += _r_d1 * x;
//CHECK-NEXT: * _d_x += x * _r_d1;
//CHECK-NEXT: _delta_x += std::abs(_r_d1 * _EERepl_x2 * {{.+}});
//CHECK-NEXT: }
//CHECK-NEXT: _cond0 ? * _d_x : * _d_x;
//CHECK-NEXT: if (_cond0) {
//CHECK-NEXT: x = _t0;
//CHECK-NEXT: float _r_d0 = * _d_x;
//CHECK-NEXT: _delta_x += std::abs(_r_d0 * _EERepl_x1 * {{.+}});
//CHECK-NEXT: } else {
//CHECK-NEXT: x = _t1;
//CHECK-NEXT: float _r_d1 = * _d_x;
//CHECK-NEXT: * _d_x -= _r_d1;
//CHECK-NEXT: * _d_x += _r_d1 * x;
//CHECK-NEXT: * _d_x += x * _r_d1;
//CHECK-NEXT: _delta_x += std::abs(_r_d1 * _EERepl_x2 * {{.+}});
//CHECK-NEXT: }
//CHECK-NEXT: _delta_x += std::abs(* _d_x * _EERepl_x0 * {{.+}});
//CHECK-NEXT: double _delta_y = 0;
Expand Down
3 changes: 0 additions & 3 deletions test/ErrorEstimation/LoopsAndArrays.C
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ float func3(float x, float y) {
//CHECK-NEXT: _d_arr[1] += _r_d2;
//CHECK-NEXT: _delta_arr[2] += std::abs(_r_d2 * _EERepl_arr2 * {{.+}});
//CHECK-NEXT: _final_error += _delta_arr[2];
//CHECK-NEXT: _d_arr[2];
//CHECK-NEXT: }
//CHECK-NEXT: {
//CHECK-NEXT: arr[1] = _t1;
Expand All @@ -168,7 +167,6 @@ float func3(float x, float y) {
//CHECK-NEXT: * _d_x += x * _r_d1;
//CHECK-NEXT: _delta_arr[1] += std::abs(_r_d1 * _EERepl_arr1 * {{.+}});
//CHECK-NEXT: _final_error += _delta_arr[1];
//CHECK-NEXT: _d_arr[1];
//CHECK-NEXT: }
//CHECK-NEXT: {
//CHECK-NEXT: arr[0] = _t0;
Expand All @@ -178,7 +176,6 @@ float func3(float x, float y) {
//CHECK-NEXT: * _d_y += _r_d0;
//CHECK-NEXT: _delta_arr[0] += std::abs(_r_d0 * _EERepl_arr0 * {{.+}});
//CHECK-NEXT: _final_error += _delta_arr[0];
//CHECK-NEXT: _d_arr[0];
//CHECK-NEXT: }
//CHECK-NEXT: double _delta_x = 0;
//CHECK-NEXT: _delta_x += std::abs(* _d_x * x * {{.+}});
Expand Down
28 changes: 2 additions & 26 deletions test/Gradient/Assignments.C
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ double f1(double x, double y) {
//CHECK-NEXT: double _r_d0 = * _d_x;
//CHECK-NEXT: * _d_x -= _r_d0;
//CHECK-NEXT: * _d_y += _r_d0;
//CHECK-NEXT: * _d_x;
//CHECK-NEXT: }
//CHECK-NEXT: }

Expand All @@ -53,7 +52,6 @@ double f2(double x, double y) {
//CHECK-NEXT: double _r_d0 = * _d_x;
//CHECK-NEXT: * _d_x -= _r_d0;
//CHECK-NEXT: * _d_y += _r_d0;
//CHECK-NEXT: * _d_x;
//CHECK-NEXT: }
//CHECK-NEXT: }

Expand Down Expand Up @@ -87,30 +85,26 @@ double f3(double x, double y) {
//CHECK-NEXT: double _r_d3 = * _d_x;
//CHECK-NEXT: * _d_x -= _r_d3;
//CHECK-NEXT: * _d_y += _r_d3;
//CHECK-NEXT: * _d_x;
//CHECK-NEXT: }
//CHECK-NEXT: {
//CHECK-NEXT: y = _t2;
//CHECK-NEXT: double _r_d2 = * _d_y;
//CHECK-NEXT: * _d_y -= _r_d2;
//CHECK-NEXT: * _d_x += _r_d2 * x;
//CHECK-NEXT: * _d_x += x * _r_d2;
//CHECK-NEXT: * _d_y;
//CHECK-NEXT: }
//CHECK-NEXT: {
//CHECK-NEXT: x = _t1;
//CHECK-NEXT: double _r_d1 = * _d_x;
//CHECK-NEXT: * _d_x -= _r_d1;
//CHECK-NEXT: * _d_x += _r_d1 * x;
//CHECK-NEXT: * _d_x += x * _r_d1;
//CHECK-NEXT: * _d_x;
//CHECK-NEXT: }
//CHECK-NEXT: {
//CHECK-NEXT: x = _t0;
//CHECK-NEXT: double _r_d0 = * _d_x;
//CHECK-NEXT: * _d_x -= _r_d0;
//CHECK-NEXT: * _d_x += _r_d0;
//CHECK-NEXT: * _d_x;
//CHECK-NEXT: }
//CHECK-NEXT: }

Expand All @@ -135,14 +129,12 @@ double f4(double x, double y) {
//CHECK-NEXT: x = _t1;
//CHECK-NEXT: double _r_d1 = * _d_x;
//CHECK-NEXT: * _d_x -= _r_d1;
//CHECK-NEXT: * _d_x;
//CHECK-NEXT: }
//CHECK-NEXT: {
//CHECK-NEXT: y = _t0;
//CHECK-NEXT: double _r_d0 = * _d_y;
//CHECK-NEXT: * _d_y -= _r_d0;
//CHECK-NEXT: * _d_x += _r_d0;
//CHECK-NEXT: * _d_y;
//CHECK-NEXT: }
//CHECK-NEXT: }

Expand Down Expand Up @@ -325,13 +317,11 @@ double f7(double x, double y) {
//CHECK-NEXT: * _d_x -= _r_d6;
//CHECK-NEXT: _d_t[0] += _r_d6;
//CHECK-NEXT: --t[0];
//CHECK-NEXT: * _d_x;
//CHECK-NEXT: }
//CHECK-NEXT: {
//CHECK-NEXT: t[0] = _t5;
//CHECK-NEXT: double _r_d5 = _d_t[0];
//CHECK-NEXT: _d_t[1] += -_r_d5;
//CHECK-NEXT: _d_t[0];
//CHECK-NEXT: }
//CHECK-NEXT: {
//CHECK-NEXT: t[0] = _t4;
Expand All @@ -340,46 +330,35 @@ double f7(double x, double y) {
//CHECK-NEXT: _d_t[0] += _r_d4 / t[1];
//CHECK-NEXT: double _r0 = _r_d4 * -t[0] / (t[1] * t[1]);
//CHECK-NEXT: _d_t[1] += _r0;
//CHECK-NEXT: _d_t[0];
//CHECK-NEXT: }
//CHECK-NEXT: {
//CHECK-NEXT: t[0] = _t3;
//CHECK-NEXT: double _r_d3 = _d_t[0];
//CHECK-NEXT: _d_t[0] -= _r_d3;
//CHECK-NEXT: _d_t[0] += _r_d3 * t[1];
//CHECK-NEXT: _d_t[1] += t[0] * _r_d3;
//CHECK-NEXT: _d_t[0];
//CHECK-NEXT: }
//CHECK-NEXT: {
//CHECK-NEXT: t[0] = _t2;
//CHECK-NEXT: double _r_d2 = _d_t[0];
//CHECK-NEXT: _d_t[1] += _r_d2;
//CHECK-NEXT: _d_t[0];
//CHECK-NEXT: }
//CHECK-NEXT: {
//CHECK-NEXT: x = _t1;
//CHECK-NEXT: double _r_d1 = * _d_x;
//CHECK-NEXT: * _d_x -= _r_d1;
//CHECK-NEXT: * _d_y += _r_d1;
//CHECK-NEXT: * _d_x;
//CHECK-NEXT: }
//CHECK-NEXT: {
//CHECK-NEXT: t[0] = _t0;
//CHECK-NEXT: double _r_d0 = _d_t[0];
//CHECK-NEXT: _d_t[0] -= _r_d0;
//CHECK-NEXT: * _d_x += _r_d0;
//CHECK-NEXT: _d_t[0];
//CHECK-NEXT: }
//CHECK-NEXT: ++t[0];
//CHECK-NEXT: --t[0];
//CHECK-NEXT: {
//CHECK-NEXT: t[0]++;
//CHECK-NEXT: _d_t[0];
//CHECK-NEXT: }
//CHECK-NEXT: {
//CHECK-NEXT: t[0]--;
//CHECK-NEXT: _d_t[0];
//CHECK-NEXT: }
//CHECK-NEXT: t[0]++;
//CHECK-NEXT: t[0]--;
//CHECK-NEXT: {
//CHECK-NEXT: * _d_x += _d_t[1];
//CHECK-NEXT: * _d_x += _d_t[2] * x;
Expand Down Expand Up @@ -426,7 +405,6 @@ double f8(double x, double y) {
//CHECK-NEXT: double _r_d3 = _d_t[1];
//CHECK-NEXT: _d_t[1] -= _r_d3;
//CHECK-NEXT: _d_t[2] += _r_d3;
//CHECK-NEXT: _d_t[3];
//CHECK-NEXT: }
//CHECK-NEXT: {
//CHECK-NEXT: * _d_x += _d_t[1];
Expand Down Expand Up @@ -853,7 +831,6 @@ double f20(double x, double y) {
//CHECK-NEXT: * _d_x -= _r_d1;
//CHECK-NEXT: *_d_r += _r_d1 * y;
//CHECK-NEXT: * _d_y += r * _r_d1;
//CHECK-NEXT: * _d_x;
//CHECK-NEXT: }
//CHECK-NEXT: {
//CHECK-NEXT: r = _t0;
Expand Down Expand Up @@ -881,7 +858,6 @@ double f21 (double x, double y) {
//CHECK-NEXT: * _d_y += 0;
//CHECK-NEXT: y--;
//CHECK-NEXT: * _d_x += _r_d0;
//CHECK-NEXT: * _d_y;
//CHECK-NEXT: }
//CHECK-NEXT: }

Expand Down
Loading

0 comments on commit d7e5434

Please sign in to comment.