gcc/cp:
[official-gcc.git] / libgfortran / caf / single.c
blob446a464e92ec23f7bcfc69713e393a6149383cb6
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)
10 any later version.
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/>. */
26 #include "libcaf.h"
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. */
44 static void
45 caf_runtime_error (const char *message, ...)
47 va_list ap;
48 fprintf (stderr, "Fortran runtime error: ");
49 va_start (ap, message);
50 vfprintf (stderr, message, ap);
51 va_end (ap);
52 fprintf (stderr, "\n");
54 /* FIXME: Shutdown the Fortran RTL to flush the buffer. PR 43849. */
55 exit (EXIT_FAILURE);
58 void
59 _gfortran_caf_init (int *argc __attribute__ ((unused)),
60 char ***argv __attribute__ ((unused)),
61 int *this_image, int *num_images)
63 *this_image = 1;
64 *num_images = 1;
68 void
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;
82 void *
83 _gfortran_caf_register (ptrdiff_t size, caf_register_t type, void ***token,
84 int *stat, char *errmsg, int errmsg_len)
86 void *local;
88 local = malloc (size);
89 *token = malloc (sizeof (void*) * 1);
90 (*token)[0] = local;
92 if (unlikely (local == NULL || token == NULL))
94 const char msg[] = "Failed to allocate coarray";
95 if (stat)
97 *stat = 1;
98 if (errmsg_len > 0)
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);
106 return NULL;
108 else
109 caf_runtime_error (msg);
112 if (stat)
113 *stat = 0;
115 if (type == CAF_REGTYPE_COARRAY_STATIC)
117 caf_static_t *tmp = malloc (sizeof (caf_static_t));
118 tmp->prev = caf_static_list;
119 tmp->token = *token;
120 caf_static_list = tmp;
122 return local;
126 void
127 _gfortran_caf_deregister (void ***token, int *stat,
128 char *errmsg __attribute__ ((unused)),
129 int errmsg_len __attribute__ ((unused)))
131 free ((*token)[0]);
132 free (*token);
134 if (stat)
135 *stat = 0;
139 void
140 _gfortran_caf_sync_all (int *stat,
141 char *errmsg __attribute__ ((unused)),
142 int errmsg_len __attribute__ ((unused)))
144 if (stat)
145 *stat = 0;
149 void
150 _gfortran_caf_sync_images (int count __attribute__ ((unused)),
151 int images[] __attribute__ ((unused)),
152 int *stat,
153 char *errmsg __attribute__ ((unused)),
154 int errmsg_len __attribute__ ((unused)))
156 #ifdef GFC_CAF_CHECK
157 int i;
159 for (i = 0; i < count; i++)
160 if (images[i] != 1)
162 fprintf (stderr, "COARRAY ERROR: Invalid image index %d to SYNC "
163 "IMAGES", images[i]);
164 exit (EXIT_FAILURE);
166 #endif
168 if (stat)
169 *stat = 0;
173 void
174 _gfortran_caf_error_stop_str (const char *string, int32_t len)
176 fputs ("ERROR STOP ", stderr);
177 while (len--)
178 fputc (*(string++), stderr);
179 fputs ("\n", stderr);
181 exit (1);
185 void
186 _gfortran_caf_error_stop (int32_t error)
188 fprintf (stderr, "ERROR STOP %d\n", error);
189 exit (error);