Snitch Runtime
Loading...
Searching...
No Matches
omp.h
1// Copyright 2021 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#pragma once
6
7#include <stdint.h>
8
9#include "eu.h"
10#include "kmp.h"
11
12//================================================================================
13// debug
14//================================================================================
15#define OPENMP_PROFILE
16
17#ifdef OPENMP_PROFILE
18#define OMP_PROF(X) \
19 do { \
20 { X; } \
21 } while (0)
22#else
23#define OMP_PROF(X) \
24 do { \
25 } while (0)
26#endif
27
28#ifdef OMP_DEBUG_LEVEL
29#include "../../../deps/riscv-opcodes/encoding.h"
30#include "printf.h"
31#define _OMP_PRINTF(...) \
32 if (1) { \
33 printf("[omp] "__VA_ARGS__); \
34 }
35#define OMP_PRINTF(d, ...) \
36 if (OMP_DEBUG_LEVEL >= d) { \
37 _OMP_PRINTF(__VA_ARGS__); \
38 }
39#else
40#define OMP_PRINTF(d, ...)
41#endif
42
43//================================================================================
44// Macros
45//================================================================================
46#ifdef OMPSTATIC_NUMTHREADS
47#define _OMP_T const omp_t
48#define _OMP_TEAM_T const omp_team_t
49#else
50#define _OMP_T omp_t
51#define _OMP_TEAM_T omp_team_t
52#endif
53
57#define __snrt_omp_bootstrap(core_idx) \
58 if (snrt_omp_bootstrap(core_idx)) do { \
59 snrt_cluster_hw_barrier(); \
60 return 0; \
61 } while (0)
62
66#define __snrt_omp_destroy(core_idx) \
67 eu_exit(core_idx); \
68 dm_exit(); \
69 snrt_cluster_hw_barrier();
70
71//================================================================================
72// types
73//================================================================================
74
75typedef struct {
76 char nbThreads;
77#ifndef OMPSTATIC_NUMTHREADS
78 int loop_epoch;
79 int loop_start;
80 int loop_end;
81 int loop_incr;
82 int loop_chunk;
83 int loop_is_setup;
84 int core_epoch[16]; // for dynamic scheduling
85#endif
87
88typedef struct {
89#ifndef OMPSTATIC_NUMTHREADS
90 omp_team_t plainTeam;
91 int numThreads;
92 int maxThreads;
93#else
94 const omp_team_t plainTeam;
95 const int numThreads;
96 const int maxThreads;
97#endif
110 _kmp_ptr32 *kmpc_args;
111} omp_t;
112
113#ifdef OPENMP_PROFILE
114typedef struct {
115 uint32_t fork_oh;
116} omp_prof_t;
117extern omp_prof_t *omp_prof;
118#endif
119
120#ifndef OMPSTATIC_NUMTHREADS
121extern __thread omp_t volatile *omp_p;
122#else
123extern omp_t omp_p;
124#endif
125
126//================================================================================
127// exported
128//================================================================================
129
130void omp_init(void);
131unsigned snrt_omp_bootstrap(uint32_t core_idx);
132void partialParallelRegion(int32_t argc, void *data,
133 void (*fn)(void *, uint32_t), int num_threads);
134
135void omp_print_prof(void);
136#ifdef OPENMP_PROFILE
137extern omp_prof_t *omp_prof;
138#endif
139
140//================================================================================
141// inlines
142//================================================================================
143
144#ifndef OMPSTATIC_NUMTHREADS
145static inline omp_t *omp_getData() { return (omp_t *)omp_p; }
146static inline omp_team_t *omp_get_team(omp_t *_this) {
147 return &_this->plainTeam;
148}
149#else
150static inline const omp_t *omp_getData() { return &omp_p; }
151static inline const omp_team_t *omp_get_team(const omp_t *_this) {
152 return &_this->plainTeam;
153}
154#endif
155
156static inline unsigned omp_get_thread_num(void) {
157 return snrt_cluster_core_idx();
158}
159
160static inline unsigned omp_get_num_threads(void) {
161 return snrt_cluster_compute_core_num();
162}
163
164static inline void parallelRegion(int32_t argc, void *data,
165 void (*fn)(void *, uint32_t),
166 int num_threads) {
167#ifndef OMPSTATIC_NUMTHREADS
168 omp_p->plainTeam.nbThreads = num_threads;
169#endif
170
171 OMP_PRINTF(10, "num_threads=%d nbThreads=%d omp_p->numThreads=%d\n",
172 num_threads, omp_p->plainTeam.nbThreads, omp_p->numThreads);
173
174 // Now that the team is ready, wake up slaves
175 (void)eu_dispatch_push(fn, argc, data, num_threads);
176
177 eu_run_empty(snrt_cluster_core_idx());
178}
Definition omp.h:114
Definition omp.h:88
_kmp_ptr32 * kmpc_args
Usually the arguments passed to __kmpc_fork_call would do a malloc with the amount of arguments passe...
Definition omp.h:110
snrt_barrier_t * kmpc_barrier
Pointer to the barrier register used for synchronization eg with #pragma omp barrier.
Definition omp.h:103
Definition omp.h:75
Definition sync_decls.h:9