| 1 |
/* Single-image implementation of GNU Fortran Coarray Library |
| 2 |
Copyright (C) 2011-2025 Free Software Foundation, Inc. |
| 3 |
Contributed by Tobias Burnus <[email protected]> |
| 4 |
|
| 5 |
This file is part of the GNU Fortran Coarray Runtime Library (libcaf). |
| 6 |
|
| 7 |
Libcaf is free software; you can redistribute it and/or modify |
| 8 |
it 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 |
Libcaf is distributed in the hope that it will be useful, |
| 13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
GNU General Public License for 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 |
#include "libcaf.h" |
| 27 |
#include <stdio.h> /* For fputs and fprintf. */ |
| 28 |
#include <stdlib.h> /* For exit and malloc. */ |
| 29 |
#include <string.h> /* For memcpy and memset. */ |
| 30 |
#include <stdarg.h> /* For variadic arguments. */ |
| 31 |
#include <stdint.h> |
| 32 |
#include <assert.h> |
| 33 |
|
| 34 |
/* Define GFC_CAF_CHECK to enable run-time checking. */ |
| 35 |
/* #define GFC_CAF_CHECK 1 */ |
| 36 |
|
| 37 |
struct caf_single_token |
| 38 |
{ |
| 39 |
/* The pointer to the memory registered. For arrays this is the data member |
| 40 |
in the descriptor. For components it's the pure data pointer. */ |
| 41 |
void *memptr; |
| 42 |
/* The descriptor when this token is associated to an allocatable array. */ |
| 43 |
gfc_descriptor_t *desc; |
| 44 |
/* Set when the caf lib has allocated the memory in memptr and is responsible |
| 45 |
for freeing it on deregister. */ |
| 46 |
bool owning_memory; |
| 47 |
}; |
| 48 |
typedef struct caf_single_token *caf_single_token_t; |
| 49 |
|
| 50 |
#define TOKEN(X) ((caf_single_token_t) (X)) |
| 51 |
#define MEMTOK(X) ((caf_single_token_t) (X))->memptr |
| 52 |
|
| 53 |
/* Single-image implementation of the CAF library. |
| 54 |
Note: For performance reasons -fcoarry=single should be used |
| 55 |
rather than this library. */ |
| 56 |
|
| 57 |
/* Global variables. */ |
| 58 |
caf_static_t *caf_static_list = NULL; |
| 59 |
|
| 60 |
typedef void (*getter_t) (void *, const int *, void **, int32_t *, void *, |
| 61 |
caf_token_t, const size_t, size_t *, const size_t *); |
| 62 |
typedef void (*is_present_t) (void *, const int *, int32_t *, void *, |
| 63 |
caf_single_token_t, const size_t); |
| 64 |
typedef void (*receiver_t) (void *, const int *, void *, const void *, |
| 65 |
caf_token_t, const size_t, const size_t *, |
| 66 |
const size_t *); |
| 67 |
struct accessor_hash_t |
| 68 |
{ |
| 69 |
int hash; |
| 70 |
int pad; |
| 71 |
union |
| 72 |
{ |
| 73 |
getter_t getter; |
| 74 |
is_present_t is_present; |
| 75 |
receiver_t receiver; |
| 76 |
} u; |
| 77 |
}; |
| 78 |
|
| 79 |
static struct accessor_hash_t *accessor_hash_table = NULL; |
| 80 |
static int aht_cap = 0; |
| 81 |
static int aht_size = 0; |
| 82 |
static enum { |
| 83 |
AHT_UNINITIALIZED, |
| 84 |
AHT_OPEN, |
| 85 |
AHT_PREPARED |
| 86 |
} accessor_hash_table_state |
| 87 |
= AHT_UNINITIALIZED; |
| 88 |
|
| 89 |
/* Keep in sync with mpi.c. */ |
| 90 |
static void |
| 91 |
caf_runtime_error (const char *message, ...) |
| 92 |
{ |
| 93 |
va_list ap; |
| 94 |
fprintf (stderr, "Fortran runtime error: "); |
| 95 |
va_start (ap, message); |
| 96 |
vfprintf (stderr, message, ap); |
| 97 |
va_end (ap); |
| 98 |
fprintf (stderr, "\n"); |
| 99 |
|
| 100 |
/* FIXME: Shutdown the Fortran RTL to flush the buffer. PR 43849. */ |
| 101 |
exit (EXIT_FAILURE); |
| 102 |
} |
| 103 |
|
| 104 |
/* Error handling is similar everytime. */ |
| 105 |
static void |
| 106 |
caf_internal_error (const char *msg, int *stat, char *errmsg, |
| 107 |
size_t errmsg_len, ...) |
| 108 |
{ |
| 109 |
va_list args; |
| 110 |
va_start (args, errmsg_len); |
| 111 |
if (stat) |
| 112 |
{ |
| 113 |
*stat = 1; |
| 114 |
if (errmsg_len > 0) |
| 115 |
{ |
| 116 |
int len = snprintf (errmsg, errmsg_len, msg, args); |
| 117 |
if (len >= 0 && errmsg_len > (size_t) len) |
| 118 |
memset (&errmsg[len], ' ', errmsg_len - len); |
| 119 |
} |
| 120 |
va_end (args); |
| 121 |
return; |
| 122 |
} |
| 123 |
else |
| 124 |
caf_runtime_error (msg, args); |
| 125 |
va_end (args); |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
void |
| 130 |
_gfortran_caf_init (int *argc __attribute__ ((unused)), |
| 131 |
char ***argv __attribute__ ((unused))) |
| 132 |
{ |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
void |
| 137 |
_gfortran_caf_finalize (void) |
| 138 |
{ |
| 139 |
free (accessor_hash_table); |
| 140 |
|
| 141 |
while (caf_static_list != NULL) |
| 142 |
{ |
| 143 |
caf_static_t *tmp = caf_static_list->prev; |
| 144 |
free (((caf_single_token_t) caf_static_list->token)->memptr); |
| 145 |
free (caf_static_list->token); |
| 146 |
free (caf_static_list); |
| 147 |
caf_static_list = tmp; |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
int |
| 153 |
_gfortran_caf_this_image (int distance __attribute__ ((unused))) |
| 154 |
{ |
| 155 |
return 1; |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
int |
| 160 |
_gfortran_caf_num_images (int distance __attribute__ ((unused)), |
| 161 |
int failed __attribute__ ((unused))) |
| 162 |
{ |
| 163 |
return 1; |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
void |
| 168 |
_gfortran_caf_register (size_t size, caf_register_t type, caf_token_t *token, |
| 169 |
gfc_descriptor_t *data, int *stat, char *errmsg, |
| 170 |
size_t errmsg_len) |
| 171 |
{ |
| 172 |
const char alloc_fail_msg[] = "Failed to allocate coarray"; |
| 173 |
void *local; |
| 174 |
caf_single_token_t single_token; |
| 175 |
|
| 176 |
if (type == CAF_REGTYPE_LOCK_STATIC || type == CAF_REGTYPE_LOCK_ALLOC |
| 177 |
|| type == CAF_REGTYPE_CRITICAL) |
| 178 |
local = calloc (size, sizeof (bool)); |
| 179 |
else if (type == CAF_REGTYPE_EVENT_STATIC || type == CAF_REGTYPE_EVENT_ALLOC) |
| 180 |
/* In the event_(wait|post) function the counter for events is a uint32, |
| 181 |
so better allocate enough memory here. */ |
| 182 |
local = calloc (size, sizeof (uint32_t)); |
| 183 |
else if (type == CAF_REGTYPE_COARRAY_ALLOC_REGISTER_ONLY) |
| 184 |
local = NULL; |
| 185 |
else |
| 186 |
local = malloc (size); |
| 187 |
|
| 188 |
if (type != CAF_REGTYPE_COARRAY_ALLOC_ALLOCATE_ONLY) |
| 189 |
*token = malloc (sizeof (struct caf_single_token)); |
| 190 |
|
| 191 |
if (unlikely (*token == NULL |
| 192 |
|| (local == NULL |
| 193 |
&& type != CAF_REGTYPE_COARRAY_ALLOC_REGISTER_ONLY))) |
| 194 |
{ |
| 195 |
/* Freeing the memory conditionally seems pointless, but |
| 196 |
caf_internal_error () may return, when a stat is given and then the |
| 197 |
memory may be lost. */ |
| 198 |
free (local); |
| 199 |
free (*token); |
| 200 |
caf_internal_error (alloc_fail_msg, stat, errmsg, errmsg_len); |
| 201 |
return; |
| 202 |
} |
| 203 |
|
| 204 |
single_token = TOKEN (*token); |
| 205 |
single_token->memptr = local; |
| 206 |
single_token->owning_memory = type != CAF_REGTYPE_COARRAY_ALLOC_REGISTER_ONLY; |
| 207 |
single_token->desc = GFC_DESCRIPTOR_RANK (data) > 0 ? data : NULL; |
| 208 |
|
| 209 |
|
| 210 |
if (stat) |
| 211 |
*stat = 0; |
| 212 |
|
| 213 |
if (type == CAF_REGTYPE_COARRAY_STATIC || type == CAF_REGTYPE_LOCK_STATIC |
| 214 |
|| type == CAF_REGTYPE_CRITICAL || type == CAF_REGTYPE_EVENT_STATIC |
| 215 |
|| type == CAF_REGTYPE_EVENT_ALLOC) |
| 216 |
{ |
| 217 |
caf_static_t *tmp = malloc (sizeof (caf_static_t)); |
| 218 |
tmp->prev = caf_static_list; |
| 219 |
tmp->token = *token; |
| 220 |
caf_static_list = tmp; |
| 221 |
} |
| 222 |
GFC_DESCRIPTOR_DATA (data) = local; |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
void |
| 227 |
_gfortran_caf_deregister (caf_token_t *token, caf_deregister_t type, int *stat, |
| 228 |
char *errmsg __attribute__ ((unused)), |
| 229 |
size_t errmsg_len __attribute__ ((unused))) |
| 230 |
{ |
| 231 |
caf_single_token_t single_token = TOKEN (*token); |
| 232 |
|
| 233 |
if (single_token->owning_memory && single_token->memptr) |
| 234 |
free (single_token->memptr); |
| 235 |
|
| 236 |
if (type != CAF_DEREGTYPE_COARRAY_DEALLOCATE_ONLY) |
| 237 |
{ |
| 238 |
free (TOKEN (*token)); |
| 239 |
*token = NULL; |
| 240 |
} |
| 241 |
else |
| 242 |
{ |
| 243 |
single_token->memptr = NULL; |
| 244 |
single_token->owning_memory = false; |
| 245 |
} |
| 246 |
|
| 247 |
if (stat) |
| 248 |
*stat = 0; |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
void |
| 253 |
_gfortran_caf_sync_all (int *stat, |
| 254 |
char *errmsg __attribute__ ((unused)), |
| 255 |
size_t errmsg_len __attribute__ ((unused))) |
| 256 |
{ |
| 257 |
__asm__ __volatile__ ("":::"memory"); |
| 258 |
if (stat) |
| 259 |
*stat = 0; |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
void |
| 264 |
_gfortran_caf_sync_memory (int *stat, |
| 265 |
char *errmsg __attribute__ ((unused)), |
| 266 |
size_t errmsg_len __attribute__ ((unused))) |
| 267 |
{ |
| 268 |
__asm__ __volatile__ ("":::"memory"); |
| 269 |
if (stat) |
| 270 |
*stat = 0; |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
void |
| 275 |
_gfortran_caf_sync_images (int count __attribute__ ((unused)), |
| 276 |
int images[] __attribute__ ((unused)), |
| 277 |
int *stat, |
| 278 |
char *errmsg __attribute__ ((unused)), |
| 279 |
size_t errmsg_len __attribute__ ((unused))) |
| 280 |
{ |
| 281 |
#ifdef GFC_CAF_CHECK |
| 282 |
int i; |
| 283 |
|
| 284 |
for (i = 0; i < count; i++) |
| 285 |
if (images[i] != 1) |
| 286 |
{ |
| 287 |
fprintf (stderr, "COARRAY ERROR: Invalid image index %d to SYNC " |
| 288 |
"IMAGES", images[i]); |
| 289 |
exit (EXIT_FAILURE); |
| 290 |
} |
| 291 |
#endif |
| 292 |
|
| 293 |
__asm__ __volatile__ ("":::"memory"); |
| 294 |
if (stat) |
| 295 |
*stat = 0; |
| 296 |
} |
| 297 |
|
| 298 |
extern void _gfortran_report_exception (void); |
| 299 |
|
| 300 |
void |
| 301 |
_gfortran_caf_stop_numeric(int stop_code, bool quiet) |
| 302 |
{ |
| 303 |
if (!quiet) |
| 304 |
{ |
| 305 |
_gfortran_report_exception (); |
| 306 |
fprintf (stderr, "STOP %d\n", stop_code); |
| 307 |
} |
| 308 |
exit (stop_code); |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
void |
| 313 |
_gfortran_caf_stop_str(const char *string, size_t len, bool quiet) |
| 314 |
{ |
| 315 |
if (!quiet) |
| 316 |
{ |
| 317 |
_gfortran_report_exception (); |
| 318 |
fputs ("STOP ", stderr); |
| 319 |
while (len--) |
| 320 |
fputc (*(string++), stderr); |
| 321 |
fputs ("\n", stderr); |
| 322 |
} |
| 323 |
exit (0); |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
void |
| 328 |
_gfortran_caf_error_stop_str (const char *string, size_t len, bool quiet) |
| 329 |
{ |
| 330 |
if (!quiet) |
| 331 |
{ |
| 332 |
_gfortran_report_exception (); |
| 333 |
fputs ("ERROR STOP ", stderr); |
| 334 |
while (len--) |
| 335 |
fputc (*(string++), stderr); |
| 336 |
fputs ("\n", stderr); |
| 337 |
} |
| 338 |
exit (1); |
| 339 |
} |
| 340 |
|
| 341 |
|
| 342 |
/* Reported that the program terminated because of a fail image issued. |
| 343 |
Because this is a single image library, nothing else than aborting the whole |
| 344 |
program can be done. */ |
| 345 |
|
| 346 |
void _gfortran_caf_fail_image (void) |
| 347 |
{ |
| 348 |
fputs ("IMAGE FAILED!\n", stderr); |
| 349 |
exit (0); |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
/* Get the status of image IMAGE. Because being the single image library all |
| 354 |
other images are reported to be stopped. */ |
| 355 |
|
| 356 |
int _gfortran_caf_image_status (int image, |
| 357 |
caf_team_t * team __attribute__ ((unused))) |
| 358 |
{ |
| 359 |
if (image == 1) |
| 360 |
return 0; |
| 361 |
else |
| 362 |
return CAF_STAT_STOPPED_IMAGE; |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
/* Single image library. There cannot be any failed images with only one |
| 367 |
image. */ |
| 368 |
|
| 369 |
void |
| 370 |
_gfortran_caf_failed_images (gfc_descriptor_t *array, |
| 371 |
caf_team_t * team __attribute__ ((unused)), |
| 372 |
int * kind) |
| 373 |
{ |
| 374 |
int local_kind = kind != NULL ? *kind : 4; |
| 375 |
|
| 376 |
array->base_addr = NULL; |
| 377 |
array->dtype.type = BT_INTEGER; |
| 378 |
array->dtype.elem_len = local_kind; |
| 379 |
/* Setting lower_bound higher then upper_bound is what the compiler does to |
| 380 |
indicate an empty array. */ |
| 381 |
array->dim[0].lower_bound = 0; |
| 382 |
array->dim[0]._ubound = -1; |
| 383 |
array->dim[0]._stride = 1; |
| 384 |
array->offset = 0; |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
/* With only one image available no other images can be stopped. Therefore |
| 389 |
return an empty array. */ |
| 390 |
|
| 391 |
void |
| 392 |
_gfortran_caf_stopped_images (gfc_descriptor_t *array, |
| 393 |
caf_team_t * team __attribute__ ((unused)), |
| 394 |
int * kind) |
| 395 |
{ |
| 396 |
int local_kind = kind != NULL ? *kind : 4; |
| 397 |
|
| 398 |
array->base_addr = NULL; |
| 399 |
array->dtype.type = BT_INTEGER; |
| 400 |
array->dtype.elem_len = local_kind; |
| 401 |
/* Setting lower_bound higher then upper_bound is what the compiler does to |
| 402 |
indicate an empty array. */ |
| 403 |
array->dim[0].lower_bound = 0; |
| 404 |
array->dim[0]._ubound = -1; |
| 405 |
array->dim[0]._stride = 1; |
| 406 |
array->offset = 0; |
| 407 |
} |
| 408 |
|
| 409 |
|
| 410 |
void |
| 411 |
_gfortran_caf_error_stop (int error, bool quiet) |
| 412 |
{ |
| 413 |
if (!quiet) |
| 414 |
{ |
| 415 |
_gfortran_report_exception (); |
| 416 |
fprintf (stderr, "ERROR STOP %d\n", error); |
| 417 |
} |
| 418 |
exit (error); |
| 419 |
} |
| 420 |
|
| 421 |
|
| 422 |
void |
| 423 |
_gfortran_caf_co_broadcast (gfc_descriptor_t *a __attribute__ ((unused)), |
| 424 |
int source_image __attribute__ ((unused)), |
| 425 |
int *stat, char *errmsg __attribute__ ((unused)), |
| 426 |
size_t errmsg_len __attribute__ ((unused))) |
| 427 |
{ |
| 428 |
if (stat) |
| 429 |
*stat = 0; |
| 430 |
} |
| 431 |
|
| 432 |
void |
| 433 |
_gfortran_caf_co_sum (gfc_descriptor_t *a __attribute__ ((unused)), |
| 434 |
int result_image __attribute__ ((unused)), |
| 435 |
int *stat, char *errmsg __attribute__ ((unused)), |
| 436 |
size_t errmsg_len __attribute__ ((unused))) |
| 437 |
{ |
| 438 |
if (stat) |
| 439 |
*stat = 0; |
| 440 |
} |
| 441 |
|
| 442 |
void |
| 443 |
_gfortran_caf_co_min (gfc_descriptor_t *a __attribute__ ((unused)), |
| 444 |
int result_image __attribute__ ((unused)), |
| 445 |
int *stat, char *errmsg __attribute__ ((unused)), |
| 446 |
int a_len __attribute__ ((unused)), |
| 447 |
size_t errmsg_len __attribute__ ((unused))) |
| 448 |
{ |
| 449 |
if (stat) |
| 450 |
*stat = 0; |
| 451 |
} |
| 452 |
|
| 453 |
void |
| 454 |
_gfortran_caf_co_max (gfc_descriptor_t *a __attribute__ ((unused)), |
| 455 |
int result_image __attribute__ ((unused)), |
| 456 |
int *stat, char *errmsg __attribute__ ((unused)), |
| 457 |
int a_len __attribute__ ((unused)), |
| 458 |
size_t errmsg_len __attribute__ ((unused))) |
| 459 |
{ |
| 460 |
if (stat) |
| 461 |
*stat = 0; |
| 462 |
} |
| 463 |
|
| 464 |
|
| 465 |
void |
| 466 |
_gfortran_caf_co_reduce (gfc_descriptor_t *a __attribute__ ((unused)), |
| 467 |
void * (*opr) (void *, void *) |
| 468 |
__attribute__ ((unused)), |
| 469 |
int opr_flags __attribute__ ((unused)), |
| 470 |
int result_image __attribute__ ((unused)), |
| 471 |
int *stat, char *errmsg __attribute__ ((unused)), |
| 472 |
int a_len __attribute__ ((unused)), |
| 473 |
size_t errmsg_len __attribute__ ((unused))) |
| 474 |
{ |
| 475 |
if (stat) |
| 476 |
*stat = 0; |
| 477 |
} |
| 478 |
|
| 479 |
|
| 480 |
void |
| 481 |
_gfortran_caf_register_accessor (const int hash, getter_t accessor) |
| 482 |
{ |
| 483 |
if (accessor_hash_table_state == AHT_UNINITIALIZED) |
| 484 |
{ |
| 485 |
aht_cap = 16; |
| 486 |
accessor_hash_table = calloc (aht_cap, sizeof (struct accessor_hash_t)); |
| 487 |
accessor_hash_table_state = AHT_OPEN; |
| 488 |
} |
| 489 |
if (aht_size == aht_cap) |
| 490 |
{ |
| 491 |
aht_cap += 16; |
| 492 |
accessor_hash_table = realloc (accessor_hash_table, |
| 493 |
aht_cap * sizeof (struct accessor_hash_t)); |
| 494 |
} |
| 495 |
if (accessor_hash_table_state == AHT_PREPARED) |
| 496 |
{ |
| 497 |
accessor_hash_table_state = AHT_OPEN; |
| 498 |
} |
| 499 |
accessor_hash_table[aht_size].hash = hash; |
| 500 |
accessor_hash_table[aht_size].u.getter = accessor; |
| 501 |
++aht_size; |
| 502 |
} |
| 503 |
|
| 504 |
static int |
| 505 |
hash_compare (const struct accessor_hash_t *lhs, |
| 506 |
const struct accessor_hash_t *rhs) |
| 507 |
{ |
| 508 |
return lhs->hash < rhs->hash ? -1 : (lhs->hash > rhs->hash ? 1 : 0); |
| 509 |
} |
| 510 |
|
| 511 |
void |
| 512 |
_gfortran_caf_register_accessors_finish (void) |
| 513 |
{ |
| 514 |
if (accessor_hash_table_state == AHT_PREPARED |
| 515 |
|| accessor_hash_table_state == AHT_UNINITIALIZED) |
| 516 |
return; |
| 517 |
|
| 518 |
qsort (accessor_hash_table, aht_size, sizeof (struct accessor_hash_t), |
| 519 |
(int (*) (const void *, const void *)) hash_compare); |
| 520 |
accessor_hash_table_state = AHT_PREPARED; |
| 521 |
} |
| 522 |
|
| 523 |
int |
| 524 |
_gfortran_caf_get_remote_function_index (const int hash) |
| 525 |
{ |
| 526 |
if (accessor_hash_table_state != AHT_PREPARED) |
| 527 |
{ |
| 528 |
caf_runtime_error ("the accessor hash table is not prepared."); |
| 529 |
} |
| 530 |
|
| 531 |
struct accessor_hash_t cand; |
| 532 |
cand.hash = hash; |
| 533 |
struct accessor_hash_t *f |
| 534 |
= bsearch (&cand, accessor_hash_table, aht_size, |
| 535 |
sizeof (struct accessor_hash_t), |
| 536 |
(int (*) (const void *, const void *)) hash_compare); |
| 537 |
|
| 538 |
int index = f ? f - accessor_hash_table : -1; |
| 539 |
return index; |
| 540 |
} |
| 541 |
|
| 542 |
void |
| 543 |
_gfortran_caf_get_from_remote ( |
| 544 |
caf_token_t token, const gfc_descriptor_t *opt_src_desc, |
| 545 |
const size_t *opt_src_charlen, const int image_index, |
| 546 |
const size_t dst_size __attribute__ ((unused)), void **dst_data, |
| 547 |
size_t *opt_dst_charlen, gfc_descriptor_t *opt_dst_desc, |
| 548 |
const bool may_realloc_dst, const int getter_index, void *add_data, |
| 549 |
const size_t add_data_size __attribute__ ((unused)), int *stat, |
| 550 |
caf_team_t *team __attribute__ ((unused)), |
| 551 |
int *team_number __attribute__ ((unused))) |
| 552 |
{ |
| 553 |
caf_single_token_t single_token = TOKEN (token); |
| 554 |
void *src_ptr = opt_src_desc ? (void *) opt_src_desc : single_token->memptr; |
| 555 |
int32_t free_buffer; |
| 556 |
void *dst_ptr = opt_dst_desc ? (void *)opt_dst_desc : dst_data; |
| 557 |
void *old_dst_data_ptr = NULL; |
| 558 |
struct caf_single_token cb_token; |
| 559 |
cb_token.memptr = add_data; |
| 560 |
cb_token.desc = NULL; |
| 561 |
cb_token.owning_memory = false; |
| 562 |
|
| 563 |
if (stat) |
| 564 |
*stat = 0; |
| 565 |
|
| 566 |
if (opt_dst_desc && !may_realloc_dst) |
| 567 |
{ |
| 568 |
old_dst_data_ptr = opt_dst_desc->base_addr; |
| 569 |
opt_dst_desc->base_addr = NULL; |
| 570 |
} |
| 571 |
|
| 572 |
accessor_hash_table[getter_index].u.getter (add_data, &image_index, dst_ptr, |
| 573 |
&free_buffer, src_ptr, &cb_token, |
| 574 |
0, opt_dst_charlen, |
| 575 |
opt_src_charlen); |
| 576 |
if (opt_dst_desc && old_dst_data_ptr && !may_realloc_dst |
| 577 |
&& opt_dst_desc->base_addr != old_dst_data_ptr) |
| 578 |
{ |
| 579 |
size_t dsize = opt_dst_desc->span; |
| 580 |
for (int i = 0; i < GFC_DESCRIPTOR_RANK (opt_dst_desc); ++i) |
| 581 |
dsize *= GFC_DESCRIPTOR_EXTENT (opt_dst_desc, i); |
| 582 |
memcpy (old_dst_data_ptr, opt_dst_desc->base_addr, dsize); |
| 583 |
free (opt_dst_desc->base_addr); |
| 584 |
opt_dst_desc->base_addr = old_dst_data_ptr; |
| 585 |
} |
| 586 |
} |
| 587 |
|
| 588 |
int32_t |
| 589 |
_gfortran_caf_is_present_on_remote (caf_token_t token, const int image_index, |
| 590 |
const int present_index, void *add_data, |
| 591 |
const size_t add_data_size |
| 592 |
__attribute__ ((unused))) |
| 593 |
{ |
| 594 |
/* Unregistered tokens are always not present. */ |
| 595 |
if (!token) |
| 596 |
return 0; |
| 597 |
|
| 598 |
caf_single_token_t single_token = TOKEN (token); |
| 599 |
int32_t result; |
| 600 |
struct caf_single_token cb_token = {add_data, NULL, false}; |
| 601 |
|
| 602 |
|
| 603 |
accessor_hash_table[present_index].u.is_present (add_data, &image_index, |
| 604 |
&result, |
| 605 |
single_token->memptr, |
| 606 |
&cb_token, 0); |
| 607 |
|
| 608 |
return result; |
| 609 |
} |
| 610 |
|
| 611 |
void |
| 612 |
_gfortran_caf_send_to_remote ( |
| 613 |
caf_token_t token, gfc_descriptor_t *opt_dst_desc, |
| 614 |
const size_t *opt_dst_charlen, const int image_index, |
| 615 |
const size_t src_size __attribute__ ((unused)), const void *src_data, |
| 616 |
const size_t *opt_src_charlen, const gfc_descriptor_t *opt_src_desc, |
| 617 |
const int accessor_index, void *add_data, |
| 618 |
const size_t add_data_size __attribute__ ((unused)), int *stat, |
| 619 |
caf_team_t *team __attribute__ ((unused)), |
| 620 |
int *team_number __attribute__ ((unused))) |
| 621 |
{ |
| 622 |
caf_single_token_t single_token = TOKEN (token); |
| 623 |
void *dst_ptr = opt_dst_desc ? (void *) opt_dst_desc : single_token->memptr; |
| 624 |
const void *src_ptr = opt_src_desc ? (void *) opt_src_desc : src_data; |
| 625 |
struct caf_single_token cb_token; |
| 626 |
cb_token.memptr = add_data; |
| 627 |
cb_token.desc = NULL; |
| 628 |
cb_token.owning_memory = false; |
| 629 |
|
| 630 |
if (stat) |
| 631 |
*stat = 0; |
| 632 |
|
| 633 |
accessor_hash_table[accessor_index].u.receiver (add_data, &image_index, |
| 634 |
dst_ptr, src_ptr, &cb_token, |
| 635 |
0, opt_dst_charlen, |
| 636 |
opt_src_charlen); |
| 637 |
} |
| 638 |
|
| 639 |
void |
| 640 |
_gfortran_caf_transfer_between_remotes ( |
| 641 |
caf_token_t dst_token, gfc_descriptor_t *opt_dst_desc, |
| 642 |
size_t *opt_dst_charlen, const int dst_image_index, |
| 643 |
const int dst_access_index, void *dst_add_data, |
| 644 |
const size_t dst_add_data_size __attribute__ ((unused)), |
| 645 |
caf_token_t src_token, const gfc_descriptor_t *opt_src_desc, |
| 646 |
const size_t *opt_src_charlen, const int src_image_index, |
| 647 |
const int src_access_index, void *src_add_data, |
| 648 |
const size_t src_add_data_size __attribute__ ((unused)), |
| 649 |
const size_t src_size, const bool scalar_transfer, int *dst_stat, |
| 650 |
int *src_stat, caf_team_t *dst_team __attribute__ ((unused)), |
| 651 |
int *dst_team_number __attribute__ ((unused)), |
| 652 |
caf_team_t *src_team __attribute__ ((unused)), |
| 653 |
int *src_team_number __attribute__ ((unused))) |
| 654 |
{ |
| 655 |
caf_single_token_t src_single_token = TOKEN (src_token), |
| 656 |
dst_single_token = TOKEN (dst_token); |
| 657 |
void *src_ptr |
| 658 |
= opt_src_desc ? (void *) opt_src_desc : src_single_token->memptr; |
| 659 |
int32_t free_buffer; |
| 660 |
void *dst_ptr |
| 661 |
= opt_dst_desc ? (void *) opt_dst_desc : dst_single_token->memptr; |
| 662 |
void *transfer_ptr, *buffer; |
| 663 |
GFC_FULL_ARRAY_DESCRIPTOR (GFC_MAX_DIMENSIONS, void) *transfer_desc = NULL; |
| 664 |
struct caf_single_token cb_token; |
| 665 |
cb_token.memptr = src_add_data; |
| 666 |
cb_token.desc = NULL; |
| 667 |
cb_token.owning_memory = false; |
| 668 |
|
| 669 |
if (src_stat) |
| 670 |
*src_stat = 0; |
| 671 |
|
| 672 |
if (!scalar_transfer) |
| 673 |
{ |
| 674 |
const size_t desc_size = sizeof (*transfer_desc); |
| 675 |
transfer_desc = __builtin_alloca (desc_size); |
| 676 |
memset (transfer_desc, 0, desc_size); |
| 677 |
transfer_ptr = transfer_desc; |
| 678 |
} |
| 679 |
else if (opt_dst_charlen) |
| 680 |
transfer_ptr = __builtin_alloca (*opt_dst_charlen * src_size); |
| 681 |
else |
| 682 |
{ |
| 683 |
buffer = NULL; |
| 684 |
transfer_ptr = &buffer; |
| 685 |
} |
| 686 |
|
| 687 |
accessor_hash_table[src_access_index].u.getter ( |
| 688 |
src_add_data, &src_image_index, transfer_ptr, &free_buffer, src_ptr, |
| 689 |
&cb_token, 0, opt_dst_charlen, opt_src_charlen); |
| 690 |
|
| 691 |
if (dst_stat) |
| 692 |
*dst_stat = 0; |
| 693 |
|
| 694 |
if (scalar_transfer) |
| 695 |
transfer_ptr = *(void **) transfer_ptr; |
| 696 |
|
| 697 |
cb_token.memptr = dst_add_data; |
| 698 |
accessor_hash_table[dst_access_index].u.receiver (dst_add_data, |
| 699 |
&dst_image_index, dst_ptr, |
| 700 |
transfer_ptr, &cb_token, 0, |
| 701 |
opt_dst_charlen, |
| 702 |
opt_src_charlen); |
| 703 |
|
| 704 |
if (free_buffer) |
| 705 |
free (transfer_desc ? transfer_desc->base_addr : transfer_ptr); |
| 706 |
} |
| 707 |
|
| 708 |
void |
| 709 |
_gfortran_caf_atomic_define (caf_token_t token, size_t offset, |
| 710 |
int image_index __attribute__ ((unused)), |
| 711 |
void *value, int *stat, |
| 712 |
int type __attribute__ ((unused)), int kind) |
| 713 |
{ |
| 714 |
assert(kind == 4); |
| 715 |
|
| 716 |
uint32_t *atom = (uint32_t *) ((char *) MEMTOK (token) + offset); |
| 717 |
|
| 718 |
__atomic_store (atom, (uint32_t *) value, __ATOMIC_RELAXED); |
| 719 |
|
| 720 |
if (stat) |
| 721 |
*stat = 0; |
| 722 |
} |
| 723 |
|
| 724 |
void |
| 725 |
_gfortran_caf_atomic_ref (caf_token_t token, size_t offset, |
| 726 |
int image_index __attribute__ ((unused)), |
| 727 |
void *value, int *stat, |
| 728 |
int type __attribute__ ((unused)), int kind) |
| 729 |
{ |
| 730 |
assert(kind == 4); |
| 731 |
|
| 732 |
uint32_t *atom = (uint32_t *) ((char *) MEMTOK (token) + offset); |
| 733 |
|
| 734 |
__atomic_load (atom, (uint32_t *) value, __ATOMIC_RELAXED); |
| 735 |
|
| 736 |
if (stat) |
| 737 |
*stat = 0; |
| 738 |
} |
| 739 |
|
| 740 |
|
| 741 |
void |
| 742 |
_gfortran_caf_atomic_cas (caf_token_t token, size_t offset, |
| 743 |
int image_index __attribute__ ((unused)), |
| 744 |
void *old, void *compare, void *new_val, int *stat, |
| 745 |
int type __attribute__ ((unused)), int kind) |
| 746 |
{ |
| 747 |
assert(kind == 4); |
| 748 |
|
| 749 |
uint32_t *atom = (uint32_t *) ((char *) MEMTOK (token) + offset); |
| 750 |
|
| 751 |
*(uint32_t *) old = *(uint32_t *) compare; |
| 752 |
(void) __atomic_compare_exchange_n (atom, (uint32_t *) old, |
| 753 |
*(uint32_t *) new_val, false, |
| 754 |
__ATOMIC_RELAXED, __ATOMIC_RELAXED); |
| 755 |
if (stat) |
| 756 |
*stat = 0; |
| 757 |
} |
| 758 |
|
| 759 |
|
| 760 |
void |
| 761 |
_gfortran_caf_atomic_op (int op, caf_token_t token, size_t offset, |
| 762 |
int image_index __attribute__ ((unused)), |
| 763 |
void *value, void *old, int *stat, |
| 764 |
int type __attribute__ ((unused)), int kind) |
| 765 |
{ |
| 766 |
assert(kind == 4); |
| 767 |
|
| 768 |
uint32_t res; |
| 769 |
uint32_t *atom = (uint32_t *) ((char *) MEMTOK (token) + offset); |
| 770 |
|
| 771 |
switch (op) |
| 772 |
{ |
| 773 |
case GFC_CAF_ATOMIC_ADD: |
| 774 |
res = __atomic_fetch_add (atom, *(uint32_t *) value, __ATOMIC_RELAXED); |
| 775 |
break; |
| 776 |
case GFC_CAF_ATOMIC_AND: |
| 777 |
res = __atomic_fetch_and (atom, *(uint32_t *) value, __ATOMIC_RELAXED); |
| 778 |
break; |
| 779 |
case GFC_CAF_ATOMIC_OR: |
| 780 |
res = __atomic_fetch_or (atom, *(uint32_t *) value, __ATOMIC_RELAXED); |
| 781 |
break; |
| 782 |
case GFC_CAF_ATOMIC_XOR: |
| 783 |
res = __atomic_fetch_xor (atom, *(uint32_t *) value, __ATOMIC_RELAXED); |
| 784 |
break; |
| 785 |
default: |
| 786 |
__builtin_unreachable(); |
| 787 |
} |
| 788 |
|
| 789 |
if (old) |
| 790 |
*(uint32_t *) old = res; |
| 791 |
|
| 792 |
if (stat) |
| 793 |
*stat = 0; |
| 794 |
} |
| 795 |
|
| 796 |
void |
| 797 |
_gfortran_caf_event_post (caf_token_t token, size_t index, |
| 798 |
int image_index __attribute__ ((unused)), |
| 799 |
int *stat, char *errmsg __attribute__ ((unused)), |
| 800 |
size_t errmsg_len __attribute__ ((unused))) |
| 801 |
{ |
| 802 |
uint32_t value = 1; |
| 803 |
uint32_t *event = (uint32_t *) ((char *) MEMTOK (token) + index |
| 804 |
* sizeof (uint32_t)); |
| 805 |
__atomic_fetch_add (event, (uint32_t) value, __ATOMIC_RELAXED); |
| 806 |
|
| 807 |
if(stat) |
| 808 |
*stat = 0; |
| 809 |
} |
| 810 |
|
| 811 |
void |
| 812 |
_gfortran_caf_event_wait (caf_token_t token, size_t index, |
| 813 |
int until_count, int *stat, |
| 814 |
char *errmsg __attribute__ ((unused)), |
| 815 |
size_t errmsg_len __attribute__ ((unused))) |
| 816 |
{ |
| 817 |
uint32_t *event = (uint32_t *) ((char *) MEMTOK (token) + index |
| 818 |
* sizeof (uint32_t)); |
| 819 |
uint32_t value = (uint32_t)-until_count; |
| 820 |
__atomic_fetch_add (event, (uint32_t) value, __ATOMIC_RELAXED); |
| 821 |
|
| 822 |
if(stat) |
| 823 |
*stat = 0; |
| 824 |
} |
| 825 |
|
| 826 |
void |
| 827 |
_gfortran_caf_event_query (caf_token_t token, size_t index, |
| 828 |
int image_index __attribute__ ((unused)), |
| 829 |
int *count, int *stat) |
| 830 |
{ |
| 831 |
uint32_t *event = (uint32_t *) ((char *) MEMTOK (token) + index |
| 832 |
* sizeof (uint32_t)); |
| 833 |
__atomic_load (event, (uint32_t *) count, __ATOMIC_RELAXED); |
| 834 |
|
| 835 |
if(stat) |
| 836 |
*stat = 0; |
| 837 |
} |
| 838 |
|
| 839 |
void |
| 840 |
_gfortran_caf_lock (caf_token_t token, size_t index, |
| 841 |
int image_index __attribute__ ((unused)), |
| 842 |
int *acquired_lock, int *stat, char *errmsg, |
| 843 |
size_t errmsg_len) |
| 844 |
{ |
| 845 |
const char *msg = "Already locked"; |
| 846 |
bool *lock = &((bool *) MEMTOK (token))[index]; |
| 847 |
|
| 848 |
if (!*lock) |
| 849 |
{ |
| 850 |
*lock = true; |
| 851 |
if (acquired_lock) |
| 852 |
*acquired_lock = (int) true; |
| 853 |
if (stat) |
| 854 |
*stat = 0; |
| 855 |
return; |
| 856 |
} |
| 857 |
|
| 858 |
if (acquired_lock) |
| 859 |
{ |
| 860 |
*acquired_lock = (int) false; |
| 861 |
if (stat) |
| 862 |
*stat = 0; |
| 863 |
return; |
| 864 |
} |
| 865 |
|
| 866 |
|
| 867 |
if (stat) |
| 868 |
{ |
| 869 |
*stat = 1; |
| 870 |
if (errmsg_len > 0) |
| 871 |
{ |
| 872 |
size_t len = (sizeof (msg) > errmsg_len) ? errmsg_len |
| 873 |
: sizeof (msg); |
| 874 |
memcpy (errmsg, msg, len); |
| 875 |
if (errmsg_len > len) |
| 876 |
memset (&errmsg[len], ' ', errmsg_len-len); |
| 877 |
} |
| 878 |
return; |
| 879 |
} |
| 880 |
_gfortran_caf_error_stop_str (msg, strlen (msg), false); |
| 881 |
} |
| 882 |
|
| 883 |
|
| 884 |
void |
| 885 |
_gfortran_caf_unlock (caf_token_t token, size_t index, |
| 886 |
int image_index __attribute__ ((unused)), |
| 887 |
int *stat, char *errmsg, size_t errmsg_len) |
| 888 |
{ |
| 889 |
const char *msg = "Variable is not locked"; |
| 890 |
bool *lock = &((bool *) MEMTOK (token))[index]; |
| 891 |
|
| 892 |
if (*lock) |
| 893 |
{ |
| 894 |
*lock = false; |
| 895 |
if (stat) |
| 896 |
*stat = 0; |
| 897 |
return; |
| 898 |
} |
| 899 |
|
| 900 |
if (stat) |
| 901 |
{ |
| 902 |
*stat = 1; |
| 903 |
if (errmsg_len > 0) |
| 904 |
{ |
| 905 |
size_t len = (sizeof (msg) > errmsg_len) ? errmsg_len |
| 906 |
: sizeof (msg); |
| 907 |
memcpy (errmsg, msg, len); |
| 908 |
if (errmsg_len > len) |
| 909 |
memset (&errmsg[len], ' ', errmsg_len-len); |
| 910 |
} |
| 911 |
return; |
| 912 |
} |
| 913 |
_gfortran_caf_error_stop_str (msg, strlen (msg), false); |
| 914 |
} |
| 915 |
|
| 916 |
|
| 917 |
/* Reference the libraries implementation. */ |
| 918 |
extern void _gfortran_random_init (int32_t, int32_t, int32_t); |
| 919 |
|
| 920 |
void _gfortran_caf_random_init (bool repeatable, bool image_distinct) |
| 921 |
{ |
| 922 |
/* In a single image implementation always forward to the gfortran |
| 923 |
routine. */ |
| 924 |
_gfortran_random_init (repeatable, image_distinct, 1); |
| 925 |
} |