From 97b4566d4c3357b4911f1b3d9e3dfaf2466ca223 Mon Sep 17 00:00:00 2001
From: Jiakun Yan <jiakunyan1998@gmail.com>
Date: Fri, 16 Feb 2024 22:32:01 -0500
Subject: [PATCH] more refactor

---
 CMakeLists.txt          | 33 ++++++++++++++-------------------
 lci/api/lci.h           | 19 ++++++++++++++-----
 lci/api/lci_config.h.in |  2 --
 lci/runtime/env.c       |  4 ++--
 4 files changed, 30 insertions(+), 28 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 0731158e..e1fb3931 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -80,29 +80,23 @@ if(NOT LCI_WITH_LCT_ONLY)
         "If using the ofi(libfabric) backend, provide a hint for the provider to use"
   )
 
-  find_package(OFI)
   find_package(IBV)
-  find_package(ucx)
-  if(IBV_FOUND AND OFI_FOUND)
-    if(LCI_SERVER STREQUAL "ofi")
-      set(FABRIC OFI)
-    else()
-      set(FABRIC IBV)
-    endif()
+  find_package(OFI)
+  find_package(UCX)
+  string(TOUPPER ${LCI_SERVER} LCI_SERVER_UPPER)
+  if(${LCI_SERVER_UPPER}_FOUND)
+    # If the user-specified server are found, just use it.
+    set(FABRIC ${LCI_SERVER_UPPER})
   elseif(IBV_FOUND)
     set(FABRIC IBV)
   elseif(OFI_FOUND)
     set(FABRIC OFI)
+  elseif(UCX_FOUND)
+    set(FABRIC UCX)
   else()
-    message(FATAL_ERROR "Find neither libfabric nor libibverbs. Give up!")
+    message(FATAL_ERROR "Cannot find any servers. Give up!")
   endif()
-  if(LCI_SERVER STREQUAL "ucx")
-    if(NOT ucx_FOUND)
-      message(FATAL_ERROR "ucx is chosen as network backend but not found!")
-    endif()
-    set(FABRIC ucx)
-  endif()
-  string(TOUPPER ${LCI_SERVER} LCI_SERVER_UPPER)
+
   if(LCI_FORCE_SERVER AND NOT LCI_SERVER_UPPER STREQUAL FABRIC)
     message(
       FATAL_ERROR
@@ -269,10 +263,11 @@ if(NOT LCI_WITH_LCT_ONLY)
                C_STANDARD 11
                C_EXTENSIONS ON)
   target_compile_definitions(LCI PRIVATE _GNU_SOURCE)
-  if(FABRIC STREQUAL ucx)
-    target_link_libraries(LCI PUBLIC Threads::Threads ${FABRIC}::ucp LCT)
+  target_link_libraries(LCI PUBLIC Threads::Threads LCT)
+  if(FABRIC STREQUAL UCX)
+    target_link_libraries(LCI PUBLIC ucx::ucp)
   else()
-    target_link_libraries(LCI PUBLIC Threads::Threads ${FABRIC}::${FABRIC} LCT)
+    target_link_libraries(LCI PUBLIC ${FABRIC}::${FABRIC})
   endif()
   if(LCI_USE_AVX)
     target_compile_options(LCI PUBLIC -mavx)
diff --git a/lci/api/lci.h b/lci/api/lci.h
index ff8e4307..acf0e7b2 100644
--- a/lci/api/lci.h
+++ b/lci/api/lci.h
@@ -593,12 +593,25 @@ typedef enum {
 extern LCI_rdv_protocol_t LCI_RDV_PROTOCOL;
 
 /**
- * @ingroup
+ * @ingroup LCI_COMM
  * @brief For the libfabric cxi provider, Try turning off the hacking to see
  * whether cxi has fixed the double mr_bind error.
  */
 extern bool LCI_OFI_CXI_TRY_NO_HACK;
 
+/**
+ * @ingroup LCI_COMM
+ * @brief For the UCX backend, use try_lock to wrap the ucx function calls.
+ */
+extern bool LCI_UCX_USE_TRY_LOCK;
+
+/**
+ * @ingroup LCI_COMM
+ * @brief For the UCX backend, use blocking lock to wrap the ucx_progress
+ * function calls.
+ */
+extern bool LCI_UCX_PROGRESS_FOCUSED;
+
 /**
  * @ingroup LCI_COMM
  * @brief Try_lock mode of network backend.
@@ -629,10 +642,6 @@ extern LCI_endpoint_t LCI_UR_ENDPOINT;
  */
 extern LCI_comp_t LCI_UR_CQ;
 
-extern bool LCI_UCX_USE_TRY_LOCK;
-
-extern bool LCI_UCX_PROGRESS_FOCUSED;
-
 /**
  * @ingroup LCI_SETUP
  * @brief Initialize the LCI runtime. No LCI calls are allowed to be called
diff --git a/lci/api/lci_config.h.in b/lci/api/lci_config.h.in
index 5e43665f..6aef269a 100644
--- a/lci/api/lci_config.h.in
+++ b/lci/api/lci_config.h.in
@@ -28,8 +28,6 @@
 #cmakedefine LCI_ENABLE_SLOWDOWN
 #cmakedefine LCI_USE_PAPI
 #cmakedefine01 LCI_USE_DREG_DEFAULT
-
-#cmakedefine LCI_UCX_NO_PROGRESS_THREAD
 #cmakedefine LCI_UCX_USE_SEGMENTED_PUT
 
 #define LCI_PACKET_SIZE_DEFAULT @LCI_PACKET_SIZE_DEFAULT@
diff --git a/lci/runtime/env.c b/lci/runtime/env.c
index a1f18005..bd6970f6 100644
--- a/lci/runtime/env.c
+++ b/lci/runtime/env.c
@@ -29,11 +29,11 @@ LCI_API bool LCI_ENABLE_PRG_NET_ENDPOINT;
 LCI_API LCI_rdv_protocol_t LCI_RDV_PROTOCOL;
 LCI_API bool LCI_OFI_CXI_TRY_NO_HACK;
 LCI_API uint64_t LCI_BACKEND_TRY_LOCK_MODE;
+LCI_API bool LCI_UCX_USE_TRY_LOCK;
+LCI_API bool LCI_UCX_PROGRESS_FOCUSED;
 LCI_API LCI_device_t LCI_UR_DEVICE;
 LCI_API LCI_endpoint_t LCI_UR_ENDPOINT;
 LCI_API LCI_comp_t LCI_UR_CQ;
-LCI_API bool LCI_UCX_USE_TRY_LOCK;
-LCI_API bool LCI_UCX_PROGRESS_FOCUSED;
 
 void LCII_env_init_cq_type();