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

Support for a sub-view like class? #1590

Open
rcarson3 opened this issue Jan 9, 2024 · 0 comments
Open

Support for a sub-view like class? #1590

rcarson3 opened this issue Jan 9, 2024 · 0 comments
Labels
API/usability feature reviewed Mark with this label when issue has been discussed by team

Comments

@rcarson3
Copy link
Member

rcarson3 commented Jan 9, 2024

While working out a solution to the later half of my issue in #1571 , it occurred to me that a generic sub-view like class would be the best way to deal with the pointer issue I was running into earlier. I was curious if this might be something that the RAJA team might be interested in supporting as I'm sure several people might also find it useful to have a subview/slice/window of a larger view.

I've included my initial proof-of-concept of such a subview / window class below that we're using in our library. The below also has the added functionality that it can act like a "rolling" window as we had a need for such a feature also arose in this same library for a couple special cases.

I do realize that making this making this sorta class work for how generic RAJA views are might not be the simplest thing, and @gberg617 has already brought up some things I hadn't considered. Since at least in my use case, we can make a number of assumptions with the below class such as only ever taking slices along the slowest index and only wanting a rolling window on the 2nd slowest index which greatly simplifies things.

// We really don't care what View class we're using as the sub-view just wraps it up
// and then allows us to take a slice/window of the original view
// Should probably work out some form of SFINAE to ensure T that we've templated on
// is an actual View class that we can use...
template<class T>
class subview {
public:
    // Delete the default constructor as that wouldn't be a valid object
    __host__ __device__
    subview() = delete;
    // where we don't want any offset within the subview itself
    __host__ __device__
    subview(const int index, T& view) : m_view(view), m_index(index), m_offset(0) {};
    // sometimes you might want to have an initial offset in your subview when constructing
    // your subview in which everything appears as 0 afterwards 
    __host__ __device__
    subview(const int index, const size_t offset, T& view) : m_view(view), m_index(index), m_offset(offset) {};

    ~subview() = default;

    // Could probably add default copy constructors as well here if need be...

    // Let the compiler figure out the correct return type here as the one from
    // RAJA at least for regular Views is non-trivial
    // make the assumption here that we're using row-major memory order for views
    // so m_index is in the location of the slowest moving index as this is the default
    // for RAJA...
    template <typename... Args>
    __host__ __device__
    inline
    constexpr
    auto&
    operator()(Args... args) const
    {
        // The use of m_offset here provides us the equivalent of a rolling
        // subview/window if our application needs it
        return m_view(m_index, m_offset + args...);
    }

    // If we need to have like a rolling subview/window type class then
    // we'd need some way to update the offset in our slowest moving index
    // in the subview (so not m_view's slowest index)
    __host__ __device__
    inline
    void set_offset(const int offset) const
    {
        // Might want an assert in here for debugs to make sure that this is within
        // the bounds of what m_view expects is a valid offset
        m_offset = offset;
    }

private:
    // Internally we shouldn't be modifying the view itself so let's make it constant
    const T& m_view;
    const int m_index = 0;
    mutable size_t m_offset = 0;
};
@rhornung67 rhornung67 added the reviewed Mark with this label when issue has been discussed by team label Mar 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API/usability feature reviewed Mark with this label when issue has been discussed by team
Projects
None yet
Development

No branches or pull requests

2 participants