]> git.ipfire.org Git - thirdparty/gcc.git/blob - libgfortran/caf/single.c
2011-07-07 Tobias Burnus <burnus@net-b.de>
[thirdparty/gcc.git] / libgfortran / caf / single.c
1 /* Single-image implementation of GNU Fortran Coarray Library
2 Copyright (C) 2011
3 Free Software Foundation, Inc.
4 Contributed by Tobias Burnus <burnus@net-b.de>
5
6 This file is part of the GNU Fortran Coarray Runtime Library (libcaf).
7
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)
11 any later version.
12
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.
17
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.
21
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/>. */
26
27 #include "libcaf.h"
28 #include <stdio.h> /* For fputs and fprintf. */
29 #include <stdlib.h> /* For exit and malloc. */
30 #include <string.h> /* For memcpy and memset. */
31
32 /* Define GFC_CAF_CHECK to enable run-time checking. */
33 /* #define GFC_CAF_CHECK 1 */
34
35 /* Single-image implementation of the CAF library.
36 Note: For performance reasons -fcoarry=single should be used
37 rather than this library. */
38
39 /* Global variables. */
40 caf_static_t *caf_static_list = NULL;
41
42
43 void
44 _gfortran_caf_init (int *argc __attribute__ ((unused)),
45 char ***argv __attribute__ ((unused)),
46 int *this_image, int *num_images)
47 {
48 *this_image = 1;
49 *num_images = 1;
50 }
51
52
53 void
54 _gfortran_caf_finalize (void)
55 {
56 while (caf_static_list != NULL)
57 {
58 free(caf_static_list->token[0]);
59 caf_static_list = caf_static_list->prev;
60 }
61 }
62
63
64 void *
65 _gfortran_caf_register (ptrdiff_t size, caf_register_t type, void **token,
66 int *stat, char *errmsg, int errmsg_len)
67 {
68 void *local;
69
70 local = malloc (size);
71 token = malloc (sizeof (void*) * 1);
72 token[0] = local;
73
74 if (unlikely (local == NULL || token == NULL))
75 {
76 if (stat)
77 {
78 *stat = 1;
79 if (errmsg_len > 0)
80 {
81 const char msg[] = "Failed to allocate coarray";
82 int len = ((int) sizeof (msg) > errmsg_len) ? errmsg_len
83 : (int) sizeof (msg);
84 memcpy (errmsg, msg, len);
85 if (errmsg_len > len)
86 memset (&errmsg[len], ' ', errmsg_len-len);
87 }
88 return NULL;
89 }
90 else
91 {
92 fprintf (stderr, "ERROR: Failed to allocate coarray");
93 exit (1);
94 }
95 }
96
97 if (stat)
98 *stat = 0;
99
100 if (type == CAF_REGTYPE_COARRAY_STATIC)
101 {
102 caf_static_t *tmp = malloc (sizeof (caf_static_t));
103 tmp->prev = caf_static_list;
104 tmp->token = token;
105 caf_static_list = tmp;
106 }
107 return local;
108 }
109
110
111 int
112 _gfortran_caf_deregister (void **token __attribute__ ((unused)))
113 {
114 return 0;
115 }
116
117
118 void
119 _gfortran_caf_sync_all (int *stat,
120 char *errmsg __attribute__ ((unused)),
121 int errmsg_len __attribute__ ((unused)))
122 {
123 if (stat)
124 *stat = 0;
125 }
126
127
128 void
129 _gfortran_caf_sync_images (int count __attribute__ ((unused)),
130 int images[] __attribute__ ((unused)),
131 int *stat,
132 char *errmsg __attribute__ ((unused)),
133 int errmsg_len __attribute__ ((unused)))
134 {
135 #ifdef GFC_CAF_CHECK
136 int i;
137
138 for (i = 0; i < count; i++)
139 if (images[i] != 1)
140 {
141 fprintf (stderr, "COARRAY ERROR: Invalid image index %d to SYNC "
142 "IMAGES", images[i]);
143 exit (1);
144 }
145 #endif
146
147 if (stat)
148 *stat = 0;
149 }
150
151
152 void
153 _gfortran_caf_error_stop_str (const char *string, int32_t len)
154 {
155 fputs ("ERROR STOP ", stderr);
156 while (len--)
157 fputc (*(string++), stderr);
158 fputs ("\n", stderr);
159
160 exit (1);
161 }
162
163
164 void
165 _gfortran_caf_error_stop (int32_t error)
166 {
167 fprintf (stderr, "ERROR STOP %d\n", error);
168 exit (error);
169 }