1 /* MPI implementation of GNU Fortran Coarray Library
2 Copyright (C) 2011-2013 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 */
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
;
44 static int caf_is_finalized
;
46 caf_static_t
*caf_static_list
= NULL
;
49 /* Keep in sync with single.c. */
51 caf_runtime_error (const char *message
, ...)
54 fprintf (stderr
, "Fortran runtime error on image %d: ", caf_this_image
);
55 va_start (ap
, message
);
56 vfprintf (stderr
, message
, ap
);
58 fprintf (stderr
, "\n");
60 /* FIXME: Shutdown the Fortran RTL to flush the buffer. PR 43849. */
61 /* FIXME: Do some more effort than just MPI_ABORT. */
62 MPI_Abort (MPI_COMM_WORLD
, EXIT_FAILURE
);
64 /* Should be unreachable, but to make sure also call exit. */
69 /* Initialize coarray program. This routine assumes that no other
70 MPI initialization happened before; otherwise MPI_Initialized
71 had to be used. As the MPI library might modify the command-line
72 arguments, the routine should be called before the run-time
73 libaray is initialized. */
76 _gfortran_caf_init (int *argc
, char ***argv
, int *this_image
, int *num_images
)
78 if (caf_num_images
== 0)
80 /* caf_mpi_initialized is only true if the main program is
81 not written in Fortran. */
82 MPI_Initialized (&caf_mpi_initialized
);
83 if (!caf_mpi_initialized
)
84 MPI_Init (argc
, argv
);
86 MPI_Comm_size (MPI_COMM_WORLD
, &caf_num_images
);
87 MPI_Comm_rank (MPI_COMM_WORLD
, &caf_this_image
);
92 *this_image
= caf_this_image
;
94 *num_images
= caf_num_images
;
98 /* Finalize coarray program. */
101 _gfortran_caf_finalize (void)
103 while (caf_static_list
!= NULL
)
105 caf_static_t
*tmp
= caf_static_list
->prev
;
107 free (caf_static_list
->token
[caf_this_image
-1]);
108 free (caf_static_list
->token
);
109 free (caf_static_list
);
110 caf_static_list
= tmp
;
113 if (!caf_mpi_initialized
)
116 caf_is_finalized
= 1;
121 _gfortran_caf_register (ptrdiff_t size
, caf_register_t type
, void ***token
,
122 int *stat
, char *errmsg
, int errmsg_len
)
127 if (unlikely (caf_is_finalized
))
130 /* Start MPI if not already started. */
131 if (caf_num_images
== 0)
132 _gfortran_caf_init (NULL
, NULL
, NULL
, NULL
);
134 /* Token contains only a list of pointers. */
135 local
= malloc (size
);
136 *token
= malloc (sizeof (void*) * caf_num_images
);
138 if (unlikely (local
== NULL
|| *token
== NULL
))
141 /* token[img-1] is the address of the token in image "img". */
142 err
= MPI_Allgather (&local
, sizeof (void*), MPI_BYTE
, *token
,
143 sizeof (void*), MPI_BYTE
, MPI_COMM_WORLD
);
152 if (type
== CAF_REGTYPE_COARRAY_STATIC
)
154 caf_static_t
*tmp
= malloc (sizeof (caf_static_t
));
155 tmp
->prev
= caf_static_list
;
157 caf_static_list
= tmp
;
169 if (caf_is_finalized
)
170 msg
= "Failed to allocate coarray - there are stopped images";
172 msg
= "Failed to allocate coarray";
176 *stat
= caf_is_finalized
? STAT_STOPPED_IMAGE
: 1;
179 int len
= ((int) strlen (msg
) > errmsg_len
) ? errmsg_len
180 : (int) strlen (msg
);
181 memcpy (errmsg
, msg
, len
);
182 if (errmsg_len
> len
)
183 memset (&errmsg
[len
], ' ', errmsg_len
-len
);
187 caf_runtime_error (msg
);
195 _gfortran_caf_deregister (void ***token
, int *stat
, char *errmsg
, int errmsg_len
)
197 if (unlikely (caf_is_finalized
))
199 const char msg
[] = "Failed to deallocate coarray - "
200 "there are stopped images";
203 *stat
= STAT_STOPPED_IMAGE
;
207 int len
= ((int) sizeof (msg
) - 1 > errmsg_len
)
208 ? errmsg_len
: (int) sizeof (msg
) - 1;
209 memcpy (errmsg
, msg
, len
);
210 if (errmsg_len
> len
)
211 memset (&errmsg
[len
], ' ', errmsg_len
-len
);
215 caf_runtime_error (msg
);
218 _gfortran_caf_sync_all (NULL
, NULL
, 0);
223 free ((*token
)[caf_this_image
-1]);
229 _gfortran_caf_sync_all (int *stat
, char *errmsg
, int errmsg_len
)
233 if (unlikely (caf_is_finalized
))
234 ierr
= STAT_STOPPED_IMAGE
;
236 ierr
= MPI_Barrier (MPI_COMM_WORLD
);
244 if (caf_is_finalized
)
245 msg
= "SYNC ALL failed - there are stopped images";
247 msg
= "SYNC ALL failed";
251 int len
= ((int) strlen (msg
) > errmsg_len
) ? errmsg_len
252 : (int) strlen (msg
);
253 memcpy (errmsg
, msg
, len
);
254 if (errmsg_len
> len
)
255 memset (&errmsg
[len
], ' ', errmsg_len
-len
);
258 caf_runtime_error (msg
);
263 /* SYNC IMAGES. Note: SYNC IMAGES(*) is passed as count == -1 while
264 SYNC IMAGES([]) has count == 0. Note further that SYNC IMAGES(*)
265 is not equivalent to SYNC ALL. */
267 _gfortran_caf_sync_images (int count
, int images
[], int *stat
, char *errmsg
,
271 if (count
== 0 || (count
== 1 && images
[0] == caf_this_image
))
282 for (i
= 0; i
< count
; i
++)
283 if (images
[i
] < 1 || images
[i
] > caf_num_images
)
285 fprintf (stderr
, "COARRAY ERROR: Invalid image index %d to SYNC "
286 "IMAGES", images
[i
]);
292 /* FIXME: SYNC IMAGES with a nontrivial argument cannot easily be
293 mapped to MPI communicators. Thus, exist early with an error message. */
296 fprintf (stderr
, "COARRAY ERROR: SYNC IMAGES not yet implemented");
300 /* Handle SYNC IMAGES(*). */
301 if (unlikely (caf_is_finalized
))
302 ierr
= STAT_STOPPED_IMAGE
;
304 ierr
= MPI_Barrier (MPI_COMM_WORLD
);
312 if (caf_is_finalized
)
313 msg
= "SYNC IMAGES failed - there are stopped images";
315 msg
= "SYNC IMAGES failed";
319 int len
= ((int) strlen (msg
) > errmsg_len
) ? errmsg_len
320 : (int) strlen (msg
);
321 memcpy (errmsg
, msg
, len
);
322 if (errmsg_len
> len
)
323 memset (&errmsg
[len
], ' ', errmsg_len
-len
);
326 caf_runtime_error (msg
);
331 /* ERROR STOP the other images. */
334 error_stop (int error
)
336 /* FIXME: Shutdown the Fortran RTL to flush the buffer. PR 43849. */
337 /* FIXME: Do some more effort than just MPI_ABORT. */
338 MPI_Abort (MPI_COMM_WORLD
, error
);
340 /* Should be unreachable, but to make sure also call exit. */
345 /* ERROR STOP function for string arguments. */
348 _gfortran_caf_error_stop_str (const char *string
, int32_t len
)
350 fputs ("ERROR STOP ", stderr
);
352 fputc (*(string
++), stderr
);
353 fputs ("\n", stderr
);
359 /* ERROR STOP function for numerical arguments. */
362 _gfortran_caf_error_stop (int32_t error
)
364 fprintf (stderr
, "ERROR STOP %d\n", error
);