Skip to content

Commit 697e2c8

Browse files
authored
Merge pull request #48314 from leobeltra/make_view_with_span
Added std::span support to make_device_view and make_host_view
2 parents fc5f2d6 + 09324ea commit 697e2c8

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

HeterogeneousCore/AlpakaInterface/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,10 @@ These helper functions instantiate zero-dimensional (scalars) and one-dimensiona
357357
instantiates a one-dimensional view over an array starting at `data` with
358358
`size` elements in host memory;
359359
360+
- `auto make_host_view<T[]>(std::span<T> span)`
361+
instantiates a one-dimensional view over an array starting at `span.data()` with
362+
`span.size()` elements in host memory;
363+
360364
361365
## Device memory views
362366
@@ -393,6 +397,10 @@ These helper functions instantiate zero-dimensional (scalars) and one-dimensiona
393397
instantiates a one-dimensional view over an array starting at `data` with
394398
`size` elements in device global memory.
395399
400+
- `auto make_device_view<T[]>(device, std::span<T> span)`
401+
instantiates a one-dimensional view over an array starting at `span.data()` with
402+
`span.size()` elements in device global memory.
403+
396404
The `make_device_view` functions can also accept as a first argument a `queue`
397405
instead of a `device`:
398406
@@ -408,5 +416,9 @@ instead of a `device`:
408416
instantiates a one-dimensional view over an array starting at `data` with
409417
`size` elements in device global memory.
410418
419+
- `auto make_device_view<T[]>(queue, std::span<T> span)`
420+
instantiates a one-dimensional view over an array starting at `span.data()` with
421+
`span.size()` elements in device global memory.
422+
411423
These functions use the device associated to the given `queue`. These operations
412424
are otherwise identical to those that take a `device` as their first argument.

HeterogeneousCore/AlpakaInterface/interface/memory.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
#include <type_traits>
55

6+
#include <span>
7+
68
#include <alpaka/alpaka.hpp>
79

810
#include "HeterogeneousCore/AlpakaInterface/interface/AllocatorPolicy.h"
@@ -157,6 +159,11 @@ namespace cms::alpakatools {
157159
return alpaka::ViewPlainPtr<DevHost, T, Dim1D, Idx>(data, host(), Vec1D{extent});
158160
}
159161

162+
template <typename T>
163+
host_view<T[]> make_host_view(std::span<T> span) {
164+
return alpaka::ViewPlainPtr<DevHost, T, Dim1D, Idx>(span.data(), host(), Vec1D{span.size()});
165+
}
166+
160167
template <typename T>
161168
std::enable_if_t<cms::is_unbounded_array_v<T> and not std::is_array_v<std::remove_extent_t<T>>, host_view<T>>
162169
make_host_view(T& data, Extent extent) {
@@ -267,6 +274,12 @@ namespace cms::alpakatools {
267274
return alpaka::ViewPlainPtr<TDev, T, Dim1D, Idx>(data, device, Vec1D{extent});
268275
}
269276

277+
template <typename T, typename TDev>
278+
std::enable_if_t<alpaka::isDevice<TDev>, device_view<TDev, T[]>> make_device_view(TDev const& device,
279+
std::span<T> span) {
280+
return alpaka::ViewPlainPtr<TDev, T, Dim1D, Idx>(span.data(), device, Vec1D{span.size()});
281+
}
282+
270283
template <typename T, typename TDev>
271284
std::enable_if_t<alpaka::isDevice<TDev> and cms::is_unbounded_array_v<T> and
272285
not std::is_array_v<std::remove_extent_t<T>>,
@@ -296,6 +309,13 @@ namespace cms::alpakatools {
296309
return alpaka::ViewPlainPtr<alpaka::Dev<TQueue>, T, Dim1D, Idx>(data, alpaka::getDev(queue), Vec1D{extent});
297310
}
298311

312+
template <typename T, typename TQueue>
313+
std::enable_if_t<alpaka::isQueue<TQueue>, device_view<alpaka::Dev<TQueue>, T[]>> make_device_view(TQueue const& queue,
314+
std::span<T> span) {
315+
return alpaka::ViewPlainPtr<alpaka::Dev<TQueue>, T, Dim1D, Idx>(
316+
span.data(), alpaka::getDev(queue), Vec1D{span.size()});
317+
}
318+
299319
template <typename T, typename TQueue>
300320
std::enable_if_t<alpaka::isQueue<TQueue> and cms::is_unbounded_array_v<T> and
301321
not std::is_array_v<std::remove_extent_t<T>>,

0 commit comments

Comments
 (0)