1 /* Single-image 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/>. */
27 #include <stdio.h> /* For fputs and fprintf. */
28 #include <stdlib.h> /* For exit and malloc. */
29 #include <string.h> /* For memcpy and memset. */
30 #include <stdarg.h> /* For variadic arguments. */
32 /* Define GFC_CAF_CHECK to enable run-time checking. */
33 /* #define GFC_CAF_CHECK 1 */
35 /* Single-image implementation of the CAF library.
36 Note: For performance reasons -fcoarry=single should be used
37 rather than this library. */
39 /* Global variables. */
40 caf_static_t
*caf_static_list
= NULL
;
43 /* Keep in sync with mpi.c. */
45 caf_runtime_error (const char *message
, ...)
48 fprintf (stderr
, "Fortran runtime error: ");
49 va_start (ap
, message
);
50 vfprintf (stderr
, message
, ap
);
52 fprintf (stderr
, "\n");
54 /* FIXME: Shutdown the Fortran RTL to flush the buffer. PR 43849. */
59 _gfortran_caf_init (int *argc
__attribute__ ((unused
)),
60 char ***argv
__attribute__ ((unused
)),
61 int *this_image
, int *num_images
)
69 _gfortran_caf_finalize (void)
71 while (caf_static_list
!= NULL
)
73 caf_static_t
*tmp
= caf_static_list
->prev
;
74 free (caf_static_list
->token
[0]);
75 free (caf_static_list
->token
);
76 free (caf_static_list
);
77 caf_static_list
= tmp
;
83 _gfortran_caf_register (ptrdiff_t size
, caf_register_t type
, void ***token
,
84 int *stat
, char *errmsg
, int errmsg_len
)
88 local
= malloc (size
);
89 *token
= malloc (sizeof (void*) * 1);
92 if (unlikely (local
== NULL
|| token
== NULL
))
94 const char msg
[] = "Failed to allocate coarray";
100 int len
= ((int) sizeof (msg
) > errmsg_len
) ? errmsg_len
101 : (int) sizeof (msg
);
102 memcpy (errmsg
, msg
, len
);
103 if (errmsg_len
> len
)
104 memset (&errmsg
[len
], ' ', errmsg_len
-len
);
109 caf_runtime_error (msg
);
115 if (type
== CAF_REGTYPE_COARRAY_STATIC
)
117 caf_static_t
*tmp
= malloc (sizeof (caf_static_t
));
118 tmp
->prev
= caf_static_list
;
120 caf_static_list
= tmp
;
127 _gfortran_caf_deregister (void ***token
, int *stat
,
128 char *errmsg
__attribute__ ((unused
)),
129 int errmsg_len
__attribute__ ((unused
)))
140 _gfortran_caf_sync_all (int *stat
,
141 char *errmsg
__attribute__ ((unused
)),
142 int errmsg_len
__attribute__ ((unused
)))
150 _gfortran_caf_sync_images (int count
__attribute__ ((unused
)),
151 int images
[] __attribute__ ((unused
)),
153 char *errmsg
__attribute__ ((unused
)),
154 int errmsg_len
__attribute__ ((unused
)))
159 for (i
= 0; i
< count
; i
++)
162 fprintf (stderr
, "COARRAY ERROR: Invalid image index %d to SYNC "
163 "IMAGES", images
[i
]);
174 _gfortran_caf_error_stop_str (const char *string
, int32_t len
)
176 fputs ("ERROR STOP ", stderr
);
178 fputc (*(string
++), stderr
);
179 fputs ("\n", stderr
);
186 _gfortran_caf_error_stop (int32_t error
)
188 fprintf (stderr
, "ERROR STOP %d\n", error
);