MP-Gadget  5.0.1.dev1-76bc7d4726-dirty
test_slotsmanager.c
Go to the documentation of this file.
1 #include <stdarg.h>
2 #include <stddef.h>
3 #include <setjmp.h>
4 #include <cmocka.h>
5 #include <math.h>
6 #include <mpi.h>
7 #include <stdio.h>
8 #include <string.h>
9 #include <time.h>
10 #include <gsl/gsl_rng.h>
11 
12 #include "stub.h"
13 
14 #include <libgadget/partmanager.h>
15 #include <libgadget/domain.h>
16 #include <libgadget/slotsmanager.h>
17 
18 struct part_manager_type PartManager[1] = {{0}};
19 
20 static int
21 setup_particles(void ** state)
22 {
23  PartManager->MaxPart = 1024;
24  PartManager->NumPart = 128 * 6;
25 
26  int64_t newSlots[6] = {128, 128, 128, 128, 128, 128};
27 
28  PartManager->Base = (struct particle_data *) mymalloc("P", PartManager->MaxPart* sizeof(struct particle_data));
29  memset(PartManager->Base, 0, sizeof(struct particle_data) * PartManager->MaxPart);
30 
32  int ptype;
36  for(ptype = 1; ptype < 4; ptype++) {
38  }
39 
40  slots_reserve(1, newSlots, SlotsManager);
41 
43 
44  int64_t i;
45  #pragma omp parallel for
46  for(i = 0; i < PartManager->NumPart; i ++) {
47  P[i].ID = i;
48  }
49 
51 
52  return 0;
53 }
54 
55 static int
56 teardown_particles(void **state)
57 {
60  return 0;
61 }
62 
63 static void
64 test_slots_gc(void **state)
65 {
66  setup_particles(state);
67  int i;
68  int compact[6];
69  for(i = 0; i < 6; i ++) {
71  compact[i] = 1;
72  }
74  assert_int_equal(PartManager->NumPart, 127 * i);
75 
76  assert_int_equal(SlotsManager->info[0].size, 127);
77  assert_int_equal(SlotsManager->info[4].size, 127);
78  assert_int_equal(SlotsManager->info[5].size, 127);
79 
81  teardown_particles(state);
82  return;
83 }
84 
85 static void
86 test_slots_gc_sorted(void **state)
87 {
88  setup_particles(state);
89  int i;
90  for(i = 0; i < 6; i ++) {
92  }
94  assert_int_equal(PartManager->NumPart, 127 * i);
95 
96  assert_int_equal(SlotsManager->info[0].size, 127);
97  assert_int_equal(SlotsManager->info[4].size, 127);
98  assert_int_equal(SlotsManager->info[5].size, 127);
99 
101  teardown_particles(state);
102  return;
103 }
104 
105 static void
106 test_slots_reserve(void **state)
107 {
108  /* FIXME: these depends on the magic numbers in slots_reserve. After
109  * moving those numbers to All.* we shall rework the code here. */
110  setup_particles(state);
111 
112  int64_t newSlots[6] = {128, 128, 128, 128, 128, 128};
113  int64_t oldSize[6];
114  int ptype;
115  for(ptype = 0; ptype < 6; ptype++) {
116  oldSize[ptype] = SlotsManager->info[ptype].maxsize;
117  }
118  slots_reserve(1, newSlots, SlotsManager);
119 
120  /* shall not increase max size*/
121  for(ptype = 0; ptype < 6; ptype++) {
122  assert_int_equal(oldSize[ptype], SlotsManager->info[ptype].maxsize);
123  }
124 
125  for(ptype = 0; ptype < 6; ptype++) {
126  newSlots[ptype] += 1;
127  }
128 
129  /* shall not increase max size; because it is small difference */
130  slots_reserve(1, newSlots, SlotsManager);
131  for(ptype = 0; ptype < 6; ptype++) {
132  assert_int_equal(oldSize[ptype], SlotsManager->info[ptype].maxsize);
133  }
134 
135  for(ptype = 0; ptype < 6; ptype++) {
136  newSlots[ptype] += 8192;
137  }
138 
139  /* shall increase max size; because it large difference */
140  slots_reserve(1, newSlots, SlotsManager);
141 
142  for(ptype = 0; ptype < 6; ptype++) {
143  assert_true(oldSize[ptype] < SlotsManager->info[ptype].maxsize);
144  }
145 
146 }
147 
148 /*Check that we behave correctly when the slot is empty*/
149 static void
150 test_slots_zero(void **state)
151 {
152  setup_particles(state);
153  int i;
154  int compact[6] = {1,0,0,0,1,1};
155  for(i = 0; i < PartManager->NumPart; i ++) {
157  }
158  slots_gc(compact, PartManager, SlotsManager);
159  assert_int_equal(PartManager->NumPart, 0);
160  assert_int_equal(SlotsManager->info[0].size, 0);
161  assert_int_equal(SlotsManager->info[1].size, 128);
162  assert_int_equal(SlotsManager->info[4].size, 0);
163  assert_int_equal(SlotsManager->info[5].size, 0);
164 
165  teardown_particles(state);
166 
167  setup_particles(state);
168  for(i = 0; i < PartManager->NumPart; i ++) {
170  }
172  assert_int_equal(PartManager->NumPart, 0);
173  assert_int_equal(SlotsManager->info[0].size, 0);
174  assert_int_equal(SlotsManager->info[4].size, 0);
175  assert_int_equal(SlotsManager->info[5].size, 0);
176 
177  teardown_particles(state);
178 
179  return;
180 
181 }
182 
183 static void
184 test_slots_fork(void **state)
185 {
186  setup_particles(state);
187  int i;
188  for(i = 0; i < 6; i ++) {
189  slots_split_particle(128 * i, 0, PartManager);
190  slots_convert(128 * i, P[i * 128].Type, -1, PartManager, SlotsManager);
191 
192  }
193 
194  assert_int_equal(PartManager->NumPart, 129 * i);
195 
196  assert_int_equal(SlotsManager->info[0].size, 129);
197  assert_int_equal(SlotsManager->info[4].size, 129);
198  assert_int_equal(SlotsManager->info[5].size, 129);
199 
200  teardown_particles(state);
201  return;
202 }
203 
204 static void
205 test_slots_convert(void **state)
206 {
207  setup_particles(state);
208  int i;
209  for(i = 0; i < 6; i ++) {
210  slots_convert(128 * i, P[i * 128].Type, -1, PartManager, SlotsManager);
211  }
212 
213  assert_int_equal(PartManager->NumPart, 128 * i);
214 
215  assert_int_equal(SlotsManager->info[0].size, 129);
216  assert_int_equal(SlotsManager->info[4].size, 129);
217  assert_int_equal(SlotsManager->info[5].size, 129);
218 
219  teardown_particles(state);
220  return;
221 }
222 
223 int main(void) {
224  const struct CMUnitTest tests[] = {
225  cmocka_unit_test(test_slots_gc),
226  cmocka_unit_test(test_slots_gc_sorted),
227  cmocka_unit_test(test_slots_reserve),
228  cmocka_unit_test(test_slots_fork),
229  cmocka_unit_test(test_slots_convert),
230  cmocka_unit_test(test_slots_zero),
231  };
232  return cmocka_run_group_tests_mpi(tests, NULL, NULL);
233 }
#define mymalloc(name, size)
Definition: mymalloc.h:15
#define myfree(x)
Definition: mymalloc.h:19
#define P
Definition: partmanager.h:88
void slots_setup_id(const struct part_manager_type *pman, struct slots_manager_type *sman)
Definition: slotsmanager.c:653
size_t slots_reserve(int where, int64_t atleast[6], struct slots_manager_type *sman)
Definition: slotsmanager.c:475
void slots_set_enabled(int ptype, size_t elsize, struct slots_manager_type *sman)
Definition: slotsmanager.c:560
void slots_mark_garbage(int i, struct part_manager_type *pman, struct slots_manager_type *sman)
Definition: slotsmanager.c:577
void slots_free(struct slots_manager_type *sman)
Definition: slotsmanager.c:570
void slots_setup_topology(struct part_manager_type *pman, int64_t *NLocal, struct slots_manager_type *sman)
Definition: slotsmanager.c:624
int slots_gc(int *compact_slots, struct part_manager_type *pman, struct slots_manager_type *sman)
Definition: slotsmanager.c:145
void slots_check_id_consistency(struct part_manager_type *pman, struct slots_manager_type *sman)
Definition: slotsmanager.c:587
struct slots_manager_type SlotsManager[1]
Definition: slotsmanager.c:7
void slots_gc_sorted(struct part_manager_type *pman, struct slots_manager_type *sman)
Definition: slotsmanager.c:442
void slots_init(double increase, struct slots_manager_type *sman)
Definition: slotsmanager.c:550
int slots_convert(int parent, int ptype, int placement, struct part_manager_type *pman, struct slots_manager_type *sman)
Definition: slotsmanager.c:60
int slots_split_particle(int parent, double childmass, struct part_manager_type *pman)
Definition: slotsmanager.c:103
struct particle_data * Base
Definition: partmanager.h:74
unsigned int Type
Definition: partmanager.h:17
int64_t maxsize
Definition: slotsmanager.h:11
int64_t size
Definition: slotsmanager.h:12
struct slot_info info[6]
Definition: slotsmanager.h:112
static void test_slots_convert(void **state)
static void test_slots_zero(void **state)
static int setup_particles(void **state)
static void test_slots_fork(void **state)
struct part_manager_type PartManager[1]
static int teardown_particles(void **state)
int main(void)
static void test_slots_gc(void **state)
static void test_slots_gc_sorted(void **state)
static void test_slots_reserve(void **state)
static enum TransferType ptype
Definition: zeldovich.c:146