From 49f8e373ac4ad3fa2c82a322c0db78f6801dd098 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Fri, 14 May 2021 02:18:07 -0400 Subject: [PATCH 1/3] support MPI and other atom_styles for LAMMPS atomic keyword fix problems left in #44 --- doc/getting-started.md | 4 ---- source/lmp/pair_deepmd.cpp | 37 +++++++++++++++++++++++++++++++++---- source/lmp/pair_deepmd.h.in | 3 +++ 3 files changed, 36 insertions(+), 8 deletions(-) diff --git a/doc/getting-started.md b/doc/getting-started.md index fb6dedacd9..ecde2c8d64 100644 --- a/doc/getting-started.md +++ b/doc/getting-started.md @@ -301,10 +301,6 @@ where `Df_i` is the absolute model deviation of the force on atom `i`, `|f_i|` i #### Restrictions - The `deepmd` pair style is provided in the USER-DEEPMD package, which is compiled from the DeePMD-kit, visit the [DeePMD-kit website](https://github.com/deepmodeling/deepmd-kit) for more information. -- The `atom_style` of the system should be `atomic`. - -- When using the `atomic` key word of `deepmd` is set, one should not use this pair style with MPI parallelization. - #### Long-range interaction The reciprocal space part of the long-range interaction can be calculated by LAMMPS command `kspace_style`. To use it with DeePMD-kit, one writes diff --git a/source/lmp/pair_deepmd.cpp b/source/lmp/pair_deepmd.cpp index 1cfa2aba3c..feefbe3077 100644 --- a/source/lmp/pair_deepmd.cpp +++ b/source/lmp/pair_deepmd.cpp @@ -592,6 +592,28 @@ void PairDeepMD::compute(int eflag, int vflag) // // total e // vector sum_e(numb_models, 0.); // MPI_Reduce (&all_energy[0], &sum_e[0], numb_models, MPI_DOUBLE, MPI_SUM, 0, world); + vector std_f_all(all_nlocal); + if (out_each == 1){ + // Gather std_f and tags + tagint *tag = atom->tag; + int nprocs = comm->nprocs; + for (int ii = 0; ii < nlocal; ii++) { + tagsend[ii] = tag[ii]; + stdfsend[ii] = std_f[ii]; + } + MPI_Gather(&nlocal, 1, MPI_INT, counts, 1, MPI_INT, 0, world); + displacements[0] = 0; + for (int ii = 0; ii < nprocs-1; ii++) displacements[ii+1] = displacements[ii] + counts[ii]; + MPI_Gatherv(tagsend, nlocal, MPI_LMP_TAGINT, + tagrecv, counts, displacements, MPI_LMP_TAGINT, 0, world); + MPI_Gatherv(stdfsend, nlocal, MPI_DOUBLE, + stdfrecv, counts, displacements, MPI_DOUBLE, 0, world); + if (rank == 0) { + for (int dd = 0; dd < all_nlocal; ++dd) { + std_f_all[tagrecv[dd]-1] = stdfrecv[dd]; + } + } + } if (rank == 0) { // double avg_e = 0; // deep_pot_model_devi.compute_avg(avg_e, sum_e); @@ -607,11 +629,8 @@ void PairDeepMD::compute(int eflag, int vflag) // << " " << setw(18) << avg_e // << " " << setw(18) << std_e_1 / all_nlocal if (out_each == 1){ - // TODO: Fix two problems: - // 1. If the atom_style is not atomic (e.g. charge), the order of std_f is different from that of atom ids. - // 2. std_f is not gathered by MPI. for (int dd = 0; dd < all_nlocal; ++dd) { - fp << " " << setw(18) << std_f[dd]; + fp << " " << setw(18) << std_f_all[dd]; } } fp << endl; @@ -925,6 +944,16 @@ void PairDeepMD::init_style() neighbor->requests[irequest]->half = 0; // neighbor->requests[irequest]->full = 1; // neighbor->requests[irequest]->newton = 2; + if (out_each == 1){ + int ntotal = atom->natoms; + int nprocs = comm->nprocs; + memory->create(counts, nprocs, "deepmd:counts"); + memory->create(displacements, nprocs, "deepmd:displacements"); + memory->create(stdfsend,ntotal,"deepmd:stdfsendall"); + memory->create(stdfrecv,ntotal,"deepmd:stdfrecvall"); + memory->create(tagsend,ntotal,"deepmd:tagsendall"); + memory->create(tagrecv,ntotal,"deepmd:tagrecvall"); + } } diff --git a/source/lmp/pair_deepmd.h.in b/source/lmp/pair_deepmd.h.in index 7e5206ca9e..2df5a611f1 100644 --- a/source/lmp/pair_deepmd.h.in +++ b/source/lmp/pair_deepmd.h.in @@ -95,6 +95,9 @@ private: ); bool do_ttm; std::string ttm_fix_id; + int *counts,*displacements; + tagint *tagsend, *tagrecv; + double *stdfsend, *stdfrecv; }; } From fb9d0b80d93d3ad4173acfb1af659df811715438 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Fri, 14 May 2021 02:30:29 -0400 Subject: [PATCH 2/3] move out_each codes together --- source/lmp/pair_deepmd.cpp | 40 +++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/source/lmp/pair_deepmd.cpp b/source/lmp/pair_deepmd.cpp index feefbe3077..97ea7e0bbf 100644 --- a/source/lmp/pair_deepmd.cpp +++ b/source/lmp/pair_deepmd.cpp @@ -592,8 +592,23 @@ void PairDeepMD::compute(int eflag, int vflag) // // total e // vector sum_e(numb_models, 0.); // MPI_Reduce (&all_energy[0], &sum_e[0], numb_models, MPI_DOUBLE, MPI_SUM, 0, world); - vector std_f_all(all_nlocal); + if (rank == 0) { + // double avg_e = 0; + // deep_pot_model_devi.compute_avg(avg_e, sum_e); + // double std_e_1 = 0; + // deep_pot_model_devi.compute_std(std_e_1, avg_e, sum_e); + fp << setw(12) << update->ntimestep + << " " << setw(18) << all_v_max + << " " << setw(18) << all_v_min + << " " << setw(18) << all_v_avg + << " " << setw(18) << all_f_max + << " " << setw(18) << all_f_min + << " " << setw(18) << all_f_avg; + // << " " << setw(18) << avg_e + // << " " << setw(18) << std_e_1 / all_nlocal + } if (out_each == 1){ + vector std_f_all(all_nlocal); // Gather std_f and tags tagint *tag = atom->tag; int nprocs = comm->nprocs; @@ -612,27 +627,12 @@ void PairDeepMD::compute(int eflag, int vflag) for (int dd = 0; dd < all_nlocal; ++dd) { std_f_all[tagrecv[dd]-1] = stdfrecv[dd]; } + for (int dd = 0; dd < all_nlocal; ++dd) { + fp << " " << setw(18) << std_f_all[dd]; + } } } - if (rank == 0) { - // double avg_e = 0; - // deep_pot_model_devi.compute_avg(avg_e, sum_e); - // double std_e_1 = 0; - // deep_pot_model_devi.compute_std(std_e_1, avg_e, sum_e); - fp << setw(12) << update->ntimestep - << " " << setw(18) << all_v_max - << " " << setw(18) << all_v_min - << " " << setw(18) << all_v_avg - << " " << setw(18) << all_f_max - << " " << setw(18) << all_f_min - << " " << setw(18) << all_f_avg; - // << " " << setw(18) << avg_e - // << " " << setw(18) << std_e_1 / all_nlocal - if (out_each == 1){ - for (int dd = 0; dd < all_nlocal; ++dd) { - fp << " " << setw(18) << std_f_all[dd]; - } - } + if (rank == 0) { fp << endl; } } From 32cfe998a1fe4a8d810566daab11a65534796e5c Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Fri, 14 May 2021 04:08:20 -0400 Subject: [PATCH 3/3] indent the code --- source/lmp/pair_deepmd.cpp | 54 +++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/source/lmp/pair_deepmd.cpp b/source/lmp/pair_deepmd.cpp index 97ea7e0bbf..b68e0c0bdc 100644 --- a/source/lmp/pair_deepmd.cpp +++ b/source/lmp/pair_deepmd.cpp @@ -606,33 +606,33 @@ void PairDeepMD::compute(int eflag, int vflag) << " " << setw(18) << all_f_avg; // << " " << setw(18) << avg_e // << " " << setw(18) << std_e_1 / all_nlocal - } - if (out_each == 1){ - vector std_f_all(all_nlocal); - // Gather std_f and tags - tagint *tag = atom->tag; - int nprocs = comm->nprocs; - for (int ii = 0; ii < nlocal; ii++) { - tagsend[ii] = tag[ii]; - stdfsend[ii] = std_f[ii]; - } - MPI_Gather(&nlocal, 1, MPI_INT, counts, 1, MPI_INT, 0, world); - displacements[0] = 0; - for (int ii = 0; ii < nprocs-1; ii++) displacements[ii+1] = displacements[ii] + counts[ii]; - MPI_Gatherv(tagsend, nlocal, MPI_LMP_TAGINT, - tagrecv, counts, displacements, MPI_LMP_TAGINT, 0, world); - MPI_Gatherv(stdfsend, nlocal, MPI_DOUBLE, - stdfrecv, counts, displacements, MPI_DOUBLE, 0, world); - if (rank == 0) { - for (int dd = 0; dd < all_nlocal; ++dd) { - std_f_all[tagrecv[dd]-1] = stdfrecv[dd]; - } - for (int dd = 0; dd < all_nlocal; ++dd) { - fp << " " << setw(18) << std_f_all[dd]; - } - } - } - if (rank == 0) { + } + if (out_each == 1){ + vector std_f_all(all_nlocal); + // Gather std_f and tags + tagint *tag = atom->tag; + int nprocs = comm->nprocs; + for (int ii = 0; ii < nlocal; ii++) { + tagsend[ii] = tag[ii]; + stdfsend[ii] = std_f[ii]; + } + MPI_Gather(&nlocal, 1, MPI_INT, counts, 1, MPI_INT, 0, world); + displacements[0] = 0; + for (int ii = 0; ii < nprocs-1; ii++) displacements[ii+1] = displacements[ii] + counts[ii]; + MPI_Gatherv(tagsend, nlocal, MPI_LMP_TAGINT, + tagrecv, counts, displacements, MPI_LMP_TAGINT, 0, world); + MPI_Gatherv(stdfsend, nlocal, MPI_DOUBLE, + stdfrecv, counts, displacements, MPI_DOUBLE, 0, world); + if (rank == 0) { + for (int dd = 0; dd < all_nlocal; ++dd) { + std_f_all[tagrecv[dd]-1] = stdfrecv[dd]; + } + for (int dd = 0; dd < all_nlocal; ++dd) { + fp << " " << setw(18) << std_f_all[dd]; + } + } + } + if (rank == 0) { fp << endl; } }