diff --git a/unittests/Kokkos/ParallelAdd.h b/unittests/Kokkos/ParallelAdd.h index 132a99f14..6d8b3d039 100644 --- a/unittests/Kokkos/ParallelAdd.h +++ b/unittests/Kokkos/ParallelAdd.h @@ -1,4 +1,5 @@ -// source: https://github.com/kliegeois/clad/blob/kokkos-PR/unittests/Kokkos/parallel_sum.hpp +// source: +// https://github.com/kliegeois/clad/blob/kokkos-PR/unittests/Kokkos/parallel_sum.hpp #ifndef KOKKOS_UNITTEST_PARALLELSUM #define KOKKOS_UNITTEST_PARALLELSUM @@ -9,29 +10,27 @@ namespace kokkos_builtin_derivative { // Parallel sum: -template +template struct ViewSum; template struct ViewSum { - template - static void execute(ResultT& result, const Viewtype& v, const ExecSpace space = ExecSpace()) { + template + static void execute(ResultT& result, const Viewtype& v, + const ExecSpace space = ExecSpace()) { - using policy_type = Kokkos::RangePolicy>; - using value_type = typename Viewtype::value_type; + using policy_type = + Kokkos::RangePolicy>; + using value_type = typename Viewtype::value_type; value_type sum; Kokkos::parallel_reduce( - "ViewSum-1D", - policy_type(space, 0, v.extent(0)), - KOKKOS_LAMBDA ( - const iType& i0, - value_type& update) { - update += v(i0); - }, - sum ); + "ViewSum-1D", policy_type(space, 0, v.extent(0)), + KOKKOS_LAMBDA(const iType& i0, value_type& update) { update += v(i0); }, + sum); result += sum; } @@ -40,8 +39,9 @@ struct ViewSum { template struct ViewSum { - template - static void execute(ResultT& result, const Viewtype& v, const ExecSpace space = ExecSpace()) { + template + static void execute(ResultT& result, const Viewtype& v, + const ExecSpace space = ExecSpace()) { static const Kokkos::Iterate outer_iteration_pattern = Kokkos::layout_iterate_type_selector::outer_iteration_pattern; @@ -49,22 +49,18 @@ struct ViewSum { Kokkos::layout_iterate_type_selector::inner_iteration_pattern; using iterate_type = Kokkos::Rank<2, outer_iteration_pattern, inner_iteration_pattern>; - using policy_type = - Kokkos::MDRangePolicy>; - using value_type = typename Viewtype::value_type; + using policy_type = Kokkos::MDRangePolicy>; + using value_type = typename Viewtype::value_type; value_type sum; Kokkos::parallel_reduce( - "ViewSum-2D", - policy_type(space, {0, 0}, {v.extent(0), v.extent(1)}), - KOKKOS_LAMBDA ( - const iType& i0, - const iType& i1, - value_type& update) { - update += v(i0, i1); + "ViewSum-2D", policy_type(space, {0, 0}, {v.extent(0), v.extent(1)}), + KOKKOS_LAMBDA(const iType& i0, const iType& i1, value_type& update) { + update += v(i0, i1); }, - sum ); + sum); result += sum; } @@ -73,8 +69,9 @@ struct ViewSum { template struct ViewSum { - template - static void execute(ResultT& result, const Viewtype& v, const ExecSpace space = ExecSpace()) { + template + static void execute(ResultT& result, const Viewtype& v, + const ExecSpace space = ExecSpace()) { static const Kokkos::Iterate outer_iteration_pattern = Kokkos::layout_iterate_type_selector::outer_iteration_pattern; @@ -82,23 +79,18 @@ struct ViewSum { Kokkos::layout_iterate_type_selector::inner_iteration_pattern; using iterate_type = Kokkos::Rank<3, outer_iteration_pattern, inner_iteration_pattern>; - using policy_type = - Kokkos::MDRangePolicy>; - using value_type = typename Viewtype::value_type; + using policy_type = Kokkos::MDRangePolicy>; + using value_type = typename Viewtype::value_type; value_type sum; Kokkos::parallel_reduce( "ViewSum-3D", policy_type(space, {0, 0}, {v.extent(0), v.extent(1), v.extent(2)}), - KOKKOS_LAMBDA ( - const iType& i0, - const iType& i1, - const iType& i2, - value_type& update) { - update += v(i0, i1, i2); - }, - sum ); + KOKKOS_LAMBDA(const iType& i0, const iType& i1, const iType& i2, + value_type& update) { update += v(i0, i1, i2); }, + sum); result += sum; } @@ -106,46 +98,44 @@ struct ViewSum { // Parallel add -template +template struct ViewAdd; template struct ViewAdd { - template - static void execute(const Viewtype& v, ResultT& update, const ExecSpace space = ExecSpace()) { + template + static void execute(const Viewtype& v, ResultT& update, + const ExecSpace space = ExecSpace()) { - using policy_type = Kokkos::RangePolicy>; + using policy_type = + Kokkos::RangePolicy>; Kokkos::parallel_for( - "ViewAdd-1D", - policy_type(space, 0, v.extent(0)), - KOKKOS_LAMBDA ( - const iType& i0) { - v(i0) += update; - }); + "ViewAdd-1D", policy_type(space, 0, v.extent(0)), + KOKKOS_LAMBDA(const iType& i0) { v(i0) += update; }); } - template - static void executeView(const Viewtype& v, ResultT& update, const ExecSpace space = ExecSpace()) { + template + static void executeView(const Viewtype& v, ResultT& update, + const ExecSpace space = ExecSpace()) { - using policy_type = Kokkos::RangePolicy>; + using policy_type = + Kokkos::RangePolicy>; Kokkos::parallel_for( - "ViewAdd-1D", - policy_type(space, 0, v.extent(0)), - KOKKOS_LAMBDA ( - const iType& i0) { - v(i0) += update(i0); - }); + "ViewAdd-1D", policy_type(space, 0, v.extent(0)), + KOKKOS_LAMBDA(const iType& i0) { v(i0) += update(i0); }); } }; template struct ViewAdd { - template - static void execute(const Viewtype& v, ResultT& update, const ExecSpace space = ExecSpace()) { + template + static void execute(const Viewtype& v, ResultT& update, + const ExecSpace space = ExecSpace()) { static const Kokkos::Iterate outer_iteration_pattern = Kokkos::layout_iterate_type_selector::outer_iteration_pattern; @@ -153,21 +143,19 @@ struct ViewAdd { Kokkos::layout_iterate_type_selector::inner_iteration_pattern; using iterate_type = Kokkos::Rank<2, outer_iteration_pattern, inner_iteration_pattern>; - using policy_type = - Kokkos::MDRangePolicy>; + using policy_type = Kokkos::MDRangePolicy>; Kokkos::parallel_for( - "ViewAdd-2D", - policy_type(space, {0, 0}, {v.extent(0), v.extent(1)}), - KOKKOS_LAMBDA ( - const iType& i0, - const iType& i1) { - v(i0, i1) += update; + "ViewAdd-2D", policy_type(space, {0, 0}, {v.extent(0), v.extent(1)}), + KOKKOS_LAMBDA(const iType& i0, const iType& i1) { + v(i0, i1) += update; }); } - template - static void executeView(const Viewtype& v, ResultT& update, const ExecSpace space = ExecSpace()) { + template + static void executeView(const Viewtype& v, ResultT& update, + const ExecSpace space = ExecSpace()) { static const Kokkos::Iterate outer_iteration_pattern = Kokkos::layout_iterate_type_selector::outer_iteration_pattern; @@ -175,16 +163,13 @@ struct ViewAdd { Kokkos::layout_iterate_type_selector::inner_iteration_pattern; using iterate_type = Kokkos::Rank<2, outer_iteration_pattern, inner_iteration_pattern>; - using policy_type = - Kokkos::MDRangePolicy>; + using policy_type = Kokkos::MDRangePolicy>; Kokkos::parallel_for( - "ViewAdd-2D", - policy_type(space, {0, 0}, {v.extent(0), v.extent(1)}), - KOKKOS_LAMBDA ( - const iType& i0, - const iType& i1) { - v(i0, i1) += update(i0, i1); + "ViewAdd-2D", policy_type(space, {0, 0}, {v.extent(0), v.extent(1)}), + KOKKOS_LAMBDA(const iType& i0, const iType& i1) { + v(i0, i1) += update(i0, i1); }); } }; @@ -192,8 +177,9 @@ struct ViewAdd { template struct ViewAdd { - template - static void execute(const Viewtype& v, ResultT& update, const ExecSpace space = ExecSpace()) { + template + static void execute(const Viewtype& v, ResultT& update, + const ExecSpace space = ExecSpace()) { static const Kokkos::Iterate outer_iteration_pattern = Kokkos::layout_iterate_type_selector::outer_iteration_pattern; @@ -201,22 +187,20 @@ struct ViewAdd { Kokkos::layout_iterate_type_selector::inner_iteration_pattern; using iterate_type = Kokkos::Rank<3, outer_iteration_pattern, inner_iteration_pattern>; - using policy_type = - Kokkos::MDRangePolicy>; + using policy_type = Kokkos::MDRangePolicy>; Kokkos::parallel_for( "ViewAdd-3D", policy_type(space, {0, 0}, {v.extent(0), v.extent(1), v.extent(2)}), - KOKKOS_LAMBDA ( - const iType& i0, - const iType& i1, - const iType& i2) { - v(i0, i1, i2) += update; + KOKKOS_LAMBDA(const iType& i0, const iType& i1, const iType& i2) { + v(i0, i1, i2) += update; }); } - template - static void executeView(const Viewtype& v, ResultT& update, const ExecSpace space = ExecSpace()) { + template + static void executeView(const Viewtype& v, ResultT& update, + const ExecSpace space = ExecSpace()) { static const Kokkos::Iterate outer_iteration_pattern = Kokkos::layout_iterate_type_selector::outer_iteration_pattern; @@ -224,24 +208,21 @@ struct ViewAdd { Kokkos::layout_iterate_type_selector::inner_iteration_pattern; using iterate_type = Kokkos::Rank<3, outer_iteration_pattern, inner_iteration_pattern>; - using policy_type = - Kokkos::MDRangePolicy>; + using policy_type = Kokkos::MDRangePolicy>; Kokkos::parallel_for( "ViewAdd-3D", policy_type(space, {0, 0}, {v.extent(0), v.extent(1), v.extent(2)}), - KOKKOS_LAMBDA ( - const iType& i0, - const iType& i1, - const iType& i2) { - v(i0, i1, i2) += update(i0, i1, i2); + KOKKOS_LAMBDA(const iType& i0, const iType& i1, const iType& i2) { + v(i0, i1, i2) += update(i0, i1, i2); }); } }; - template -void parallel_sum(typename Kokkos::ViewTraits::value_type &sum, const Kokkos::View A) { +void parallel_sum(typename Kokkos::ViewTraits::value_type& sum, + const Kokkos::View A) { using ViewtypeA = Kokkos::View; Kokkos::fence(); if (A.span_is_contiguous()) { @@ -249,22 +230,25 @@ void parallel_sum(typename Kokkos::ViewTraits::value_type &sum, const using ViewTypeFlat = Kokkos::View< typename ViewtypeA::value_type*, Kokkos::LayoutRight, Kokkos::Device>, Kokkos::MemoryTraits<0>>; ViewTypeFlat A_flat(A.data(), A.size()); - ViewSum::template execute(sum, A_flat); - } - else { - ViewSum::template execute(sum, A); + ViewSum::template execute< + typename ViewTypeFlat::execution_space>(sum, A_flat); + } else { + ViewSum::template execute(sum, A); } Kokkos::fence(); } template -void parallel_sum(const ExecSpace& space, typename Kokkos::ViewTraits::value_type &sum, const Kokkos::View A) { +void parallel_sum(const ExecSpace& space, + typename Kokkos::ViewTraits::value_type& sum, + const Kokkos::View A) { using ViewtypeA = Kokkos::View; space.fence(); if (A.span_is_contiguous()) { @@ -272,22 +256,24 @@ void parallel_sum(const ExecSpace& space, typename Kokkos::ViewTraits using ViewTypeFlat = Kokkos::View< typename ViewtypeA::value_type*, Kokkos::LayoutRight, Kokkos::Device>, Kokkos::MemoryTraits<0>>; ViewTypeFlat A_flat(A.data(), A.size()); - ViewSum::template execute(sum, A_flat, space); - } - else { - ViewSum::template execute(sum, A, space); + ViewSum::template execute< + typename ViewTypeFlat::execution_space>(sum, A_flat, space); + } else { + ViewSum::template execute(sum, A, space); } space.fence(); } template -void parallel_add(Kokkos::View A, typename Kokkos::ViewTraits::const_value_type b) { +void parallel_add(Kokkos::View A, + typename Kokkos::ViewTraits::const_value_type b) { using ViewtypeA = Kokkos::View; Kokkos::fence(); if (A.span_is_contiguous()) { @@ -295,22 +281,24 @@ void parallel_add(Kokkos::View A, typename Kokkos::ViewTraits>, Kokkos::MemoryTraits<0>>; ViewTypeFlat A_flat(A.data(), A.size()); - ViewAdd::template execute(A_flat, b); - } - else { - ViewAdd::template execute(A, b); + ViewAdd::template execute< + typename ViewTypeFlat::execution_space>(A_flat, b); + } else { + ViewAdd::template execute(A, b); } Kokkos::fence(); } template -void parallel_add(const ExecSpace& space, Kokkos::View A, typename Kokkos::ViewTraits::const_value_type b) { +void parallel_add(const ExecSpace& space, Kokkos::View A, + typename Kokkos::ViewTraits::const_value_type b) { using ViewtypeA = Kokkos::View; space.fence(); if (A.span_is_contiguous()) { @@ -318,16 +306,17 @@ void parallel_add(const ExecSpace& space, Kokkos::View A, typename Ko using ViewTypeFlat = Kokkos::View< typename ViewtypeA::value_type*, Kokkos::LayoutRight, Kokkos::Device>, Kokkos::MemoryTraits<0>>; ViewTypeFlat A_flat(A.data(), A.size()); - ViewAdd::template execute(A_flat, b, space); - } - else { - ViewAdd::template execute(A, b, space); + ViewAdd::template execute< + typename ViewTypeFlat::execution_space>(A_flat, b, space); + } else { + ViewAdd::template execute(A, b, space); } space.fence(); } @@ -338,11 +327,12 @@ void parallel_add(Kokkos::View A, const Kokkos::View B) { using ViewtypeA = Kokkos::View; Kokkos::fence(); - ViewAdd::template executeView(A, B); + ViewAdd::template executeView(A, B); Kokkos::fence(); } -} +} // namespace kokkos_builtin_derivative #endif \ No newline at end of file diff --git a/unittests/Kokkos/ParallelFor.cpp b/unittests/Kokkos/ParallelFor.cpp index 4d589f48a..69494226b 100644 --- a/unittests/Kokkos/ParallelFor.cpp +++ b/unittests/Kokkos/ParallelFor.cpp @@ -5,92 +5,88 @@ #include "ParallelAdd.h" TEST(ParallelFor, HelloWorldLambdaLoopForward) { - // // check finite difference and forward mode similarity - // const double eps = 1e-5; - // const double tau = 1e-6; // tolerance + // // check finite difference and forward mode similarity + // const double eps = 1e-5; + // const double tau = 1e-6; // tolerance - std::function _f = [](double x) { - using Policy = Kokkos::RangePolicy; - double res[5] = {0}; - Kokkos::parallel_for("HelloWorld-forward", Policy(0, 5), - [&res, x](const int i) { - res[i] = x*x; - }); - // all elements of res should be the same, so return one of them arbitrarily - return res[2]; - }; + std::function _f = [](double x) { + using Policy = Kokkos::RangePolicy; + double res[5] = {0}; + Kokkos::parallel_for("HelloWorld-forward", Policy(0, 5), + [&res, x](const int i) { res[i] = x * x; }); + // all elements of res should be the same, so return one of them arbitrarily + return res[2]; + }; - // // TODO: uncomment this once it has been implemented - // auto f_diff = clad::differentiate(_f, 0/*x*/); - // for (double x = -2; x <= 2; x += 1) { - // double f_diff_ex = f_diff.execute(x); - // double dx_f_FD = finite_difference_tangent(_f, x, eps); - // EXPECT_NEAR(f_diff_ex, dx_f_FD, abs(tau*dx_f_FD)); - // } + // // TODO: uncomment this once it has been implemented + // auto f_diff = clad::differentiate(_f, 0/*x*/); + // for (double x = -2; x <= 2; x += 1) { + // double f_diff_ex = f_diff.execute(x); + // double dx_f_FD = finite_difference_tangent(_f, x, eps); + // EXPECT_NEAR(f_diff_ex, dx_f_FD, abs(tau*dx_f_FD)); + // } } TEST(ParallelFor, HelloWorldLambdaLoopReverse) { - // // check finite difference and reverse mode similarity - // const double eps = 1e-5; - // const double tau = 1e-6; // tolerance + // // check finite difference and reverse mode similarity + // const double eps = 1e-5; + // const double tau = 1e-6; // tolerance - std::function _f = [](double x) { - using Policy = Kokkos::RangePolicy; - double res[5] = {0}; - Kokkos::parallel_for("HelloWorld-reverse", Policy(0, 5), - [&res, x](const int i) { - res[i] = x*x; - }); - // all elements of res should be the same, so return one of them arbitrarily - return res[2]; - }; + std::function _f = [](double x) { + using Policy = Kokkos::RangePolicy; + double res[5] = {0}; + Kokkos::parallel_for("HelloWorld-reverse", Policy(0, 5), + [&res, x](const int i) { res[i] = x * x; }); + // all elements of res should be the same, so return one of them arbitrarily + return res[2]; + }; - // // TODO: uncomment this once it has been implemented - // auto f_grad = clad::gradient(_f); - // for (double x = -2; x <= 2; x += 1) { - // double dx_f_FD = finite_difference_tangent(_f, x, eps); - // double dx; - // f_grad.execute(x, &dx); - // EXPECT_NEAR(dx_f_FD, dx, abs(tau*dx)); - // } + // // TODO: uncomment this once it has been implemented + // auto f_grad = clad::gradient(_f); + // for (double x = -2; x <= 2; x += 1) { + // double dx_f_FD = finite_difference_tangent(_f, x, eps); + // double dx; + // f_grad.execute(x, &dx); + // EXPECT_NEAR(dx_f_FD, dx, abs(tau*dx)); + // } } double parallel_polynomial_for(double x) { // data races - using Policy = Kokkos::RangePolicy; - Kokkos::View res("res"); - res(0) = 0; - Kokkos::parallel_for("polycalc", Policy(0, 5), KOKKOS_LAMBDA(const int i) { - res(0) += pow(x, i+1)/(i+1); - }); - // for (int i = 0; i < 6; ++i) { - // res(0) += pow(x, i+1)/(i+1); - // } - return res(0); + using Policy = Kokkos::RangePolicy; + Kokkos::View res("res"); + res(0) = 0; + Kokkos::parallel_for( + "polycalc", Policy(0, 5), + KOKKOS_LAMBDA(const int i) { res(0) += pow(x, i + 1) / (i + 1); }); + // for (int i = 0; i < 6; ++i) { + // res(0) += pow(x, i+1)/(i+1); + // } + return res(0); } TEST(ParallelFor, ParallelPolynomialForward) { - // // check true derivative and forward mode similarity - // const double tau = 1e-5; // tolerance + // // check true derivative and forward mode similarity + // const double tau = 1e-5; // tolerance - // // TODO: uncomment this once it has been implemented - // auto f_diff = clad::differentiate(parallel_polynomial_for, "x"); - // for (double x = -2; x <= 2; x += 1) { - // double f_diff_ex = f_diff.execute(x); - // double dx_f_true = parallel_polynomial_true_derivative(x); - // EXPECT_NEAR(f_diff_ex, dx_f_true, abs(tau*dx_f_true)); - // } + // // TODO: uncomment this once it has been implemented + // auto f_diff = clad::differentiate(parallel_polynomial_for, "x"); + // for (double x = -2; x <= 2; x += 1) { + // double f_diff_ex = f_diff.execute(x); + // double dx_f_true = parallel_polynomial_true_derivative(x); + // EXPECT_NEAR(f_diff_ex, dx_f_true, abs(tau*dx_f_true)); + // } } TEST(ParallelFor, ParallelPolynomialReverse) { - // // check true derivative and reverse mode similarity - // const double tau = 1e-5; // tolerance + // // check true derivative and reverse mode similarity + // const double tau = 1e-5; // tolerance - // // TODO: uncomment this once it has been implemented - // auto f_grad = clad::gradient(parallel_polynomial_for); - // for (double x = -2; x <= 2; x += 1) { - // double dx_f_true = parallel_polynomial_true_derivative(x); - // double dx = 0; - // f_grad.execute(x, &dx); - // EXPECT_NEAR(dx_f_true, dx, abs(tau*dx)); - // } + // // TODO: uncomment this once it has been implemented + // auto f_grad = clad::gradient(parallel_polynomial_for); + // for (double x = -2; x <= 2; x += 1) { + // double dx_f_true = parallel_polynomial_true_derivative(x); + // double dx = 0; + // f_grad.execute(x, &dx); + // EXPECT_NEAR(dx_f_true, dx, abs(tau*dx)); + // } } \ No newline at end of file diff --git a/unittests/Kokkos/ParallelReduce.cpp b/unittests/Kokkos/ParallelReduce.cpp index 7eb9c1b62..7ee7b92cd 100644 --- a/unittests/Kokkos/ParallelReduce.cpp +++ b/unittests/Kokkos/ParallelReduce.cpp @@ -5,84 +5,91 @@ #include "ParallelAdd.h" TEST(ParallelReduce, HelloWorldLambdaLoopForward) { - // // check finite difference and forward mode similarity - // const double eps = 1e-5; - // const double tau = 1e-6; // tolerance + // // check finite difference and forward mode similarity + // const double eps = 1e-5; + // const double tau = 1e-6; // tolerance - std::function _f = [](double x) { - double res = 0.; - Kokkos::parallel_reduce("HelloWorld-forward", 5, KOKKOS_LAMBDA(const int& i, double& _res){ _res += x; }, res); - // res = 5*x; - return res; - }; + std::function _f = [](double x) { + double res = 0.; + Kokkos::parallel_reduce( + "HelloWorld-forward", 5, + KOKKOS_LAMBDA(const int& i, double& _res) { _res += x; }, res); + // res = 5*x; + return res; + }; - // // TODO: uncomment this once it has been implemented - // auto f_diff = clad::differentiate(_f, 0/*x*/); - // for (double x = -2; x <= 2; x += 1) { - // double f_diff_ex = f_diff.execute(x); - // double dx_f_FD = finite_difference_tangent(_f, x, eps); - // EXPECT_NEAR(f_diff_ex, dx_f_FD, abs(tau*dx_f_FD)); - // } + // // TODO: uncomment this once it has been implemented + // auto f_diff = clad::differentiate(_f, 0/*x*/); + // for (double x = -2; x <= 2; x += 1) { + // double f_diff_ex = f_diff.execute(x); + // double dx_f_FD = finite_difference_tangent(_f, x, eps); + // EXPECT_NEAR(f_diff_ex, dx_f_FD, abs(tau*dx_f_FD)); + // } } TEST(ParallelReduce, HelloWorldLambdaLoopReverse) { - // // check finite difference and reverse mode similarity - // const double eps = 1e-5; - // const double tau = 1e-6; // tolerance + // // check finite difference and reverse mode similarity + // const double eps = 1e-5; + // const double tau = 1e-6; // tolerance - std::function _f = [](double x) { - double res = 0.; - Kokkos::parallel_reduce("HelloWorld-reverse", 5, KOKKOS_LAMBDA(const int& i, double& _res){ _res += x; }, res); - // res = 5*x; - return res; - }; + std::function _f = [](double x) { + double res = 0.; + Kokkos::parallel_reduce( + "HelloWorld-reverse", 5, + KOKKOS_LAMBDA(const int& i, double& _res) { _res += x; }, res); + // res = 5*x; + return res; + }; - // // TODO: uncomment this once it has been implemented - // auto f_grad = clad::gradient(_f); - // for (double x = -2; x <= 2; x += 1) { - // double dx_f_FD = finite_difference_tangent(_f, x, eps); - // double dx; - // f_grad.execute(x, &dx); - // EXPECT_NEAR(dx_f_FD, dx, abs(tau*dx)); - // } + // // TODO: uncomment this once it has been implemented + // auto f_grad = clad::gradient(_f); + // for (double x = -2; x <= 2; x += 1) { + // double dx_f_FD = finite_difference_tangent(_f, x, eps); + // double dx; + // f_grad.execute(x, &dx); + // EXPECT_NEAR(dx_f_FD, dx, abs(tau*dx)); + // } } double parallel_polynomial_reduce(double x) { - using Policy = Kokkos::RangePolicy; - Kokkos::View res("res"); - res(0) = 0; - Kokkos::parallel_reduce("polycalc", Policy(0, 5), KOKKOS_LAMBDA(const int& i, double& _res) { - _res += pow(x, i+1)/(i+1); - }, res(0)); - // for (int i = 0; i < 6; ++i) { - // res(0) += pow(x, i+1)/(i+1); - // } - return res(0); + using Policy = Kokkos::RangePolicy; + Kokkos::View res("res"); + res(0) = 0; + Kokkos::parallel_reduce( + "polycalc", Policy(0, 5), + KOKKOS_LAMBDA(const int& i, double& _res) { + _res += pow(x, i + 1) / (i + 1); + }, + res(0)); + // for (int i = 0; i < 6; ++i) { + // res(0) += pow(x, i+1)/(i+1); + // } + return res(0); } TEST(ParallelReduce, ParallelPolynomialForward) { - // // check true derivative and forward mode similarity - // const double tau = 1e-5; // tolerance + // // check true derivative and forward mode similarity + // const double tau = 1e-5; // tolerance - // // TODO: uncomment this once it has been implemented - // auto f_diff = clad::differentiate(parallel_polynomial_reduce, "x"); - // for (double x = -2; x <= 2; x += 1) { - // double f_diff_ex = f_diff.execute(x); - // double dx_f_true = parallel_polynomial_true_derivative(x); - // EXPECT_NEAR(f_diff_ex, dx_f_true, abs(tau*dx_f_true)); - // } + // // TODO: uncomment this once it has been implemented + // auto f_diff = clad::differentiate(parallel_polynomial_reduce, "x"); + // for (double x = -2; x <= 2; x += 1) { + // double f_diff_ex = f_diff.execute(x); + // double dx_f_true = parallel_polynomial_true_derivative(x); + // EXPECT_NEAR(f_diff_ex, dx_f_true, abs(tau*dx_f_true)); + // } } TEST(ParallelReduce, ParallelPolynomialReverse) { - // // check true derivative and reverse mode similarity - // const double tau = 1e-5; // tolerance + // // check true derivative and reverse mode similarity + // const double tau = 1e-5; // tolerance - // // TODO: uncomment this once it has been implemented - // auto f_grad = clad::gradient(parallel_polynomial_reduce); - // for (double x = -2; x <= 2; x += 1) { - // double dx_f_true = parallel_polynomial_true_derivative(x); - // double dx = 0; - // f_grad.execute(x, &dx); - // EXPECT_NEAR(dx_f_true, dx, abs(tau*dx)); - // } + // // TODO: uncomment this once it has been implemented + // auto f_grad = clad::gradient(parallel_polynomial_reduce); + // for (double x = -2; x <= 2; x += 1) { + // double dx_f_true = parallel_polynomial_true_derivative(x); + // double dx = 0; + // f_grad.execute(x, &dx); + // EXPECT_NEAR(dx_f_true, dx, abs(tau*dx)); + // } } \ No newline at end of file diff --git a/unittests/Kokkos/TestUtils.h b/unittests/Kokkos/TestUtils.h index cb72905b7..ab55219aa 100644 --- a/unittests/Kokkos/TestUtils.h +++ b/unittests/Kokkos/TestUtils.h @@ -3,19 +3,24 @@ #ifndef KOKKOS_UNITTEST_UTILS #define KOKKOS_UNITTEST_UTILS -template // comparison with the finite difference approx. has been tested in the initial PR for Kokkos-aware Clad by Kim Liegeois -T finite_difference_tangent(std::function func, const T& x, const T& epsilon) { - return (func(x+epsilon)-func(x-epsilon)) / (2 * epsilon); +template // comparison with the finite difference approx. has been + // tested in the initial PR for Kokkos-aware Clad by Kim + // Liegeois +T finite_difference_tangent(std::function func, const T& x, + const T& epsilon) { + return (func(x + epsilon) - func(x - epsilon)) / (2 * epsilon); } -double parallel_polynomial_true_derivative(double x) { // the true derivative of the polynomial tested in ParallelFor.cpp and ParallelReduce.cpp - double res = 0; - double x_c = 1; - for (unsigned i = 0; i < 6; ++i) { - res += x_c; - x_c *= x; - } - return res; +double parallel_polynomial_true_derivative( + double x) { // the true derivative of the polynomial tested in + // ParallelFor.cpp and ParallelReduce.cpp + double res = 0; + double x_c = 1; + for (unsigned i = 0; i < 6; ++i) { + res += x_c; + x_c *= x; + } + return res; } #endif \ No newline at end of file diff --git a/unittests/Kokkos/ViewBasics.cpp b/unittests/Kokkos/ViewBasics.cpp index 3453c4738..d08c94a61 100644 --- a/unittests/Kokkos/ViewBasics.cpp +++ b/unittests/Kokkos/ViewBasics.cpp @@ -1,23 +1,24 @@ // Very basic Kokkos::View usage test that should work by all means -// inspired by https://github.com/kliegeois/clad/blob/kokkos-PR/unittests/Kokkos/view_access.cpp +// inspired by +// https://github.com/kliegeois/clad/blob/kokkos-PR/unittests/Kokkos/view_access.cpp // it has been modified to match gtest guidelines and improve readability +#include "ParallelAdd.h" +#include "TestUtils.h" #include #include "clad/Differentiator/Differentiator.h" #include "gtest/gtest.h" -#include "TestUtils.h" -#include "ParallelAdd.h" double f(double x, double y) { const int N = 2; - Kokkos::View a("a", N); - Kokkos::View b("b", N); + Kokkos::View a("a", N); + Kokkos::View b("b", N); - a(0,0) = x; - b(0,0) = y*x; + a(0, 0) = x; + b(0, 0) = y * x; - return a(0,0) + a(0,0)*b(0,0) + b(0,0); + return a(0, 0) + a(0, 0) * b(0, 0) + b(0, 0); } TEST(ViewBasics, TestAccessForward) { @@ -32,7 +33,7 @@ TEST(ViewBasics, TestAccessForward) { // for (double x = 3; x <= 5; x += 1) { // double f_x_ex = f_x.execute(x, y); // double dx_f_FD = finite_difference_tangent(f_tmp, x, eps); - // EXPECT_NEAR(f_x_ex, dx_f_FD, abs(tau*dx_f_FD)); + // EXPECT_NEAR(f_x_ex, dx_f_FD, abs(tau*dx_f_FD)); // } // } } @@ -58,16 +59,16 @@ TEST(ViewBasics, TestAccessReverse) { double f_2(double x, double y) { const int N = 2; - Kokkos::View a("a", N); - Kokkos::View b("b", N); + Kokkos::View a("a", N); + Kokkos::View b("b", N); - Kokkos::deep_copy(a, 3*x+y); - b(0,0) = x; + Kokkos::deep_copy(a, 3 * x + y); + b(0, 0) = x; Kokkos::deep_copy(b, a); - b(0,0) = b(0,0) + a(0,0) * b(0,0); + b(0, 0) = b(0, 0) + a(0, 0) * b(0, 0); - return a(0,0); // derivative wrt x is constantly 3 + return a(0, 0); // derivative wrt x is constantly 3 } TEST(ViewBasics, TestDeepCopyForward) { @@ -82,7 +83,7 @@ TEST(ViewBasics, TestDeepCopyForward) { // for (double x = 3; x <= 5; x += 1) { // double f_x_ex = f_x.execute(x, y); // double dx_f_FD = finite_difference_tangent(f_tmp, x, eps); - // EXPECT_NEAR(f_x_ex, dx_f_FD, abs(tau*dx_f_FD)); + // EXPECT_NEAR(f_x_ex, dx_f_FD, abs(tau*dx_f_FD)); // } // } } @@ -111,13 +112,13 @@ double f_3(double x, double y) { Kokkos::View a("a", N); Kokkos::View b("b", N); - Kokkos::deep_copy(a, 3*y+x+50); - b(1) = x*y; + Kokkos::deep_copy(a, 3 * y + x + 50); + b(1) = x * y; Kokkos::deep_copy(b, a); b(1) = b(1) + a(0) * b(1); - a(1) = x*x*x; + a(1) = x * x * x; a(0) += a(1); return a(0); // derivative of this wrt y is constantly 3 @@ -135,7 +136,7 @@ TEST(ViewBasics, TestDeepCopy2Forward) { // for (double y = 3; y <= 5; y += 1) { // double f_y_ex = f_y.execute(x, y); // double dy_f_FD = finite_difference_tangent(f_tmp, y, eps); - // EXPECT_NEAR(f_y_ex, dy_f_FD, abs(tau*dy_f_FD)); + // EXPECT_NEAR(f_y_ex, dy_f_FD, abs(tau*dy_f_FD)); // } // } }