File: test_library_mpi.cpp

package info (click to toggle)
lammps 20250204%2Bdfsg.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 474,368 kB
  • sloc: cpp: 1,060,070; python: 27,785; ansic: 8,956; f90: 7,254; sh: 6,044; perl: 4,171; fortran: 2,442; xml: 1,714; makefile: 1,352; objc: 238; lisp: 188; yacc: 58; csh: 16; awk: 14; tcl: 6; javascript: 2
file content (336 lines) | stat: -rw-r--r-- 10,444 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// unit tests for checking LAMMPS configuration settings  through the library interface

#define LAMMPS_LIB_MPI 1
#include "lammps.h"
#include "library.h"
#include "timer.h"
#include <string>

#include "gmock/gmock.h"
#include "gtest/gtest.h"

#include "../testing/test_mpi_main.h"

using ::testing::ExitedWithCode;
using ::testing::HasSubstr;
using ::testing::StartsWith;
using ::testing::StrEq;

TEST(MPI, global_box)
{
    int nprocs, me;
    MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
    MPI_Comm_rank(MPI_COMM_WORLD, &me);
    EXPECT_EQ(nprocs, 4);
    EXPECT_GT(me, -1);
    EXPECT_LT(me, 5);

    double boxlo[3];
    double boxhi[3];
    double xy = 0.0;
    double yz = 0.0;
    double xz = 0.0;
    int pflags[3];
    int boxflag;

    ::testing::internal::CaptureStdout();
    const char *args[] = {"LAMMPS_test", "-log", "none", "-echo", "screen", "-nocite", nullptr};
    char **argv        = (char **)args;
    int argc           = (sizeof(args) / sizeof(char *)) - 1;
    void *lmp          = lammps_open(argc, argv, MPI_COMM_WORLD, nullptr);
    lammps_command(lmp, "units           lj");
    lammps_command(lmp, "atom_style      atomic");
    lammps_command(lmp, "region          box block 0 2 0 2 0 2");
    lammps_command(lmp, "create_box      1 box");

    lammps_extract_box(lmp, boxlo, boxhi, &xy, &yz, &xz, pflags, &boxflag);
    ::testing::internal::GetCapturedStdout();

    EXPECT_EQ(boxlo[0], 0.0);
    EXPECT_EQ(boxlo[1], 0.0);
    EXPECT_EQ(boxlo[2], 0.0);

    EXPECT_EQ(boxhi[0], 2.0);
    EXPECT_EQ(boxhi[1], 2.0);
    EXPECT_EQ(boxhi[2], 2.0);

    ::testing::internal::CaptureStdout();
    lammps_close(lmp);
    ::testing::internal::GetCapturedStdout();
};

TEST(MPI, sub_box)
{
    int nprocs, me;
    MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
    MPI_Comm_rank(MPI_COMM_WORLD, &me);
    EXPECT_EQ(nprocs, 4);
    EXPECT_GT(me, -1);
    EXPECT_LT(me, 5);

    double boxlo[3];
    double boxhi[3];
    double xy = 0.0;
    double yz = 0.0;
    double xz = 0.0;
    int pflags[3];
    int boxflag;

    ::testing::internal::CaptureStdout();
    const char *args[] = {"LAMMPS_test", "-log", "none", "-echo", "screen", "-nocite", nullptr};
    char **argv        = (char **)args;
    int argc           = (sizeof(args) / sizeof(char *)) - 1;
    void *lmp          = lammps_open(argc, argv, MPI_COMM_WORLD, nullptr);
    lammps_command(lmp, "units           lj");
    lammps_command(lmp, "atom_style      atomic");
    lammps_command(lmp, "region          box block 0 2 0 2 0 2");
    lammps_command(lmp, "create_box      1 box");

    lammps_extract_box(lmp, boxlo, boxhi, &xy, &yz, &xz, pflags, &boxflag);
    ::testing::internal::GetCapturedStdout();

    EXPECT_EQ(boxlo[0], 0.0);
    EXPECT_EQ(boxlo[1], 0.0);
    EXPECT_EQ(boxlo[2], 0.0);

    EXPECT_EQ(boxhi[0], 2.0);
    EXPECT_EQ(boxhi[1], 2.0);
    EXPECT_EQ(boxhi[2], 2.0);

    auto *sublo = (double *)lammps_extract_global(lmp, "sublo");
    auto *subhi = (double *)lammps_extract_global(lmp, "subhi");

    ASSERT_NE(sublo, nullptr);
    ASSERT_NE(subhi, nullptr);

    EXPECT_GE(sublo[0], boxlo[0]);
    EXPECT_GE(sublo[1], boxlo[1]);
    EXPECT_GE(sublo[2], boxlo[2]);
    EXPECT_LE(subhi[0], boxhi[0]);
    EXPECT_LE(subhi[1], boxhi[1]);
    EXPECT_LE(subhi[2], boxhi[2]);

    ::testing::internal::CaptureStdout();
    lammps_command(lmp, "change_box all triclinic");
    ::testing::internal::GetCapturedStdout();

    sublo = (double *)lammps_extract_global(lmp, "sublo_lambda");
    subhi = (double *)lammps_extract_global(lmp, "subhi_lambda");
    ASSERT_NE(sublo, nullptr);
    ASSERT_NE(subhi, nullptr);

    EXPECT_GE(sublo[0], 0.0);
    EXPECT_GE(sublo[1], 0.0);
    EXPECT_GE(sublo[2], 0.0);
    EXPECT_LE(subhi[0], 1.0);
    EXPECT_LE(subhi[1], 1.0);
    EXPECT_LE(subhi[2], 1.0);

    ::testing::internal::CaptureStdout();
    lammps_close(lmp);
    ::testing::internal::GetCapturedStdout();
};

