Skip to content

Commit

Permalink
moved outer_trapezoid to trapezoid.cpp and added test for it
Browse files Browse the repository at this point in the history
Signed-off-by: Konstantin Läufer <[email protected]>
  • Loading branch information
klaeufer committed Dec 10, 2023
1 parent 15783f2 commit d8a29cb
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 22 deletions.
1 change: 0 additions & 1 deletion integration/f.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#ifndef F_H_
#define F_H_


// {{UnoAPI:f-interface:begin}}
#include <sycl/sycl.hpp>

Expand Down
20 changes: 0 additions & 20 deletions integration/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,6 @@ template <class Indexable> void print_function_values(const Indexable & values,
}
// {{UnoAPI:main-print-function-values:end}}

// {{UnoAPI:main-compute-outer-trapezoid:begin}}
// common function to compute a single outer trapezoid
// from as many inner trapezoids as the grain size
double outer_trapezoid(
const int grain_size,
const double x_pos,
const double dx_inner,
const double half_dx_inner
) {
auto area{0.0};
auto y_left{f(x_pos)};
for (auto j{0UL}; j < grain_size; j++) {
auto y_right{f(x_pos + (j + 1) * dx_inner)};
area += single_trapezoid(y_left, y_right, half_dx_inner);
y_left = y_right;
}
return area;
}
// {{UnoAPI:main-compute-outer-trapezoid:end}}

int main(const int argc, const char * const argv[]) {
// {{UnoAPI:main-declarations:begin}}
size_t total_workload{1000};
Expand Down
14 changes: 13 additions & 1 deletion integration/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class IntegrationTest : public testing::Test {
};
// {{UnoAPI:integration-test-scaffolding:end}}


// {{UnoAPI:integration-test-simple1:begin}}
TEST_F(IntegrationTest, Simple1) {
EXPECT_NEAR(single_trapezoid(1, 1, 0.5), 1, EPS);
Expand All @@ -43,3 +42,16 @@ TEST_F(IntegrationTest, F1) {
EXPECT_NEAR(f(0.5), 0.75, EPS);
}
// {{UnoAPI:integration-test-f1:end}}

// double outer_trapezoid(
// const int grain_size,
// const double x_pos,
// const double dx_inner,
// const double half_dx_inner
// )

// {{UnoAPI:integration-test-outer1:begin}}
TEST_F(IntegrationTest, Outer1) {
EXPECT_NEAR(outer_trapezoid(1000, 0.0, 0.001, 0.0005), 1, EPS);
}
// {{UnoAPI:integration-test-outer1:end}}
21 changes: 21 additions & 0 deletions integration/trapezoid.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,28 @@
#include "trapezoid.h"
#include "f.h"

// {{UnoAPI:trapezoid-implementation:begin}}
double single_trapezoid(const double f1, const double f2, const double half_dx) {
return (f1 + f2) * half_dx;
}
// {{UnoAPI:trapezoid-implementation:end}}

// {{UnoAPI:main-compute-outer-trapezoid:begin}}
// common function to compute a single outer trapezoid
// from as many inner trapezoids as the grain size
double outer_trapezoid(
const int grain_size,
const double x_pos,
const double dx_inner,
const double half_dx_inner
) {
auto area{0.0};
auto y_left{f(x_pos)};
for (auto j{0UL}; j < grain_size; j++) {
auto y_right{f(x_pos + (j + 1) * dx_inner)};
area += single_trapezoid(y_left, y_right, half_dx_inner);
y_left = y_right;
}
return area;
}
// {{UnoAPI:main-compute-outer-trapezoid:end}}
7 changes: 7 additions & 0 deletions integration/trapezoid.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,11 @@
SYCL_EXTERNAL double single_trapezoid(double f1, double f2, double dx);
// {{UnoAPI:trapezoid-interface:end}}

SYCL_EXTERNAL double outer_trapezoid(
const int grain_size,
const double x_pos,
const double dx_inner,
const double half_dx_inner
);

#endif // INTEGRATION_H_

0 comments on commit d8a29cb

Please sign in to comment.