Skip to content

Commit d9dad5e

Browse files
committed
supoort reset cuda error state
1 parent 8294acb commit d9dad5e

5 files changed

Lines changed: 70 additions & 9 deletions

File tree

flagcx/adaptor/device/cuda_adaptor.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -930,6 +930,11 @@ flagcxResult_t cudaAdaptorSymMulticastFree(void *) { return flagcxSuccess; }
930930

931931
#endif // CUDART_VERSION >= 12010
932932

933+
flagcxResult_t cudaAdaptorGetLastError() {
934+
cudaError_t err = cudaGetLastError();
935+
return err == cudaSuccess ? flagcxSuccess : flagcxSystemError;
936+
}
937+
933938
struct flagcxDeviceAdaptor cudaAdaptor {
934939
"CUDA",
935940
// Basic functions
@@ -996,6 +1001,7 @@ struct flagcxDeviceAdaptor cudaAdaptor {
9961001
cudaAdaptorSymFlatUnmap, cudaAdaptorSymMulticastSupported,
9971002
cudaAdaptorSymMulticastCreate, cudaAdaptorSymMulticastBind,
9981003
cudaAdaptorSymMulticastTeardown, cudaAdaptorSymMulticastFree,
1004+
cudaAdaptorGetLastError,
9991005
};
10001006

10011007
#endif // USE_NVIDIA_ADAPTOR

flagcx/adaptor/include/flagcx_device_adaptor.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,8 @@ struct flagcxDeviceAdaptor_latest {
243243
// Release the multicast object handle returned by symMulticastCreate.
244244
// Must be called after all ranks have torn down their mappings.
245245
flagcxResult_t (*symMulticastFree)(void *mcHandle);
246+
247+
flagcxResult_t (*getLastError)();
246248
};
247249

248250
#define flagcxDeviceAdaptor flagcxDeviceAdaptor_latest

flagcx/core/flagcx_p2p.cc

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1477,7 +1477,7 @@ static int detectPtrTypeAndMaybeCacheIpc(void *ptr, char *ipcHandleBuf,
14771477
deviceAdaptor->ipcMemHandleFree(handle);
14781478
return FLAGCX_PTR_CUDA;
14791479
}
1480-
1480+
if (deviceAdaptor->getLastError) deviceAdaptor->getLastError();
14811481
deviceAdaptor->ipcMemHandleFree(handle);
14821482
return FLAGCX_PTR_HOST;
14831483
}
@@ -2632,11 +2632,22 @@ bool flagcxP2pEngineConnIsLocal(FlagcxP2pConn *conn) {
26322632
return conn != NULL && conn->isLocal;
26332633
}
26342634

