Host-Side Algorithms

alpaka provides host-side algorithms that execute on the selected backend through an alpaka queue and executor. They operate on alpaka Data Storage. All algorithms except scan can operate on n-dimensional data. They are implemented to use SIMD data packs to improve the memory bandwidth and compute utilization. SIMD is often explicitly exposed to provide you the full control and optimization potential.

General

A host-side algorithm consists of the following components:

  • Algorithm: The function itself determines how the input data or configuration is processed.

  • Queue: An alpaka queue for organizing the execution order along with other operations.

  • Executor: Determines the level of parallelism used to execute the algorithm. The Executor is optional. If not specified, alpaka is using a fitting executor depending on the API of the backend.

  • Output: A Data Storage that contains the result of the algorithm’s execution.

  • Input(s) or configuration: Depending on the algorithm, one or more input Data Storage are supported, or an initial configuration is required, as is the case with the Iota algorithm, for example.

  • Functor: The functor defines how each element of the input is processed. The signature of the required functor varies depending on the algorithm and functor type. The Transform (ScalarFunc) illustrates the different functors. Some algorithms, such as Iota, do not require a functor because they use a configuration.

Transform (ScalarFunc)

onHost::transform is the host-side algorithm equivalent of an element-wise kernel. It applies a functor to one or more inputs and writes the result into an output buffer. The data type of the input and output buffers can differ.

struct SquareValueScalar
{
    // int is the data type of the input buffer and float the type of the output buffer
    ALPAKA_FN_ACC auto operator()(int const& value) const -> float
    {
        return static_cast<float>(value * value);
    }
};
// we are free to combine different data types for the in- and output
auto inputBuffer = onHost::alloc<int>(device, extents);
auto outputBuffer = onHost::alloc<float>(device, extents);

onHost::fill(queue, inputBuffer, 2);
onHost::fill(queue, outputBuffer, 0.f);

// call transform with ScalarFunc wrapper
onHost::transform(queue, exec, outputBuffer, ScalarFunc{SquareValueScalar{}}, inputBuffer);

Functor Types

The algorithm determines how many input arguments are available in the kernel. The data type is defined by the Data Storage for input and output. The function type defines the wrapper type around the data types of the kernel arguments.

The default functor provides SIMD support. The sample code above already modifies this functor. Instead of a SIMD type, we use the scalar types int and float. This functor is typically used, if operations are used that do not work on SIMD types (e.g. math::min, math::max, …). To use scalar types instead of SIMD types, the functor object is wrapped in the ScalarFunc object when the onHost::transform algorithm is called. The next section shows the default functor with SIMD support.

Default Functor (SIMD)

The functor takes SIMD packs as input and returns SIMD packs. Although the operations are still written as if they were working with scalar types, they are applied in parallel to many elements.

struct SquareValue
{
    ALPAKA_FN_ACC auto operator()(concepts::Simd auto const& value) const -> concepts::Simd auto
    {
        // SIMD operation
        // For example, int is 32 bit and the processor support AVX (128 bit),
        // 4 multiplications are done in one operation.
        return value * value;
    }
};

The onHost::transform algorithm is executed without a wrapper around the functor.

auto inputBuffer = onHost::alloc<int>(device, extents);
auto outputBuffer = onHost::alloc<int>(device, extents);

onHost::fill(queue, inputBuffer, 2);
onHost::fill(queue, outputBuffer, 0);

// call transform without wrapper -> enables SIMD support
onHost::transform(queue, exec, outputBuffer, SquareValue{}, inputBuffer);

StencilFunc

The StencilFunc functor accepts SimdPtr as arguments. A SimdPtr supports operator[] for relative indexing, enabling stencil operations. The dimension of the SimdPtr arguments is the same as the in- and output Data Storage object.

The following example is a 2D stencil code.

