Debian Codegraph @

libgomp/config/gcn/team.c

raw
1 /* Copyright (C) 2017-2025 Free Software Foundation, Inc.
2 Contributed by Mentor Embedded.
3
4 This file is part of the GNU Offloading and Multi Processing Library
5 (libgomp).
6
7 Libgomp is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 more details.
16
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
20
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
25
26 /* This file handles maintenance of threads on AMD GCN. */
27
28 #include "libgomp.h"
29 #include <stdlib.h>
30 #include <string.h>
31
32 #define LITTLEENDIAN_CPU
33 #include "hsa.h"
34
35 /* Defined in basic-allocator.c via config/amdgcn/allocator.c. */
36 void __gcn_lowlat_init (void *heap, size_t size);
37
38 static void gomp_thread_start (struct gomp_thread_pool *);
39 extern void build_indirect_map (void);
40
41 /* This externally visible function handles target region entry. It
42 sets up a per-team thread pool and transfers control by returning to
43 the kernel in the master thread or gomp_thread_start in other threads.
44
45 The name of this function is part of the interface with the compiler: for
46 each OpenMP kernel the compiler configures the stack, then calls here.
47
48 Likewise, gomp_gcn_exit_kernel is called during the kernel epilogue. */
49
50 void
51 gomp_gcn_enter_kernel (void)
52 {
53 int threadid = __builtin_gcn_dim_pos (1);
54
55 if (threadid == 0)
56 {
57 int numthreads = __builtin_gcn_dim_size (1);
58 int teamid = __builtin_gcn_dim_pos(0);
59
60 /* Initialize indirect function support. */
61 if (teamid == 0)
62 build_indirect_map ();
63
64 /* Set up the global state.
65 Every team will do this, but that should be harmless. */
66 gomp_global_icv.nthreads_var = 16;
67 gomp_global_icv.thread_limit_var = numthreads;
68 /* Starting additional threads is not supported. */
69 gomp_global_icv.dyn_var = true;
70
71 int __lds *gomp_team_num = (int __lds *) GOMP_TEAM_NUM;
72 *gomp_team_num = 0;
73
74 /* Initialize the team arena for optimized memory allocation.
75 The arena has been allocated on the host side, and the address
76 passed in via the kernargs. Each team takes a small slice of it. */
77 struct kernargs_abi *kernargs =
78 (struct kernargs_abi*) __builtin_gcn_kernarg_ptr ();
79 void *team_arena = ((void*)kernargs->arena_ptr
80 + kernargs->arena_size_per_team * teamid);
81 void * __lds *arena_start = (void * __lds *)TEAM_ARENA_START;
82 void * __lds *arena_free = (void * __lds *)TEAM_ARENA_FREE;
83 void * __lds *arena_end = (void * __lds *)TEAM_ARENA_END;
84 *arena_start = team_arena;
85 *arena_free = team_arena;
86 *arena_end = team_arena + kernargs->arena_size_per_team;
87
88 /* Initialize the low-latency heap. The header is the size. */
89 void __lds *lowlat = (void __lds *)GCN_LOWLAT_HEAP;
90 hsa_kernel_dispatch_packet_t *queue_ptr = __builtin_gcn_dispatch_ptr ();
91 __gcn_lowlat_init ((void*)(uintptr_t)(void __flat*)lowlat,
92 queue_ptr->group_segment_size - GCN_LOWLAT_HEAP);
93
94 /* Allocate and initialize the team-local-storage data. */
95 struct gomp_thread *thrs = team_malloc_cleared (sizeof (*thrs)
96 * numthreads);
97 set_gcn_thrs (thrs);
98
99 /* Allocate and initialize a pool of threads in the team.
100 The threads are already running, of course, we just need to manage
101 the communication between them. */
102 struct gomp_thread_pool *pool = team_malloc (sizeof (*pool));
103 pool->threads = team_malloc (sizeof (void *) * numthreads);
104 for (int tid = 0; tid < numthreads; tid++)
105 pool->threads[tid] = &thrs[tid];
106 pool->threads_size = numthreads;
107 pool->threads_used = numthreads;
108 pool->threads_busy = 1;
109 pool->last_team = NULL;
110 gomp_simple_barrier_init (&pool->threads_dock, numthreads);
111 thrs->thread_pool = pool;
112
113 asm ("s_barrier" ::: "memory");
114 return; /* Return to kernel. */
115 }
116 else
117 {
118 asm ("s_barrier" ::: "memory");
119 gomp_thread_start (gcn_thrs ()[0].thread_pool);
120 /* gomp_thread_start does not return. */
121 }
122 }
123
124 void
125 gomp_gcn_exit_kernel (void)
126 {
127 gomp_free_thread (gcn_thrs ());
128 team_free (gcn_thrs ());
129 }
130
131 /* This function contains the idle loop in which a thread waits
132 to be called up to become part of a team. */
133
134 static void
135 gomp_thread_start (struct gomp_thread_pool *pool)
136 {
137 struct gomp_thread *thr = gomp_thread ();
138
139 gomp_sem_init (&thr->release, 0);
140 thr->thread_pool = pool;
141
142 /* The loop exits only when "fn" is assigned "gomp_free_pool_helper",
143 which contains "s_endpgm", or an infinite no-op loop is
144 suspected (this happens when the thread master crashes). */
145 int nul_limit = 99;
146 do
147 {
148 gomp_simple_barrier_wait (&pool->threads_dock);
149 if (!thr->fn)
150 {
151 if (nul_limit-- > 0)
152 continue;
153 else
154 {
155 const char msg[] = ("team master not responding;"
156 " slave thread aborting");
157 write (2, msg, sizeof (msg)-1);
158 abort();
159 }
160 }
161 thr->fn (thr->data);
162 thr->fn = NULL;
163
164 struct gomp_task *task = thr->task;
165 gomp_team_barrier_wait_final (&thr->ts.team->barrier);
166 gomp_finish_task (task);
167 }
168 while (1);
169 }
170
171 /* Launch a team. */
172
173 void
174 gomp_team_start (void (*fn) (void *), void *data, unsigned nthreads,
175 unsigned flags, struct gomp_team *team,
176 struct gomp_taskgroup *taskgroup)
177 {
178 struct gomp_thread *thr, *nthr;
179 struct gomp_task *task;
180 struct gomp_task_icv *icv;
181 struct gomp_thread_pool *pool;
182 unsigned long nthreads_var;
183
184 thr = gomp_thread ();
185 pool = thr->thread_pool;
186 task = thr->task;
187 icv = task ? &task->icv : &gomp_global_icv;
188
189 /* Always save the previous state, even if this isn't a nested team.
190 In particular, we should save any work share state from an outer
191 orphaned work share construct. */
192 team->prev_ts = thr->ts;
193
194 thr->ts.team = team;
195 thr->ts.team_id = 0;
196 ++thr->ts.level;
197 if (nthreads > 1)
198 ++thr->ts.active_level;
199 thr->ts.work_share = &team->work_shares[0];
200 thr->ts.last_work_share = NULL;
201 thr->ts.single_count = 0;
202 thr->ts.static_trip = 0;
203 thr->task = &team->implicit_task[0];
204 nthreads_var = icv->nthreads_var;
205 gomp_init_task (thr->task, task, icv);
206 team->implicit_task[0].icv.nthreads_var = nthreads_var;
207 team->implicit_task[0].taskgroup = taskgroup;
208
209 if (nthreads == 1)
210 return;
211
212 /* Release existing idle threads. */
213 for (unsigned i = 1; i < nthreads; ++i)
214 {
215 nthr = pool->threads[i];
216 nthr->ts.team = team;
217 nthr->ts.work_share = &team->work_shares[0];
218 nthr->ts.last_work_share = NULL;
219 nthr->ts.team_id = i;
220 nthr->ts.level = team->prev_ts.level + 1;
221 nthr->ts.active_level = thr->ts.active_level;
222 nthr->ts.single_count = 0;
223 nthr->ts.static_trip = 0;
224 nthr->task = &team->implicit_task[i];
225 gomp_init_task (nthr->task, task, icv);
226 team->implicit_task[i].icv.nthreads_var = nthreads_var;
227 team->implicit_task[i].taskgroup = taskgroup;
228 nthr->fn = fn;
229 nthr->data = data;
230 team->ordered_release[i] = &nthr->release;
231 }
232
233 gomp_simple_barrier_wait (&pool->threads_dock);
234 }
235
236 #include "../../team.c"