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. */
34 /* Define GFC_CAF_CHECK to enable run-time checking. */
35 /* #define GFC_CAF_CHECK 1 */
38 static void error_stop (int error) __attribute__ ((noreturn));
40 /* Global variables. */
41 static int caf_mpi_initialized;
42 static int caf_this_image;
43 static int caf_num_images;
46 /* Initialize coarray program. This routine assumes that no other
47 MPI initialization happened before; otherwise MPI_Initialized
48 had to be used. As the MPI library might modify the command-line
49 arguments, the routine should be called before the run-time
50 libaray is initialized. */
53 _gfortran_caf_init (int *argc, char ***argv, int *this_image, int *num_images)
55 /* caf_mpi_initialized is only true if the main program is not written in
57 MPI_Initialized (&caf_mpi_initialized);
58 if (!caf_mpi_initialized)
59 MPI_Init (argc, argv);
61 MPI_Comm_rank (MPI_COMM_WORLD, &caf_this_image);
62 *this_image = ++caf_this_image;
63 MPI_Comm_size (MPI_COMM_WORLD, &caf_num_images);
64 *num_images = caf_num_images;
68 /* Finalize coarray program. */
71 _gfortran_caf_finalize (void)
73 if (!caf_mpi_initialized)
79 _gfortran_caf_register (ptrdiff_t size,
80 caf_register_t type __attribute__ ((unused)),
89 _gfortran_caf_deregister (void **token __attribute__ ((unused)))
96 _gfortran_caf_sync_all (int *stat, char *errmsg, int errmsg_len)
98 /* TODO: Is ierr correct? When should STAT_STOPPED_IMAGE be used? */
99 int ierr = MPI_Barrier (MPI_COMM_WORLD);
106 const char msg[] = "SYNC ALL failed";
109 int len = ((int) sizeof (msg) > errmsg_len) ? errmsg_len
110 : (int) sizeof (msg);
111 memcpy (errmsg, msg, len);
112 if (errmsg_len > len)
113 memset (&errmsg[len], ' ', errmsg_len-len);
117 fprintf (stderr, "SYNC ALL failed\n");
124 /* SYNC IMAGES. Note: SYNC IMAGES(*) is passed as count == -1 while
125 SYNC IMAGES([]) has count == 0. Note further that SYNC IMAGES(*)
126 is not equivalent to SYNC ALL. */
128 _gfortran_caf_sync_images (int count, int images[], int *stat, char *errmsg,
132 if (count == 0 || (count == 1 && images[0] == caf_this_image))
143 for (i = 0; i < count; i++)
144 if (images[i] < 1 || images[i] > caf_num_images)
146 fprintf (stderr, "COARRAY ERROR: Invalid image index %d to SYNC "
147 "IMAGES", images[i]);
153 /* FIXME: SYNC IMAGES with a nontrivial argument cannot easily be
154 mapped to MPI communicators. Thus, exist early with an error message. */
157 fprintf (stderr, "COARRAY ERROR: SYNC IMAGES not yet implemented");
161 /* Handle SYNC IMAGES(*). */
162 /* TODO: Is ierr correct? When should STAT_STOPPED_IMAGE be used? */
163 ierr = MPI_Barrier (MPI_COMM_WORLD);
169 const char msg[] = "SYNC IMAGES failed";
172 int len = ((int) sizeof (msg) > errmsg_len) ? errmsg_len
173 : (int) sizeof (msg);
174 memcpy (errmsg, msg, len);
175 if (errmsg_len > len)
176 memset (&errmsg[len], ' ', errmsg_len-len);
180 fprintf (stderr, "SYNC IMAGES failed\n");
187 /* ERROR STOP the other images. */
190 error_stop (int error)
192 /* FIXME: Shutdown the Fortran RTL to flush the buffer. PR 43849. */
193 /* FIXME: Do some more effort than just MPI_ABORT. */
194 MPI_Abort (MPI_COMM_WORLD, error);
196 /* Should be unreachable, but to make sure also call exit. */
201 /* ERROR STOP function for string arguments. */
204 _gfortran_caf_error_stop_str (const char *string, int32_t len)
206 fputs ("ERROR STOP ", stderr);
208 fputc (*(string++), stderr);
209 fputs ("\n", stderr);
215 /* ERROR STOP function for numerical arguments. */
218 _gfortran_caf_error_stop (int32_t error)
220 fprintf (stderr, "ERROR STOP %d\n", error);