struct SumNeighbors
{
    ALPAKA_FN_ACC auto operator()(concepts::SimdPtr auto const& in) const
    {
        using ResultType = ALPAKA_TYPEOF(in.load());
        ResultType result = ResultType::fill(0);

        using VecIdxType = typename ALPAKA_TYPEOF(in)::IdxType;
        // the functor is written for a 2D stencil
        static_assert(VecIdxType::dim() == 2);

        // top neighbor
        result += in[VecIdxType{-1, 0}].load();
        // right neighbor
        result += in[VecIdxType{0, 1}].load();
        // bottom neighbor
        result += in[VecIdxType{1, 0}].load();
        // left neighbor
        result += in[VecIdxType{0, -1}].load();

        return result;
    }
};

You must ensure memory accesses stay in bounds. Typically a sub-view of the in- and output buffer is created that excludes the halo.

Vec extents(size, size);
auto inputBuffer = onHost::alloc<int>(device, extents);
auto outputBuffer = onHost::alloc<int>(device, extents);

// we use a 2D stencil code
STATIC_REQUIRE(inputBuffer.dim() == 2);
STATIC_REQUIRE(outputBuffer.dim() == 2);

onHost::fill(queue, inputBuffer, 1);
onHost::fill(queue, outputBuffer, 0);

onHost::transform(
    queue,
    exec,
    // the user must provide a subview that takes the halo into account.
    outputBuffer.getView().getSubView(Vec{1, 1}, Vec{size - 2, size - 2}),
    // use stencil wrapper
    StencilFunc{SumNeighbors{}},
    // both input and output needs to take the halo into account
    inputBuffer.getView().getSubView(Vec{1, 1}, Vec{size - 2, size - 2}));

Iota

onHost::iota fills one or more output buffers with a linear sequence of scalar indices. For multidimensional buffers, the linear value increases fastest in the last dimension. In this example we start the enumeration with 10.

auto outputBuffer = onHost::alloc<int>(device, extents);
onHost::iota<int>(queue, exec, 10, outputBuffer);

Reduction

Reduction writes its result into the first element of the output buffer. The neutral element of the operation binary reduce functor must be provided explicitly. This allows keeping the data in device memory without going at any time to host memory. That is different compared to std::reduce.

auto inputBuffer = onHost::alloc<int>(device, extents);
onHost::memcpy(queue, inputBuffer, data);

auto reduceBuffer = onHost::alloc<int>(device, Vec{1u});
int neutralElement = 0;
onHost::reduce(queue, exec, neutralElement, reduceBuffer, std::plus{}, inputBuffer);

Transform-Reduce

transformReduce combines a data transformation step with a reduction step. It is used for dot products, weighted sums, norms, and many “compute a value per element and then accumulate it” patterns. The first functor is the binary reduction operator. The second one is the element-wise transform and must accept as many arguments as there are input Data Storage objects passed to onHost::transformReduce. Compared to std::transform_reduce the function supports any amount of input Data Storage objects. The following example iterates through inputBuffer1 and inputBuffer2, multiplies inputBuffer1[i] by inputBuffer2[i], and accumulates the products.

struct MultiplyValues
{
    ALPAKA_FN_ACC auto operator()(concepts::Simd auto const& a, concepts::Simd auto const& b) const
    {
        return a * b;
    }
};
auto resultBuffer = onHost::alloc<int>(device, Vec{1u});
onHost::transformReduce(
    queue,
    exec,
    // neutral element
    0,
    resultBuffer,
    // reduce functor
    std::plus{},
    // transform functor
    MultiplyValues{},
    // At least one input is required, we use two
    inputBuffer1,
    inputBuffer2);

Writing to input arguments

Both transform and transformReduce can write to their input arguments when the functor is wrapped in StencilFunc. The SimdPtr passed to a stencil functor supports assignment (operator=), allowing in-place modification of the input data. This is useful for algorithms that need to update their inputs as they process them. However, the user is responsible for ensuring no data races occur — the algorithm does not enforce ordering between reads and writes to the same element.

Concurrent

