Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example for 2D triangle-point intersection #542

Merged
merged 22 commits into from
Aug 24, 2023

Conversation

masterleinad
Copy link
Collaborator

This should be useful for XGC. In particular, this pull request separates TreeTraversal construction and actually running the search allowing applications to use the TreeTraversal class in their own Kokkos kernels.

@aprokop
Copy link
Contributor

aprokop commented Aug 9, 2021

I'm deeply confused about the purpose of this PR and its relationship to XGC. In my understanding, XGC would require calling tree traversal individually from each thread, with no collaboration. This PR seems to still assume that there's a place in XGC where all thread are able to launch the search at the same time.

I thought that instead the API for TreeTraversal should be changed to allow a single thread (like team_member) as an argument.

I may be missing something here.

Also, this PR tries to do too many things simultaneously, and should be split in multiple, like the part touching the core (with the addition of unit testing) and adding an example.

@masterleinad masterleinad changed the title Add example for 2D triangle-point intersection [WIP] Add example for 2D triangle-point intersection Aug 9, 2021
@masterleinad
Copy link
Collaborator Author

Also, this PR tries to do too many things simultaneously, and should be split in multiple, like the part touching the core (with the addition of unit testing) and adding an example.

This is a draft showing my current progress.

@masterleinad
Copy link
Collaborator Author

I'm deeply confused about the purpose of this PR and its relationship to XGC. In my understanding, XGC would require calling tree traversal individually from each thread, with no collaboration. This PR seems to still assume that there's a place in XGC where all thread are able to launch the search at the same time.

I'm happy to discuss why this approach might or might not work.

@masterleinad masterleinad changed the title [WIP] Add example for 2D triangle-point intersection Add example for 2D triangle-point intersection Aug 8, 2023
@masterleinad
Copy link
Collaborator Author

This should now be in decent shape to be reviewed again. As opposed to the initial approach, we now run a "classical" batched query.

@masterleinad masterleinad marked this pull request as ready for review August 8, 2023 16:04
@masterleinad
Copy link
Collaborator Author

We only see SYCL warnings that are being addressed in #923.

Copy link
Contributor

@aprokop aprokop left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Global comments:

  • Template on MemorySpace, not DeviceType
  • Use _blah instead of blah_ for class member variables
  • Prefix all views and kernels with Example::

I'd like to think whether #915 makes this easier. If we construct a hierarchy on triangles, how do we get the performance out of it? Would not having Mapping stored significantly affect the performance? It seems to do a few calculations, is it worth storing?

examples/triangle_intersection/CMakeLists.txt Outdated Show resolved Hide resolved
examples/triangle_intersection/triangle_intersection.cpp Outdated Show resolved Hide resolved
examples/triangle_intersection/triangle_intersection.cpp Outdated Show resolved Hide resolved
examples/triangle_intersection/triangle_intersection.cpp Outdated Show resolved Hide resolved
examples/triangle_intersection/triangle_intersection.cpp Outdated Show resolved Hide resolved
ExecutionSpace execution_space;

std::cout << "Create grid with triangles.\n";
Triangles<DeviceType> triangles(execution_space);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be nice if we have a parameter n in main() for the number of triangles. Could be constexpr.

Comment on lines 339 to 342
ArborX::BasicBoundingVolumeHierarchy<
MemorySpace, ArborX::Details::PairIndexVolume<
ArborX::ExperimentalHyperGeometry::Box<2>>> const
tree(execution_space, triangles);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#915 will affect this. We would want to construct the hierarchy based on triangles. However, not sure how mapping plays into this. Ideally, would want to do an intersection of a triangle with a point within ArborX without and extra data. Is that much slower?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It shouldn't be very difficult to derive a variant or adapt once #915 is merged.

std::cout << "BVH tree set up.\n";

std::cout << "Create the points used for queries.\n";
Points<DeviceType> points(execution_space);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Provide a parameter with the number of points.

Comment on lines 351 to 352
Kokkos::View<int *, MemorySpace> offsets("offsets", n);
Kokkos::View<ArborX::Point *, MemorySpace> coefficients("coefficients", n);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do you distinguish whether intersection was found or not? The default values for these views are indistinguishable for the wrong ones.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Debug mode we run a full test checking that we detected the expected intersections and get the correct coefficients.

