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)))
95 /* SYNC ALL - the return value matches Fortran's STAT argument. */
98 _gfortran_caf_sync_all (char *errmsg, int errmsg_len)
101 ierr = MPI_Barrier (MPI_COMM_WORLD);
103 if (ierr && errmsg_len > 0)
105 const char msg[] = "SYNC ALL failed";
106 int len = ((int) sizeof (msg) > errmsg_len) ? errmsg_len
107 : (int) sizeof (msg);
108 memcpy (errmsg, msg, len);
109 if (errmsg_len > len)
110 memset (&errmsg[len], ' ', errmsg_len-len);
113 /* TODO: Is ierr correct? When should STAT_STOPPED_IMAGE be used? */
118 /* SYNC IMAGES. Note: SYNC IMAGES(*) is passed as count == -1 while
119 SYNC IMAGES([]) has count == 0. Note further that SYNC IMAGES(*)
120 is not equivalent to SYNC ALL. The return value matches Fortran's
123 _gfortran_caf_sync_images (int count, int images[], char *errmsg,
128 if (count == 0 || (count == 1 && images[0] == caf_this_image))
135 for (i = 0; i < count; i++)
136 if (images[i] < 1 || images[i] > caf_num_images)
138 fprintf (stderr, "COARRAY ERROR: Invalid image index %d to SYNC "
139 "IMAGES", images[i]);
145 /* FIXME: SYNC IMAGES with a nontrivial argument cannot easily be
146 mapped to MPI communicators. Thus, exist early with an error message. */
149 fprintf (stderr, "COARRAY ERROR: SYNC IMAGES not yet implemented");
153 /* Handle SYNC IMAGES(*). */
154 ierr = MPI_Barrier (MPI_COMM_WORLD);
156 if (ierr && errmsg_len > 0)
158 const char msg[] = "SYNC IMAGES failed";
159 int len = ((int) sizeof (msg) > errmsg_len) ? errmsg_len
160 : (int) sizeof (msg);
161 memcpy (errmsg, msg, len);
162 if (errmsg_len > len)
163 memset (&errmsg[len], ' ', errmsg_len-len);
166 /* TODO: Is ierr correct? When should STAT_STOPPED_IMAGE be used? */
171 /* ERROR STOP the other images. */
174 error_stop (int error)
176 /* FIXME: Shutdown the Fortran RTL to flush the buffer. PR 43849. */
177 /* FIXME: Do some more effort than just MPI_ABORT. */
178 MPI_Abort (MPI_COMM_WORLD, error);
180 /* Should be unreachable, but to make sure also call exit. */
185 /* ERROR STOP function for string arguments. */
188 _gfortran_caf_error_stop_str (const char *string, int32_t len)
190 fputs ("ERROR STOP ", stderr);
192 fputc (*(string++), stderr);
193 fputs ("\n", stderr);
199 /* ERROR STOP function for numerical arguments. */
202 _gfortran_caf_error_stop (int32_t error)
204 fprintf (stderr, "ERROR STOP %d\n", error);