qlat_utils.cutils — Low-Level Filesystem Utilities¶
Source: qlat-utils/qlat_utils/cutils.pyx
Note: Update this document when updating the source file.
Outline¶
Overview¶
cutils exposes the C++ filesystem layer of qlat-utils to Python. It provides:
Path manipulation —
basename,dirname,all_dirname_vec,remove_trailing_slashesDirectory listing —
qls,qls_all(local) andqls_sync_node,qls_all_sync_node(MPI-synchronized)File/directory queries —
does_file_exist,is_directory,is_regular_fileFile/directory operations —
qmkdir,qmkdir_p,qrename,qremove,qremove_all,qtruncateCached variants —
_cache-suffixed functions that memoize results for repeated queriesInfo-logged variants —
_info-suffixed functions that log operations viadisplayln_infoMPI-synchronized variants —
_sync_node-suffixed functions that ensure all MPI ranks agree on filesystem stateCache management —
clear_all_caches,clear_mem_cache,get_all_caches_info
Python access:
import qlat_utils as q
Path Manipulation¶
Function |
Description |
|---|---|
|
Return the filename portion of path |
|
Return the directory portion of path |
|
Return a list of all ancestor directories of |
|
Return |
Directory Listing¶
qls(path, is_sort=True) -> list[str]¶
List the immediate contents of directory path (returned as full paths). If is_sort is True, results are sorted alphabetically.
qls_all(path, is_folder_before_files=False, is_sort=True) -> list[str]¶
Recursively list all entries under path (returned as full paths). If is_folder_before_files is True, directories appear before their contents. If is_sort is True, entries are sorted within each directory level.
File and Directory Queries¶
Function |
Return Type |
Description |
|---|---|---|
|
|
|
|
|
|
|
|
|
File and Directory Operations¶
Function |
Description |
|---|---|
|
Create a single directory. Fails if parent does not exist. |
|
Create directory and all missing parents (like |
|
Rename/move |
|
Remove a single file or empty directory. |
|
Recursively remove |
|
Truncate file at |
Cached Queries¶
These functions memoize their results. Use clear_is_directory_cache() or remove_entry_directory_cache(path) to invalidate stale entries.
Function |
Description |
|---|---|
|
Cached version of |
|
Cached version of |
|
Cached version of |
|
Clear the entire directory-existence cache. |
|
Remove a single entry from the cache. |
Info-Logged Operations¶
These are identical to their non-_info counterparts but log each operation via displayln_info, which is useful for debugging MPI runs.
Function |
Description |
|---|---|
|
|
|
|
|
|
|
|
|
|
MPI-Synchronized Operations¶
In an MPI environment, all ranks must agree on the filesystem state before proceeding. These _sync_node functions perform a barrier and broadcast so that every rank sees the same result.
Function |
Description |
|---|---|
|
MPI-synchronized |
|
MPI-synchronized |
|
MPI-synchronized |
|
MPI-synchronized |
|
MPI-synchronized |
|
MPI-synchronized |
|
MPI-synchronized |
|
MPI-synchronized |
|
MPI-synchronized |
Cache Management¶
Function |
Description |
|---|---|
|
Return a list of strings describing all internal caches and their sizes. |
|
Clear all internal caches (directory, mem, etc.). |
|
Clear only the memory buffer cache. |
Diagnostics¶
displayln_malloc_stats()¶
Print glibc malloc_stats() output to stdout. Useful for tracking memory allocation in long-running jobs.
get_eigen_type() -> str¶
Return the name of the Eigen backend type used by the C++ layer (e.g., "Eigen" or "Grid").
Examples¶
Path Manipulation¶
import qlat_utils as q
q.basename("/home/user/data/config.lime") # "config.lime"
q.dirname("/home/user/data/config.lime") # "/home/user/data"
q.all_dirname_vec("/a/b/c/d") # ["/a/b/c", "/a/b", "/a", "/"]
q.remove_trailing_slashes("/home/user/") # "/home/user"
Listing and Querying¶
import qlat_utils as q
entries = q.qls("/tmp/data")
print(entries)
if q.is_directory("/tmp/data/run-1"):
print("exists")
if not q.does_file_exist("/tmp/data/output.bin"):
print("not found")
Creating and Removing Directories¶
import qlat_utils as q
q.qmkdir_p("/tmp/data/run-1/gauge")
# ... do work ...
q.qremove_all("/tmp/data/run-1")
MPI-Synchronized File Operations¶
import qlat_utils as q
# All MPI ranks agree on whether the directory exists
if not q.is_directory_sync_node("/shared/output"):
q.qmkdir_p_sync_node("/shared/output")
Using Caches¶
import qlat_utils as q
# Repeated queries hit the cache
for i in range(1000):
if q.is_directory_cache("/tmp/data"):
pass # fast after first call
# Invalidate when filesystem changes
q.clear_is_directory_cache()
Diagnostics¶
import qlat_utils as q
info = q.get_all_caches_info()
for line in info:
print(line)
q.displayln_malloc_stats()
print(q.get_eigen_type())