from __future__ import annotations
import sys
from mpi4py import MPI
from readcon_db import ConCorpus, bcast_packed_frame, bcast_packed_frames
def main(argv: list[str]) -> int:
comm = MPI.COMM_WORLD.Dup()
try:
rank = comm.Get_rank()
if len(argv) < 2:
if rank == 0:
print(
f"usage: {argv[0]} <corpus_dir> [traj] [frame]",
file=sys.stderr,
)
return 1
corpus = argv[1]
traj = int(argv[2]) if len(argv) > 2 else 1
frame = int(argv[3]) if len(argv) > 3 else 0
blob = bcast_packed_frame(comm, corpus, traj, frame, root=0)
batch = bcast_packed_frames(comm, corpus, [(traj, frame)], root=0)
xyz = ConCorpus.unpack_positions(blob)
_ = ConCorpus.unpack_batch(batch)
if rank == 0:
x0, y0, z0 = xyz[0]
print(
f"bcast {len(blob)} bytes, natoms={len(xyz)} "
f"xyz0=({x0:.4f},{y0:.4f},{z0:.4f})"
)
return 0
finally:
comm.Free()
if __name__ == "__main__":
sys.exit(main(sys.argv))