|
| 1 | +// SPDX-License-Identifier: LGPL-3.0-or-later |
| 2 | +#pragma once |
| 3 | + |
| 4 | +#ifdef BUILD_PYTORCH |
| 5 | +#include <torch/torch.h> |
| 6 | + |
| 7 | +#include <cstdint> |
| 8 | +#include <vector> |
| 9 | + |
| 10 | +#include "common.h" |
| 11 | +#include "neighbor_list.h" |
| 12 | + |
| 13 | +namespace deepmd { |
| 14 | + |
| 15 | +/** |
| 16 | + * @brief Build comm_dict tensors from sendlist/sendnum/recvnum buffers. |
| 17 | + * |
| 18 | + * This is the shared tensor-building logic for all PyTorch backends |
| 19 | + * (DeepPotPT, DeepSpinPT). Backend-specific entries (e.g. has_spin) |
| 20 | + * should be added by the caller after this function returns. |
| 21 | + * |
| 22 | + * @param[out] comm_dict The communication dictionary to populate. |
| 23 | + * @param[in] lmp_list The LAMMPS neighbor list (for sendproc/recvproc/world). |
| 24 | + * @param[in] sendlist Pointer array (int**) for each swap's send list. |
| 25 | + * @param[in] sendnum Number of send atoms per swap. |
| 26 | + * @param[in] recvnum Number of recv atoms per swap. |
| 27 | + */ |
| 28 | +inline void build_comm_dict(torch::Dict<std::string, torch::Tensor>& comm_dict, |
| 29 | + const InputNlist& lmp_list, |
| 30 | + int** sendlist, |
| 31 | + int* sendnum, |
| 32 | + int* recvnum) { |
| 33 | + int nswap = lmp_list.nswap; |
| 34 | + auto int32_option = |
| 35 | + torch::TensorOptions().device(torch::kCPU).dtype(torch::kInt32); |
| 36 | + auto int64_option = |
| 37 | + torch::TensorOptions().device(torch::kCPU).dtype(torch::kInt64); |
| 38 | + |
| 39 | + torch::Tensor sendlist_tensor = |
| 40 | + torch::from_blob(static_cast<void*>(sendlist), {nswap}, int64_option); |
| 41 | + torch::Tensor sendnum_tensor = |
| 42 | + torch::from_blob(sendnum, {nswap}, int32_option); |
| 43 | + torch::Tensor recvnum_tensor = |
| 44 | + torch::from_blob(recvnum, {nswap}, int32_option); |
| 45 | + torch::Tensor sendproc_tensor = |
| 46 | + torch::from_blob(lmp_list.sendproc, {nswap}, int32_option); |
| 47 | + torch::Tensor recvproc_tensor = |
| 48 | + torch::from_blob(lmp_list.recvproc, {nswap}, int32_option); |
| 49 | + |
| 50 | + torch::Tensor communicator_tensor; |
| 51 | + static std::int64_t null_communicator = 0; |
| 52 | + if (lmp_list.world == nullptr) { |
| 53 | + communicator_tensor = |
| 54 | + torch::from_blob(&null_communicator, {1}, torch::kInt64); |
| 55 | + } else { |
| 56 | + communicator_tensor = |
| 57 | + torch::from_blob(const_cast<void*>(lmp_list.world), {1}, torch::kInt64); |
| 58 | + } |
| 59 | + |
| 60 | + comm_dict.insert_or_assign("send_list", sendlist_tensor); |
| 61 | + comm_dict.insert_or_assign("send_proc", sendproc_tensor); |
| 62 | + comm_dict.insert_or_assign("recv_proc", recvproc_tensor); |
| 63 | + comm_dict.insert_or_assign("send_num", sendnum_tensor); |
| 64 | + comm_dict.insert_or_assign("recv_num", recvnum_tensor); |
| 65 | + comm_dict.insert_or_assign("communicator", communicator_tensor); |
| 66 | +} |
| 67 | + |
| 68 | +/** |
| 69 | + * @brief Build comm_dict with sendlist remapping for virtual (NULL-type) atoms. |
| 70 | + * |
| 71 | + * Calls remap_comm_sendlist() to remap indices through fwd_map, then |
| 72 | + * build_comm_dict() to create tensors. Backend-specific entries (e.g. |
| 73 | + * has_spin) should be added by the caller after this function returns. |
| 74 | + * |
| 75 | + * @param[out] comm_dict The communication dictionary to populate. |
| 76 | + * @param[in] lmp_list The LAMMPS neighbor list containing communication info. |
| 77 | + * @param[in] fwd_map Map from original atom index to real-atom index (-1 for |
| 78 | + * virtual atoms). |
| 79 | + * @param[out] remapped_sendlist Storage for remapped send lists (kept alive for |
| 80 | + * tensor lifetime). |
| 81 | + * @param[out] remapped_sendlist_ptrs Pointer array into remapped_sendlist. |
| 82 | + * @param[out] remapped_sendnum Remapped send counts per swap. |
| 83 | + * @param[out] remapped_recvnum Remapped recv counts per swap. |
| 84 | + */ |
| 85 | +inline void build_comm_dict_with_virtual_atoms( |
| 86 | + torch::Dict<std::string, torch::Tensor>& comm_dict, |
| 87 | + const InputNlist& lmp_list, |
| 88 | + const std::vector<int>& fwd_map, |
| 89 | + std::vector<std::vector<int>>& remapped_sendlist, |
| 90 | + std::vector<int*>& remapped_sendlist_ptrs, |
| 91 | + std::vector<int>& remapped_sendnum, |
| 92 | + std::vector<int>& remapped_recvnum) { |
| 93 | + remap_comm_sendlist(remapped_sendlist, remapped_sendnum, remapped_recvnum, |
| 94 | + lmp_list, fwd_map); |
| 95 | + int nswap = lmp_list.nswap; |
| 96 | + remapped_sendlist_ptrs.resize(nswap); |
| 97 | + for (int s = 0; s < nswap; ++s) { |
| 98 | + remapped_sendlist_ptrs[s] = remapped_sendlist[s].data(); |
| 99 | + } |
| 100 | + build_comm_dict(comm_dict, lmp_list, remapped_sendlist_ptrs.data(), |
| 101 | + remapped_sendnum.data(), remapped_recvnum.data()); |
| 102 | +} |
| 103 | + |
| 104 | +} // namespace deepmd |
| 105 | + |
| 106 | +#endif // BUILD_PYTORCH |
0 commit comments