@@ -659,6 +659,7 @@ flagcxResult_t flagcxDevCommCreate(flagcxComm_t comm,
659659 return flagcxSystemError;
660660 }
661661 memset (handle, 0 , sizeof (struct flagcxDevCommInternal ));
662+ pthread_mutex_init (&handle->cachedPtrMutex , NULL );
662663 handle->barrierIpcIndex = -1 ;
663664
664665 // ---- Baseline: always ----
@@ -691,6 +692,7 @@ flagcxResult_t flagcxDevCommCreate(flagcxComm_t comm,
691692 if (ret != flagcxSuccess && ret != flagcxNotSupported &&
692693 ret != flagcxInvalidArgument) {
693694 WARN (" flagcxDevCommCreate: vendor devCommCreate failed (%d)" , ret);
695+ pthread_mutex_destroy (&handle->cachedPtrMutex );
694696 free (handle);
695697 return ret;
696698 }
@@ -719,6 +721,7 @@ flagcxResult_t flagcxDevCommCreate(flagcxComm_t comm,
719721 WARN (" flagcxDevCommCreate: IPC barrier setup failed (%d), "
720722 " barriers unavailable" ,
721723 res);
724+ pthread_mutex_destroy (&handle->cachedPtrMutex );
722725 free (handle);
723726 return res;
724727 }
@@ -988,9 +991,10 @@ flagcxResult_t flagcxDevCommDestroy(flagcxComm_t comm,
988991
989992 // Device pointer cache cleanup
990993 if (devComm->cachedDevicePtr ) {
991- deviceAdaptor-> deviceFree ( devComm->cachedDevicePtr , flagcxMemDevice, NULL );
994+ flagcxCommDeferFree (comm, devComm->cachedDevicePtr , flagcxMemDevice);
992995 }
993996
997+ pthread_mutex_destroy (&devComm->cachedPtrMutex );
994998 free (devComm->localRankToRank );
995999 free (devComm);
9961000 return flagcxSuccess;
@@ -1015,6 +1019,7 @@ flagcxResult_t flagcxDevMemCreate(flagcxComm_t comm, void *buff, size_t size,
10151019 return flagcxSystemError;
10161020 }
10171021 memset (handle, 0 , sizeof (struct flagcxDevMemInternal ));
1022+ pthread_mutex_init (&handle->cachedPtrMutex , NULL );
10181023
10191024 // ---- Baseline: always ----
10201025 handle->rawPtr = buff;
@@ -1093,6 +1098,7 @@ flagcxResult_t flagcxDevMemCreate(flagcxComm_t comm, void *buff, size_t size,
10931098 auto *kWin = new (std::nothrow) typename DeviceAPI::Window{};
10941099 if (kWin == nullptr ) {
10951100 WARN (" flagcxDevMemCreate: failed to allocate DeviceAPI::Window" );
1101+ pthread_mutex_destroy (&handle->cachedPtrMutex );
10961102 free (handle);
10971103 return flagcxSystemError;
10981104 }
@@ -1140,9 +1146,10 @@ flagcxResult_t flagcxDevMemDestroy(flagcxComm_t comm, flagcxDevMem_t devMem) {
11401146
11411147 // Free cached device pointer if present
11421148 if (devMem->cachedDevicePtr ) {
1143- deviceAdaptor-> deviceFree ( devMem->cachedDevicePtr , flagcxMemDevice, NULL );
1149+ flagcxCommDeferFree (comm, devMem->cachedDevicePtr , flagcxMemDevice);
11441150 }
11451151
1152+ pthread_mutex_destroy (&devMem->cachedPtrMutex );
11461153 free (devMem);
11471154 return flagcxSuccess;
11481155}
@@ -1156,8 +1163,11 @@ flagcxResult_t flagcxDevCommGetDevicePtr(flagcxDevComm_t devComm,
11561163 if (!devComm || !devPtr)
11571164 return flagcxInvalidArgument;
11581165
1166+ pthread_mutex_lock (&devComm->cachedPtrMutex );
1167+
11591168 if (devComm->cachedDevicePtr ) {
11601169 *devPtr = devComm->cachedDevicePtr ;
1170+ pthread_mutex_unlock (&devComm->cachedPtrMutex );
11611171 return flagcxSuccess;
11621172 }
11631173
@@ -1166,24 +1176,39 @@ flagcxResult_t flagcxDevCommGetDevicePtr(flagcxDevComm_t devComm,
11661176
11671177 // Allocate device memory and copy
11681178 void *dPtr = nullptr ;
1169- FLAGCXCHECK (deviceAdaptor->deviceMalloc (&dPtr, sizeof (flagcxDevComm),
1170- flagcxMemDevice, NULL ));
1171- FLAGCXCHECK (
1179+ flagcxResult_t res = flagcxSuccess;
1180+ FLAGCXCHECKGOTO (deviceAdaptor->deviceMalloc (&dPtr, sizeof (flagcxDevComm),
1181+ flagcxMemDevice, NULL ),
1182+ res, fail);
1183+ FLAGCXCHECKGOTO (
11721184 deviceAdaptor->deviceMemcpy (dPtr, &hostCopy, sizeof (flagcxDevComm),
1173- flagcxMemcpyHostToDevice, NULL , NULL ));
1185+ flagcxMemcpyHostToDevice, NULL , NULL ),
1186+ res, fail);
11741187
11751188 devComm->cachedDevicePtr = dPtr;
11761189 *devPtr = dPtr;
1190+ pthread_mutex_unlock (&devComm->cachedPtrMutex );
11771191 return flagcxSuccess;
1192+
1193+ fail:
1194+ pthread_mutex_unlock (&devComm->cachedPtrMutex );
1195+ if (dPtr) {
1196+ deviceAdaptor->deviceFree (dPtr, flagcxMemDevice, NULL );
1197+ }
1198+ return res;
11781199}
11791200
11801201flagcxResult_t flagcxDevCommFreeDevicePtr (flagcxDevComm_t devComm) {
11811202 if (!devComm)
11821203 return flagcxInvalidArgument;
1183- if (devComm->cachedDevicePtr ) {
1184- FLAGCXCHECK (deviceAdaptor->deviceFree (devComm->cachedDevicePtr ,
1185- flagcxMemDevice, NULL ));
1186- devComm->cachedDevicePtr = nullptr ;
1204+
1205+ pthread_mutex_lock (&devComm->cachedPtrMutex );
1206+ void *ptr = devComm->cachedDevicePtr ;
1207+ devComm->cachedDevicePtr = nullptr ;
1208+ pthread_mutex_unlock (&devComm->cachedPtrMutex );
1209+
1210+ if (ptr) {
1211+ FLAGCXCHECK (deviceAdaptor->deviceFree (ptr, flagcxMemDevice, NULL ));
11871212 }
11881213 return flagcxSuccess;
11891214}
@@ -1192,8 +1217,11 @@ flagcxResult_t flagcxDevMemGetDevicePtr(flagcxDevMem_t devMem, void **devPtr) {
11921217 if (!devMem || !devPtr)
11931218 return flagcxInvalidArgument;
11941219
1220+ pthread_mutex_lock (&devMem->cachedPtrMutex );
1221+
11951222 if (devMem->cachedDevicePtr ) {
11961223 *devPtr = devMem->cachedDevicePtr ;
1224+ pthread_mutex_unlock (&devMem->cachedPtrMutex );
11971225 return flagcxSuccess;
11981226 }
11991227
@@ -1202,24 +1230,39 @@ flagcxResult_t flagcxDevMemGetDevicePtr(flagcxDevMem_t devMem, void **devPtr) {
12021230
12031231 // Allocate device memory and copy
12041232 void *dPtr = nullptr ;
1205- FLAGCXCHECK (deviceAdaptor->deviceMalloc (&dPtr, sizeof (flagcxDevMem),
1206- flagcxMemDevice, NULL ));
1207- FLAGCXCHECK (deviceAdaptor->deviceMemcpy (dPtr, &hostCopy, sizeof (flagcxDevMem),
1208- flagcxMemcpyHostToDevice, NULL ,
1209- NULL ));
1233+ flagcxResult_t res = flagcxSuccess;
1234+ FLAGCXCHECKGOTO (deviceAdaptor->deviceMalloc (&dPtr, sizeof (flagcxDevMem),
1235+ flagcxMemDevice, NULL ),
1236+ res, fail);
1237+ FLAGCXCHECKGOTO (
1238+ deviceAdaptor->deviceMemcpy (dPtr, &hostCopy, sizeof (flagcxDevMem),
1239+ flagcxMemcpyHostToDevice, NULL , NULL ),
1240+ res, fail);
12101241
12111242 devMem->cachedDevicePtr = dPtr;
12121243 *devPtr = dPtr;
1244+ pthread_mutex_unlock (&devMem->cachedPtrMutex );
12131245 return flagcxSuccess;
1246+
1247+ fail:
1248+ pthread_mutex_unlock (&devMem->cachedPtrMutex );
1249+ if (dPtr) {
1250+ deviceAdaptor->deviceFree (dPtr, flagcxMemDevice, NULL );
1251+ }
1252+ return res;
12141253}
12151254
12161255flagcxResult_t flagcxDevMemFreeDevicePtr (flagcxDevMem_t devMem) {
12171256 if (!devMem)
12181257 return flagcxInvalidArgument;
1219- if (devMem->cachedDevicePtr ) {
1220- FLAGCXCHECK (deviceAdaptor->deviceFree (devMem->cachedDevicePtr ,
1221- flagcxMemDevice, NULL ));
1222- devMem->cachedDevicePtr = nullptr ;
1258+
1259+ pthread_mutex_lock (&devMem->cachedPtrMutex );
1260+ void *ptr = devMem->cachedDevicePtr ;
1261+ devMem->cachedDevicePtr = nullptr ;
1262+ pthread_mutex_unlock (&devMem->cachedPtrMutex );
1263+
1264+ if (ptr) {
1265+ FLAGCXCHECK (deviceAdaptor->deviceFree (ptr, flagcxMemDevice, NULL ));
12231266 }
12241267 return flagcxSuccess;
12251268}
0 commit comments