2635-
int flagcxP2pEngineReg(FlagcxP2pEngine *engine, uintptr_t data, size_t size,
2636-
FlagcxP2pMr &mrId) {
2635+
int flagcxP2pEngineRegEx(FlagcxP2pEngine *engine, uintptr_t data, size_t size,
2636+
int hintType, FlagcxP2pMr &mrId) {
26372637
if (engine == NULL || data == 0)
26382638
return -1;
26392639

2640+
auto resolvePtrType = [&](char *ipcHandleBuf,
2641+
uint32_t *ipcHandleSize) -> int {
2642+
if (hintType == FLAGCX_PTR_HOST || hintType == FLAGCX_PTR_CUDA) {
2643+
if (ipcHandleSize)
2644+
*ipcHandleSize = 0;
2645+
return hintType;
2646+
}
2647+
return detectPtrTypeAndMaybeCacheIpc(reinterpret_cast<void *>(data),
2648+
ipcHandleBuf, ipcHandleSize);
2649+
};
2650+
26402651
if (!flagcxParamMrSortedLookup()) {
26412652
/* Legacy: mutex + hash maps */
26422653
std::lock_guard<std::mutex> lock(gMemMutex);
@@ -2666,8 +2677,7 @@ int flagcxP2pEngineReg(FlagcxP2pEngine *engine, uintptr_t data, size_t size,
26662677
entry.ibDevN = ibDevN;
26672678

26682679
setEngineDevice(engine);
2669-
entry.ptrType = detectPtrTypeAndMaybeCacheIpc(
2670-
reinterpret_cast<void *>(data), entry.ipcHandle, &entry.ipcHandleSize);
2680+
entry.ptrType = resolvePtrType(entry.ipcHandle, &entry.ipcHandleSize);
26712681
entry.hasIpc = entry.ptrType == FLAGCX_PTR_CUDA && entry.ipcHandleSize > 0;
26722682

26732683
if (engine->adaptor->regMr(&devCtx, reinterpret_cast<void *>(data), size,
@@ -2721,8 +2731,7 @@ int flagcxP2pEngineReg(FlagcxP2pEngine *engine, uintptr_t data, size_t size,
27212731
memset(ipcHandle, 0, sizeof(ipcHandle));
27222732

27232733
setEngineDevice(engine);
2724-
int ptrType = detectPtrTypeAndMaybeCacheIpc(reinterpret_cast<void *>(data),
2725-
ipcHandle, &ipcHandleSize);
2734+
int ptrType = resolvePtrType(ipcHandle, &ipcHandleSize);
27262735
bool hasIpc = ptrType == FLAGCX_PTR_CUDA && ipcHandleSize > 0;
27272736

27282737
/* Register with adaptor */
@@ -2766,6 +2775,11 @@ int flagcxP2pEngineReg(FlagcxP2pEngine *engine, uintptr_t data, size_t size,
27662775
return 0;
27672776
}
27682777

2778+
int flagcxP2pEngineReg(FlagcxP2pEngine *engine, uintptr_t data, size_t size,
2779+
FlagcxP2pMr &mrId) {
2780+
return flagcxP2pEngineRegEx(engine, data, size, 0, mrId);
2781+
}
2782+
27692783
void flagcxP2pEngineMrDestroy(FlagcxP2pEngine *engine, FlagcxP2pMr mr) {
27702784
if (engine == NULL)
27712785
return;
@@ -3480,6 +3494,21 @@ int flagcxP2pRpcRegister(void *engine, uint64_t addr, uint64_t size,
34803494
return 0;
34813495
}
34823496

3497+
int flagcxP2pRpcRegisterHost(void *engine, uint64_t addr, uint64_t size,
3498+
uint64_t *mrIdOut) {
3499+
if (mrIdOut == NULL)
3500+
return -1;
3501+
FlagcxP2pMr mrId = 0;
3502+
const int rc = flagcxP2pEngineRegEx(
3503+
reinterpret_cast<FlagcxP2pEngine *>(engine),
3504+
static_cast<uintptr_t>(addr), static_cast<size_t>(size),
3505+
FLAGCX_PTR_HOST, mrId);
3506+
if (rc != 0)
3507+
return rc;
3508+
*mrIdOut = mrId;
3509+
return 0;
3510+
}
3511+
34833512
void *flagcxP2pRpcGetConn(void *engine, const char *session) {
34843513
return reinterpret_cast<void *>(flagcxP2pEngineGetConn(
34853514
reinterpret_cast<FlagcxP2pEngine *>(engine), session));

flagcx/include/flagcx_p2p.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,12 @@ int flagcxP2pRpcStartServer(void *engine);
618618
int flagcxP2pRpcRegister(void *engine, uint64_t addr, uint64_t size,
619619
uint64_t *mrIdOut);
620620

621+
/* Same interface as flagcxP2pRpcRegister, but on chips/adaptors whose runtime
622+
cannot reset the current error state, this host entry avoids producing the
623+
error at all. */
624+
int flagcxP2pRpcRegisterHost(void *engine, uint64_t addr, uint64_t size,
625+
uint64_t *mrIdOut);
626+
621627
/* Get (or lazily establish) a cached connection to "host:port". */
622628
void *flagcxP2pRpcGetConn(void *engine, const char *session);
623629

plugin/interservice/flagcx_wrapper.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,10 @@ class FLAGCXLibrary:
419419
flagcxP2pEngine_t, ctypes.c_uint64, ctypes.c_uint64,
420420
ctypes.POINTER(ctypes.c_uint64)
421421
]),
422+
Function("flagcxP2pRpcRegisterHost", ctypes.c_int, [
423+
flagcxP2pEngine_t, ctypes.c_uint64, ctypes.c_uint64,
424+
ctypes.POINTER(ctypes.c_uint64)
425+
]),
422426
Function("flagcxP2pRpcGetConn", flagcxP2pConn_t, [
423427
flagcxP2pEngine_t, ctypes.c_char_p
424428
]),
@@ -447,9 +451,12 @@ def _find_default_library() -> str:
447451
so_path = os.path.join(flagcx_path, "lib", "libflagcx.so")
448452
if os.path.isfile(so_path):
449453
return so_path
454+
build_so_path = os.path.join(flagcx_path, "build", "lib", "libflagcx.so")
455+
if os.path.isfile(build_so_path):
456+
return build_so_path
450457
raise FileNotFoundError(
451-
f"FLAGCX_PATH is set to '{flagcx_path}' but "
452-
f"'{so_path}' does not exist. "
458+
f"FLAGCX_PATH is set to '{flagcx_path}' but neither "
459+
f"'{so_path}' nor '{build_so_path}' exists. "
453460
f"Please build FlagCX or check FLAGCX_PATH."
454461
)
455462
# 2. Fall back to <repo_root>/build/lib/libflagcx.so
@@ -818,6 +825,17 @@ def flagcxP2pRegister(self, engine: flagcxP2pEngine_t,
818825
f"flagcxP2pRegister failed (addr={hex(addr)}, size={size})")
819826
return mr_id.value
820827

828+
def flagcxP2pRegisterHost(self, engine: flagcxP2pEngine_t,
829+
addr: int, size: int) -> int:
830+
mr_id = ctypes.c_uint64(0)
831+
rc = self._funcs["flagcxP2pRpcRegisterHost"](
832+
engine, ctypes.c_uint64(addr), ctypes.c_uint64(size),
833+
ctypes.byref(mr_id))
834+
if rc != 0:
835+
raise RuntimeError(
836+
f"flagcxP2pRegisterHost failed (addr={hex(addr)}, size={size})")
837+
return mr_id.value
838+
821839
def flagcxP2pGetConn(self, engine: flagcxP2pEngine_t,
822840
session: str) -> flagcxP2pConn_t:
823841
conn = self._funcs["flagcxP2pRpcGetConn"](

0 commit comments

Comments
 (0)