1 /* MPI implementation of GNU Fortran Coarray Library
3 Free Software Foundation, Inc.
4 Contributed by Tobias Burnus <burnus@net-b.de>
6 This file is part of the GNU Fortran Coarray Runtime Library (libcaf).
8 Libcaf is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 Libcaf is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 <http://www.gnu.org/licenses/>. */
30 #include <string.h> /* For memcpy. */
31 #include <stdarg.h> /* For variadic arguments. */
35 /* Define GFC_CAF_CHECK to enable run-time checking. */
36 /* #define GFC_CAF_CHECK 1 */
39 static void error_stop (int error) __attribute__ ((noreturn));
41 /* Global variables. */
42 static int caf_mpi_initialized;
43 static int caf_this_image;
44 static int caf_num_images;
45 static int caf_is_finalized;
47 caf_static_t *caf_static_list = NULL;
50 /* Keep in sync with single.c. */
52 caf_runtime_error (const char *message, ...)
55 fprintf (stderr, "Fortran runtime error on image %d: ", caf_this_image);
56 va_start (ap, message);
57 vfprintf (stderr, message, ap);
59 fprintf (stderr, "\n");
61 /* FIXME: Shutdown the Fortran RTL to flush the buffer. PR 43849. */
62 /* FIXME: Do some more effort than just MPI_ABORT. */
63 MPI_Abort (MPI_COMM_WORLD, EXIT_FAILURE);
65 /* Should be unreachable, but to make sure also call exit. */
70 /* Initialize coarray program. This routine assumes that no other
71 MPI initialization happened before; otherwise MPI_Initialized
72 had to be used. As the MPI library might modify the command-line
73 arguments, the routine should be called before the run-time
74 libaray is initialized. */
77 _gfortran_caf_init (int *argc, char ***argv, int *this_image, int *num_images)
79 if (caf_num_images == 0)
81 /* caf_mpi_initialized is only true if the main program is
82 not written in Fortran. */
83 MPI_Initialized (&caf_mpi_initialized);
84 if (!caf_mpi_initialized)
85 MPI_Init (argc, argv);
87 MPI_Comm_size (MPI_COMM_WORLD, &caf_num_images);
88 MPI_Comm_rank (MPI_COMM_WORLD, &caf_this_image);
93 *this_image = caf_this_image;
95 *num_images = caf_num_images;
99 /* Finalize coarray program. */
102 _gfortran_caf_finalize (void)
104 while (caf_static_list != NULL)
106 free(caf_static_list->token[caf_this_image-1]);
107 caf_static_list = caf_static_list->prev;
110 if (!caf_mpi_initialized)
113 caf_is_finalized = 1;
118 _gfortran_caf_register (ptrdiff_t size, caf_register_t type, void **token,
119 int *stat, char *errmsg, int errmsg_len)
124 if (unlikely (caf_is_finalized))
127 /* Start MPI if not already started. */
128 if (caf_num_images == 0)
129 _gfortran_caf_init (NULL, NULL, NULL, NULL);
131 /* Token contains only a list of pointers. */
132 local = malloc (size);
133 token = malloc (sizeof (void*) * caf_num_images);
135 if (unlikely (local == NULL || token == NULL))
138 /* token[img-1] is the address of the token in image "img". */
139 err = MPI_Allgather (&local, sizeof (void*), MPI_BYTE, token,
140 sizeof (void*), MPI_BYTE, MPI_COMM_WORLD);
148 if (type == CAF_REGTYPE_COARRAY_STATIC)
150 caf_static_t *tmp = malloc (sizeof (caf_static_t));
151 tmp->prev = caf_static_list;
153 caf_static_list = tmp;
165 if (caf_is_finalized)
166 msg = "Failed to allocate coarray - there are stopped images";
168 msg = "Failed to allocate coarray";
172 *stat = caf_is_finalized ? STAT_STOPPED_IMAGE : 1;
175 int len = ((int) strlen (msg) > errmsg_len) ? errmsg_len
176 : (int) strlen (msg);
177 memcpy (errmsg, msg, len);
178 if (errmsg_len > len)
179 memset (&errmsg[len], ' ', errmsg_len-len);
183 caf_runtime_error (msg);
191 _gfortran_caf_deregister (void **token __attribute__ ((unused)))
198 _gfortran_caf_sync_all (int *stat, char *errmsg, int errmsg_len)
202 if (unlikely (caf_is_finalized))
203 ierr = STAT_STOPPED_IMAGE;
205 ierr = MPI_Barrier (MPI_COMM_WORLD);
213 if (caf_is_finalized)
214 msg = "SYNC ALL failed - there are stopped images";
216 msg = "SYNC ALL failed";
220 int len = ((int) strlen (msg) > errmsg_len) ? errmsg_len
221 : (int) strlen (msg);
222 memcpy (errmsg, msg, len);
223 if (errmsg_len > len)
224 memset (&errmsg[len], ' ', errmsg_len-len);
227 caf_runtime_error (msg);
232 /* SYNC IMAGES. Note: SYNC IMAGES(*) is passed as count == -1 while
233 SYNC IMAGES([]) has count == 0. Note further that SYNC IMAGES(*)
234 is not equivalent to SYNC ALL. */
236 _gfortran_caf_sync_images (int count, int images[], int *stat, char *errmsg,
240 if (count == 0 || (count == 1 && images[0] == caf_this_image))
251 for (i = 0; i < count; i++)
252 if (images[i] < 1 || images[i] > caf_num_images)
254 fprintf (stderr, "COARRAY ERROR: Invalid image index %d to SYNC "
255 "IMAGES", images[i]);
261 /* FIXME: SYNC IMAGES with a nontrivial argument cannot easily be
262 mapped to MPI communicators. Thus, exist early with an error message. */
265 fprintf (stderr, "COARRAY ERROR: SYNC IMAGES not yet implemented");
269 /* Handle SYNC IMAGES(*). */
270 if (unlikely(caf_is_finalized))
271 ierr = STAT_STOPPED_IMAGE;
273 ierr = MPI_Barrier (MPI_COMM_WORLD);
281 if (caf_is_finalized)
282 msg = "SYNC IMAGES failed - there are stopped images";
284 msg = "SYNC IMAGES failed";
288 int len = ((int) strlen (msg) > errmsg_len) ? errmsg_len
289 : (int) strlen (msg);
290 memcpy (errmsg, msg, len);
291 if (errmsg_len > len)
292 memset (&errmsg[len], ' ', errmsg_len-len);
295 caf_runtime_error (msg);
300 /* ERROR STOP the other images. */
303 error_stop (int error)
305 /* FIXME: Shutdown the Fortran RTL to flush the buffer. PR 43849. */
306 /* FIXME: Do some more effort than just MPI_ABORT. */
307 MPI_Abort (MPI_COMM_WORLD, error);
309 /* Should be unreachable, but to make sure also call exit. */
314 /* ERROR STOP function for string arguments. */
317 _gfortran_caf_error_stop_str (const char *string, int32_t len)
319 fputs ("ERROR STOP ", stderr);
321 fputc (*(string++), stderr);
322 fputs ("\n", stderr);
328 /* ERROR STOP function for numerical arguments. */
331 _gfortran_caf_error_stop (int32_t error)
333 fprintf (stderr, "ERROR STOP %d\n", error);