1 /* MPI implementation of GNU Fortran Coarray Library
2 Copyright (C) 2011-2023 Free Software Foundation, Inc.
3 Contributed by Tobias Burnus <burnus@net-b.de>
5 This file is part of the GNU Fortran Coarray Runtime Library (libcaf).
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)
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.
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.
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/>. */
29 #include <string.h> /* For memcpy. */
30 #include <stdarg.h> /* For variadic arguments. */
34 /* Define GFC_CAF_CHECK to enable run-time checking. */
35 /* #define GFC_CAF_CHECK 1 */
37 typedef void ** mpi_token_t
;
38 #define TOKEN(X) ((mpi_token_t) (X))
40 static void error_stop (int error
) __attribute__ ((noreturn
));
42 /* Global variables. */
43 static int caf_mpi_initialized
;
44 static int caf_this_image
;
45 static int caf_num_images
;
46 static int caf_is_finalized
;
48 caf_static_t
*caf_static_list
= NULL
;
51 /* Keep in sync with single.c. */
53 caf_runtime_error (const char *message
, ...)
56 fprintf (stderr
, "Fortran runtime error on image %d: ", caf_this_image
);
57 va_start (ap
, message
);
58 vfprintf (stderr
, message
, ap
);
60 fprintf (stderr
, "\n");
62 /* FIXME: Shutdown the Fortran RTL to flush the buffer. PR 43849. */
63 /* FIXME: Do some more effort than just MPI_ABORT. */
64 MPI_Abort (MPI_COMM_WORLD
, EXIT_FAILURE
);
66 /* Should be unreachable, but to make sure also call exit. */
71 /* Initialize coarray program. This routine assumes that no other
72 MPI initialization happened before; otherwise MPI_Initialized
73 had to be used. As the MPI library might modify the command-line
74 arguments, the routine should be called before the run-time
75 libaray is initialized. */
78 _gfortran_caf_init (int *argc
, char ***argv
)
80 if (caf_num_images
== 0)
82 /* caf_mpi_initialized is only true if the main program is
83 not written in Fortran. */
84 MPI_Initialized (&caf_mpi_initialized
);
85 if (!caf_mpi_initialized
)
86 MPI_Init (argc
, argv
);
88 MPI_Comm_size (MPI_COMM_WORLD
, &caf_num_images
);
89 MPI_Comm_rank (MPI_COMM_WORLD
, &caf_this_image
);
95 /* Finalize coarray program. */
98 _gfortran_caf_finalize (void)
100 while (caf_static_list
!= NULL
)
102 caf_static_t
*tmp
= caf_static_list
->prev
;
104 free (TOKEN (caf_static_list
->token
)[caf_this_image
-1]);
105 free (TOKEN (caf_static_list
->token
));
106 free (caf_static_list
);
107 caf_static_list
= tmp
;
110 if (!caf_mpi_initialized
)
113 caf_is_finalized
= 1;
118 _gfortran_caf_this_image (int distance
__attribute__ ((unused
)))
120 return caf_this_image
;
125 _gfortran_caf_num_images (int distance
__attribute__ ((unused
)),
126 int failed
__attribute__ ((unused
)))
128 return caf_num_images
;
133 _gfortran_caf_register (size_t size
, caf_register_t type
, caf_token_t
*token
,
134 int *stat
, char *errmsg
, size_t errmsg_len
,
135 int num_alloc_comps
__attribute__ ((unused
)))
140 if (unlikely (caf_is_finalized
))
143 /* Start MPI if not already started. */
144 if (caf_num_images
== 0)
145 _gfortran_caf_init (NULL
, NULL
);
147 /* Token contains only a list of pointers. */
148 local
= malloc (size
);
149 *token
= malloc (sizeof (mpi_token_t
) * caf_num_images
);
151 if (unlikely (local
== NULL
|| *token
== NULL
))
154 /* token[img-1] is the address of the token in image "img". */
155 err
= MPI_Allgather (&local
, sizeof (void*), MPI_BYTE
, TOKEN (*token
),
156 sizeof (void*), MPI_BYTE
, MPI_COMM_WORLD
);
165 if (type
== CAF_REGTYPE_COARRAY_STATIC
)
167 caf_static_t
*tmp
= malloc (sizeof (caf_static_t
));
168 tmp
->prev
= caf_static_list
;
170 caf_static_list
= tmp
;
182 if (caf_is_finalized
)
183 msg
= "Failed to allocate coarray - there are stopped images";
185 msg
= "Failed to allocate coarray";
189 *stat
= caf_is_finalized
? STAT_STOPPED_IMAGE
: 1;
192 size_t len
= (strlen (msg
) > errmsg_len
) ? errmsg_len
194 memcpy (errmsg
, msg
, len
);
195 if (errmsg_len
> len
)
196 memset (&errmsg
[len
], ' ', errmsg_len
-len
);
200 caf_runtime_error (msg
);
208 _gfortran_caf_deregister (caf_token_t
*token
, int *stat
, char *errmsg
, size_t errmsg_len
)
210 if (unlikely (caf_is_finalized
))
212 const char msg
[] = "Failed to deallocate coarray - "
213 "there are stopped images";
216 *stat
= STAT_STOPPED_IMAGE
;
220 size_t len
= (sizeof (msg
) - 1 > errmsg_len
)
221 ? errmsg_len
: sizeof (msg
) - 1;
222 memcpy (errmsg
, msg
, len
);
223 if (errmsg_len
> len
)
224 memset (&errmsg
[len
], ' ', errmsg_len
-len
);
228 caf_runtime_error (msg
);
231 _gfortran_caf_sync_all (NULL
, NULL
, 0);
236 free (TOKEN (*token
)[caf_this_image
-1]);
242 _gfortran_caf_sync_all (int *stat
, char *errmsg
, size_t errmsg_len
)
246 if (unlikely (caf_is_finalized
))
247 ierr
= STAT_STOPPED_IMAGE
;
249 ierr
= MPI_Barrier (MPI_COMM_WORLD
);
257 if (caf_is_finalized
)
258 msg
= "SYNC ALL failed - there are stopped images";
260 msg
= "SYNC ALL failed";
264 size_t len
= (strlen (msg
) > errmsg_len
) ? errmsg_len
266 memcpy (errmsg
, msg
, len
);
267 if (errmsg_len
> len
)
268 memset (&errmsg
[len
], ' ', errmsg_len
-len
);
271 caf_runtime_error (msg
);
276 /* SYNC IMAGES. Note: SYNC IMAGES(*) is passed as count == -1 while
277 SYNC IMAGES([]) has count == 0. Note further that SYNC IMAGES(*)
278 is not equivalent to SYNC ALL. */
280 _gfortran_caf_sync_images (int count
, int images
[], int *stat
, char *errmsg
,
284 if (count
== 0 || (count
== 1 && images
[0] == caf_this_image
))
295 for (i
= 0; i
< count
; i
++)
296 if (images
[i
] < 1 || images
[i
] > caf_num_images
)
298 fprintf (stderr
, "COARRAY ERROR: Invalid image index %d to SYNC "
299 "IMAGES", images
[i
]);
305 /* FIXME: SYNC IMAGES with a nontrivial argument cannot easily be
306 mapped to MPI communicators. Thus, exist early with an error message. */
309 fprintf (stderr
, "COARRAY ERROR: SYNC IMAGES not yet implemented");
313 /* Handle SYNC IMAGES(*). */
314 if (unlikely (caf_is_finalized
))
315 ierr
= STAT_STOPPED_IMAGE
;
317 ierr
= MPI_Barrier (MPI_COMM_WORLD
);
325 if (caf_is_finalized
)
326 msg
= "SYNC IMAGES failed - there are stopped images";
328 msg
= "SYNC IMAGES failed";
332 size_t len
= (strlen (msg
) > errmsg_len
) ? errmsg_len
334 memcpy (errmsg
, msg
, len
);
335 if (errmsg_len
> len
)
336 memset (&errmsg
[len
], ' ', errmsg_len
-len
);
339 caf_runtime_error (msg
);
344 /* ERROR STOP the other images. */
347 error_stop (int error
)
349 /* FIXME: Shutdown the Fortran RTL to flush the buffer. PR 43849. */
350 /* FIXME: Do some more effort than just MPI_ABORT. */
351 MPI_Abort (MPI_COMM_WORLD
, error
);
353 /* Should be unreachable, but to make sure also call exit. */
358 /* ERROR STOP function for string arguments. */
361 _gfortran_caf_error_stop_str (const char *string
, size_t len
, bool quiet
)
365 fputs ("ERROR STOP ", stderr
);
367 fputc (*(string
++), stderr
);
368 fputs ("\n", stderr
);
374 /* ERROR STOP function for numerical arguments. */
377 _gfortran_caf_error_stop (int error
, bool quiet
)
380 fprintf (stderr
, "ERROR STOP %d\n", error
);