-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.c
1559 lines (1418 loc) · 50.3 KB
/
client.c
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <inttypes.h>
#include <assert.h>
#include <sys/stat.h>
#include <unistd.h>
#include <time.h>
#include <sys/errno.h>
#include <sys/wait.h>
#include "check_syscalls.h"
#include "particle.h"
#include "rockstar.h"
#include "groupies.h"
#include "inet/rsocket.h"
#include "inet/socket.h"
#include "io/meta_io.h"
#include "io/io_bgc2.h"
#include "bitarray.h"
#include "config_vars.h"
#include "server.h"
#include "client.h"
#include "merger.h"
#include "distance.h"
#include "bounds.h"
#include "fun_times.h"
#include "universe_time.h"
#include "interleaving.h"
#include "config.h"
#define CLIENT_DEBUG 0
extern struct rsocket *rsockets;
struct recipient *recipients = NULL;
int64_t num_recipients = 0;
int64_t *part_ids = NULL;
int64_t *part_id_buffer = NULL;
int64_t num_buffer_part_ids = 0;
struct chunk_info *chunk_info = NULL;
struct projection *prj = NULL;
struct projection_request *prq = NULL;
int64_t num_proj = 0;
int64_t in_error_state = 0;
int64_t RECIPIENT_BUFFER=100000;
FILE *profile_out = NULL;
void network_io_err(int64_t s) {
if (in_error_state) return;
in_error_state = 1;
send_to_socket(s, "err!", 4);
}
void network_error_cleanup() {
p = check_realloc(p, 0, "Freeing particle memory");
num_p = 0;
halos = check_realloc(halos, 0, "Freeing halos");
num_halos = 0;
rockstar_cleanup();
clear_merger_tree();
in_error_state = 0;
}
void reset_projection_count(void) {
prj = check_realloc(prj, sizeof(struct projection)*num_proj,
"Allocating projections.");
prq = check_realloc(prq, sizeof(struct projection_request)*num_proj,
"Allocating projection requests.");
}
struct recipient *add_recipient(int64_t struct_size, int64_t c) {
struct recipient *r;
num_recipients++;
recipients = check_realloc(recipients, (sizeof(struct recipient)
* num_recipients),
"Allocating particle data recipients.");
r = recipients+num_recipients-1;
memset(r, 0, sizeof(struct recipient));
r->buffer = check_realloc(NULL, struct_size*RECIPIENT_BUFFER,
"Allocating recipient transmit buffer.");
recv_from_socket(c, r->bounds, sizeof(float)*6);
recv_from_socket(c, &(r->chunk), sizeof(int64_t));
r->address = recv_msg_nolength(c, r->address);
r->port = recv_msg_nolength(c, r->port);
return r;
}
void clear_recipients(void) {
int64_t i;
for (i=0; i<num_recipients; i++) {
free(recipients[i].buffer);
free(recipients[i].port);
free(recipients[i].address);
}
free(recipients);
recipients = NULL;
num_recipients = 0;
}
void calc_particle_bounds(float *bounds) {
int64_t i,j;
for (j=0; j<6; j++) bounds[j]=0;
if (!num_p) return;
memcpy(bounds, p[0].pos, sizeof(float)*3);
memcpy(bounds+3, p[0].pos, sizeof(float)*3);
for (i=1; i<num_p; i++) {
for (j=0; j<3; j++) {
if (bounds[j] > p[i].pos[j])
bounds[j] = p[i].pos[j];
if (bounds[j+3] < p[i].pos[j])
bounds[j+3] = p[i].pos[j];
}
}
}
void calc_particle_bounds_periodic(float *bounds) {
int64_t i,j;
for (j=0; j<6; j++) bounds[j]=0;
if (!num_p) return;
memcpy(bounds, p[0].pos, sizeof(float)*3);
memcpy(bounds+3, p[0].pos, sizeof(float)*3);
for (i=1; i<num_p; i++) {
for (j=0; j<3; j++) {
float pos = p[i].pos[j];
if (p[0].pos[j]-pos > BOX_SIZE/2.0) pos+=BOX_SIZE;
else if (p[0].pos[j]-pos < -BOX_SIZE/2.0) pos-=BOX_SIZE;
if (bounds[j] > pos) bounds[j] = pos;
if (bounds[j+3] < pos) bounds[j+3] = pos;
}
}
}
void calc_halo_bounds(float *bounds) {
int64_t i,j;
for (j=0; j<6; j++) bounds[j]=0;
if (!num_halos) return;
memcpy(bounds, halos[0].pos, sizeof(float)*3);
memcpy(bounds+3, halos[0].pos, sizeof(float)*3);
for (i=0; i<num_halos; i++) {
float r = BGC2_R*halos[i].r;
if (STRICT_SO_MASSES) r = BGC2_R*max_halo_radius(halos+i);
for (j=0; j<3; j++) {
if (bounds[j] > halos[i].pos[j]-r) bounds[j] = halos[i].pos[j]-r;
if (bounds[j+3] < halos[i].pos[j]+r) bounds[j+3] = halos[i].pos[j]+r;
}
}
}
void trim_particles(float *bounds) {
int64_t i;
if (!TRIM_OVERLAP) return;
for (i=0; i<3; i++) {
bounds[i] += TRIM_OVERLAP;
bounds[i+3] -= TRIM_OVERLAP;
}
if (ROUND_AFTER_TRIM)
for (i=0; i<6; i++)
bounds[i]=((int64_t)(bounds[i]/ROUND_AFTER_TRIM + 0.5))*ROUND_AFTER_TRIM;
for (i=0; i<num_p; i++)
if (!_check_bounds_raw(p[i].pos, bounds)) {
num_p--;
p[i] = p[num_p];
i--;
}
p = check_realloc(p, sizeof(struct particle)*num_p, "Removing overlap.");
}
void clear_particle_rbuffer(struct recipient *r) {
if (!r->buffered) return;
send_to_socket_noconfirm(r->cs, "part", 4);
send_to_socket_noconfirm(r->cs, r->buffer, sizeof(struct particle)*r->buffered);
r->buffered = 0;
}
void clear_bparticle_rbuffer(struct recipient *r) {
if (!r->buffered) return;
send_to_socket_noconfirm(r->cs, "bprt", 4);
send_to_socket_noconfirm(r->cs, r->buffer, sizeof(struct bparticle)*r->buffered);
r->buffered = 0;
}
void clear_halo_rbuffer(struct recipient *r) {
int64_t i, pids=0;
struct halo *bh = r->buffer;
if (!r->buffered) return;
send_to_socket_noconfirm(r->cs, "halo", 4);
send_to_socket_noconfirm(r->cs, r->buffer, sizeof(struct halo)*r->buffered);
for (i=0; i<r->buffered; i++) pids+=bh[i].num_p;
if (pids>num_buffer_part_ids) {
part_id_buffer = check_realloc(part_id_buffer, sizeof(int64_t)*pids,
"Allocating particle ID buffer.");
num_buffer_part_ids = pids;
}
pids = 0;
for (i=0; i<r->buffered; i++) {
memcpy(part_id_buffer + pids, part_ids + bh[i].p_start,
sizeof(int64_t)*bh[i].num_p);
pids += bh[i].num_p;
}
send_to_socket_noconfirm(r->cs, "pids", 4);
send_to_socket_noconfirm(r->cs, part_id_buffer, sizeof(int64_t)*pids);
r->buffered = 0;
}
void add_sp_to_buffer(struct recipient *r, struct sphere_request *sp) {
assert(r->buffered >= 0);
if (!(r->buffered%1000))
r->buffer = check_realloc(r->buffer, sizeof(struct sphere_request)*
(r->buffered+1000), "Sphere request buffer");
struct sphere_request *buffer = r->buffer;
buffer[r->buffered] = *sp;
r->buffered++;
}
void add_particle_to_buffer(struct recipient *r, struct particle *p1) {
struct particle *buffer = r->buffer;
if (r->buffered == RECIPIENT_BUFFER) clear_particle_rbuffer(r);
buffer[r->buffered] = *p1;
r->buffered++;
}
void add_halo_to_buffer(struct recipient *r, struct halo *h1) {
struct halo *buffer = r->buffer;
if (r->buffered == RECIPIENT_BUFFER) clear_halo_rbuffer(r);
buffer[r->buffered] = *h1;
r->buffered++;
}
void add_bparticle_to_buffer(struct recipient *r, struct bparticle *tbp) {
struct bparticle *buffer = r->buffer;
if (r->buffered == RECIPIENT_BUFFER) clear_bparticle_rbuffer(r);
buffer[r->buffered] = *tbp;
r->buffered++;
}
int64_t check_particle_bounds(struct particle *p1, struct recipient *r) {
if (_check_bounds_raw(p1->pos, r->bounds)) {
add_particle_to_buffer(r, p1);
return 1;
}
return 0;
}
//No longer corrects periodic!!!!
void check_bparticle_bounds(struct bparticle *tbp, struct recipient *r) {
struct bparticle pt = *tbp;
// if (_check_bounds(tbp->pos, pt.pos, r->bounds)) add_bparticle_to_buffer(r, &pt);
if (_check_bounds(tbp->pos, pt.pos, r->bounds)) add_bparticle_to_buffer(r, tbp);
}
void check_halo_bounds(struct halo *h1, struct recipient *r) {
struct halo ht = *h1;
if (_check_bounds(h1->pos, ht.pos, r->bounds)) add_halo_to_buffer(r, &ht);
}
void check_bgc2_bounds(struct halo *h1, struct recipient *r) {
struct sphere_request sp;
float bounds[6];
int64_t i;
sp.r = h1->r * BGC2_R;
if (STRICT_SO_MASSES) sp.r = BGC2_R * max_halo_radius(h1);
for (i=0; i<3; i++) {
bounds[i] = r->bounds[i]-sp.r;
bounds[i+3] = r->bounds[i+3]+sp.r;
}
if (_check_bounds(h1->pos, sp.cen, bounds)) add_sp_to_buffer(r, &sp);
}
int64_t check_projection_bounds(struct particle *p1, struct projection *pr) {
return (_check_bounds_raw(p1->pos, pr->bounds));
}
void send_config(int64_t c) {
double data;
#define snd(x) { data = x; send_to_socket_noconfirm(c, &data, sizeof(double)); }
snd(PARTICLE_MASS);
snd(AVG_PARTICLE_SPACING);
snd(SCALE_NOW);
snd(BOX_SIZE);
snd(Ol);
snd(Om);
snd(h0);
snd(TRIM_OVERLAP);
snd(ROUND_AFTER_TRIM);
#undef snd
}
void recv_config(int64_t c) {
int64_t i;
#define rcv(x) { recv_from_socket(c, &x, sizeof(double)); }
rcv(PARTICLE_MASS);
rcv(AVG_PARTICLE_SPACING);
rcv(SCALE_NOW);
rcv(BOX_SIZE);
rcv(Ol);
rcv(Om);
rcv(h0);
rcv(TRIM_OVERLAP);
rcv(ROUND_AFTER_TRIM);
#undef rcv
if (strlen(LIGHTCONE_ALT_SNAPS)) {
for (i=0; i<3; i++)
if (LIGHTCONE_ORIGIN[i] || LIGHTCONE_ALT_ORIGIN[i]) break;
if (i==3) {
for (i=0; i<3; i++) LIGHTCONE_ORIGIN[i] = LIGHTCONE_ALT_ORIGIN[i] = BOX_SIZE/2.0;
}
}
}
void send_particles(int64_t c, float *bounds) {
int64_t i,j;
for (j=0; j<num_recipients; j++)
recipients[j].cs =
connect_to_addr(recipients[j].address, recipients[j].port);
for (i=num_p-1; i>=0; i--) {
for (j=0; j<num_recipients; j++)
if (check_particle_bounds(p+i, recipients+j)) break;
if (!(i%PARTICLE_REALLOC_NUM)) {
p = check_realloc(p,sizeof(struct particle)*i,"Freeing particle memory.");
check_mtrim();
}
}
num_p = 0;
p = check_realloc(p,0,"Freeing particle memory.");
check_mtrim();
for (j=0; j<num_recipients; j++) {
clear_particle_rbuffer(recipients+j);
send_to_socket(recipients[j].cs, "done", 4);
close_rsocket(recipients[j].cs);
}
clear_recipients();
send_to_socket(c, "done", 4);
}
void send_bparticles(char *c_address, char *c_port) {
int64_t i,j, c;
for (j=0; j<num_recipients; j++) {
recipients[j].cs=connect_to_addr(recipients[j].address, recipients[j].port);
}
for (i=0; i<num_bp; i++)
for (j=0; j<num_recipients; j++) check_bparticle_bounds(bp+i, recipients+j);
for (j=0; j<num_recipients; j++) {
clear_bparticle_rbuffer(recipients+j);
send_to_socket(recipients[j].cs, "done", 4);
close_rsocket(recipients[j].cs);
}
c = connect_to_addr(c_address, c_port);
send_to_socket(c, "rdne", 4);
exit(0);
}
void gather_spheres(char *c_address, char *c_port, float *bounds, int64_t id_offset, int64_t snap, int64_t chunk) {
int64_t i,j,k,c;
for (i=0; i<num_halos; i++) {
if (!_should_print(halos+i, bounds)) continue;
float r = BGC2_R*halos[i].r;
if (STRICT_SO_MASSES) r = BGC2_R*max_halo_radius(halos+i);
for (j=0; j<3; j++) {
if (halos[i].pos[j]-r < bounds[j]) break;
if (halos[i].pos[j]+r > bounds[j+3]) break;
}
if (j==3) continue;
for (j=0; j<num_recipients; j++) {
if (recipients[j].chunk == our_chunk) continue;
check_bgc2_bounds(halos+i, recipients+j);
}
}
for (j=0; j<num_recipients; j++) {
if (recipients[j].chunk == our_chunk) continue;
c = connect_to_addr(recipients[j].address, recipients[j].port);
send_to_socket_noconfirm(c, "sphr", 4);
send_to_socket_noconfirm(c, recipients[j].buffer,
sizeof(struct sphere_request)*recipients[j].buffered);
k=1;
while (1) {
recv_from_socket(c, &k, sizeof(int64_t));
if (!k) break;
ep2 = check_realloc(ep2, sizeof(struct extended_particle)*(num_ep2+k),
"Allocating secondary extended particles.");
recv_from_socket(c, ep2+num_ep2, sizeof(struct extended_particle)*k);
num_ep2+=k;
}
send_to_socket_noconfirm(c, "done", 4);
close_rsocket(c);
}
output_bgc2(id_offset, snap, chunk, bounds);
c = connect_to_addr(c_address, c_port);
// send_to_socket_noconfirm(c, "mass", 4);
//send_to_socket(c, halos, sizeof(struct halo)*num_halos);
send_to_socket(c, "rdne", 4);
exit(0);
}
int sort_by_chunk(const void *a, const void *b) {
const struct bgroup *c = a;
const struct bgroup *d = b;
if (c->chunk < d->chunk) return -1;
if (c->chunk > d->chunk) return 1;
if (c->id < d->id) return -1;
if (c->id > d->id) return 1;
//assert(c->id != d->id);
return 0;
}
void collect_bgroups(int64_t chunk) {
int64_t i,j,k, c, connect_chunk=chunk, total_bg;
free(p);
rockstar_cleanup();
bgroups_to_setlist();
clear_bg_data();
for (i=0,total_bg=0; i<num_bg_sets; i++) total_bg+=bg_set_sizes[i];
while (num_bg_sets && total_bg) {
c = connect_to_addr(chunk_info[connect_chunk].address,
chunk_info[connect_chunk].port);
send_to_socket_noconfirm(c, "lnkb", 4);
send_to_socket_noconfirm(c, &chunk, sizeof(int64_t));
send_to_socket_noconfirm(c, &num_bg_sets, sizeof(int64_t));
send_to_socket_noconfirm(c, bg_set_sizes, sizeof(int64_t)*num_bg_sets);
for (i=0,total_bg=0; i<num_bg_sets; i++) total_bg+=bg_set_sizes[i];
send_to_socket_noconfirm(c, final_bg, sizeof(struct bgroup)*total_bg);
recv_from_socket(c, &num_bg_sets, sizeof(int64_t));
if (num_bg_sets) {
bg_set_sizes = recv_and_alloc(c, bg_set_sizes, sizeof(int64_t)*num_bg_sets);
final_bg = recv_msg_nolength(c, final_bg);
}
send_to_socket(c, "done", 4);
close_rsocket(c);
connect_chunk = calc_next_bgroup_chunk();
if (connect_chunk < 0) break;
for (i=0,total_bg=0; i<num_bg_sets; i++) total_bg+=bg_set_sizes[i];
}
num_bg_sets = prune_setlist();
int64_t *set_chunks = NULL, *sets_per_chunk = NULL, *chunk_indices = NULL;
check_realloc_s(set_chunks, sizeof(int64_t), num_bg_sets);
check_realloc_s(sets_per_chunk, sizeof(int64_t), NUM_WRITERS);
check_realloc_s(chunk_indices, sizeof(int64_t), NUM_WRITERS);
for (i=0; i<NUM_WRITERS; i++) sets_per_chunk[i] = chunk_indices[i] = 0;
//Sort bg sets by the chunk with max # of particles
j=0;
for (i=0; i<num_bg_sets; i++) {
int64_t max_c = final_bg[j].chunk;
int64_t max_p = final_bg[j].num_p;
int64_t cur_c = max_c, cur_p = 0;
assert(bg_set_sizes[i]);
qsort(final_bg+j, bg_set_sizes[i], sizeof(struct bgroup), sort_by_chunk);
assert(final_bg[j].chunk == chunk);
cur_c = final_bg[j].chunk;
for (k=j; k<j+bg_set_sizes[i]; k++) {
if (cur_c != final_bg[k].chunk) {
if (cur_p > max_p) {
max_c = cur_c;
max_p = cur_p;
}
cur_c = final_bg[k].chunk;
cur_p = 0;
}
cur_p += final_bg[k].num_p;
}
if (cur_p > max_p) max_c = cur_c;
set_chunks[i] = max_c;
sets_per_chunk[max_c]+=bg_set_sizes[i];
j+=bg_set_sizes[i];
}
for (i=1; i<NUM_WRITERS; i++)
chunk_indices[i] = chunk_indices[i-1]+sets_per_chunk[i-1];
for (i=0,total_bg=0; i<num_bg_sets; i++) total_bg+=bg_set_sizes[i];
//Reorder sets
struct bgroup *new_groups = NULL;
check_realloc_s(new_groups, sizeof(struct bgroup), total_bg);
j=0;
for (i=0; i<num_bg_sets; i++) {
for (k=j; k<j+bg_set_sizes[i]; k++) {
new_groups[chunk_indices[set_chunks[i]]] = final_bg[k];
chunk_indices[set_chunks[i]]++;
}
j+=bg_set_sizes[i];
}
free(final_bg);
final_bg = new_groups;
int64_t *new_set_sizes = NULL;
check_realloc_s(new_set_sizes, sizeof(int64_t), num_bg_sets);
for (i=0; i<NUM_WRITERS; i++) sets_per_chunk[i] = chunk_indices[i] = 0;
for (i=0; i<num_bg_sets; i++) sets_per_chunk[set_chunks[i]]++;
for (i=1; i<NUM_WRITERS; i++) chunk_indices[i] = chunk_indices[i-1]+sets_per_chunk[i-1];
for (i=0; i<num_bg_sets; i++) {
new_set_sizes[chunk_indices[set_chunks[i]]] = bg_set_sizes[i];
chunk_indices[set_chunks[i]]++;
}
free(bg_set_sizes);
free(set_chunks);
bg_set_sizes = new_set_sizes;
//Send sets to appropriate chunk
int64_t chunk_offset = 0;
for (connect_chunk=0; connect_chunk<NUM_WRITERS; connect_chunk++) {
if (!sets_per_chunk[connect_chunk]) continue;
c = connect_to_addr(chunk_info[connect_chunk].address,
chunk_info[connect_chunk].port);
send_to_socket_noconfirm(c, "strb", 4);
send_to_socket_noconfirm(c, &chunk, sizeof(int64_t));
send_to_socket_noconfirm(c, sets_per_chunk+connect_chunk, sizeof(int64_t));
chunk_indices[connect_chunk]-=sets_per_chunk[connect_chunk];
send_to_socket_noconfirm(c, bg_set_sizes+chunk_indices[connect_chunk],
sizeof(int64_t)*sets_per_chunk[connect_chunk]);
for (j=0,total_bg=0; j<sets_per_chunk[connect_chunk]; j++)
total_bg+=bg_set_sizes[chunk_indices[connect_chunk]+j];
send_to_socket_noconfirm(c, final_bg+chunk_offset, total_bg*sizeof(struct bgroup));
chunk_offset += total_bg;
send_to_socket(c, "done", 4);
close_rsocket(c);
}
c = connect_to_addr(chunk_info[chunk].address,
chunk_info[chunk].port);
send_to_socket(c, "rdne", 4);
close_rsocket(c);
exit(0);
}
void send_halos(char *c_address, char *c_port, int64_t snap, int64_t chunk) {
int64_t i,j, c;
struct binary_output_header bheader;
load_binary_halos(snap, chunk, &bheader, &halos, &part_ids, 0);
for (j=0; j<num_recipients; j++)
recipients[j].cs=connect_to_addr(recipients[j].address, recipients[j].port);
for (i=0; i<bheader.num_halos; i++)
for (j=0; j<num_recipients; j++) check_halo_bounds(halos+i, recipients+j);
for (j=0; j<num_recipients; j++) {
clear_halo_rbuffer(recipients+j);
send_to_socket_noconfirm(recipients[j].cs, "cnfg", 4);
send_config(recipients[j].cs);
send_to_socket(recipients[j].cs, "done", 4);
close_rsocket(recipients[j].cs);
}
c = connect_to_addr(c_address, c_port);
send_to_socket(c, "rdne", 4);
exit(0);
}
void close_connection(int64_t cs, int64_t *cslist, int64_t *num_cs) {
int64_t i;
for (i=0; i<*num_cs; i++) {
if (cslist[i] == cs) {
close_rsocket(cslist[i]);
*num_cs = (*num_cs)-1;
cslist[i] = cslist[*num_cs];
break;
}
}
}
void transfer_stuff(int64_t s, int64_t c, int64_t timestep) {
int64_t i, j, k, num_senders = 0, done = 0, length, new_p_start, *senders = NULL;
char cmd[5] = {0};
struct binary_output_header *bheader;
struct halo **halos_recv, *th;
struct sphere_request *sp;
struct extended_particle *epbuffer = NULL;
int64_t max_conn, **pids_recv;
char *bitarray = NULL;
while (!in_error_state && (num_senders || !done)) {
clear_rsocket_tags();
if (!done) {
tag_rsocket(s);
tag_rsocket(c);
}
for (i=0; i<num_senders; i++) tag_rsocket(senders[i]);
max_conn = select_rsocket(RSOCKET_READ, 0);
for (i=0; i<max_conn; i++) {
if (!check_rsocket_tag(i)) continue;
if (i==s) {
num_senders++;
senders = check_realloc(senders, sizeof(int64_t)*num_senders,
"Allocating particle sender FDs.");
senders[num_senders-1] = accept_connection(s,NULL,NULL);
}
else if (i==c) {
recv_from_socket(c, cmd, 4);
if (!strcmp(cmd, "done")) { done = 1; }
else if (!strcmp(cmd, "err!")) {
in_error_state = 1;
for (j=0; j<num_senders; j++) close_rsocket(senders[j]);
num_senders = 0;
break;
}
else { fprintf(stderr, "[Error] Server protocol error rs (%s)!\n", cmd); exit(1); }
}
else {
if (recv_from_socket(i, cmd, 4)<=0) {
network_io_err(c);
for (j=0; j<num_senders; j++) close_rsocket(senders[j]);
num_senders = 0;
break;
}
if (!strcmp(cmd, "part")) {
length = num_p*sizeof(struct particle);
p = recv_msg(i, p, &length, length);
assert(!(length%(sizeof(struct particle))));
num_p = length / sizeof(struct particle);
}
else if (!strcmp(cmd, "sphr")) {
length = 0;
sp = recv_msg(i, NULL, &length, length);
assert(!(length%(sizeof(struct sphere_request))));
length /= sizeof(struct sphere_request);
if (!epbuffer) epbuffer = check_realloc(NULL, sizeof(struct extended_particle)*PARTICLE_REALLOC_NUM, "Particle buffer");
if (!bitarray) bitarray = BIT_ALLOC(num_p+num_additional_p);
BIT_ALL_CLEAR(bitarray, num_p+num_additional_p);
k=0;
for (j=0; j<length; j++) {
int64_t l, num_sp;
struct extended_particle **result =
do_sphere_request(sp[j].cen, sp[j].r, &num_sp);
for (l=0; l<num_sp; l++) {
if (BIT_TST(bitarray, result[l]-ep)) continue;
BIT_SET(bitarray, result[l]-ep);
epbuffer[k] = result[l][0];
epbuffer[k].hid = -1;
k++;
if (k==PARTICLE_REALLOC_NUM) {
send_to_socket_noconfirm(i, &k, sizeof(int64_t));
send_to_socket_noconfirm(i, epbuffer, sizeof(struct extended_particle)*k);
k=0;
}
}
}
if (k) {
send_to_socket_noconfirm(i, &k, sizeof(int64_t));
send_to_socket_noconfirm(i, epbuffer, sizeof(struct extended_particle)*k);
k=0;
}
send_to_socket_noconfirm(i, &k, sizeof(int64_t));
free(sp);
}
else if (!strcmp(cmd, "bprt")) {
length = num_bp*sizeof(struct bparticle);
bp = recv_msg(i, bp, &length, length);
assert(!(length%(sizeof(struct bparticle))));
int64_t old_bp = num_bp;
num_bp = length / sizeof(struct bparticle);
num_new_bp += num_bp - old_bp;
}
else if (!strcmp(cmd, "halo")) {
assert(timestep > 0);
bheader = (timestep > 1) ? &head2 : &head1;
pids_recv = (timestep > 1) ? &part2 : &part1;
halos_recv = (timestep > 1) ? &halos2 : &halos1;
length = bheader->num_halos*sizeof(struct halo);
*halos_recv = recv_msg(i, *halos_recv, &length, length);
assert(!(length%sizeof(struct halo)));
length /= sizeof(struct halo);
//Redo particle pointers
new_p_start = bheader->num_particles;
for (j=bheader->num_halos; j<length; j++) {
th = (*halos_recv) + j;
th->p_start = new_p_start;
new_p_start += th->num_p;
}
bheader->num_halos = length;
recv_from_socket(i, cmd, 4);
assert(!strcmp(cmd, "pids"));
length = bheader->num_particles*sizeof(int64_t);
*pids_recv = recv_msg(i, *pids_recv, &length, length);
assert(!(length%sizeof(int64_t)));
bheader->num_particles = length / sizeof(int64_t);
}
else if (!strcmp(cmd, "lnkb") || !strcmp(cmd, "strb")) {
int64_t num_sets = 0, client_chunk, total_groups, total_bg;
recv_from_socket(i, &client_chunk, sizeof(int64_t));
recv_from_socket(i, &num_sets, sizeof(int64_t));
int64_t *set_sizes = NULL;
struct bgroup *bgroup_request = NULL;
if (!strcmp(cmd, "lnkb")) {
if (num_sets) {
set_sizes = recv_and_alloc(i, NULL, sizeof(int64_t)*num_sets);
for (j=0,total_bg=0; j<num_sets; j++) total_bg += set_sizes[j];
bgroup_request = recv_and_alloc(i, NULL, sizeof(struct bgroup)*total_bg);
}
find_bgroup_sets(client_chunk, &num_sets, &set_sizes,
&bgroup_request, &total_groups);
send_to_socket_noconfirm(i, &num_sets, sizeof(int64_t));
if (num_sets) {
send_to_socket_noconfirm(i, set_sizes, sizeof(int64_t)*num_sets);
send_to_socket_noconfirm(i, bgroup_request,
sizeof(struct bgroup)*total_groups);
free(set_sizes);
free(bgroup_request);
}
}
else if (num_sets) {
check_realloc_s(bg_set_sizes, sizeof(int64_t), num_bg_sets+num_sets);
recv_from_socket(i, bg_set_sizes+num_bg_sets, sizeof(int64_t)*num_sets);
for (j=0,total_bg=0; j<num_bg_sets+num_sets; j++) total_bg += bg_set_sizes[j];
check_realloc_s(final_bg, sizeof(struct bgroup), total_bg);
bgroup_request = final_bg + total_bg;
for (j=0,total_bg=0; j<num_sets; j++) total_bg += bg_set_sizes[j+num_bg_sets];
bgroup_request -= total_bg;
recv_from_socket(i, bgroup_request, sizeof(struct bgroup)*total_bg);
num_bg_sets += num_sets;
}
}
else if (!strcmp(cmd, "cnfg")) {
recv_config(i);
}
else if (!strcmp(cmd, "done")) {
close_connection(i, senders, &num_senders);
}
else if (!strcmp(cmd, "rdne")) {
close_connection(i, senders, &num_senders);
send_to_socket_noconfirm(c, "done", 4);
}
else { fprintf(stderr, "[Error] Client protocol error rs (%s)!\n", cmd); exit(1); }
}
}
}
free(senders);
if (bitarray) free(bitarray);
if (epbuffer) free(epbuffer);
}
void do_projections(void) {
int64_t i, j, idx, dir=0;
assert(BOX_SIZE > 0);
for (i=0; i<num_proj; i++) {
prj[i].id = prq[i].id;
dir = prj[i].dir = prq[i].dir;
memcpy(prj[i].bounds, prq[i].bounds, sizeof(float)*6);
for (j=0; j<PROJECTION_SIZE; j++) prj[i].data[j] = 0;
}
for (j=0; j<num_p; j++) {
for (i=0; i<num_proj; i++) {
if (check_projection_bounds(p+j, prj+i)) {
idx = (double)PROJECTION_SIZE*p[j].pos[dir]/(double)BOX_SIZE;
if (idx >= PROJECTION_SIZE) idx = PROJECTION_SIZE-1;
prj[i].data[idx]++;
}
}
}
}
void accept_workloads(char *c_address, char *c_port, int64_t snap, int64_t chunk) {
char *address = NULL, *port = NULL;
int64_t s = connect_to_addr(c_address, c_port);
int64_t m = s;
int64_t id = -1, new_bounds = 1;
int64_t i, minus1 = -1, loc;
char cmd[5] = {0};
float zero_bounds[6];
struct fof *fofs = NULL;
struct workunit_info w;
struct bgroup *bgroup_list = NULL;
int64_t *chunks = NULL;
int64_t num_chunks = 0;
int64_t *set_sizes = NULL;
struct particle *pbuffer = NULL;
particle_cleanup();
rockstar_cleanup();
clear_final_bg_data();
int64_t time_a, time_b;
int64_t idle_time = 0, recv_time = 0, send_time = 0, bp_time = 0, f_time = 0,
work_time = 0, ph_time = 0, total_pp = 0, total_h = 0, total_wku = 0;
chunks = check_realloc(NULL, sizeof(int64_t)*NUM_WRITERS, "chunk ids");
if (s < 0) exit(1);
memset(zero_bounds, 0, sizeof(float)*6);
time_a = time(NULL);
#define record_time(x) { time_b = time(NULL); (x) += time_b-time_a; time_a = time_b; }
send_to_socket(m, &chunk, sizeof(int64_t));
while (1) {
if (recv_from_socket(m, cmd, 4)<=0) {
fprintf(stderr, "[Warning] Failed to receive instruction from server for chunk %"PRId64" (shutting down)!\n", chunk);
send_to_socket(s, "clos", 4);
exit(1);
}
if (!strcmp(cmd, "wrku")) {
record_time(idle_time);
recv_from_socket(m, &w, sizeof(struct workunit_info));
assert((w.num_particles+w.num_meta_p) >= 0 && w.num_fofs >= 0 && !w.num_halos);
num_p = w.num_particles+w.num_meta_p;
fofs = recv_and_alloc(m, fofs, sizeof(struct fof)*w.num_fofs);
p = check_realloc(p, sizeof(struct particle)*num_p, "particles");
assert(w.chunk >= 0 && w.chunk < NUM_WRITERS);
memcpy(chunk_info[w.chunk].bounds, w.bounds, sizeof(float)*6);
if (w.num_particles)
recv_from_socket(m, p, sizeof(struct particle)*w.num_particles);
record_time(recv_time);
if (w.num_meta_fofs) {
set_sizes=recv_and_alloc(m, set_sizes, sizeof(int64_t)*w.num_meta_fofs);
bgroup_list = recv_and_alloc(m, bgroup_list, sizeof(struct bgroup)*w.total_bg);
loc = 0;
for (i=0; i<w.num_meta_fofs; i++) {
qsort(bgroup_list+loc, set_sizes[i], sizeof(struct bgroup), sort_by_chunk);
loc += set_sizes[i];
}
loc = w.num_particles;
for (i=0; i<w.total_bg; i++) {
bgroup_list[i].tagged = loc;
loc += bgroup_list[i].num_p;
}
qsort(bgroup_list, w.total_bg, sizeof(struct bgroup), sort_by_chunk);
int64_t dup_ids = 0;
for (i=1; i<w.total_bg; i++)
if (bgroup_list[i].chunk == bgroup_list[i-1].chunk &&
bgroup_list[i].id == bgroup_list[i-1].id) dup_ids=1;
i=0;
num_chunks = 0;
while (i<w.total_bg) {
loc = i;
int64_t part_in_chunk = 0, part_received = 0;
for (; i<w.total_bg; i++) {
if (bgroup_list[loc].chunk != bgroup_list[i].chunk) break;
part_in_chunk += bgroup_list[i].num_p;
}
int64_t num_groups = i-loc;
if (!num_groups || !part_in_chunk) break;
int64_t cchunk = bgroup_list[loc].chunk;
chunks[num_chunks] = cchunk;
num_chunks++;
int64_t m2 = connect_to_addr(chunk_info[cchunk].address,
chunk_info[cchunk].port);
send_to_socket_noconfirm(m2, &minus1, sizeof(int64_t));
send_to_socket_noconfirm(m2, "part", 4);
send_to_socket_noconfirm(m2, &num_groups, sizeof(int64_t));
send_to_socket_noconfirm(m2, bgroup_list + loc, sizeof(struct bgroup)*num_groups);
recv_from_socket(m2, chunk_info[cchunk].bounds, sizeof(float)*6);
int64_t k=0,l=0;
while (part_received < part_in_chunk) {
int64_t this_received = 0;
pbuffer = recv_msg(m2, pbuffer, &this_received, 0);
assert((this_received % (sizeof(struct particle)))==0);
this_received /= sizeof(struct particle);
part_received += this_received;
for (l=0; l<this_received; l++,k++) {
while (k==bgroup_list[loc].num_p) { k=0; loc++; }
p[bgroup_list[loc].tagged+k] = pbuffer[l];
}
}
close_rsocket(m2);
}
if (memcmp(w.bounds, zero_bounds, sizeof(float)*6)!=0) {
if (!PERIODIC || !BOX_SIZE) calc_particle_bounds(w.bounds);
else calc_particle_bounds_periodic(w.bounds);
}
new_bounds = 1;
assert(!dup_ids);
}
else {
num_chunks = 1;
chunks[0] = w.chunk;
}
record_time(bp_time);
if (CLIENT_DEBUG) fprintf(stderr, "Received %"PRId64" particles and %"PRId64" fofs from id %"PRId64" (Worker %"PRId64")\n", num_p, w.num_fofs, id-NUM_READERS, chunk);
if (new_bounds && TEMPORAL_HALO_FINDING) {
new_bounds = 0;
if (!memcmp(w.bounds, zero_bounds, sizeof(float)*6))
load_previous_halos(snap, w.chunk, NULL); //Single processor
else load_previous_halos(snap, w.chunk, w.bounds);
}
record_time(ph_time);
do_workunit(&w, fofs);
record_time(work_time);
if (CLIENT_DEBUG) fprintf(stderr, "Analyzed %"PRId64" particles and %"PRId64" fofs from id %"PRId64", and found %"PRId64" halos. (Worker %"PRId64")\n", w.num_particles, w.num_fofs, id-NUM_READERS, num_halos, chunk);
w.num_halos = num_halos;
total_pp += w.num_particles + w.num_meta_p;
total_h += num_halos;
total_wku++;
// fprintf(stderr, "Workunit done (%"PRId64" halos found; %"PRId64" chunks)!!!\n", num_halos, num_chunks);
for (i=0; i<num_halos; i++) wrap_into_box(halos[i].pos);
for (i=0; i<num_chunks; i++) {
struct fof *chunk_fofs=NULL;
struct halo *chunk_halos=NULL;
struct extra_halo_info *chunk_ei=NULL;
struct particle *chunk_p=NULL;
struct workunit_info chunk_w = w;
sort_out_halos_for_chunk(chunks[i], chunk_info[chunks[i]].bounds, &chunk_w, &chunk_fofs, &chunk_halos, &chunk_ei, &chunk_p, fofs);
if (chunk_w.num_halos) {
int64_t m2 = connect_to_addr(chunk_info[chunks[i]].address,
chunk_info[chunks[i]].port);
send_to_socket_noconfirm(m2, &minus1, sizeof(int64_t));
send_to_socket_noconfirm(m2, "wrkd", 4);
send_to_socket_noconfirm(m2, &chunk_w, sizeof(struct workunit_info));
send_to_socket_noconfirm(m2, chunk_fofs, sizeof(struct fof)*chunk_w.num_fofs);
send_to_socket_noconfirm(m2, chunk_halos, sizeof(struct halo)*chunk_w.num_halos);
send_to_socket_noconfirm(m2, chunk_ei, sizeof(struct extra_halo_info)*chunk_w.num_halos);
send_to_socket(m2, chunk_p, sizeof(struct particle)*(chunk_w.num_particles+chunk_w.num_meta_p));
close_rsocket(m2);
if (chunk_p != p) {
free(chunk_fofs);
free(chunk_halos);
free(chunk_ei);
free(chunk_p);
}
}
}
send_to_socket(m, "wrku", 4);
record_time(send_time);
}
else if (!strcmp(cmd, "nmwk")) {
if (m != s) close_rsocket(m);
p = check_realloc(p, 0, "Freeing particles.");
clear_prev_files();
send_to_socket(s, "nmwk", 4);
send_to_socket(s, &id, sizeof(int64_t));
m = s;
}
else if (!strcmp(cmd, "work")) {
assert(m==s);
recv_from_socket(s, &id, sizeof(int64_t));
address = recv_msg_nolength(s, address);
port = recv_msg_nolength(s, port);
m = connect_to_addr(address, port);
if (m<0) {
send_to_socket(s, "clos", 4);
exit(1);
}
send_to_socket(m, &chunk, sizeof(int64_t));
new_bounds = 1;
}
else if (!strcmp(cmd, "fini")) {
record_time(f_time);
if (profile_out)
fprintf(profile_out, "[Prof] S%"PRId64",C%"PRId64": %"PRId64"p,%"PRId64"h,%"PRId64"w; wt:%"PRId64"s; rcv:%"PRId64"s,%"PRId64"s; snd:%"PRId64"s; wk:%"PRId64"s; idl:%"PRId64"s\n", snap, chunk, total_pp, total_h, total_wku, idle_time, recv_time, bp_time, send_time, work_time, f_time);
fflush(profile_out);
exit(0);
}
else {
fprintf(stderr, "[Error] Error in client protocol aw (%s)!\n", cmd);
send_to_socket_noconfirm(s, "clos", 4);
exit(1);
}
}
}
int send_workunit(int64_t sock, struct workunit_info *w,
struct fof **fofs, struct particle **particles,
int64_t **set_sizes, struct bgroup **bgroup_list,
int64_t *no_more_work, int64_t chunk, float *bounds) {
if (!(*no_more_work))
find_unfinished_workunit(w, fofs, particles, set_sizes, bgroup_list);
if ((*no_more_work) || (!w->num_fofs)) {
*no_more_work = 1;
send_to_socket(sock, "nmwk", 4);
return 0;