Snitch Runtime
Loading...
Searching...
No Matches
sync.h
Go to the documentation of this file.
1// Copyright 2023 ETH Zurich and University of Bologna.
2// Licensed under the Apache License, Version 2.0, see LICENSE for details.
3// SPDX-License-Identifier: Apache-2.0
4//
5// Luca Colagrande <colluca@iis.ee.ethz.ch>
6// Viviane Potocnik <vivianep@iis.ee.ethz.ch>
7
13#pragma once
14
15#include <math.h>
16
17//================================================================================
18// Mutex functions
19//================================================================================
20
24inline volatile uint32_t *snrt_mutex() { return &_snrt_mutex; }
25
34inline void snrt_mutex_acquire(volatile uint32_t *pmtx) {
35 asm volatile(
36 "li t0,1 # t0 = 1\n"
37 "1:\n"
38 " amoswap.w.aq t0,t0,(%0) # t0 = oldlock & lock = 1\n"
39 " bnez t0,1b # Retry if previously set)\n"
40 : "+r"(pmtx)
41 :
42 : "t0");
43}
44
50inline void snrt_mutex_ttas_acquire(volatile uint32_t *pmtx) {
51 asm volatile(
52 "1:\n"
53 " lw t0, 0(%0)\n"
54 " bnez t0, 1b\n"
55 " li t0,1 # t0 = 1\n"
56 "2:\n"
57 " amoswap.w.aq t0,t0,(%0) # t0 = oldlock & lock = 1\n"
58 " bnez t0,2b # Retry if previously set)\n"
59 : "+r"(pmtx)
60 :
61 : "t0");
62}
63
67inline void snrt_mutex_release(volatile uint32_t *pmtx) {
68 asm volatile("amoswap.w.rl x0,x0,(%0) # Release lock by storing 0\n"
69 : "+r"(pmtx));
70}
71
72//================================================================================
73// Barrier functions
74//================================================================================
75
82 asm volatile("csrr x0, 0x7C2" ::: "memory");
83}
84
92 // Remember previous iteration
93 uint32_t prev_barrier_iteration = _snrt_barrier.iteration;
94 uint32_t cnt =
95 __atomic_add_fetch(&(_snrt_barrier.cnt), 1, __ATOMIC_RELAXED);
96
97 // Increment the barrier counter
98 if (cnt == snrt_cluster_num()) {
99 _snrt_barrier.cnt = 0;
100 __atomic_add_fetch(&(_snrt_barrier.iteration), 1, __ATOMIC_RELAXED);
101 } else {
102 while (prev_barrier_iteration == _snrt_barrier.iteration)
103 ;
104 }
105}
106
116inline void snrt_global_barrier() {
118
119 // Synchronize all DM cores in software
120 if (snrt_is_dm_core()) {
122 }
123 // Synchronize cores in a cluster with the HW barrier
125}
126
134inline void snrt_partial_barrier(snrt_barrier_t *barr, uint32_t n) {
135 // Remember previous iteration
136 uint32_t prev_it = barr->iteration;
137 uint32_t cnt = __atomic_add_fetch(&barr->cnt, 1, __ATOMIC_RELAXED);
138
139 // Increment the barrier counter
140 if (cnt == n) {
141 barr->cnt = 0;
142 __atomic_add_fetch(&barr->iteration, 1, __ATOMIC_RELAXED);
143 } else {
144 // Some threads have not reached the barrier --> Let's wait
145 while (prev_it == barr->iteration)
146 ;
147 }
148}
149
150//================================================================================
151// Reduction functions
152//================================================================================
153
164inline uint32_t snrt_global_all_to_all_reduction(uint32_t value) {
165 __atomic_add_fetch(&_reduction_result, value, __ATOMIC_RELAXED);
167 return _reduction_result;
168}
169
185inline void snrt_global_reduction_dma(double *dst_buffer, double *src_buffer,
186 size_t len) {
187 // If we have a single cluster the reduction degenerates to a memcpy
188 if (snrt_cluster_num() == 1) {
189 if (!snrt_is_compute_core()) {
190 snrt_dma_start_1d(dst_buffer, src_buffer, len * sizeof(double));
191 snrt_dma_wait_all();
192 }
194 } else {
195 // Iterate levels in the binary reduction tree
196 int num_levels = ceil(log2(snrt_cluster_num()));
197 for (unsigned int level = 0; level < num_levels; level++) {
198 // Determine whether the current cluster is an active cluster.
199 // An active cluster is a cluster that participates in the current
200 // level of the reduction tree. Every second cluster among the
201 // active ones is a sender.
202 uint32_t is_active = (snrt_cluster_idx() % (1 << level)) == 0;
203 uint32_t is_sender = (snrt_cluster_idx() % (1 << (level + 1))) != 0;
204
205 // If the cluster is a sender, it sends the data in its source
206 // buffer to the respective receiver's destination buffer
207 if (is_active && is_sender) {
208 if (!snrt_is_compute_core()) {
209 void *dst =
210 (void *)dst_buffer - (1 << level) * SNRT_CLUSTER_OFFSET;
211 snrt_dma_start_1d(dst, src_buffer, len * sizeof(double));
212 snrt_dma_wait_all();
213 }
214 }
215
216 // Synchronize senders and receivers
218
219 // Every cluster which is not a sender performs the reduction
220 if (is_active && !is_sender) {
221 // Computation is parallelized over the compute cores
222 if (snrt_is_compute_core()) {
223 uint32_t items_per_core =
224 len / snrt_cluster_compute_core_num();
225 uint32_t core_offset =
226 snrt_cluster_core_idx() * items_per_core;
227 for (uint32_t i = 0; i < items_per_core; i++) {
228 uint32_t abs_i = core_offset + i;
229 dst_buffer[abs_i] += src_buffer[abs_i];
230 }
231 }
232 }
233
234 // Synchronize compute and DM cores for next tree level
236 }
237 }
238}
Definition sync_decls.h:9
void snrt_partial_barrier(snrt_barrier_t *barr, uint32_t n)
Generic software barrier.
Definition sync.h:134
void snrt_mutex_ttas_acquire(volatile uint32_t *pmtx)
Acquire a mutex, blocking.
Definition sync.h:50
void snrt_mutex_acquire(volatile uint32_t *pmtx)
Acquire a mutex, blocking.
Definition sync.h:34
volatile uint32_t * snrt_mutex()
Get a pointer to a mutex variable.
Definition sync.h:24
void snrt_inter_cluster_barrier()
Synchronize one core from every cluster with the others.
Definition sync.h:91
uint32_t snrt_global_all_to_all_reduction(uint32_t value)
Perform a global sum reduction, blocking.
Definition sync.h:164
void snrt_global_barrier()
Synchronize all Snitch cores.
Definition sync.h:116
void snrt_cluster_hw_barrier()
Synchronize cores in a cluster with a hardware barrier, blocking.
Definition sync.h:81
void snrt_mutex_release(volatile uint32_t *pmtx)
Release a previously-acquired mutex.
Definition sync.h:67
void snrt_global_reduction_dma(double *dst_buffer, double *src_buffer, size_t len)
Perform a sum reduction among clusters, blocking.
Definition sync.h:185