@@ -156,44 +156,61 @@ bool useHeteroComm() {
156156 return false ;
157157}
158158
159- flagcxResult_t flagcxHandleInit (flagcxHandlerGroup_t *handler) {
159+ flagcxResult_t flagcxDeviceHandleInit (flagcxDeviceHandle_t *devHandle) {
160+ if (devHandle == NULL ) {
161+ WARN (" flagcxDeviceHandleInit: devHandle is NULL" );
162+ return flagcxInvalidArgument;
163+ }
160164 flagcxResult_t res = flagcxSuccess;
161165 flagcxDeviceAdaptorPluginInit ();
162166 flagcxCCLAdaptorPluginInit ();
167+ (*devHandle) = NULL ;
168+ FLAGCXCHECKGOTO (flagcxCalloc (devHandle, 1 ), res, fail);
169+ **devHandle = globalDeviceHandle;
170+ return flagcxSuccess;
171+
172+ fail:
173+ if (*devHandle) {
174+ free (*devHandle);
175+ *devHandle = NULL ;
176+ }
177+ flagcxCCLAdaptorPluginFinalize ();
178+ flagcxDeviceAdaptorPluginFinalize ();
179+ return res;
180+ }
181+
182+ flagcxResult_t flagcxDeviceHandleFree (flagcxDeviceHandle_t devHandle) {
183+ if (devHandle == NULL )
184+ return flagcxSuccess;
185+ free (devHandle);
186+ flagcxCCLAdaptorPluginFinalize ();
187+ flagcxDeviceAdaptorPluginFinalize ();
188+ return flagcxSuccess;
189+ }
190+
191+ flagcxResult_t flagcxHandleInit (flagcxHandlerGroup_t *handler) {
192+ flagcxResult_t res = flagcxSuccess;
163193 (*handler) = NULL ;
164194 FLAGCXCHECKGOTO (flagcxCalloc (handler, 1 ), res, fail);
165- FLAGCXCHECKGOTO (flagcxCalloc (&(*handler)->uniqueId , 1 ), res, fail);
166- // comm left NULL — allocated by flagcxCommInitRank
167- FLAGCXCHECKGOTO (flagcxCalloc (&(*handler)->devHandle , 1 ), res, fail);
168- *(*handler)->devHandle = globalDeviceHandle;
195+ FLAGCXCHECKGOTO (flagcxDeviceHandleInit (&(*handler)->devHandle ), res, fail);
169196 return flagcxSuccess;
170197
171198fail:
172199 if (*handler) {
173- free ((*handler)->uniqueId );
174- free ((*handler)->devHandle );
175200 free (*handler);
176201 *handler = NULL ;
177202 }
178- flagcxCCLAdaptorPluginFinalize ();
179- flagcxDeviceAdaptorPluginFinalize ();
180203 return res;
181204}
182205
183206flagcxResult_t flagcxHandleFree (flagcxHandlerGroup_t handler) {
184207 if (handler != NULL ) {
185- if (handler->uniqueId )
186- free (handler->uniqueId );
187- if (handler->devHandle )
188- free (handler->devHandle );
208+ flagcxDeviceHandleFree (handler->devHandle );
209+ handler->devHandle = NULL ;
189210 handler->uniqueId = NULL ;
190211 handler->comm = NULL ;
191- handler->devHandle = NULL ;
192212 free (handler);
193- handler = NULL ;
194213 }
195- flagcxCCLAdaptorPluginFinalize ();
196- flagcxDeviceAdaptorPluginFinalize ();
197214 return flagcxSuccess;
198215}
199216
@@ -1180,10 +1197,10 @@ flagcxResult_t flagcxGetVersion(int *version) {
11801197 return flagcxHeteroGetVersion (version);
11811198}
11821199
1183- flagcxResult_t flagcxGetUniqueId (flagcxUniqueId_t * uniqueId) {
1184- // Allocate if caller passed a NULL pointer
1185- if (* uniqueId == nullptr ) {
1186- FLAGCXCHECK ( flagcxCalloc (uniqueId, 1 )) ;
1200+ flagcxResult_t flagcxGetUniqueId (flagcxUniqueId_t uniqueId) {
1201+ if (uniqueId == NULL ) {
1202+ WARN ( " flagcxGetUniqueId: uniqueId is NULL " );
1203+ return flagcxInvalidArgument ;
11871204 }
11881205
11891206 // Init bootstrap net
@@ -1194,9 +1211,9 @@ flagcxResult_t flagcxGetUniqueId(flagcxUniqueId_t *uniqueId) {
11941211 FLAGCXCHECK (bootstrapGetUniqueId (&handle));
11951212 // flagcxUniqueId and bootstrapHandle don't have the same size and alignment
11961213 // reset to 0 to avoid undefined data
1197- memset ((void *)* uniqueId, 0 , sizeof (* *uniqueId));
1214+ memset ((void *)uniqueId, 0 , sizeof (*uniqueId));
11981215 // copy to avoid alignment mismatch
1199- memcpy ((void *)* uniqueId, &handle, sizeof (handle));
1216+ memcpy ((void *)uniqueId, &handle, sizeof (handle));
12001217 return flagcxSuccess;
12011218}
12021219
@@ -1523,6 +1540,14 @@ flagcxResult_t flagcxCommInitRank(flagcxComm_t *comm, int nranks,
15231540 WARN (" Invalid rank requested : %d/%d" , rank, nranks);
15241541 return flagcxInvalidArgument;
15251542 }
1543+ if (commId == NULL || comm == NULL ) {
1544+ WARN (" flagcxCommInitRank: commId or comm is NULL" );
1545+ return flagcxInvalidArgument;
1546+ }
1547+
1548+ // Ensure device/CCL plugins are loaded (idempotent, ref-counted)
1549+ flagcxDeviceAdaptorPluginInit ();
1550+ flagcxCCLAdaptorPluginInit ();
15261551
15271552 (*comm) = NULL ;
15281553 flagcxCalloc (comm, 1 );
@@ -1729,7 +1754,8 @@ flagcxResult_t flagcxCommInitRank(flagcxComm_t *comm, int nranks,
17291754 INFO (FLAGCX_INIT , " Flagcx USE_TUNER flag set to %d" , useTuner);
17301755 if (useTuner) {
17311756 (*comm)->tuner = &internalTuner;
1732- (*comm)->commId = commId;
1757+ FLAGCXCHECK (flagcxCalloc (&(*comm)->commId , 1 ));
1758+ memcpy ((*comm)->commId , commId, sizeof (flagcxUniqueId));
17331759 (*comm)->uniqueIdData = uniqueIdData;
17341760 (*comm)->tunerInnerComm = NULL ;
17351761 (*comm)->isTunningComm = false ;
@@ -2013,13 +2039,18 @@ flagcxResult_t flagcxCommDestroy(flagcxComm_t comm) {
20132039 // Destroy tuner
20142040 if (comm->tuner ) {
20152041 comm->tuner ->destroy (comm->tunerContext );
2016- // Free uniqueIdData
2042+ // Free uniqueIdData and commId
20172043 free (comm->uniqueIdData );
2044+ free (comm->commId );
20182045 }
20192046
20202047 // Finalize net adaptor plugin (dlclose)
20212048 FLAGCXCHECK (flagcxNetAdaptorPluginFinalize ());
20222049
2050+ // Finalize device/CCL adaptor plugins (ref-counted)
2051+ flagcxCCLAdaptorPluginFinalize ();
2052+ flagcxDeviceAdaptorPluginFinalize ();
2053+
20232054 free (comm);
20242055 return flagcxSuccess;
20252056}
0 commit comments