1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
|
#include "libgfortran.h"
#include "util.h"
#include <string.h>
#include <stddef.h>
#include <stdlib.h>
#include <limits.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <assert.h>
#include <errno.h>
/* Shared Memory objects live in their own namspace (usually found under
* /dev/shm/), so the "/" is needed. It is for some reason impossible to
* create a shared memory object without name.
*
* Apple, for some reason, only allows 31 characters in memfd names, so we need
* to make the name a bit shorter in that case. */
#ifndef __APPLE__
#define MEMOBJ_NAME "/gfortran_coarray_memfd"
#define CUT_INT(x) (x)
#else
#define MEMOBJ_NAME "/gfccas_"
#define CUT_INT(x) (x % 100000)
#endif
size_t
alignto (size_t size, size_t align)
{
return align * ((size + align - 1) / align);
}
size_t pagesize;
size_t
round_to_pagesize (size_t s)
{
return alignto (s, pagesize);
}
size_t
next_power_of_two (size_t size)
{
assert (size);
return 1 << (PTR_BITS - __builtin_clzl (size - 1));
}
#define ERRCHECK(a) do { \
int rc = a; \
if (rc) { \
errno = rc; \
perror (#a " failed"); \
exit (1); \
} \
} while(0)
void
initialize_shared_mutex (pthread_mutex_t *mutex)
{
pthread_mutexattr_t mattr;
ERRCHECK (pthread_mutexattr_init (&mattr));
ERRCHECK (pthread_mutexattr_setpshared (&mattr, PTHREAD_PROCESS_SHARED));
ERRCHECK (pthread_mutex_init (mutex, &mattr));
ERRCHECK (pthread_mutexattr_destroy (&mattr));
}
void
initialize_shared_condition (pthread_cond_t *cond)
{
pthread_condattr_t cattr;
ERRCHECK (pthread_condattr_init (&cattr));
ERRCHECK (pthread_condattr_setpshared (&cattr, PTHREAD_PROCESS_SHARED));
ERRCHECK (pthread_cond_init (cond, &cattr));
ERRCHECK (pthread_condattr_destroy (&cattr));
}
int
get_shmem_fd (void)
{
char buffer[1 << 10];
int fd, id;
id = random ();
do
{
snprintf (buffer, sizeof (buffer), MEMOBJ_NAME "_%u_%d",
(unsigned int)getpid (), CUT_INT(id++));
fd = shm_open (buffer, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
if (fd == -1 && errno != EEXIST)
{
perror("Failed to create the memfd");
exit(1);
}
}
while (fd == -1);
shm_unlink (buffer);
return fd;
}
bool
pack_array_prepare (pack_info *restrict pi,
const gfc_array_char *restrict source)
{
index_type dim;
bool packed;
index_type span;
index_type type_size;
index_type ssize;
dim = GFC_DESCRIPTOR_RANK (source);
type_size = GFC_DESCRIPTOR_SIZE (source);
ssize = type_size;
pi->num_elem = 1;
packed = true;
span = source->span != 0 ? source->span : type_size;
for (index_type n = 0; n < dim; n++)
{
pi->stride[n] = GFC_DESCRIPTOR_STRIDE (source, n) * span;
pi->extent[n] = GFC_DESCRIPTOR_EXTENT (source, n);
if (pi->extent[n] <= 0)
{
/* Do nothing. */
packed = 1;
pi->num_elem = 0;
break;
}
if (ssize != pi->stride[n])
packed = 0;
pi->num_elem *= pi->extent[n];
ssize *= pi->extent[n];
}
return packed;
}
void
pack_array_finish (pack_info *const restrict pi,
const gfc_array_char *const restrict source,
char *restrict dest)
{
index_type dim;
const char *restrict src;
index_type size;
index_type stride0;
index_type count[GFC_MAX_DIMENSIONS];
dim = GFC_DESCRIPTOR_RANK (source);
src = source->base_addr;
stride0 = pi->stride[0];
size = GFC_DESCRIPTOR_SIZE (source);
memset (count, '\0', sizeof (count) * dim);
while (src)
{
/* Copy the data. */
memcpy (dest, src, size);
/* Advance to the next element. */
dest += size;
src += stride0;
count[0]++;
/* Advance to the next source element. */
index_type n = 0;
while (count[n] == pi->extent[n])
{
/* When we get to the end of a dimension, reset it and increment
the next dimension. */
count[n] = 0;
/* We could precalculate these products, but this is a less
frequently used path so probably not worth it. */
src -= pi->stride[n] * pi->extent[n];
n++;
if (n == dim)
{
src = NULL;
break;
}
else
{
count[n]++;
src += pi->stride[n];
}
}
}
}
void
unpack_array_finish (pack_info *const restrict pi,
const gfc_array_char *restrict d,
const char *restrict src)
{
index_type stride0;
char *restrict dest;
index_type size;
index_type count[GFC_MAX_DIMENSIONS];
index_type dim;
size = GFC_DESCRIPTOR_SIZE (d);
stride0 = pi->stride[0];
dest = d->base_addr;
dim = GFC_DESCRIPTOR_RANK (d);
memset (count, '\0', sizeof (count) * dim);
while (dest)
{
memcpy (dest, src, size);
src += size;
dest += stride0;
count[0]++;
index_type n = 0;
while (count[n] == pi->extent[n])
{
count[n] = 0;
dest -= pi->stride[n] * pi->extent[n];
n++;
if (n == dim)
{
dest = NULL;
break;
}
else
{
count[n]++;
dest += pi->stride[n];
}
}
}
}
|