onHost::concurrent executes an n-ary functor on each element of all buffers. There is no restriction on whether a buffer is used for read operations, write operations, or both. This lets you implement a transform with a free number of outputs, or fuse multiple reads and writes into a single kernel launch. The functor receives SimdPtr arguments, so stencil-style indexing works without wrapping in StencilFunc. You can also pass generators as inputs, see the generator section.

The following example uses three buffers. The first buffer is used for both reading and writing. The second buffer is read-only, and the third buffer is used only for writing (but can also be read).

struct MultiOutput
{
    ALPAKA_FN_ACC void operator()(concepts::SimdPtr auto a, concepts::SimdPtr auto const& b, concepts::SimdPtr auto c)
        const
    {
        a = a.load() + b.load();
        c = b.load() * b.load();
    }
};
auto bufferA = onHost::alloc<int>(device, extents);
auto bufferB = onHost::alloc<int>(device, extents);
auto bufferC = onHost::alloc<int>(device, extents);
onHost::memcpy(queue, bufferA, data);
onHost::memcpy(queue, bufferB, data);
onHost::fill(queue, bufferC, 0);

onHost::concurrent<int>(queue, exec, bufferA.getExtents(), MultiOutput{}, bufferA, bufferB, bufferC);

Note: The template parameter T_DataType is an optimization parameter for SIMD support. If all buffers have the same data type, use that data type. Otherwise, you’ll need to run tests and take measurements to achieve the best performance.

Scan

Unlike the other algorithms in this chapter, the current scan implementation is restricted to one-dimensional data. That fits common prefix-sum use cases such as offsets, compaction maps, and cumulative counters, where the logical input is already a linear sequence. This example uses an explicit temporary storage where the size is provided via getScanBufferSize, this can be used to optimize the performance in cases where scan is called multiple times.

auto outputBuffer = onHost::alloc<int>(device, extents);
auto tmpBuffer = onHost::alloc<std::byte>(device, onHost::getScanBufferSize<int>(inputBuffer.getExtents()));
onHost::inclusiveScan(queue, exec, tmpBuffer, outputBuffer, inputBuffer);

alpaka provides the following scan variants:

  • inclusiveScan and exclusiveScan: Explanation on Wikipedia

  • inclusiveScanInPlace and exclusiveScanInPlace: same as inclusiveScan and exclusiveScan, but the result is written to the input buffer instead of an extra output buffer

Generators Instead of Input Buffers

Several alpaka algorithms also accept generators as inputs. That is useful when one input is synthetic, such as a linear index, and you do not want to materialize another buffer that consumes memory just to hold it. LinearizedIdxGenerator generates scalar indices from n-dimensional indices and behaves like an IDataSource. It behaves like a virtual buffer whose value at each position is the corresponding linear index. In this example, we add a value from one input buffer to the generated index.

struct AddLinearIdx
{
    ALPAKA_FN_ACC auto operator()(int const& value, size_t const& linearIdx) const -> int
    {
        return value + static_cast<int>(linearIdx);
    }
};
auto generator = LinearizedIdxGenerator{extents};
auto outputBuffer = onHost::alloc<int>(device, extents);
onHost::transform(queue, exec, outputBuffer, ScalarFunc{AddLinearIdx{}}, inputBuffer, generator);

Complete Source File