TEST(MPI, split_comm)
{
    int nprocs, me, color, key;
    MPI_Comm newcomm;
    lammps_mpi_init();
    MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
    MPI_Comm_rank(MPI_COMM_WORLD, &me);
    color = me % 2;
    key   = me;

    MPI_Comm_split(MPI_COMM_WORLD, color, key, &newcomm);

    const char *args[] = {"LAMMPS_test", "-log", "none", "-echo", "screen", "-nocite", nullptr};
    char **argv        = (char **)args;
    int argc           = (sizeof(args) / sizeof(char *)) - 1;
    void *lmp          = lammps_open(argc, argv, newcomm, nullptr);
    lammps_command(lmp, "units           lj");
    lammps_command(lmp, "atom_style      atomic");
    lammps_command(lmp, "region          box block 0 2 0 2 0 2");
    lammps_command(lmp, "create_box      1 box");

    MPI_Comm_size(newcomm, &nprocs);
    MPI_Comm_rank(newcomm, &me);
    EXPECT_EQ(nprocs, 2);
    EXPECT_GT(me, -1);
    EXPECT_LT(me, 2);
    EXPECT_EQ(lammps_extract_setting(lmp, "universe_size"), nprocs);
    EXPECT_EQ(lammps_extract_setting(lmp, "universe_rank"), me);
    EXPECT_EQ(lammps_extract_setting(lmp, "world_size"), nprocs);
    EXPECT_EQ(lammps_extract_setting(lmp, "world_rank"), me);

    lammps_close(lmp);
};

TEST(MPI, multi_partition)
{
    int nprocs, me;
    lammps_mpi_init();
    MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
    MPI_Comm_rank(MPI_COMM_WORLD, &me);

    const char *args[] = {"LAMMPS_test", "-log",    "none", "-partition", "4x1",  "-echo",
                          "screen",      "-nocite", "-in",  "none",       nullptr};
    char **argv        = (char **)args;
    int argc           = (sizeof(args) / sizeof(char *)) - 1;
    void *lmp          = lammps_open(argc, argv, MPI_COMM_WORLD, nullptr);

    lammps_command(lmp, "units           lj");
    lammps_command(lmp, "atom_style      atomic");
    lammps_command(lmp, "region          box block 0 2 0 2 0 2");
    lammps_command(lmp, "create_box      1 box");
    lammps_command(lmp, "variable        partition universe 1 2 3 4");

    EXPECT_EQ(lammps_extract_setting(lmp, "universe_size"), nprocs);
    EXPECT_EQ(lammps_extract_setting(lmp, "universe_rank"), me);
    EXPECT_EQ(lammps_extract_setting(lmp, "world_size"), 1);
    EXPECT_EQ(lammps_extract_setting(lmp, "world_rank"), 0);

    char *part_id = (char *)lammps_extract_variable(lmp, "partition", nullptr);
    ASSERT_THAT(part_id, StrEq(std::to_string(me + 1)));

    lammps_close(lmp);
};

class MPITest : public ::testing::Test {
public:
    void command(const std::string &line) { lammps_command(lmp, line.c_str()); }

protected:
    const char *testbinary = "LAMMPSTest";
    void *lmp;

