Patchwork [Fortran] Coarray: libcaf patch for _gfortran_caf_deregister

login
register
mail settings
Submitter Mikael Morin
Date Aug. 28, 2011, 10:33 p.m.
Message ID <201108290033.28616.mikael.morin@sfr.fr>
Download mbox | patch
Permalink /patch/111953/
State New
Headers show

Comments

Mikael Morin - Aug. 28, 2011, 10:33 p.m.
On Friday 26 August 2011 08:51:37 Tobias Burnus wrote:
> Allocatable coarrays are freed and deregistered via the libcaf function
> _gfortran_caf_deregister. Currently, the front end does not generate
> calls to the that function, however, this patch already implements the
> function.
> 
> See http://gcc.gnu.org/wiki/CoarrayLib and
> http://gcc.gnu.org/ml/fortran/2010-04/msg00168.html for details.
> 
> The function is called with the coarray token as argument. The token
> identifies the coarray in a way defined by the library. In case of
> single.c, it just contains the address of the allocated memory of the
> coarray. In case of mpi.c, it is an array of memory addresses on all
> images such that token[this_image()-1] is the memory location of the
> current image.
> 
> The patch also adds stat= and errmsg= diagnostic.
> 
> TODO: Adding calls to the function in code generated by the compiler -
> and testing the function.
> 
> 
> Tested by compiling with mpicc and gcc with "-Wall -Wextra -std=c99".
> OK for the trunk?
> 
> Tobias

Not something introduced by this patch, but I would like to point that 
caf_static_list should be freed too.



@@ -187,10 +187,36 @@ error:
 }
 
 
-int
-_gfortran_caf_deregister (void **token __attribute__ ((unused)))
+void
+_gfortran_caf_deregister (void **token, int *stat, char *errmsg, int 
errmsg_len)
 {
-  return 0;
+  if (unlikely (caf_is_finalized))
+    {
+      const char msg[] = "Failed to deallocate coarray - "
+                         "there are stopped images";
+      if (stat)
+       {
+         *stat = STAT_STOPPED_IMAGE;
+       
+         if (errmsg_len > 0)
+           {
+             int len = ((int) sizeof (msg) - 1 > errmsg_len)
+                       ? errmsg_len : (int) sizeof (msg) - 1;
+             memcpy (errmsg, msg, len);
+             if (errmsg_len > len)
+               memset (&errmsg[len], ' ', errmsg_len-len);
+           }
+         return;
+       }
+      caf_runtime_error (msg);
+    }
+
+  _gfortran_caf_sync_all (NULL, NULL, 0);
+
+  if (stat)
+    *stat = 0;
+
+  free (token[caf_this_image-1]);
 }
 

To be consistent with _gfortran_caf_register, I think you should be freeing 
token itself (the whole array).
OK with that change.

Mikael

Patch

diff --git a/libgfortran/caf/mpi.c b/libgfortran/caf/mpi.c
index ea4c0f0..711c6ee 100644
--- a/libgfortran/caf/mpi.c
+++ b/libgfortran/caf/mpi.c
@@ -103,7 +103,7 @@  _gfortran_caf_finalize (void)
 {
   while (caf_static_list != NULL)
     {
-      free(caf_static_list->token[caf_this_image-1]);
+      free (caf_static_list->token[caf_this_image-1]);
       caf_static_list = caf_static_list->prev;
     }