Skip to content

Improvements to ArrayView interface #221

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

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion benchmarks/BenchmarkArrayView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ static void benchmark_array_view_2d(benchmark::State& state) {
const int extent2 = state.range(0);
care::host_device_ptr<int> data(extent1*extent2, "data");

// Make sure memory transfers are not considered
// Make sure memory transfers are not considered in the timing
auto temp = care::makeArrayView2D(data, extent1, extent2);
(void) temp;

Expand Down
62 changes: 62 additions & 0 deletions src/care/ArrayView.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,23 @@
#include "RAJA/util/View.hpp"

namespace care {
template <class T>
using ArrayView1D = RAJA::View<T, RAJA::Layout<1>>;

template <class T>
using ArrayCView1D = ArrayView1D<const T>;
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is nit picky, but why did you call this ArrayCView1D rather than ArrayViewC1D (Array View Constant 1D) or CArrayView1D (Constant Array View 1D)?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I guess elsewhere you call this a const view so maybe that makes sense nvm

Copy link
Member Author

Choose a reason for hiding this comment

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

I was actually going to ask what you think of the naming. Would it make sense to drop "Array" from the naming scheme and just have View1D, ConstView1D, View2D, ConstView2D, etc...?

Copy link
Collaborator

@dtaller dtaller Apr 11, 2023

Choose a reason for hiding this comment

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

That makes sense. We have RAJA::View, so having care::View would be consistent and it would be clear as long as it is in the care namespace. Spelling out Constant makes things clear also, harder to miss the difference. But I would ask some of the others before making a sweeping change.

Copy link
Member Author

Choose a reason for hiding this comment

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

This isn't used anywhere yet, so now is a great time to make changes.


#if defined(CARE_GPUCC)
template <class T>
using ArrayView2D = RAJA::View<T, RAJA::Layout<2, int, 1>>;
#else
template <class T>
using ArrayView2D = RAJA::View<T, RAJA::Layout<2, int, 0>>;
#endif

template <class T>
using ArrayCView2D = ArrayView2D<const T>;

///
/// Creates a 1D view on top of a flat allocation (non-owning semantics)
///
Expand All @@ -21,6 +38,21 @@ namespace care {
auto makeArrayView1D(care::host_device_ptr<T> data,
int extent);

///
/// Creates a 1D const view on top of a flat allocation (non-owning semantics)
///
/// Moves the data to the default execution space (determined at
/// compile time) and wraps it in a RAJA::View.
///
/// @param[in] data Container for the allocation
/// @param[in] extent Length of the view
///
/// @return A RAJA::View object
///
template <class T>
auto makeArrayCView1D(care::host_device_ptr<T> data,
int extent);

///
/// Creates a 2D view on top of a flat allocation (non-owning semantics)
///
Expand All @@ -38,6 +70,24 @@ namespace care {
template <class T>
auto makeArrayView2D(care::host_device_ptr<T> data,
int extent1, int extent2);

///
/// Creates a 2D const view on top of a flat allocation (non-owning semantics)
///
/// Moves the data to the default execution space (determined at
/// compile time) and wraps it in a RAJA::View. When using with a
/// RAJA loop, extent2 should correspond to the length of the RAJA
/// loop for optimal performance.
///
/// @param[in] data Container for the allocation
/// @param[in] extent1 Length of the 1st dimension
/// @param[in] extent2 Length of the 2nd dimension
///
/// @return A RAJA::View object
///
template <class T>
auto makeArrayCView2D(care::host_device_ptr<T> data,
int extent1, int extent2);

template <class T>
auto makeArrayView1D(care::host_device_ptr<T> data,
Expand All @@ -49,6 +99,12 @@ namespace care {
#endif
}

template <class T>
auto makeArrayCView1D(care::host_device_ptr<T> data,
int extent) {
return makeArrayView1D(care::host_device_ptr<T const>(data), extent);
}

template <class T>
auto makeArrayView2D(care::host_device_ptr<T> data,
int extent1, int extent2) {
Expand All @@ -62,6 +118,12 @@ namespace care {
return RAJA::View<T, RAJA::Layout<2, int, 0>>(data.data(chai::CPU), layout);
#endif
}

template <class T>
auto makeArrayCView2D(care::host_device_ptr<T> data,
int extent1, int extent2) {
return makeArrayView2D(care::host_device_ptr<T const>(data), extent1, extent2);
}
} // namespace care

#endif // CARE_ARRAY_VIEW_H
Expand Down