examples/triangle_intersection/triangle_intersection.cpp Outdated Show resolved Hide resolved
Copy link
Contributor

@aprokop aprokop left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to have NDEBUG (except for the final one checking the results) and print outs removed (print outs may be replaced by leading comments).
I think it would work well if you have them in one of the commits in history to be able to easily restore the code, but they add a bulk to the example. If we want a user to read easily, we want to make it as short as possible.

examples/triangle_intersection/triangle_intersection.cpp Outdated Show resolved Hide resolved
examples/triangle_intersection/triangle_intersection.cpp Outdated Show resolved Hide resolved
examples/triangle_intersection/triangle_intersection.cpp Outdated Show resolved Hide resolved
examples/triangle_intersection/triangle_intersection.cpp Outdated Show resolved Hide resolved
examples/triangle_intersection/triangle_intersection.cpp Outdated Show resolved Hide resolved
if (intersects)
{
_offsets(query_index) = primitive.index;
_coefficients(query_index) = coeffs;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we store coefficients? Is that because the problem we consider is interpolation? I don't remember what XGC or the like want.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. In my opinion, this is one of the points of this example.

Kokkos::parallel_for(
Kokkos::RangePolicy<ExecutionSpace>(execution_space, 0, n),
KOKKOS_LAMBDA(int i) {
constexpr float eps = 1.e-3;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How was this eps chosen? What happens with smaller values?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC, this was about the magnitude I needed to pass CI.

examples/triangle_intersection/triangle_intersection.cpp Outdated Show resolved Hide resolved
examples/triangle_intersection/triangle_intersection.cpp Outdated Show resolved Hide resolved
@masterleinad
Copy link
Collaborator Author

I'd like to have NDEBUG (except for the final one checking the results) and print outs removed (print outs may be replaced by leading comments).
I think it would work well if you have them in one of the commits in history to be able to easily restore the code, but they add a bulk to the example. If we want a user to read easily, we want to make it as short as possible.

Here you go!

Copy link
Contributor

@aprokop aprokop left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, this seems ok

There's one thing that I'm pretty uncomfortable with in the current version. Suppose, I'm a user that wants to solve this problem. Why should I care about the mapping? What I really want is to have a set of triangles, and a set of points, and get the results. I wouldn't want to be concerned about constructing some mapping, passing it in, or something. Ideally, it would be as simple as

  auto bvh = BVH(space, triangles); // for example, a Kokkos::View<Triangle*, MemorySpace>
  bvh.query(space, points_intersect, callback); // with callback being called only on the positive match

So I wonder about it. How much performance do we lose if we compute coefficients alpha and beta inside the callback? Is there a way for us to avoid that?

Currently, while it may be efficient, the example's interface is not self contained, and requires a lot of work from the user. I think it is important to keep simple things simple, while allowing the flexibility for a user to get the performance out with some effort.

@aprokop
Copy link
Contributor

aprokop commented Aug 17, 2023

masterleinad#7

I also noticed that det and inv_det are actually equal to 1.f for this configuration. So, if you accidentally miss using them in any of the formulas, the check would still succeed. It may be better to shift some of the nodes slightly during the triangle generation so that it doesn't happen.

masterleinad and others added 3 commits August 18, 2023 10:07
Co-authored-by: Andrey Prokopenko <andrey.prok@gmail.com>
Co-authored-by: Andrey Prokopenko <andrey.prok@gmail.com>
@aprokop aprokop added the examples Anything to do with examples label Aug 21, 2023
Copy link
Contributor

@aprokop aprokop left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with the understanding of major simplification after APIv2 is finalized.

@aprokop
Copy link
Contributor

aprokop commented Aug 22, 2023

I made a crack at what the example would look like in APIv2 (without mapping) here. I think it would be much cleaner and shorter. There's a question of how to not repeat the calculations, though.

@masterleinad
Copy link
Collaborator Author

LGTM with the understanding of major simplification after APIv2 is finalized.

Sure.

@aprokop aprokop merged commit 1fcc505 into arborx:master Aug 24, 2023
1 check passed
@aprokop aprokop deleted the add_triangles branch August 24, 2023 16:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
examples Anything to do with examples
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants