diff --git a/enzyme/test/Integration/Sparse/cloth.cpp b/enzyme/test/Integration/Sparse/cloth.cpp index db8be5579b5d..7c78a08ff566 100644 --- a/enzyme/test/Integration/Sparse/cloth.cpp +++ b/enzyme/test/Integration/Sparse/cloth.cpp @@ -209,20 +209,20 @@ static void err_store(T val, unsigned long long offset, size_t i) { template __attribute__((always_inline)) -static T zero_load(unsigned long long offset, size_t i, std::vector> &hess) { +static T zero_load(unsigned long long offset, size_t i, std::vector> &hess) { return T(0); } __attribute__((enzyme_sparse_accumulate)) -void inner_store(size_t offset, size_t i, float val, std::vector> &hess) { - hess.push_back(std::tuple(offset, i, val)); +void inner_store(size_t offset, size_t i, float val, std::vector> &hess) { + hess.push_back(Triple(offset, i, val)); } template __attribute__((always_inline)) -static void csr_store(T val, unsigned long long offset, size_t i, std::vector> &hess) { +static void csr_store(T val, unsigned long long offset, size_t i, std::vector> &hess) { if (val == 0.0) return; offset /= sizeof(T); inner_store(offset, i, val, hess); @@ -230,7 +230,7 @@ static void csr_store(T val, unsigned long long offset, size_t i, std::vector __attribute__((noinline)) -std::vector> hessian( +std::vector> hessian( const T *__restrict__ pos0, const int *__restrict__ edges, const int num_edges, @@ -244,7 +244,7 @@ std::vector> hessian( const T *__restrict__ pos, const size_t num_verts) { - std::vector> hess; + std::vector> hess; __builtin_assume(num_verts != 0); for (size_t i=0; i<3*num_verts; i++) __enzyme_fwddiff((void *)gradient_ip, @@ -309,8 +309,8 @@ int main() { const size_t num_verts = 4; auto hess_verts = hessian(pos0, edges, num_edges, faces, num_faces, flaps, num_flaps, edge_coefficient, face_coefficient, bending_stiffness, pos, num_verts); - for (auto hess : hess_verts) { - printf("i=%lu, j=%lu, val=%f", std::get<0>(hess), std::get<1>(hess), std::get<2>(hess)); + for (auto &hess : hess_verts) { + printf("i=%lu, j=%lu, val=%f", hess.row, hess.col, hess.val); } return 0; diff --git a/enzyme/test/Integration/Sparse/eigen_analysis.cpp b/enzyme/test/Integration/Sparse/eigen_analysis.cpp index ba96ec1f58b7..0e0ae9c1137e 100644 --- a/enzyme/test/Integration/Sparse/eigen_analysis.cpp +++ b/enzyme/test/Integration/Sparse/eigen_analysis.cpp @@ -9,7 +9,6 @@ #include #include -#include #include #include #include @@ -18,16 +17,16 @@ #include "matrix.h" - +template __attribute__((always_inline)) -static float eigenstuffM(const float *__restrict__ x, size_t n, const int *__restrict__ faces, const float *__restrict__ pos0) { - float sum = 0; +static T eigenstuffM(const T *__restrict__ x, size_t n, const int *__restrict__ faces, const T *__restrict__ pos0) { + T sum = 0; __builtin_assume(n != 0); for (size_t idx=0; idx __attribute__((always_inline)) -static float eigenstuffL(const float *__restrict__ x, size_t num_faces, const int *__restrict__ faces, const float *__restrict__ verts) { - float sum = 0; +static T eigenstuffL(const T *__restrict__ x, size_t num_faces, const int *__restrict__ faces, const T *__restrict__ verts) { + T sum = 0; __builtin_assume(num_faces != 0); for (size_t idx=0; idx(g, g) * area(&verts[3*i], &verts[3*j], &verts[3*k]); + sum += dot_product(g, g) * area(&verts[3*i], &verts[3*j], &verts[3*k]); } return sum; } - +template __attribute__((always_inline)) -static void gradient_ip(const float *__restrict__ x, const size_t num_faces, const int* faces, const float *__restrict__ pos, float *__restrict__ out) +static void gradient_ip(const T *__restrict__ x, const size_t num_faces, const int* faces, const T *__restrict__ pos, T *__restrict__ out) { - __enzyme_autodiff((void *)eigenstuffM, + __enzyme_autodiff((void *)eigenstuffM, enzyme_const, x, enzyme_const, num_faces, enzyme_const, faces, @@ -102,37 +102,38 @@ static void err_store(T val, unsigned long long offset, size_t i) { template __attribute__((always_inline)) -static T zero_load(unsigned long long offset, size_t i, std::vector> &hess) { +static T zero_load(unsigned long long offset, size_t i, std::vector> &hess) { return T(0); } __attribute__((enzyme_sparse_accumulate)) -void inner_store(size_t offset, size_t i, float val, std::vector> &hess) { - hess.push_back(std::tuple(offset, i, val)); +void inner_store(size_t offset, size_t i, float val, std::vector> &hess) { + hess.push_back(Triple(offset, i, val)); } template __attribute__((always_inline)) -static void csr_store(T val, unsigned long long offset, size_t i, std::vector> &hess) { +static void csr_store(T val, unsigned long long offset, size_t i, std::vector> &hess) { if (val == 0.0) return; offset /= sizeof(T); inner_store(offset, i, val, hess); } +template __attribute__((noinline)) -std::vector> hessian(const float* x, size_t num_faces, const int* faces, const float* pos, size_t num_verts) +std::vector> hessian(const T* x, size_t num_faces, const int* faces, const T* pos, size_t num_verts) { - std::vector> hess; + std::vector> hess; __builtin_assume(num_verts != 0); for (size_t i=0; i<3*num_verts; i++) - __enzyme_fwddiff((void *)gradient_ip, + __enzyme_fwddiff((void *)gradient_ip, enzyme_const, x, enzyme_const, num_faces, enzyme_const, faces, - enzyme_dup, pos, __enzyme_todense(ident_load, err_store, i), - enzyme_dupnoneed, nullptr, __enzyme_todense(zero_load, csr_store, i, &hess)); + enzyme_dup, pos, __enzyme_todense(ident_load, err_store, i), + enzyme_dupnoneed, nullptr, __enzyme_todense(zero_load, csr_store, i, &hess)); return hess; } @@ -168,8 +169,8 @@ int main() { auto hess_verts = hessian(x, num_faces, faces, verts, num_verts); - for (auto hess : hess_verts) { - printf("i=%lu, j=%lu, val=%f", std::get<0>(hess), std::get<1>(hess), std::get<2>(hess)); + for (auto &hess : hess_verts) { + printf("i=%lu, j=%lu, val=%f", hess.row, hess.col, hess.val); } return 0; diff --git a/enzyme/test/Integration/Sparse/elastic.cpp b/enzyme/test/Integration/Sparse/elastic.cpp index fc3c9afb4bc9..07a8bbebb9f2 100644 --- a/enzyme/test/Integration/Sparse/elastic.cpp +++ b/enzyme/test/Integration/Sparse/elastic.cpp @@ -124,23 +124,23 @@ static void err_store(T val, unsigned long long offset, size_t i) { template __attribute__((always_inline)) -static T zero_load(unsigned long long offset, size_t i, std::vector> &hess) { +static T zero_load(unsigned long long offset, size_t i, std::vector> &hess) { return T(0); } __attribute__((enzyme_sparse_accumulate)) -void inner_store(size_t offset, size_t i, float val, std::vector> &hess) { - hess.push_back(std::tuple(offset, i, val)); +void inner_store(size_t offset, size_t i, float val, std::vector> &hess) { + hess.push_back(Triple(offset, i, val)); } __attribute__((enzyme_sparse_accumulate)) -void inner_store(size_t offset, size_t i, double val, std::vector> &hess) { - hess.push_back(std::tuple(offset, i, val)); +void inner_store(size_t offset, size_t i, double val, std::vector> &hess) { + hess.push_back(Triple(offset, i, val)); } template __attribute__((always_inline)) -static void csr_store(T val, unsigned long long offset, size_t i, std::vector> &hess) { +static void csr_store(T val, unsigned long long offset, size_t i, std::vector> &hess) { if (val == 0.0) return; offset /= sizeof(T); inner_store(offset, i, val, hess); @@ -148,7 +148,7 @@ static void csr_store(T val, unsigned long long offset, size_t i, std::vector __attribute__((noinline)) -std::vector> hessian( +std::vector> hessian( const T *__restrict__ pos0, const int tets[][4], const size_t n, @@ -157,7 +157,7 @@ std::vector> hessian( const T *__restrict__ pos, const size_t num_tets) { - std::vector> hess; + std::vector> hess; __builtin_assume(num_tets != 0); for (size_t i=0; i<4*num_tets; i++) __enzyme_fwddiff((void *)gradient_ip, @@ -208,8 +208,8 @@ int main() { const size_t num_tets = 1; auto hess_verts = hessian(pos0, tets, n, youngs_modulus, poisson_ratio, pos, num_tets); - for (auto hess : hess_verts) { - printf("i=%lu, j=%lu, val=%f", std::get<0>(hess), std::get<1>(hess), std::get<2>(hess)); + for (auto &hess : hess_verts) { + printf("i=%lu, j=%lu, val=%f", hess.row, hess.col, hess.val); } return 0; diff --git a/enzyme/test/Integration/Sparse/matrix.h b/enzyme/test/Integration/Sparse/matrix.h index c9fd565f587e..0f754dc8823a 100644 --- a/enzyme/test/Integration/Sparse/matrix.h +++ b/enzyme/test/Integration/Sparse/matrix.h @@ -1,4 +1,14 @@ #include +#include + +template +struct Triple { + size_t row; + size_t col; + T val; + Triple(Triple&&) = default; + Triple(size_t row, size_t col, T val) : row(row), col(col), val(val) {} +}; extern int enzyme_width; extern int enzyme_dup;