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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
|
/* Copyright (C) 2019-2020 Free Software Foundation, Inc.
Contributed by Nicolas Koenig
This file is part of the GNU Fortran Native Coarray Library (libnca).
Libnca is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
Libnca is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
Under Section 7 of GPL version 3, you are granted additional
permissions described in the GCC Runtime Library Exception, version
3.1, as published by the Free Software Foundation.
You should have received a copy of the GNU General Public License and
a copy of the GCC Runtime Library Exception along with this program;
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
<http://www.gnu.org/licenses/>. */
#include "libgfortran.h"
#include "libcoarraynative.h"
#include "lock.h"
#include <string.h>
static inline int
div_ru (int divident, int divisor)
{
return (divident + divisor - 1) / divisor;
}
/* Need to keep this in sync with
trans-array.h:gfc_coarray_allocation_type. */
enum gfc_coarray_allocation_type
{
GFC_NCA_NORMAL_COARRAY = 1,
GFC_NCA_LOCK_COARRAY,
GFC_NCA_EVENT_COARRAY,
};
void cas_coarray_alloc (gfc_array_void *, size_t, int, int);
export_proto (cas_coarray_alloc);
void cas_coarray_alloc_chk (gfc_array_void *, size_t, int, int, int *,
char *, size_t);
export_proto (cas_coarray_alloc_chk);
void cas_coarray_free (gfc_array_void *, int);
export_proto (cas_coarray_free);
int cas_coarray_this_image (int);
export_proto (cas_coarray_this_image);
int cas_coarray_num_images (int);
export_proto (cas_coarray_num_images);
void cas_coarray_sync_all (int *);
export_proto (cas_coarray_sync_all);
void cas_sync_images (int, int *, int *, char *, size_t);
export_proto (cas_sync_images);
void cas_lock (void *);
export_proto (cas_lock);
void cas_unlock (void *);
export_proto (cas_unlock);
void cas_collsub_reduce_array (gfc_array_char *, void (*) (void *, void *),
int *, int *, char *, size_t);
export_proto (cas_collsub_reduce_array);
void cas_collsub_reduce_scalar (void *, index_type, void (*) (void *, void *),
int *, int *, char *, size_t);
export_proto (cas_collsub_reduce_scalar);
void cas_collsub_broadcast_array (gfc_array_char *restrict, int, int *, char *,
size_t);
export_proto (cas_collsub_broadcast_array);
void cas_collsub_broadcast_scalar (void *restrict, size_t, int, int *, char *,
size_t);
export_proto (cas_collsub_broadcast_scalar);
static void
cas_coarray_alloc_work (gfc_array_void *desc, size_t elem_size, int corank,
int alloc_type)
{
int i, last_rank_index;
int num_coarray_elems, num_elems; /* Excludes the last dimension, because it
will have to be determined later. */
int extent_last_codimen;
size_t last_lbound;
size_t size_in_bytes;
if (alloc_type == GFC_NCA_LOCK_COARRAY)
elem_size = sizeof (pthread_mutex_t);
else if (alloc_type == GFC_NCA_EVENT_COARRAY)
elem_size = sizeof (char); /* replace with proper type. */
last_rank_index = GFC_DESCRIPTOR_RANK (desc) + corank - 1;
num_elems = 1;
num_coarray_elems = 1;
for (i = 0; i < GFC_DESCRIPTOR_RANK (desc); i++)
num_elems *= GFC_DESCRIPTOR_EXTENT (desc, i);
for (i = GFC_DESCRIPTOR_RANK (desc); i < last_rank_index; i++)
{
num_elems *= GFC_DESCRIPTOR_EXTENT (desc, i);
num_coarray_elems *= GFC_DESCRIPTOR_EXTENT (desc, i);
}
extent_last_codimen = div_ru (local->total_num_images, num_coarray_elems);
last_lbound = GFC_DIMENSION_LBOUND (desc->dim[last_rank_index]);
GFC_DIMENSION_SET (desc->dim[last_rank_index], last_lbound,
last_lbound + extent_last_codimen - 1, num_elems);
size_in_bytes = elem_size * num_elems * extent_last_codimen;
if (alloc_type == GFC_NCA_LOCK_COARRAY)
{
lock_array *addr;
int expected = 0;
/* Allocate enough space for the metadata infront of the lock
array. */
addr = get_memory_by_id_zero (
&local->ai, size_in_bytes + sizeof (lock_array), (intptr_t)desc);
/* Use of a traditional spin lock to avoid race conditions with
the initization of the mutex. We could alternatively put a
global lock around allocate, but that would probably be
slower. */
while (!__atomic_compare_exchange_n (&addr->owner, &expected,
this_image.image_num + 1, false,
__ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST))
;
if (!addr->initialized++)
{
for (i = 0; i < local->total_num_images; i++)
initialize_shared_mutex (&addr->arr[i]);
}
__atomic_store_n (&addr->owner, 0, __ATOMIC_SEQ_CST);
desc->base_addr = &addr->arr;
}
else if (alloc_type == GFC_NCA_EVENT_COARRAY)
(void)0; // TODO
else
desc->base_addr =
get_memory_by_id (&local->ai, size_in_bytes, (intptr_t) desc);
}
void
cas_coarray_alloc (gfc_array_void *desc, size_t elem_size, int corank,
int alloc_type)
{
ensure_initialization (); /* This function might be the first one to be
called, if it is called in a constructor. */
cas_coarray_alloc_work (desc, elem_size, corank, alloc_type);
}
void
cas_coarray_alloc_chk (gfc_array_void *desc, size_t elem_size, int corank,
int alloc_type, int *status, char *errmsg,
size_t errmsg_len)
{
STAT_ERRMSG_ENTRY_CHECK (status, errmsg, errmsg_len);
if (unlikely(GFC_DESCRIPTOR_DATA (desc) != NULL))
{
if (status == NULL)
{
fprintf (stderr,"Image %d: Attempting to allocate already allocated "
"variable at %p %p\n", this_image.image_num + 1, (void *) desc,
desc->base_addr);
exit (1);
}
else
{
*status = LIBERROR_ALLOCATION;
if (errmsg)
{
size_t errmsg_written_bytes;
errmsg_written_bytes
= snprintf (errmsg, errmsg_len, "Attempting to allocate already "
"allocated variable");
if (errmsg_written_bytes > errmsg_len - 1)
errmsg_written_bytes = errmsg_len - 1;
memset (errmsg + errmsg_written_bytes, ' ',
errmsg_len - errmsg_written_bytes);
}
return;
}
}
cas_coarray_alloc_work (desc, elem_size, corank, alloc_type);
sync_all (&local->si);
}
void
cas_coarray_free (gfc_array_void *desc, int alloc_type)
{
int i;
if (alloc_type == GFC_NCA_LOCK_COARRAY)
{
lock_array *la;
int expected = 0;
la = desc->base_addr - offsetof (lock_array, arr);
/* TODO: Fix this, replace with some kind of atomic initilization. */
while (!__atomic_compare_exchange_n (&la->owner, &expected,
this_image.image_num + 1, false,
__ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST))
;
if (!--la->initialized)
{
/* Coarray locks can be removed and just normal
pthread_mutex can be used. */
for (i = 0; i < local->total_num_images; i++)
pthread_mutex_destroy (&la->arr[i]);
}
__atomic_store_n (&la->owner, 0, __ATOMIC_SEQ_CST);
}
else if (alloc_type == GFC_NCA_EVENT_COARRAY)
(void)0; // TODO
free_memory_with_id (&local->ai, (intptr_t)desc);
desc->base_addr = NULL;
}
int
cas_coarray_this_image (int distance __attribute__ ((unused)))
{
return this_image.image_num + 1;
}
int
cas_coarray_num_images (int distance __attribute__ ((unused)))
{
return local->total_num_images;
}
void
cas_coarray_sync_all (int *stat)
{
STAT_ERRMSG_ENTRY_CHECK (stat, NULL, 0);
sync_all (&local->si);
}
void
cas_sync_images (int s, int *images, int *stat, char *error,
size_t err_size)
{
STAT_ERRMSG_ENTRY_CHECK (stat, error, err_size);
sync_table (&local->si, images, s);
}
void
cas_lock (void *lock)
{
pthread_mutex_lock (lock);
}
void
cas_unlock (void *lock)
{
pthread_mutex_unlock (lock);
}
void
cas_collsub_reduce_array (gfc_array_char *desc,
void (*assign_function) (void *, void *),
int *result_image, int *stat, char *errmsg,
size_t errmsg_len)
{
STAT_ERRMSG_ENTRY_CHECK (stat, errmsg, errmsg_len);
collsub_reduce_array (&local->ci, desc, result_image, assign_function);
}
void
cas_collsub_reduce_scalar (void *obj, index_type elem_size,
void (*assign_function) (void *, void *),
int *result_image, int *stat, char *errmsg,
size_t errmsg_len)
{
STAT_ERRMSG_ENTRY_CHECK (stat, errmsg, errmsg_len);
collsub_reduce_scalar (&local->ci, obj, elem_size, result_image,
assign_function);
}
void
cas_collsub_broadcast_array (gfc_array_char *restrict a, int source_image,
int *stat, char *errmsg, size_t errmsg_len)
{
STAT_ERRMSG_ENTRY_CHECK (stat, errmsg, errmsg_len);
collsub_broadcast_array (&local->ci, a, source_image - 1);
}
void
cas_collsub_broadcast_scalar (void *restrict obj, size_t size,
int source_image, int *stat, char *errmsg,
size_t errmsg_len)
{
STAT_ERRMSG_ENTRY_CHECK (stat, errmsg, errmsg_len);
collsub_broadcast_scalar (&local->ci, obj, size, source_image - 1);
}
|