130_algorithms.cpp
  1/* Copyright 2026 René Widera
  2 * SPDX-License-Identifier: ISC
  3 */
  4
  5
  6#include "docsTest.hpp"
  7
  8#include <alpaka/alpaka.hpp>
  9
 10#include <catch2/catch_template_test_macros.hpp>
 11#include <catch2/catch_test_macros.hpp>
 12
 13#include <array>
 14#include <functional>
 15
 16using namespace alpaka;
 17
 18struct SquareValueScalar
 19{
 20    // int is the data type of the input buffer and float the type of the output buffer
 21    ALPAKA_FN_ACC auto operator()(int const& value) const -> float
 22    {
 23        return static_cast<float>(value * value);
 24    }
 25};
 26
 27
 28TEMPLATE_LIST_TEST_CASE("tutorial onHost algorithms - transform", "[docs]", docs::test::TestBackends)
 29{
 30    auto cfg = TestType::makeDict();
 31    auto selector = onHost::makeDeviceSelector(cfg);
 32    if(!selector.isAvailable())
 33        return;
 34    onHost::concepts::Device auto device = selector.makeDevice(0);
 35    onHost::Queue queue = device.makeQueue(queueKind::blocking);
 36    auto exec = cfg[object::exec];
 37
 38    constexpr size_t size = 10;
 39    Vec extents(size);
 40
 41    // we are free to combine different data types for the in- and output
 42    auto inputBuffer = onHost::alloc<int>(device, extents);
 43    auto outputBuffer = onHost::alloc<float>(device, extents);
 44
 45    onHost::fill(queue, inputBuffer, 2);
 46    onHost::fill(queue, outputBuffer, 0.f);
 47
 48    // call transform with ScalarFunc wrapper
 49    onHost::transform(queue, exec, outputBuffer, ScalarFunc{SquareValueScalar{}}, inputBuffer);
 50
 51
 52    auto hostOutput = onHost::allocHost<float>(extents);
 53    onHost::memcpy(queue, hostOutput, outputBuffer);
 54    onHost::wait(queue);
 55
 56    for(size_t i = 0; i < size; ++i)
 57    {
 58        REQUIRE(hostOutput[i] == 4.f);
 59    }
 60}
 61
 62struct SquareValue
 63{
 64    ALPAKA_FN_ACC auto operator()(concepts::Simd auto const& value) const -> concepts::Simd auto
 65    {
 66        // SIMD operation
 67        // For example, int is 32 bit and the processor support AVX (128 bit),
 68        // 4 multiplications are done in one operation.
 69        return value * value;
 70    }
 71};
 72
 73
 74TEMPLATE_LIST_TEST_CASE("tutorial onHost algorithms - transform default functor", "[docs]", docs::test::TestBackends)
 75{
 76    auto cfg = TestType::makeDict();
 77    auto selector = onHost::makeDeviceSelector(cfg);
 78    if(!selector.isAvailable())
 79        return;
 80    onHost::concepts::Device auto device = selector.makeDevice(0);
 81    onHost::Queue queue = device.makeQueue(queueKind::blocking);
 82    auto exec = cfg[object::exec];
 83
 84    constexpr size_t size = 10;
 85    Vec extents(size);
 86
 87    auto inputBuffer = onHost::alloc<int>(device, extents);
 88    auto outputBuffer = onHost::alloc<int>(device, extents);
 89
 90    onHost::fill(queue, inputBuffer, 2);
 91    onHost::fill(queue, outputBuffer, 0);
 92
 93    // call transform without wrapper -> enables SIMD support
 94    onHost::transform(queue, exec, outputBuffer, SquareValue{}, inputBuffer);
 95
 96    auto hostOutput = onHost::allocHost<int>(extents);
 97    onHost::memcpy(queue, hostOutput, outputBuffer);
 98    onHost::wait(queue);
 99
100    for(size_t i = 0; i < size; ++i)
101    {
102        REQUIRE(hostOutput[i] == 4);
103    }
104}
105
106struct SumNeighbors
107{
108    ALPAKA_FN_ACC auto operator()(concepts::SimdPtr auto const& in) const
109    {
110        using ResultType = ALPAKA_TYPEOF(in.load());
111        ResultType result = ResultType::fill(0);
112
113        using VecIdxType = typename ALPAKA_TYPEOF(in)::IdxType;
114        // the functor is written for a 2D stencil
115        static_assert(VecIdxType::dim() == 2);
116
117        // top neighbor
118        result += in[VecIdxType{-1, 0}].load();
119        // right neighbor
120        result += in[VecIdxType{0, 1}].load();
121        // bottom neighbor
122        result += in[VecIdxType{1, 0}].load();
123        // left neighbor
124        result += in[VecIdxType{0, -1}].load();
125
126        return result;
127    }
128};
129
130
131TEMPLATE_LIST_TEST_CASE("tutorial onHost algorithms - transform stencil functor", "[docs]", docs::test::TestBackends)
132{
133    auto cfg = TestType::makeDict();
134    auto selector = onHost::makeDeviceSelector(cfg);
135    if(!selector.isAvailable())
136        return;
137    onHost::concepts::Device auto device = selector.makeDevice(0);
138    onHost::Queue queue = device.makeQueue(queueKind::blocking);
139    auto exec = cfg[object::exec];
140
141    constexpr int size = 10;
142
143    Vec extents(size, size);
144    auto inputBuffer = onHost::alloc<int>(device, extents);
145    auto outputBuffer = onHost::alloc<int>(device, extents);
146
147    // we use a 2D stencil code
148    STATIC_REQUIRE(inputBuffer.dim() == 2);
149    STATIC_REQUIRE(outputBuffer.dim() == 2);
150
151    onHost::fill(queue, inputBuffer, 1);
152    onHost::fill(queue, outputBuffer, 0);
153
154    onHost::transform(
155        queue,
156        exec,
157        // the user must provide a subview that takes the halo into account.
158        outputBuffer.getView().getSubView(Vec{1, 1}, Vec{size - 2, size - 2}),
159        // use stencil wrapper
160        StencilFunc{SumNeighbors{}},
161        // both input and output needs to take the halo into account
162        inputBuffer.getView().getSubView(Vec{1, 1}, Vec{size - 2, size - 2}));
163
164    auto hostOutput = onHost::allocHost<int>(extents);
165    onHost::memcpy(queue, hostOutput, outputBuffer);
166    onHost::wait(queue);
167
168    for(int y = 0; y < size; ++y)
169    {
170        for(int x = 0; x < size; ++x)
171        {
172            if(y == 0 || y == (size - 1) || x == 0 || x == (size - 1))
173            {
174                CHECK(hostOutput[Vec{y, x}] == 0);
175            }
176            else
177            {
178                CHECK(hostOutput[Vec{y, x}] == 4);
179            }
180        }
181    }
182}
183
184TEMPLATE_LIST_TEST_CASE("tutorial onHost algorithms - iota", "[docs]", docs::test::TestBackends)
185{
186    auto cfg = TestType::makeDict();
187    auto selector = onHost::makeDeviceSelector(cfg);
188    if(!selector.isAvailable())
189        return;
190    onHost::concepts::Device auto device = selector.makeDevice(0);
191    onHost::Queue queue = device.makeQueue(queueKind::blocking);
192    auto exec = cfg[object::exec];
193
194    constexpr size_t size = 8;
195    Vec extents(size);
196
197    auto outputBuffer = onHost::alloc<int>(device, extents);
198    onHost::iota<int>(queue, exec, 10, outputBuffer);
199
200    auto hostOutput = onHost::allocHost<int>(extents);
201    onHost::memcpy(queue, hostOutput, outputBuffer);
202    onHost::wait(queue);
203    for(size_t i = 0; i < size; ++i)
204    {
205        REQUIRE(hostOutput[i] == static_cast<int>(i) + 10);
206    }
207}
208
209TEMPLATE_LIST_TEST_CASE("tutorial onHost algorithms - reduce", "[docs]", docs::test::TestBackends)
210{
211    auto cfg = TestType::makeDict();
212    auto selector = onHost::makeDeviceSelector(cfg);
213    if(!selector.isAvailable())
214        return;
215    onHost::concepts::Device auto device = selector.makeDevice(0);
216    onHost::Queue queue = device.makeQueue(queueKind::blocking);
217    auto exec = cfg[object::exec];
218
219    constexpr size_t size = 8;
220    Vec extents(size);
221
222    std::array<int, size> data{1, 2, 3, 4, 5, 6, 7, 8};
223
224    auto inputBuffer = onHost::alloc<int>(device, extents);
225    onHost::memcpy(queue, inputBuffer, data);
226
227    auto reduceBuffer = onHost::alloc<int>(device, Vec{1u});
228    int neutralElement = 0;
229    onHost::reduce(queue, exec, neutralElement, reduceBuffer, std::plus{}, inputBuffer);
230
231    auto hostOutput = onHost::allocHost<int>(size_t{1});
232    onHost::memcpy(queue, hostOutput, reduceBuffer);
233    onHost::wait(queue);
234
235    REQUIRE(hostOutput[0] == 36);
236}
237
238struct MultiplyValues
239{
240    ALPAKA_FN_ACC auto operator()(concepts::Simd auto const& a, concepts::Simd auto const& b) const
241    {
242        return a * b;
243    }
244};
245
246
247TEMPLATE_LIST_TEST_CASE("tutorial onHost algorithms - tranformReduce", "[docs]", docs::test::TestBackends)
248{
249    auto cfg = TestType::makeDict();
250    auto selector = onHost::makeDeviceSelector(cfg);
251    if(!selector.isAvailable())
252        return;
253    onHost::concepts::Device auto device = selector.makeDevice(0);
254    onHost::Queue queue = device.makeQueue(queueKind::blocking);
255    auto exec = cfg[object::exec];
256
257    constexpr size_t size = 8;
258    Vec extents(size);
259
260    std::array<int, size> data{1, 2, 3, 4, 5, 6, 7, 8};
261
262    auto inputBuffer1 = onHost::alloc<int>(device, extents);
263    auto inputBuffer2 = onHost::alloc<int>(device, extents);
264    onHost::memcpy(queue, inputBuffer1, data);
265    onHost::memcpy(queue, inputBuffer2, data);
266
267    auto resultBuffer = onHost::alloc<int>(device, Vec{1u});
268    onHost::transformReduce(
269        queue,
270        exec,
271        // neutral element
272        0,
273        resultBuffer,
274        // reduce functor
275        std::plus{},
276        // transform functor
277        MultiplyValues{},
278        // At least one input is required, we use two
279        inputBuffer1,
280        inputBuffer2);
281
282    auto hostOutput = onHost::allocHost<int>(size_t{1});
283    onHost::memcpy(queue, hostOutput, resultBuffer);
284    onHost::wait(queue);
285
286    REQUIRE(hostOutput[0] == 204);
287}
288
289struct MultiOutput
290{
291    ALPAKA_FN_ACC void operator()(concepts::SimdPtr auto a, concepts::SimdPtr auto const& b, concepts::SimdPtr auto c)
292        const
293    {
294        a = a.load() + b.load();
295        c = b.load() * b.load();
296    }
297};
298
299
300TEMPLATE_LIST_TEST_CASE("tutorial onHost algorithms - concurrent", "[docs]", docs::test::TestBackends)
301{
302    auto cfg = TestType::makeDict();
303    auto selector = onHost::makeDeviceSelector(cfg);
304    if(!selector.isAvailable())
305        return;
306    onHost::concepts::Device auto device = selector.makeDevice(0);
307    onHost::Queue queue = device.makeQueue(queueKind::blocking);
308    auto exec = cfg[object::exec];
309
310    constexpr size_t size = 8;
311    Vec extents(size);
312
313    std::array<int, size> data{1, 2, 3, 4, 5, 6, 7, 8};
314
315    auto bufferA = onHost::alloc<int>(device, extents);
316    auto bufferB = onHost::alloc<int>(device, extents);
317    auto bufferC = onHost::alloc<int>(device, extents);
318    onHost::memcpy(queue, bufferA, data);
319    onHost::memcpy(queue, bufferB, data);
320    onHost::fill(queue, bufferC, 0);
321
322    onHost::concurrent<int>(queue, exec, bufferA.getExtents(), MultiOutput{}, bufferA, bufferB, bufferC);
323
324    auto hostOutputA = onHost::allocHost<int>(extents);
325    auto hostOutputC = onHost::allocHost<int>(extents);
326    onHost::memcpy(queue, hostOutputA, bufferA);
327    onHost::memcpy(queue, hostOutputC, bufferC);
328    onHost::wait(queue);
329
330    for(size_t i = 0; i < size; ++i)
331    {
332        int const v = static_cast<int>(i) + 1;
333        CHECK(hostOutputA[i] == v * 2);
334        CHECK(hostOutputC[i] == v * v);
335    }
336}
337
338TEMPLATE_LIST_TEST_CASE("tutorial onHost algorithms - scan", "[docs]", docs::test::TestBackends)
339{
340    auto cfg = TestType::makeDict();
341    auto selector = onHost::makeDeviceSelector(cfg);
342    if(!selector.isAvailable())
343        return;
344    onHost::concepts::Device auto device = selector.makeDevice(0);
345    onHost::Queue queue = device.makeQueue(queueKind::blocking);
346    auto exec = cfg[object::exec];
347
348    constexpr size_t size = 8;
349    Vec extents(size);
350
351    std::array<int, size> data{1, 2, 3, 4, 5, 6, 7, 8};
352
353    auto inputBuffer = onHost::alloc<int>(device, extents);
354    onHost::memcpy(queue, inputBuffer, data);
355
356    auto outputBuffer = onHost::alloc<int>(device, extents);
357    auto tmpBuffer = onHost::alloc<std::byte>(device, onHost::getScanBufferSize<int>(inputBuffer.getExtents()));
358    onHost::inclusiveScan(queue, exec, tmpBuffer, outputBuffer, inputBuffer);
359
360    auto hostOutput = onHost::allocHost<int>(extents);
361    onHost::memcpy(queue, hostOutput, outputBuffer);
362    onHost::wait(queue);
363
364    CHECK(hostOutput[0] == 1);
365    CHECK(hostOutput[1] == 3);
366    CHECK(hostOutput[2] == 6);
367    CHECK(hostOutput[3] == 10);
368    CHECK(hostOutput[4] == 15);
369    CHECK(hostOutput[5] == 21);
370    CHECK(hostOutput[6] == 28);
371    CHECK(hostOutput[7] == 36);
372}
373
374struct AddLinearIdx
375{
376    ALPAKA_FN_ACC auto operator()(int const& value, size_t const& linearIdx) const -> int
377    {
378        return value + static_cast<int>(linearIdx);
379    }
380};
381
382
383TEMPLATE_LIST_TEST_CASE("tutorial onHost algorithms - linear generator", "[docs]", docs::test::TestBackends)
384{
385    auto cfg = TestType::makeDict();
386    auto selector = onHost::makeDeviceSelector(cfg);
387    if(!selector.isAvailable())
388        return;
389    onHost::concepts::Device auto device = selector.makeDevice(0);
390    onHost::Queue queue = device.makeQueue(queueKind::blocking);
391    auto exec = cfg[object::exec];
392
393    constexpr size_t size = 8;
394    Vec extents(size);
395
396    std::array<int, size> data{1, 2, 3, 4, 5, 6, 7, 8};
397    auto inputBuffer = onHost::alloc<int>(device, extents);
398    onHost::memcpy(queue, inputBuffer, data);
399
400    auto generator = LinearizedIdxGenerator{extents};
401    auto outputBuffer = onHost::alloc<int>(device, extents);
402    onHost::transform(queue, exec, outputBuffer, ScalarFunc{AddLinearIdx{}}, inputBuffer, generator);
403
404    auto hostOutput = onHost::allocHost<int>(extents);
405    onHost::memcpy(queue, hostOutput, outputBuffer);
406    onHost::wait(queue);
407
408    for(size_t i = 0; i < size; ++i)
409    {
410        CHECK(hostOutput[i] == (data[i] + static_cast<int>(generator[i])));
411    }
412}