Arcane  4.1.15.0
Developer documentation
Loading...
Searching...
No Matches
TestLambda_Accelerator.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7
8#include <gtest/gtest.h>
9
10#include "arccore/base/MDIndex.h"
11
12#include "arccore/common/accelerator/RunQueue.h"
13#include "arccore/common/NumArray.h"
14
16
17/*---------------------------------------------------------------------------*/
18/*---------------------------------------------------------------------------*/
19
20using namespace Arcane;
21using namespace Arcane::Accelerator;
22
23/*---------------------------------------------------------------------------*/
24/*---------------------------------------------------------------------------*/
25
26template<typename Extents> void
27_fillArray(NumArray<Int64, Extents>& num_array, Int64 base_value)
28{
29 using ExtentIndexType = Extents::ExtentIndexType;
30 ExtentIndexType nb_value = num_array.extent0();
31 for (ExtentIndexType i = 0; i < nb_value; ++i) {
32 num_array(i) = (i + base_value);
33 }
34}
35
36void
37_doTestLambda1(RunQueue queue)
38{
39 Int32 nb_value = 50000;
40
41 std::cout << "Using accelerator policy name=" << queue.executionPolicy() << "\n";
42 std::cout << " nb_value=" << nb_value << "\n";
43
44 NumArray<Int64, MDDim1> a(nb_value);
45 _fillArray(a, 3);
46
47 NumArray<Int64, MDDim1> b(nb_value);
48
49 // Test direct lambda launch
50 {
51 SmallSpan<const Int64> a_view(a.to1DSmallSpan());
52 SmallSpan<Int64> b_view(a.to1DSmallSpan());
53 auto lambda1 = [=] ARCCORE_HOST_DEVICE(Int32 index) {
54 b_view[index] = a_view[index];
55 };
56
57 {
58 std::cout << "Test lambda direct\n";
59 RunCommand command = makeCommand(queue);
60 auto range = makeLoopRanges(nb_value);
61 Arcane::Accelerator::run(command, range, lambda1);
62 }
63
64 for (Int32 k = 0; k < nb_value; ++k) {
65 ASSERT_EQ(a_view[k], b_view[k]) << " Index1=" << k;
66 }
67 }
68
69 // Test direct mutable lambda launch
70 {
71 _fillArray(a, 5);
72
73 ConstArrayView<Int64> a_view(nb_value, a.data());
74 ArrayView<Int64> b_view(nb_value, b.data());
75 auto mutable_lambda1 = [=] ARCCORE_HOST_DEVICE(Int32 index) mutable {
76 b_view[index] = a_view[index];
77 };
78
79 {
80 std::cout << "Test mutable lambda direct\n";
81 RunCommand command = makeCommand(queue);
82 auto range = makeLoopRanges(nb_value);
83 Arcane::Accelerator::launchRunCommand(command, range, mutable_lambda1);
84 }
85
86 for (Int32 k = 0; k < nb_value; ++k) {
87 ASSERT_EQ(a_view[k], b_view[k]) << " Index2=" << k;
88 }
89 }
90}
91
92// Check using a loop with a NumArray using 'Int64' as index type
93void
94_doTestLambda2(RunQueue queue)
95{
96 Int32 nb_value = 50000;
97
98 std::cout << "Using accelerator policy name=" << queue.executionPolicy() << "\n";
99 std::cout << " nb_value=" << nb_value << "\n";
100
102 _fillArray(a, 3);
103
105
106 // Test direct lambda launch
107 {
108 Span<const Int64> a_view(a.to1DSpan());
109 Span<Int64> b_view(a.to1DSpan());
110 auto lambda1 = [=] ARCCORE_HOST_DEVICE(Int64 index) {
111 b_view[index] = a_view[index];
112 };
113
114 {
115 std::cout << "Test lambda direct\n";
116 RunCommand command = makeCommand(queue);
117 SimpleForLoopRanges<1,Int64> loop_range(nb_value);
118 launchRunCommand(command, loop_range, lambda1);
119 }
120
121 for (Int32 k = 0; k < nb_value; ++k) {
122 ASSERT_EQ(a_view[k], b_view[k]) << " Index1=" << k;
123 }
124 }
125
126}
127
128/*---------------------------------------------------------------------------*/
129/*---------------------------------------------------------------------------*/
130
131extern "C++" void
132_doTestLambda(RunQueue queue)
133{
134 _doTestLambda1(queue);
135 _doTestLambda2(queue);
136}
137
138/*---------------------------------------------------------------------------*/
139/*---------------------------------------------------------------------------*/
Types and macros for managing loops on accelerators.
eExecutionPolicy executionPolicy() const
Execution policy of the queue.
Definition RunQueue.cc:169
Modifiable view of an array of type T.
Constant view of an array of type T.
Multi-dimensional arrays for numerical types accessible on accelerators.
constexpr ExtentIndexType extent0() const
Value of the first dimension.
View of an array of elements of type T.
Definition Span.h:805
View of an array of elements of type T.
Definition Span.h:635
RunCommand makeCommand(const RunQueue &run_queue)
Creates a command associated with the queue run_queue.
void launchRunCommand(RunCommand &command, SimpleForLoopRanges< N, IndexType_ > bounds, Lambda func)
Applies the lambda func on the iteration range given by bounds.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
SimpleForLoopRanges< 1 > makeLoopRanges(Int32 n1)
Creates an iteration range [0,n1[, [0,n2[.