    void SetUp() override
    {
        const char *args[] = {testbinary, "-log", "none", "-echo", "screen", "-nocite", nullptr};
        char **argv        = (char **)args;
        int argc           = (sizeof(args) / sizeof(char *)) - 1;
        if (!verbose) ::testing::internal::CaptureStdout();
        lmp = lammps_open(argc, argv, MPI_COMM_WORLD, nullptr);
        InitSystem();
        if (!verbose) ::testing::internal::GetCapturedStdout();
    }

    virtual void InitSystem()
    {
        command("units           lj");
        command("atom_style      atomic");
        command("atom_modify     map yes");

        command("lattice         fcc 0.8442");
        command("region          box block 0 2 0 2 0 2");
        command("create_box      1 box");
        command("create_atoms    1 box");
        command("mass            1 1.0");

        command("velocity        all create 3.0 87287");

        command("pair_style      lj/cut 2.5");
        command("pair_coeff      1 1 1.0 1.0 2.5");

        command("neighbor        0.3 bin");
        command("neigh_modify    every 20 delay 0 check no");
    }

    void TearDown() override
    {
        if (!verbose) ::testing::internal::CaptureStdout();
        lammps_close(lmp);
        lmp = nullptr;
        if (!verbose) ::testing::internal::GetCapturedStdout();
    }
};

TEST_F(MPITest, size_rank)
{
    int nprocs, me;
    MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
    MPI_Comm_rank(MPI_COMM_WORLD, &me);

    EXPECT_EQ(nprocs, lammps_extract_setting(lmp, "world_size"));
    EXPECT_EQ(me, lammps_extract_setting(lmp, "world_rank"));
}

#if !defined(LAMMPS_BIGBIG)

TEST_F(MPITest, gather)
{
    auto natoms = (int64_t)lammps_get_natoms(lmp);
    ASSERT_EQ(natoms, 32);
    int *p_nlocal = (int *)lammps_extract_global(lmp, "nlocal");
    int nlocal    = *p_nlocal;
    EXPECT_LT(nlocal, 32);
    EXPECT_EQ(nlocal, 8);

    // get the entire x on all procs
    auto *x = new double[natoms * 3];
    lammps_gather(lmp, (char *)"x", 1, 3, x);

    int *tag       = (int *)lammps_extract_atom(lmp, "id");
    auto **x_local = (double **)lammps_extract_atom(lmp, "x");

    // each proc checks its local atoms
    for (int i = 0; i < nlocal; i++) {
        int64_t j   = tag[i] - 1;
        double *x_i = x_local[i];
        double *x_g = &x[j * 3];
        EXPECT_DOUBLE_EQ(x_g[0], x_i[0]);
        EXPECT_DOUBLE_EQ(x_g[1], x_i[1]);
        EXPECT_DOUBLE_EQ(x_g[2], x_i[2]);
    }

    delete[] x;
}

TEST_F(MPITest, scatter)
{
    int *p_nlocal  = (int *)lammps_extract_global(lmp, "nlocal");
    int nlocal     = *p_nlocal;
    auto *x_orig   = new double[3 * nlocal];
    auto **x_local = (double **)lammps_extract_atom(lmp, "x");

    // make copy of original local x vector
    for (int i = 0; i < nlocal; i++) {
        int j         = 3 * i;
        x_orig[j]     = x_local[i][0];
        x_orig[j + 1] = x_local[i][1];
        x_orig[j + 2] = x_local[i][2];
    }

    // get the entire x on all procs
    auto natoms = (int64_t)lammps_get_natoms(lmp);
    auto *x     = new double[natoms * 3];
    lammps_gather(lmp, (char *)"x", 1, 3, x);

    // shift all coordinates by 0.001
    const double delta = 0.001;
    for (int64_t i = 0; i < 3 * natoms; i++)
        x[i] += delta;

    // update positions of all atoms
    lammps_scatter(lmp, (char *)"x", 1, 3, x);
    delete[] x;
    x = nullptr;

    // get new nlocal and x_local
    p_nlocal = (int *)lammps_extract_global(lmp, "nlocal");
    nlocal   = *p_nlocal;
    x_local  = (double **)lammps_extract_atom(lmp, "x");

    ASSERT_EQ(nlocal, 8);

    // each proc checks its local atoms for shift
    for (int i = 0; i < nlocal; i++) {
        double *x_a = x_local[i];
        double *x_b = &x_orig[i * 3];
        EXPECT_DOUBLE_EQ(x_a[0], x_b[0] + delta);
        EXPECT_DOUBLE_EQ(x_a[1], x_b[1] + delta);
        EXPECT_DOUBLE_EQ(x_a[2], x_b[2] + delta);
    }

    delete[] x_orig;
}
#endif