@@ -49,6 +49,15 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
49
49
50
50
51
51
#ifdef GCC_GE_7
52
+ enum flags_t {
53
+ /** Set when this token has memory associated to it. */
54
+ FLAG_ASSOCIATED = 1 ,
55
+ /** Set when the memory associated to this token is not owned by the
56
+ mpi-library, i.e., the mpi-library does not free the memory. This happens for
57
+ asynchronously allocated memory, which is attached using MPI_Win_attach. */
58
+ FLAG_MEM_NOT_OWNED = 2 ,
59
+ };
60
+
52
61
/** The caf-token of the mpi-library.
53
62
54
63
Objects of this data structure are owned by the library and are treated as a
@@ -76,11 +85,12 @@ typedef struct mpi_caf_token_t
76
85
window gives access to the descriptor on remote images. When the object is
77
86
scalar, then this is NULL. */
78
87
MPI_Win * desc ;
79
- /** This window allows access the local_memptr member of the associated token.
80
- With async allocation the token may be registered, but the memory not yet. To
81
- be able to check (using is_present()), that the memory on a remote image is
82
- present, this win can be used. */
83
- MPI_Win local_memptr_win ;
88
+ /** Flags specifying the state of this token. For a description of available
89
+ flags see the flags_t. */
90
+ unsigned * flags ;
91
+ /** This window allows access the local flags member of the associated
92
+ token. */
93
+ MPI_Win flags_win ;
84
94
} mpi_caf_token_t ;
85
95
#define TOKEN (X ) &(((mpi_caf_token_t *) (X))->memptr)
86
96
#else
@@ -498,10 +508,17 @@ PREFIX (finalize) (void)
498
508
MPI_Win_free (mpi_token -> desc );
499
509
free (mpi_token -> desc );
500
510
}
501
- CAF_Win_unlock_all (mpi_token -> local_memptr_win );
502
- MPI_Win_free (& mpi_token -> local_memptr_win );
503
- #endif // GCC_GE_7
504
511
MPI_Win_free (p );
512
+ /* Free the memory only, when it was allocated by the caf-library. */
513
+ if ((* (mpi_token -> flags ) & FLAG_MEM_NOT_OWNED ) > 0 )
514
+ free (mpi_token -> local_memptr );
515
+ /* Free the flags window only after accessing. */
516
+ CAF_Win_unlock_all (mpi_token -> flags_win );
517
+ MPI_Win_free (& mpi_token -> flags_win );
518
+ free (tmp_tot -> token );
519
+ #else // GCC_GE_7
520
+ MPI_Win_free (p );
521
+ #endif // GCC_GE_7
505
522
free (tmp_tot );
506
523
tmp_tot = prev ;
507
524
}
@@ -583,7 +600,14 @@ PREFIX (register) (size_t size, caf_register_t type, caf_token_t *token,
583
600
/* The token has to be present, when COARRAY_ALLOC_ALLOCATE_ONLY is
584
601
specified. */
585
602
if (type != CAF_REGTYPE_COARRAY_ALLOC_ALLOCATE_ONLY )
586
- * token = malloc (sizeof (mpi_caf_token_t ));
603
+ {
604
+ * token = malloc (sizeof (mpi_caf_token_t ));
605
+ MPI_Win_allocate (sizeof (unsigned ), 1 , mpi_info_same_size , CAF_COMM_WORLD ,
606
+ & ((mpi_caf_token_t * )* token )-> flags ,
607
+ & ((mpi_caf_token_t * )* token )-> flags_win );
608
+ CAF_Win_lock_all (((mpi_caf_token_t * )* token )-> flags_win );
609
+ * ((mpi_caf_token_t * )* token )-> flags = 0 ;
610
+ }
587
611
588
612
mpi_token = (mpi_caf_token_t * ) * token ;
589
613
p = TOKEN (mpi_token );
@@ -617,28 +641,18 @@ PREFIX (register) (size_t size, caf_register_t type, caf_token_t *token,
617
641
{
618
642
int ierr ;
619
643
mem = malloc (actual_size );
644
+ CAF_Win_unlock_all (* p );
620
645
ierr = MPI_Win_attach (* p , mem , actual_size );
621
- fprintf (stderr , "%d/%d: Attach mem %p to win = %p, ierr: %d\n" , caf_this_image , caf_num_images ,
622
- mem , * p , ierr );
646
+ CAF_Win_lock_all (* p );
647
+ * (mpi_token -> flags ) |= (FLAG_ASSOCIATED | FLAG_MEM_NOT_OWNED );
648
+ fprintf (stderr , "%d/%d: Attach mem %p to win = %p, ierr: %d, flags: %u\n" ,
649
+ caf_this_image , caf_num_images , mem , * p , ierr , * mpi_token -> flags );
623
650
}
624
651
else
625
652
{
626
653
MPI_Win_allocate (actual_size , 1 , MPI_INFO_NULL , CAF_COMM_WORLD , & mem , p );
627
654
CAF_Win_lock_all (* p );
628
- }
629
-
630
- /* When doing a allocate only, the token is initialized already, and the
631
- * window for the local_memptr exists already. Any time else create a window
632
- * to monitor whether the data-pointer of this token is associated or not. */
633
- if (type != CAF_REGTYPE_COARRAY_ALLOC_ALLOCATE_ONLY )
634
- {
635
- int ierr ;
636
- ierr = MPI_Win_create (& mpi_token -> local_memptr , sizeof (void * ), 1 ,
637
- mpi_info_same_size , CAF_COMM_WORLD ,
638
- & mpi_token -> local_memptr_win );
639
- fprintf (stderr , "%d/%d: Creating win return error code: %d\n" , caf_this_image ,
640
- caf_num_images , ierr );
641
- CAF_Win_lock_all (mpi_token -> local_memptr_win );
655
+ * (mpi_token -> flags ) = FLAG_ASSOCIATED ;
642
656
}
643
657
#else // MPI_VERSION
644
658
MPI_Alloc_mem (actual_size , MPI_INFO_NULL , & mem );
@@ -872,10 +886,20 @@ PREFIX (deregister) (caf_token_t *token, int *stat, char *errmsg, int errmsg_len
872
886
/* Unlock only, when removing the window. */
873
887
CAF_Win_unlock_all (* p );
874
888
MPI_Win_free (p );
889
+ if ((* (mpi_token -> flags ) & FLAG_MEM_NOT_OWNED ) > 0 )
890
+ free (mpi_token -> local_memptr );
875
891
}
876
892
else
877
- MPI_Win_detach (* p , mpi_token -> local_memptr );
893
+ {
894
+ MPI_Win_detach (* p , mpi_token -> local_memptr );
895
+ /* Free the memory, when we have allocated it. */
896
+ if ((* (mpi_token -> flags ) & FLAG_MEM_NOT_OWNED ) > 0 )
897
+ free (mpi_token -> local_memptr );
898
+ * (mpi_token -> flags ) = (* (mpi_token -> flags ) & ~FLAG_MEM_NOT_OWNED );
899
+ }
878
900
mpi_token -> local_memptr = NULL ;
901
+ /* Clear the flag, that memory is associated. */
902
+ * (mpi_token -> flags ) = (* (mpi_token -> flags ) & ~FLAG_ASSOCIATED );
879
903
}
880
904
if ((* (mpi_caf_token_t * * )token )-> desc
881
905
&& type != CAF_DEREGTYPE_COARRAY_DEALLOCATE_ONLY )
@@ -886,8 +910,8 @@ PREFIX (deregister) (caf_token_t *token, int *stat, char *errmsg, int errmsg_len
886
910
}
887
911
if (type != CAF_DEREGTYPE_COARRAY_DEALLOCATE_ONLY )
888
912
{
889
- CAF_Win_unlock_all (mpi_token -> local_memptr_win );
890
- MPI_Win_free (& mpi_token -> local_memptr_win );
913
+ CAF_Win_unlock_all (mpi_token -> flags_win );
914
+ MPI_Win_free (& mpi_token -> flags_win );
891
915
}
892
916
#else
893
917
CAF_Win_unlock_all (* p );
@@ -934,6 +958,7 @@ PREFIX (sync_all) (int *stat, char *errmsg, int errmsg_len)
934
958
{
935
959
int ierr = 0 ;
936
960
961
+ fprintf (stderr , "%d/%d: Entering sync all.\n" , caf_this_image , caf_num_images );
937
962
if (unlikely (caf_is_finalized ))
938
963
ierr = STAT_STOPPED_IMAGE ;
939
964
else
@@ -967,6 +992,7 @@ PREFIX (sync_all) (int *stat, char *errmsg, int errmsg_len)
967
992
else
968
993
caf_runtime_error (msg );
969
994
}
995
+ fprintf (stderr , "%d/%d: Leaving sync all.\n" , caf_this_image , caf_num_images );
970
996
}
971
997
972
998
/* token: The token of the array to be written to. */
@@ -3108,14 +3134,14 @@ PREFIX(is_present) (caf_token_t token, int image_index, caf_reference_t *refs)
3108
3134
riter = riter -> next ;
3109
3135
}
3110
3136
3111
- void * remote_local_memory = NULL ;
3112
- MPI_Datatype dtype = sizeof ( void * ) == 8 ? MPI_INTEGER8 : MPI_INTEGER4 ;
3113
- int ierr = MPI_Get ( & remote_local_memory , 1 , dtype , image_index - 1 , 0 , 1 , dtype ,
3114
- mpi_token -> local_memptr_win );
3115
- fprintf (stderr , "%d/%d: Got remote_local_memory [%d] for win %p to be: %p , ierr = %d\n" ,
3116
- caf_this_image , caf_num_images , image_index , mpi_token -> memptr ,
3117
- remote_local_memory , ierr );
3118
- return remote_local_memory != NULL ;
3137
+ unsigned remote_flags = 0U ;
3138
+ int ierr = MPI_Get ( & remote_flags , sizeof ( unsigned ), MPI_BYTE ,
3139
+ image_index - 1 , 0 , sizeof ( unsigned ), MPI_BYTE ,
3140
+ mpi_token -> flags_win );
3141
+ fprintf (stderr , "%d/%d: Got remote_flags [%d] for win %p to be: %u , ierr = %d\n" ,
3142
+ caf_this_image , caf_num_images , image_index , mpi_token -> flags_win ,
3143
+ remote_flags , ierr );
3144
+ return ( remote_flags & FLAG_ASSOCIATED ) > 0 ;
3119
3145
}
3120
3146
#endif
3121
3147
0 commit comments