1 /* Single-image implementation of GNU Fortran Coarray Library
2 Copyright (C) 2011-2014 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 typedef void* single_token_t
;
36 #define TOKEN(X) ((single_token_t) (X))
38 /* Single-image implementation of the CAF library.
39 Note: For performance reasons -fcoarry=single should be used
40 rather than this library. */
42 /* Global variables. */
43 caf_static_t
*caf_static_list
= NULL
;
46 /* Keep in sync with mpi.c. */
48 caf_runtime_error (const char *message
, ...)
51 fprintf (stderr
, "Fortran runtime error: ");
52 va_start (ap
, message
);
53 vfprintf (stderr
, message
, ap
);
55 fprintf (stderr
, "\n");
57 /* FIXME: Shutdown the Fortran RTL to flush the buffer. PR 43849. */
62 _gfortran_caf_init (int *argc
__attribute__ ((unused
)),
63 char ***argv
__attribute__ ((unused
)))
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
);
75 free (caf_static_list
);
76 caf_static_list
= tmp
;
82 _gfortran_caf_this_image (int distance
__attribute__ ((unused
)))
89 _gfortran_caf_num_images (int distance
__attribute__ ((unused
)),
90 int failed
__attribute__ ((unused
)))
97 _gfortran_caf_register (size_t size
, caf_register_t type
, caf_token_t
*token
,
98 int *stat
, char *errmsg
, int errmsg_len
)
102 local
= malloc (size
);
103 *token
= malloc (sizeof (single_token_t
));
105 if (unlikely (local
== NULL
|| token
== NULL
))
107 const char msg
[] = "Failed to allocate coarray";
113 int len
= ((int) sizeof (msg
) > errmsg_len
) ? errmsg_len
114 : (int) sizeof (msg
);
115 memcpy (errmsg
, msg
, len
);
116 if (errmsg_len
> len
)
117 memset (&errmsg
[len
], ' ', errmsg_len
-len
);
122 caf_runtime_error (msg
);
130 if (type
== CAF_REGTYPE_COARRAY_STATIC
)
132 caf_static_t
*tmp
= malloc (sizeof (caf_static_t
));
133 tmp
->prev
= caf_static_list
;
135 caf_static_list
= tmp
;
142 _gfortran_caf_deregister (caf_token_t
*token
, int *stat
,
143 char *errmsg
__attribute__ ((unused
)),
144 int errmsg_len
__attribute__ ((unused
)))
146 free (TOKEN(*token
));
154 _gfortran_caf_sync_all (int *stat
,
155 char *errmsg
__attribute__ ((unused
)),
156 int errmsg_len
__attribute__ ((unused
)))
164 _gfortran_caf_sync_images (int count
__attribute__ ((unused
)),
165 int images
[] __attribute__ ((unused
)),
167 char *errmsg
__attribute__ ((unused
)),
168 int errmsg_len
__attribute__ ((unused
)))
173 for (i
= 0; i
< count
; i
++)
176 fprintf (stderr
, "COARRAY ERROR: Invalid image index %d to SYNC "
177 "IMAGES", images
[i
]);
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
);
200 _gfortran_caf_error_stop (int32_t error
)
202 fprintf (stderr
, "ERROR STOP %d\n", error
);
208 _gfortran_caf_co_sum (void *a
__attribute__ ((unused
)),
209 caf_vector_t vector
[] __attribute__ ((unused
)),
210 int result_image
__attribute__ ((unused
)),
211 int *stat
, char *errmsg
__attribute__ ((unused
)),
212 int errmsg_len
__attribute__ ((unused
)))
219 _gfortran_caf_co_min (void *a
__attribute__ ((unused
)),
220 caf_vector_t vector
[] __attribute__ ((unused
)),
221 int result_image
__attribute__ ((unused
)),
222 int *stat
, char *errmsg
__attribute__ ((unused
)),
223 int src_len
__attribute__ ((unused
)),
224 int errmsg_len
__attribute__ ((unused
)))
231 _gfortran_caf_co_max (void *a
__attribute__ ((unused
)),
232 caf_vector_t vector
[] __attribute__ ((unused
)),
233 int result_image
__attribute__ ((unused
)),
234 int *stat
, char *errmsg
__attribute__ ((unused
)),
235 int src_len
__attribute__ ((unused
)),
236 int errmsg_len
__attribute__ ((unused
)))