Debian Codegraph @

libcudacxx/test/libcudacxx/cuda/pipeline_memcpy_async_producer_consumer.pass.cpp

raw
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of libcu++, the C++ Standard Library for your entire system,
4 // under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 // SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES.
8 //
9 //===----------------------------------------------------------------------===//
10
11 // UNSUPPORTED: pre-sm-70
12
13 #include <cuda/pipeline>
14
15 #include "cuda_space_selector.h"
16 #include <cooperative_groups.h>
17
18 template <class T, cuda::thread_scope PipelineScope>
19 __device__ __noinline__ void test_producer(T* dest, T* source, cuda::pipeline<PipelineScope>& pipe)
20 {
21 pipe.producer_acquire();
22 cuda::memcpy_async(static_cast<void*>(&dest[0]), static_cast<void*>(&source[0]), sizeof(T), pipe);
23 pipe.producer_commit();
24
25 pipe.producer_acquire();
26 cuda::memcpy_async(static_cast<void*>(&dest[1]), static_cast<void*>(&source[1]), sizeof(T), pipe);
27 pipe.producer_commit();
28
29 pipe.producer_acquire();
30 cuda::memcpy_async(static_cast<void*>(&dest[2]), static_cast<void*>(&source[2]), sizeof(T), pipe);
31 pipe.producer_commit();
32 }
33
34 template <class T, cuda::thread_scope PipelineScope>
35 __device__ __noinline__ void test_consumer(T* dest, T* source, cuda::pipeline<PipelineScope>& pipe)
36 {
37 pipe.consumer_wait();
38 assert(source[0] == 12);
39 assert(dest[0] == 12);
40 pipe.consumer_release();
41
42 pipe.consumer_wait();
43 assert(source[1] == 24);
44 assert(dest[1] == 24);
45 pipe.consumer_release();
46
47 pipe.consumer_wait();
48 assert(source[2] == 36);
49 assert(dest[2] == 36);
50 pipe.consumer_release();
51 }
52
53 template <class T,
54 template <typename, typename>
55 class PipelineSelector,
56 cuda::thread_scope PipelineScope,
57 uint8_t PipelineStages>
58 __device__ __noinline__ void test_fully_specialized()
59 {
60 __shared__ T dest[3];
61 __shared__ T* source;
62
63 PipelineSelector<cuda::pipeline_shared_state<PipelineScope, PipelineStages>, constructor_initializer> pipe_state_sel;
64 cuda::pipeline_shared_state<PipelineScope, PipelineStages>* pipe_state = pipe_state_sel.construct();
65
66 auto group = cooperative_groups::this_thread_block();
67
68 if (group.thread_rank() == 0)
69 {
70 source = new T[3];
71 for (size_t i = 0; i < 3; ++i)
72 {
73 source[i] = 12 * (i + 1);
74 }
75 }
76 group.sync();
77
78 {
79 if (group.thread_rank() == 0)
80 {
81 memset(dest, 0, sizeof(T) * 3);
82 }
83 group.sync();
84
85 const cuda::pipeline_role role =
86 (group.thread_rank() % 2) ? cuda::pipeline_role::producer : cuda::pipeline_role::consumer;
87 cuda::pipeline<PipelineScope> pipe = make_pipeline(group, pipe_state, role);
88
89 if (role == cuda::pipeline_role::producer)
90 {
91 test_producer(dest, source, pipe);
92 }
93 else
94 {
95 test_consumer(dest, source, pipe);
96 }
97
98 pipe.quit();
99 }
100
101 group.sync();
102
103 {
104 if (group.thread_rank() == 0)
105 {
106 memset(dest, 0, sizeof(T) * 3);
107 }
108 group.sync();
109
110 const cuda::pipeline_role role =
111 (group.thread_rank() < 32) ? cuda::pipeline_role::producer : cuda::pipeline_role::consumer;
112 cuda::pipeline<PipelineScope> pipe = make_pipeline(group, pipe_state, group.size() / 2);
113
114 if (role == cuda::pipeline_role::producer)
115 {
116 test_producer(dest, source, pipe);
117 }
118 else
119 {
120 test_consumer(dest, source, pipe);
121 }
122
123 pipe.quit();
124 }
125 }
126
127 template <class T, template <typename, typename> class PipelineSelector, cuda::thread_scope PipelineScope>
128 __host__ __device__ __noinline__ void test_select_stages()
129 {
130 test_fully_specialized<T, PipelineSelector, PipelineScope, 1>();
131 test_fully_specialized<T, PipelineSelector, PipelineScope, 8>();
132 }
133
134 template <class T, template <typename, typename> class PipelineSelector>
135 __host__ __device__ __noinline__ void test_select_scope()
136 {
137 test_select_stages<T, PipelineSelector, cuda::thread_scope_block>();
138 test_select_stages<T, PipelineSelector, cuda::thread_scope_device>();
139 test_select_stages<T, PipelineSelector, cuda::thread_scope_system>();
140 }
141
142 template <class T>
143 __host__ __device__ __noinline__ void test_select_pipeline()
144 {
145 test_select_scope<T, shared_memory_selector>();
146 test_select_scope<T, global_memory_selector>();
147 }
148
149 int main(int argc, char** argv)
150 {
151 NV_IF_ELSE_TARGET(
152 NV_IS_HOST, cuda_thread_count = 64;
153 ,
154 (test_select_pipeline<uint8_t>(); test_select_pipeline<uint16_t>(); test_select_pipeline<uint32_t>();
155 test_select_pipeline<uint64_t>();))
156
157 return 0;
158 }