mpi.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /* MPI implementation of GNU Fortran Coarray Library
  2. Copyright (C) 2011-2015 Free Software Foundation, Inc.
  3. Contributed by Tobias Burnus <burnus@net-b.de>
  4. This file is part of the GNU Fortran Coarray Runtime Library (libcaf).
  5. Libcaf is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3, or (at your option)
  8. any later version.
  9. Libcaf is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. Under Section 7 of GPL version 3, you are granted additional
  14. permissions described in the GCC Runtime Library Exception, version
  15. 3.1, as published by the Free Software Foundation.
  16. You should have received a copy of the GNU General Public License and
  17. a copy of the GCC Runtime Library Exception along with this program;
  18. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. <http://www.gnu.org/licenses/>. */
  20. #include "libcaf.h"
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h> /* For memcpy. */
  24. #include <stdarg.h> /* For variadic arguments. */
  25. #include <mpi.h>
  26. /* Define GFC_CAF_CHECK to enable run-time checking. */
  27. /* #define GFC_CAF_CHECK 1 */
  28. typedef void ** mpi_token_t;
  29. #define TOKEN(X) ((mpi_token_t) (X))
  30. static void error_stop (int error) __attribute__ ((noreturn));
  31. /* Global variables. */
  32. static int caf_mpi_initialized;
  33. static int caf_this_image;
  34. static int caf_num_images;
  35. static int caf_is_finalized;
  36. caf_static_t *caf_static_list = NULL;
  37. /* Keep in sync with single.c. */
  38. static void
  39. caf_runtime_error (const char *message, ...)
  40. {
  41. va_list ap;
  42. fprintf (stderr, "Fortran runtime error on image %d: ", caf_this_image);
  43. va_start (ap, message);
  44. vfprintf (stderr, message, ap);
  45. va_end (ap);
  46. fprintf (stderr, "\n");
  47. /* FIXME: Shutdown the Fortran RTL to flush the buffer. PR 43849. */
  48. /* FIXME: Do some more effort than just MPI_ABORT. */
  49. MPI_Abort (MPI_COMM_WORLD, EXIT_FAILURE);
  50. /* Should be unreachable, but to make sure also call exit. */
  51. exit (EXIT_FAILURE);
  52. }
  53. /* Initialize coarray program. This routine assumes that no other
  54. MPI initialization happened before; otherwise MPI_Initialized
  55. had to be used. As the MPI library might modify the command-line
  56. arguments, the routine should be called before the run-time
  57. libaray is initialized. */
  58. void
  59. _gfortran_caf_init (int *argc, char ***argv)
  60. {
  61. if (caf_num_images == 0)
  62. {
  63. /* caf_mpi_initialized is only true if the main program is
  64. not written in Fortran. */
  65. MPI_Initialized (&caf_mpi_initialized);
  66. if (!caf_mpi_initialized)
  67. MPI_Init (argc, argv);
  68. MPI_Comm_size (MPI_COMM_WORLD, &caf_num_images);
  69. MPI_Comm_rank (MPI_COMM_WORLD, &caf_this_image);
  70. caf_this_image++;
  71. }
  72. }
  73. /* Finalize coarray program. */
  74. void
  75. _gfortran_caf_finalize (void)
  76. {
  77. while (caf_static_list != NULL)
  78. {
  79. caf_static_t *tmp = caf_static_list->prev;
  80. free (TOKEN (caf_static_list->token)[caf_this_image-1]);
  81. free (TOKEN (caf_static_list->token));
  82. free (caf_static_list);
  83. caf_static_list = tmp;
  84. }
  85. if (!caf_mpi_initialized)
  86. MPI_Finalize ();
  87. caf_is_finalized = 1;
  88. }
  89. int
  90. _gfortran_caf_this_image (int distance __attribute__ ((unused)))
  91. {
  92. return caf_this_image;
  93. }
  94. int
  95. _gfortran_caf_num_images (int distance __attribute__ ((unused)),
  96. int failed __attribute__ ((unused)))
  97. {
  98. return caf_num_images;
  99. }
  100. void *
  101. _gfortran_caf_register (size_t size, caf_register_t type, caf_token_t *token,
  102. int *stat, char *errmsg, int errmsg_len)
  103. {
  104. void *local;
  105. int err;
  106. if (unlikely (caf_is_finalized))
  107. goto error;
  108. /* Start MPI if not already started. */
  109. if (caf_num_images == 0)
  110. _gfortran_caf_init (NULL, NULL);
  111. /* Token contains only a list of pointers. */
  112. local = malloc (size);
  113. *token = malloc (sizeof (mpi_token_t) * caf_num_images);
  114. if (unlikely (local == NULL || *token == NULL))
  115. goto error;
  116. /* token[img-1] is the address of the token in image "img". */
  117. err = MPI_Allgather (&local, sizeof (void*), MPI_BYTE, TOKEN (*token),
  118. sizeof (void*), MPI_BYTE, MPI_COMM_WORLD);
  119. if (unlikely (err))
  120. {
  121. free (local);
  122. free (*token);
  123. goto error;
  124. }
  125. if (type == CAF_REGTYPE_COARRAY_STATIC)
  126. {
  127. caf_static_t *tmp = malloc (sizeof (caf_static_t));
  128. tmp->prev = caf_static_list;
  129. tmp->token = *token;
  130. caf_static_list = tmp;
  131. }
  132. if (stat)
  133. *stat = 0;
  134. return local;
  135. error:
  136. {
  137. char *msg;
  138. if (caf_is_finalized)
  139. msg = "Failed to allocate coarray - there are stopped images";
  140. else
  141. msg = "Failed to allocate coarray";
  142. if (stat)
  143. {
  144. *stat = caf_is_finalized ? STAT_STOPPED_IMAGE : 1;
  145. if (errmsg_len > 0)
  146. {
  147. int len = ((int) strlen (msg) > errmsg_len) ? errmsg_len
  148. : (int) strlen (msg);
  149. memcpy (errmsg, msg, len);
  150. if (errmsg_len > len)
  151. memset (&errmsg[len], ' ', errmsg_len-len);
  152. }
  153. }
  154. else
  155. caf_runtime_error (msg);
  156. }
  157. return NULL;
  158. }
  159. void
  160. _gfortran_caf_deregister (caf_token_t *token, int *stat, char *errmsg, int errmsg_len)
  161. {
  162. if (unlikely (caf_is_finalized))
  163. {
  164. const char msg[] = "Failed to deallocate coarray - "
  165. "there are stopped images";
  166. if (stat)
  167. {
  168. *stat = STAT_STOPPED_IMAGE;
  169. if (errmsg_len > 0)
  170. {
  171. int len = ((int) sizeof (msg) - 1 > errmsg_len)
  172. ? errmsg_len : (int) sizeof (msg) - 1;
  173. memcpy (errmsg, msg, len);
  174. if (errmsg_len > len)
  175. memset (&errmsg[len], ' ', errmsg_len-len);
  176. }
  177. return;
  178. }
  179. caf_runtime_error (msg);
  180. }
  181. _gfortran_caf_sync_all (NULL, NULL, 0);
  182. if (stat)
  183. *stat = 0;
  184. free (TOKEN (*token)[caf_this_image-1]);
  185. free (*token);
  186. }
  187. void
  188. _gfortran_caf_sync_all (int *stat, char *errmsg, int errmsg_len)
  189. {
  190. int ierr;
  191. if (unlikely (caf_is_finalized))
  192. ierr = STAT_STOPPED_IMAGE;
  193. else
  194. ierr = MPI_Barrier (MPI_COMM_WORLD);
  195. if (stat)
  196. *stat = ierr;
  197. if (ierr)
  198. {
  199. char *msg;
  200. if (caf_is_finalized)
  201. msg = "SYNC ALL failed - there are stopped images";
  202. else
  203. msg = "SYNC ALL failed";
  204. if (errmsg_len > 0)
  205. {
  206. int len = ((int) strlen (msg) > errmsg_len) ? errmsg_len
  207. : (int) strlen (msg);
  208. memcpy (errmsg, msg, len);
  209. if (errmsg_len > len)
  210. memset (&errmsg[len], ' ', errmsg_len-len);
  211. }
  212. else
  213. caf_runtime_error (msg);
  214. }
  215. }
  216. /* SYNC IMAGES. Note: SYNC IMAGES(*) is passed as count == -1 while
  217. SYNC IMAGES([]) has count == 0. Note further that SYNC IMAGES(*)
  218. is not equivalent to SYNC ALL. */
  219. void
  220. _gfortran_caf_sync_images (int count, int images[], int *stat, char *errmsg,
  221. int errmsg_len)
  222. {
  223. int ierr;
  224. if (count == 0 || (count == 1 && images[0] == caf_this_image))
  225. {
  226. if (stat)
  227. *stat = 0;
  228. return;
  229. }
  230. #ifdef GFC_CAF_CHECK
  231. {
  232. int i;
  233. for (i = 0; i < count; i++)
  234. if (images[i] < 1 || images[i] > caf_num_images)
  235. {
  236. fprintf (stderr, "COARRAY ERROR: Invalid image index %d to SYNC "
  237. "IMAGES", images[i]);
  238. error_stop (1);
  239. }
  240. }
  241. #endif
  242. /* FIXME: SYNC IMAGES with a nontrivial argument cannot easily be
  243. mapped to MPI communicators. Thus, exist early with an error message. */
  244. if (count > 0)
  245. {
  246. fprintf (stderr, "COARRAY ERROR: SYNC IMAGES not yet implemented");
  247. error_stop (1);
  248. }
  249. /* Handle SYNC IMAGES(*). */
  250. if (unlikely (caf_is_finalized))
  251. ierr = STAT_STOPPED_IMAGE;
  252. else
  253. ierr = MPI_Barrier (MPI_COMM_WORLD);
  254. if (stat)
  255. *stat = ierr;
  256. if (ierr)
  257. {
  258. char *msg;
  259. if (caf_is_finalized)
  260. msg = "SYNC IMAGES failed - there are stopped images";
  261. else
  262. msg = "SYNC IMAGES failed";
  263. if (errmsg_len > 0)
  264. {
  265. int len = ((int) strlen (msg) > errmsg_len) ? errmsg_len
  266. : (int) strlen (msg);
  267. memcpy (errmsg, msg, len);
  268. if (errmsg_len > len)
  269. memset (&errmsg[len], ' ', errmsg_len-len);
  270. }
  271. else
  272. caf_runtime_error (msg);
  273. }
  274. }
  275. /* ERROR STOP the other images. */
  276. static void
  277. error_stop (int error)
  278. {
  279. /* FIXME: Shutdown the Fortran RTL to flush the buffer. PR 43849. */
  280. /* FIXME: Do some more effort than just MPI_ABORT. */
  281. MPI_Abort (MPI_COMM_WORLD, error);
  282. /* Should be unreachable, but to make sure also call exit. */
  283. exit (error);
  284. }
  285. /* ERROR STOP function for string arguments. */
  286. void
  287. _gfortran_caf_error_stop_str (const char *string, int32_t len)
  288. {
  289. fputs ("ERROR STOP ", stderr);
  290. while (len--)
  291. fputc (*(string++), stderr);
  292. fputs ("\n", stderr);
  293. error_stop (1);
  294. }
  295. /* ERROR STOP function for numerical arguments. */
  296. void
  297. _gfortran_caf_error_stop (int32_t error)
  298. {
  299. fprintf (stderr, "ERROR STOP %d\n", error);
  300. error_stop (error);
  301. }