@@ -216,6 +216,9 @@ char err_buffer[MPI_MAX_ERROR_STRING];
216216 MPI_COMM_WORLD for interoperability purposes. */
217217MPI_Comm CAF_COMM_WORLD ;
218218
219+ static caf_teams_list * teams_list = NULL ;
220+ static caf_used_teams_list * used_teams = NULL ;
221+
219222/* Emitted when a theorectically unreachable part is reached. */
220223const char unreachable [] = "Fatal error: unreachable alternative found.\n" ;
221224
@@ -801,6 +804,16 @@ PREFIX (init) (int *argc, char ***argv)
801804
802805 stat_tok = malloc (sizeof (MPI_Win ));
803806
807+ teams_list = (caf_teams_list * )calloc (1 ,sizeof (caf_teams_list ));
808+ teams_list -> team_id = -1 ;
809+ MPI_Comm * tmp_comm = (MPI_Comm * )calloc (1 ,sizeof (MPI_Comm ));
810+ * tmp_comm = CAF_COMM_WORLD ;
811+ teams_list -> team = tmp_comm ;
812+ teams_list -> prev = NULL ;
813+ used_teams = (caf_used_teams_list * )calloc (1 ,sizeof (caf_used_teams_list ));
814+ used_teams -> team_list_elem = teams_list ;
815+ used_teams -> prev = NULL ;
816+
804817#ifdef WITH_FAILED_IMAGES
805818 MPI_Comm_dup (MPI_COMM_WORLD , & alive_comm );
806819 /* Handling of failed/stopped images is done by setting an error handler
@@ -7142,7 +7155,6 @@ PREFIX(atomic_ref) (caf_token_t token, size_t offset,
71427155 return ;
71437156}
71447157
7145-
71467158void
71477159PREFIX (atomic_cas ) (caf_token_t token , size_t offset ,
71487160 int image_index , void * old , void * compare ,
@@ -7634,3 +7646,140 @@ unimplemented_alloc_comps_message (const char * functionname)
76347646 exit (EXIT_FAILURE );
76357647#endif
76367648}
7649+
7650+ void PREFIX (form_team ) (int team_id , caf_team_t * team , int index __attribute__ ((unused )))
7651+ {
7652+ struct caf_teams_list * tmp ;
7653+ void * tmp_team ;
7654+ MPI_Comm * newcomm ;
7655+ MPI_Comm * current_comm = & CAF_COMM_WORLD ;
7656+
7657+ MPI_Barrier (CAF_COMM_WORLD );
7658+ newcomm = (MPI_Comm * )calloc (1 ,sizeof (MPI_Comm ));
7659+ MPI_Comm_split (* current_comm , team_id , caf_this_image , newcomm );
7660+
7661+ tmp = calloc (1 ,sizeof (struct caf_teams_list ));
7662+ tmp -> prev = teams_list ;
7663+ teams_list = tmp ;
7664+ teams_list -> team_id = team_id ;
7665+ teams_list -> team = newcomm ;
7666+ * team = tmp ;
7667+ }
7668+
7669+ void PREFIX (change_team ) (caf_team_t * team , int coselector __attribute__ ((unused )))
7670+ {
7671+ caf_used_teams_list * tmp_used = NULL ;
7672+ caf_teams_list * tmp_list = NULL ;
7673+ void * tmp_team ;
7674+ MPI_Comm * tmp_comm ;
7675+
7676+ MPI_Barrier (CAF_COMM_WORLD );
7677+ tmp_list = (struct caf_teams_list * )* team ;
7678+ tmp_team = (void * )tmp_list -> team ;
7679+ tmp_comm = (MPI_Comm * )tmp_team ;
7680+
7681+ tmp_used = (caf_used_teams_list * )calloc (1 ,sizeof (caf_used_teams_list ));
7682+ tmp_used -> prev = used_teams ;
7683+
7684+ /* /\* We need to look in the teams_list and find the appropriate element. */
7685+ /* * This is not efficient but can be easily fixed in the future. */
7686+ /* * Instead of keeping track of the communicator in the compiler */
7687+ /* * we should keep track of the caf_teams_list element associated with it. *\/ */
7688+
7689+ /* tmp_list = teams_list; */
7690+
7691+ /* while(tmp_list) */
7692+ /* { */
7693+ /* if(tmp_list->team == tmp_team) */
7694+ /* break; */
7695+ /* tmp_list = tmp_list->prev; */
7696+ /* } */
7697+
7698+ if (tmp_list == NULL )
7699+ caf_runtime_error ("CHANGE TEAM called on a non-existing team" );
7700+
7701+ tmp_used -> team_list_elem = tmp_list ;
7702+ used_teams = tmp_used ;
7703+ tmp_team = tmp_used -> team_list_elem -> team ;
7704+ tmp_comm = (MPI_Comm * )tmp_team ;
7705+ CAF_COMM_WORLD = * tmp_comm ;
7706+ MPI_Comm_rank (* tmp_comm ,& caf_this_image );
7707+ caf_this_image ++ ;
7708+ MPI_Comm_size (* tmp_comm ,& caf_num_images );
7709+ }
7710+
7711+ MPI_Fint
7712+ PREFIX (get_communicator ) (caf_team_t * team )
7713+ {
7714+ if (team != NULL ) caf_runtime_error ("get_communicator does not yet support the optional team argument" );
7715+
7716+ MPI_Comm * comm_ptr = teams_list -> team ;
7717+
7718+ MPI_Fint ret = MPI_Comm_c2f (* comm_ptr );
7719+
7720+ return ret ;
7721+
7722+ // return *(int*)comm_ptr;
7723+ }
7724+
7725+ int
7726+ PREFIX (team_number ) (caf_team_t * team )
7727+ {
7728+ if (team != NULL ) caf_runtime_error ("team_number does not yet support the optional team argument" );
7729+
7730+ /* if(used_teams->prev == NULL) */
7731+ /* return -1; */
7732+ return used_teams -> team_list_elem -> team_id ;
7733+ }
7734+
7735+ void PREFIX (end_team ) (caf_team_t * team __attribute__ ((unused )))
7736+ {
7737+ caf_used_teams_list * tmp_used = NULL ;
7738+ void * tmp_team ;
7739+ MPI_Comm * tmp_comm ;
7740+
7741+ MPI_Barrier (CAF_COMM_WORLD );
7742+ if (used_teams -> prev == NULL )
7743+ caf_runtime_error ("END TEAM called on initial team" );
7744+
7745+ tmp_used = used_teams ;
7746+ used_teams = used_teams -> prev ;
7747+ free (tmp_used );
7748+ tmp_used = used_teams ;
7749+ tmp_team = tmp_used -> team_list_elem -> team ;
7750+ tmp_comm = (MPI_Comm * )tmp_team ;
7751+ CAF_COMM_WORLD = * tmp_comm ;
7752+ MPI_Barrier (CAF_COMM_WORLD );
7753+ /* CAF_COMM_WORLD = (MPI_Comm)*tmp_used->team_list_elem->team; */
7754+ MPI_Comm_rank (CAF_COMM_WORLD ,& caf_this_image );
7755+ caf_this_image ++ ;
7756+ MPI_Comm_size (CAF_COMM_WORLD ,& caf_num_images );
7757+ }
7758+
7759+ void PREFIX (sync_team ) (caf_team_t * team , int unused __attribute__ ((unused )))
7760+ {
7761+ caf_teams_list * tmp_list = NULL ;
7762+ caf_used_teams_list * tmp_used = NULL ;
7763+ void * tmp_team ;
7764+ MPI_Comm * tmp_comm ;
7765+
7766+ /* Check if the team is the current, and ancestor or a descendant. To be implemented. */
7767+
7768+ tmp_used = used_teams ;
7769+ tmp_list = (struct caf_teams_list * )* team ;
7770+ tmp_team = (void * )tmp_list -> team ;
7771+ tmp_comm = (MPI_Comm * )tmp_team ;
7772+
7773+ while (tmp_used )
7774+ {
7775+ if (tmp_used -> team_list_elem == tmp_list )
7776+ break ;
7777+ tmp_used = tmp_used -> prev ;
7778+ }
7779+
7780+ if (tmp_used == NULL )
7781+ caf_runtime_error ("SYNC TEAM called on team different from current or ancestor or descendant" );
7782+
7783+ MPI_Barrier (* tmp_comm );
7784+
7785+ }
0 commit comments