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 Method to Resize View without Initialization #2048

Closed
stanmoore1 opened this issue Mar 25, 2019 · 17 comments
Closed

Add Method to Resize View without Initialization #2048

stanmoore1 opened this issue Mar 25, 2019 · 17 comments
Assignees
Labels
Enhancement Improve existing capability; will potentially require voting

Comments

@stanmoore1
Copy link
Contributor

stanmoore1 commented Mar 25, 2019

Can a way to resize a view without any initialization please be added? The values are just going to be overwritten in the next parallel region.

@stanmoore1
Copy link
Contributor Author

See also #1877.

@Char-Aznable
Copy link
Contributor

I asked the same question on the Slack channel. It seems the answer for now is to use unmanaged view

@mhoemmen
Copy link
Contributor

You don't have to use an unmanaged View. Just create a new View, uninitialized, and assign to the old one. That's what Kokkos::resize does anyway.

@stanmoore1
Copy link
Contributor Author

Can only resize managed views, so can't use unmanaged in this case. A static assert prevents this, see https://github.com/kokkos/kokkos/blob/master/core/src/Kokkos_CopyViews.hpp#L1839.

That's what Kokkos::resize does anyway

No, I don't think that is true, for example see https://github.com/kokkos/kokkos/blob/master/core/src/Kokkos_CopyViews.hpp#L470.

@stanmoore1
Copy link
Contributor Author

@mhoemmen unless by "assign" you mean copy element by element...

@stanmoore1
Copy link
Contributor Author

Maybe I need to explain what I need a little better. When I call Kokkos::resize on a view, I want to preserve the existing values, without initializing the extra empty space, since the new space will just be overwritten in the next parallel region. So the existing values in the old view have to be copied element by element in the new view.

@stanmoore1
Copy link
Contributor Author

stanmoore1 commented Mar 25, 2019

@mhoemmen pretty sure you are thinking of Kokkos::realloc instead of Kokkos::resize.

@Char-Aznable
Copy link
Contributor

@stanmoore1 I didn't mean resizing the unmanaged directly. Can you create an unmanaged view with the desired dimension by wrapping the pointer from the old view?

@stanmoore1
Copy link
Contributor Author

Can you create an unmanaged view with the desired dimension by wrapping the pointer from the old view?

I could create a new larger view that is uninitialized and copy element by element manually, but then I'm recreating the Kokkos::resize method on my own, which is not convenient. I'm not trying to get around the element by element copy, I don't think that is possible except for really special cases. I am trying to get around the initialization that wastes another CUDA kernel launch, the overhead of which can be significant when you are launch latency bound.

@stanmoore1
Copy link
Contributor Author

Just to be 100% clear, Kokkos::resize preserves old values, while Kokkos::realloc does not. Kokkos::resize copies all the values from the old view into the new view element-by-element by launching a parallel_for on the device. It also initializes the new values in the new view by launching another parallel_for on the device, which is what I want to avoid.

@stanmoore1
Copy link
Contributor Author

stanmoore1 commented Mar 25, 2019

Actually, it is worse than I thought. Kokkos isn't just initializing the "extra" in the new view, it is initializing the entire new view, even though most of the values will just be copied over in the next operation. You can see that using this code.

#include <Kokkos_Core.hpp>
#include <cstdio>

typedef Kokkos::View<int*> view_type;

int main (int argc, char* argv[]) {
  Kokkos::initialize (argc, argv);
  {
    const int N = 10;
    view_type a (Kokkos::view_alloc("A",Kokkos::WithoutInitializing), N);
    Kokkos::resize(a,2*N);
  }
  Kokkos::finalize ();
}

I used kernel_logger and also put a printf statement in the view initialization function:

KokkosP: Allocate<Cuda> name: A pointer: 0x7fff5a200080 size: 40
KokkosP: Allocate<Cuda> name: A pointer: 0x7fff5a200280 size: 80
KokkosP: Executing parallel-for kernel on device 0 with unique execution identifier 0
KokkosP: Kokkos::View::initialization
Initialized 80
KokkosP: Execution of kernel 0 is completed.
KokkosP: Executing parallel-for kernel on device 0 with unique execution identifier 1
KokkosP: Kokkos::ViewCopy-1D
KokkosP: Execution of kernel 1 is completed.
KokkosP: Deallocate<Cuda> name: A pointer: 0x7fff5a200080 size: 40
KokkosP: Deallocate<Cuda> name: A pointer: 0x7fff5a200280 size: 80

@mhoemmen
Copy link
Contributor

pretty sure you are thinking of Kokkos::realloc instead of Kokkos::resize

my bad; thanks!

@ibaned
Copy link
Contributor

ibaned commented Mar 27, 2019

I opened a duplicate earlier, (#2028), but I'll close that one in favor of this one because this one has user data showing why this is expensive.

@ibaned
Copy link
Contributor

ibaned commented Mar 27, 2019

@dhollman volunteered on #2028, so I'll assign him to this one

@ibaned ibaned added the Enhancement Improve existing capability; will potentially require voting label Mar 27, 2019
@ibaned
Copy link
Contributor

ibaned commented Mar 27, 2019

The CoPA/Cabana team also needs this feature.

@crtrott crtrott assigned vqd8a and unassigned dhollman Aug 22, 2019
@vqd8a vqd8a added this to the 3.0 Release milestone Aug 28, 2019
@vqd8a
Copy link
Contributor

vqd8a commented Sep 4, 2019

Is adding something like Kokkos::resize( Kokkos::view_resize (WithoutInitializing), A, 2*N ) okay?

@stanmoore1
Copy link
Contributor Author

Is adding something like Kokkos::resize( Kokkos::view_resize (WithoutInitializing), A, 2*N ) okay?

Yes, seems fine to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Improve existing capability; will potentially require voting
Projects
None yet
Development

No branches or pull requests

7 participants