-
Notifications
You must be signed in to change notification settings - Fork 4
/
intermediary_b.cpp
1196 lines (1128 loc) · 50.4 KB
/
intermediary_b.cpp
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
#ifndef TEST
#pragma check_stack(off)
#pragma runtime_checks( "scu", off )
#pragma strict_gs_check(off)
#endif
#pragma float_control(except, off)
#pragma fenv_access(off)
#pragma float_control(precise, off)
#pragma auto_inline on
//#pragma optimize("agtw", on)
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
#include <cassert>
#include <list>
#include <unordered_map>
#include <map>
#include <queue>
#include <algorithm>
#include <set>
#include <cstring>
#include <iostream>
#include <fstream>
#include <string>
#include <pthread.h>
#include <ctime>
#include <unistd.h>
#include <cmath>
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
using namespace std;
typedef unsigned int ui;
#ifdef TEST
#include <sys/time.h>
timeval starttime, endtime;
#define CURTIME(x) \
gettimeofday(&endtime,0); \
cout << #x << ": " << (double)(1000000*(endtime.tv_sec - starttime.tv_sec) + endtime.tv_usec - starttime.tv_usec) / 1000000 << endl
#else
#define CURTIME(x)
#endif
//线程数量
#define THREADNUM 4
#define GROUPSIZE 32
typedef struct Edge {
uint64_t weight;
ui vertice;
bool operator<(const Edge &a) const { return vertice < a.vertice; }
bool operator>(const Edge &a) const { return vertice > a.vertice; }
Edge() {
}
Edge(ui v, uint64_t w) {
vertice = v;
weight = w;
}
} Edge;
#define NODENEXTLISTLISTSIZE 2046
typedef struct NodeNextList {
int num;
struct NodeNextList *next;
Edge node[NODENEXTLISTLISTSIZE];
inline void add(struct NodeNextList **s, const Edge &num){
if(unlikely(this->num == NODENEXTLISTLISTSIZE)){
*s = next = (struct NodeNextList*)malloc(sizeof(struct NodeNextList));
*next = {.num=1, .next=nullptr, .node={num,}};
}
else{
node[this->num++] = num;
}
}
} NodeNextList;
// 控制struct大小在2的幂次大小
#define NODELISTSIZE 21
typedef struct Node {
char num = 0;
NodeNextList *nextList = nullptr, *endList = nullptr;
Edge node[NODELISTSIZE];
inline void add(const Edge &num) {
if(this->num == NODELISTSIZE){
if(!endList){
nextList = endList = (NodeNextList*)malloc(sizeof(NodeNextList));
endList->num = 0;
endList->next = NULL;
}
endList->add(&endList, num);
}
else{
node[this->num++] = num;
}
}
} Node;
typedef struct NodeIter{
Edge *cur, *end;
NodeNextList *nextList;
NodeIter(Node *node){
cur = node->node-1;
end = node->node+node->num;
nextList = node->nextList;
__builtin_prefetch(cur+1, 0);
}
inline Edge *next() {
cur++;
if(unlikely(cur == end)){
if(nextList){
cur = nextList->node;
end = nextList->node + nextList->num;
nextList = nextList->next;
}
else{
return NULL;
}
}
__builtin_prefetch(cur+1, 0);
return cur;
}
} NodeIter;
typedef union Path {
struct {
ui stp3;
ui stp2;
ui stp1;
uint64_t wout;
uint64_t win;
};
struct{
unsigned long num;
ui notUsedMember;
};
} Path;
inline bool limit(uint64_t w1, uint64_t w2) {
return w2 * 5ul >= w1 && w2 <= w1 * 3ul;
/*
ui max = w1 <= 3 * w1 ? 3 * w1 : 4294967295;
return ((w2 >= 0.2 * w1) && (w2 <= max));
// */
}
extern uint32_t const NUMTOSTR1[];
extern uint32_t const NUMTOSTR2[];
extern uint32_t const NUMTOSTR3[];
inline void writeNum(char *output, unsigned int &p, const ui num) {
if (num < 10) {
*(int *) (output + p) = NUMTOSTR1[num];
p += 1+1;
return;
} else if (num < 100) {
*(int *) (output + p) = NUMTOSTR2[num];
p += 2+1;
return;
} else if (num < 1000) {
*(int *) (output + p) = NUMTOSTR3[num];
p += 3+1;
return;
} else if (num < 10000) {
ui x = num / 10;
*(int *) (output + p) = NUMTOSTR3[x];
*(int *) (output + p + 3) = NUMTOSTR1[num - x * 10];
p += 4+1;
return;
} else if (num < 100000) {
ui x = num / 100;
*(int *) (output + p) = NUMTOSTR3[x];
*(int *) (output + p + 3) = NUMTOSTR2[num - x * 100];
p += 5+1;
return;
} else if (num < 1000000) {
ui x = num / 1000;
*(int *) (output + p) = NUMTOSTR3[x];
*(int *) (output + p + 3) = NUMTOSTR3[num - x * 1000];
p += 6+1;
return;
} else if (num < 10000000) {
ui x = num / 10000;
*(int *) (output + p) = NUMTOSTR3[x];
*(int *) (output + p + 3) = NUMTOSTR3[(num - x * 10000) / 10];
*(int *) (output + p + 6) = NUMTOSTR1[num % 10];
p += 7+1;
return;
} else if (num < 100000000) {
ui x = num / 100000;
*(int *) (output + p) = NUMTOSTR3[x];
*(int *) (output + p + 3) = NUMTOSTR3[(num - x * 100000) / 100];
*(int *) (output + p + 6) = NUMTOSTR2[num % 100];
p += 8+1;
return;
} else if (num < 1000000000) {
ui x = num / 1000000;
*(int *) (output + p) = NUMTOSTR3[x];
*(int *) (output + p + 3) = NUMTOSTR3[(num - x * 1000000) / 1000];
*(int *) (output + p + 6) = NUMTOSTR3[num % 1000];
p += 9+1;
return;
} else {
ui x = num / 10000000;
*(int *) (output + p) = NUMTOSTR3[x];
*(int *) (output + p + 3) = NUMTOSTR3[(num - x * 10000000) / 10000];
*(int *) (output + p + 6) = NUMTOSTR3[num % 10000 / 10];
*(int *) (output + p + 9) = NUMTOSTR1[num % 10];
p += 10 + 1;
return;
}
}
struct str{
char str[19];
char add = 0;
unsigned int len = 0;
};
#define MAXDATANUM (1<<21u)
Node nodeSetHashList[MAXDATANUM];
Node inNodeList[MAXDATANUM];
pthread_spinlock_t nodeLock[MAXDATANUM][2];
//ui nodeIDList[MAXDATANUM]; //保存结点
ui nodePreList[MAXDATANUM]; //保存结点
str nodePreListStr[MAXDATANUM]; //保存节点字符串
int nodeSizeCount[MAXDATANUM];
//int nodeLessInSize[MAXDATANUM];
//double totalCountSize = 0;
//ui nodeSetSize = 0;
ui nodeIDNum = 0;
//int gapId = 1;
//ui threadGap[THREADNUM] = {0, };
ui resultLen[THREADNUM];
char *inputStr;
int inputStrLen;
int totalDataNum = 0;
inline Node *nodeSet(const ui i, Node *list) {
return &list[i];
}
inline Node *getNodeSet(const ui i) {
return &nodeSetHashList[i];
}
inline Node *getInNodeSet(const ui i) {
return &inNodeList[i];
}
/*
#define COUNTSIZE 1000000
inline void nodeDivideTask(ui id, ui nodeCnt) {
if(gapId >= THREADNUM){
return;
}
//计算线程分配
static unsigned long curTotalNum = 0;
unsigned long n1 = nodeLessInSize[id], n2 = nodeLargerOutSize[id];
unsigned long cur = n1*n1*n1 + n2*n2*n2*n2;
cur = cur > COUNTSIZE ? COUNTSIZE : cur;
if(curTotalNum + cur >= totalCountSize / THREADNUM){
threadGap[gapId++] = id-1;
curTotalNum = cur;
//cout << "gap" << gapId - 1 << ":" <<nodeCnt << endl;
}
else{
curTotalNum += cur;
}
}
*/
inline void nodeSort(Node &node) {
//sort(node.node.begin(), node.node.end());
if ((node.num < NODELISTSIZE)) {
sort(node.node, node.node + node.num);
} else {
// 使用优先队列排序
NodeIter inIter(&node);
priority_queue<Edge, vector<Edge>, greater<Edge>> q;
for(Edge *edge = inIter.next(); edge; edge = inIter.next()){
q.push(*edge);
}
NodeIter outIter(&node);
for(Edge *edge = outIter.next(); edge; edge = outIter.next()){
*edge = q.top();
q.pop();
}
}
}
struct Result {
unsigned int strLen = 0;
char *str;
int groupLen[MAXDATANUM / GROUPSIZE] = {0,}; // 前面组号,后面位置
int groupPointer = 1;
int outputPointer[THREADNUM] = {1, 1, 1, 1};
inline void insert3(const ui &id, const ui &x, const ui &y) {
memcpy(str+strLen, nodePreListStr[id].str, nodePreListStr[id].len);
strLen += nodePreListStr[id].len;
memcpy(str+strLen, nodePreListStr[x].str, nodePreListStr[x].len);
strLen += nodePreListStr[x].len;
memcpy(str+strLen, nodePreListStr[y].str, nodePreListStr[y].len);
strLen += nodePreListStr[y].len;
str[strLen - 1] = '\n';
}
inline void insert4(const ui &id, const ui &x, const ui &y, const ui &z) {
memcpy(str+strLen, nodePreListStr[id].str, nodePreListStr[id].len);
strLen += nodePreListStr[id].len;
memcpy(str+strLen, nodePreListStr[x].str, nodePreListStr[x].len);
strLen += nodePreListStr[x].len;
memcpy(str+strLen, nodePreListStr[y].str, nodePreListStr[y].len);
strLen += nodePreListStr[y].len;
memcpy(str+strLen, nodePreListStr[z].str, nodePreListStr[z].len);
strLen += nodePreListStr[z].len;
str[strLen - 1] = '\n';
}
inline void insert5678(char *start, const int len, const Path &path) {
memcpy(str+strLen, start, len);
strLen += len;
memcpy(str+strLen, nodePreListStr[path.stp1].str, nodePreListStr[path.stp1].len);
strLen += nodePreListStr[path.stp1].len;
memcpy(str+strLen, nodePreListStr[path.stp2].str, nodePreListStr[path.stp2].len);
strLen += nodePreListStr[path.stp2].len;
memcpy(str+strLen, nodePreListStr[path.stp3].str, nodePreListStr[path.stp3].len);
strLen += nodePreListStr[path.stp3].len;
str[strLen-1] = '\n';
}
inline void newGroup(){
groupLen[groupPointer++] = strLen;
}
inline void outputResult(char *output, unsigned int &p, int threadId) {
int len = groupLen[outputPointer[threadId]] - groupLen[outputPointer[threadId]-1];
memcpy(output + p, str+groupLen[outputPointer[threadId]-1], len);
p += len;
outputPointer[threadId]++;
}
inline void moveForward(unsigned int &p, int threadId){
int len = groupLen[outputPointer[threadId]] - groupLen[outputPointer[threadId]-1];
p += len;
outputPointer[threadId]++;
}
} result[THREADNUM][6];
typedef struct InputFormat{
ui start;
ui end;
uint64_t weight;
}InputFormat;
InputFormat *inputTemp[THREADNUM];
int dataNum[THREADNUM]; //每个线程数组储存的数量
int readStart[THREADNUM]; // 输入时读的开头
int dataSum[THREADNUM]; //从第一个线程开始的累加和
//int nodeIDListEnd[THREADNUM];
# define SIXTYTHOUSAND (2800000 / THREADNUM + 10000)
inline InputFormat *readInputData(const ui i) {
if (i < dataSum[0])
return &inputTemp[0][i];
for (int x = 1; x < THREADNUM; x++)
if (i < dataSum[x])
return &inputTemp[x][i - dataSum[x - 1]];
}
void *readLineThread(void *threadId) {
const int id = *((int *) threadId);
int p = readStart[id];
//cout << id;
int stop;
//stop = inputStrLen;
dataNum[id] = 0;
inputTemp[id] = (InputFormat *) (malloc(sizeof(InputFormat) * SIXTYTHOUSAND));
if (id == THREADNUM - 1)
stop = inputStrLen;
else
stop = readStart[id + 1];
for (int i = 0; p < stop; i++) {
ui start = 0;
ui end = 0;
uint64_t weight = 0;
while (inputStr[p] != ',') {
start = start*10 + (inputStr[p] - '0');
p++;
}
inputTemp[id][i].start = start;
p++;
while (inputStr[p] != ',') {
end = end*10 + (inputStr[p] - '0');
p++;
}
inputTemp[id][i].end = end;
p++;
while (inputStr[p] != '\n' && inputStr[p] != '\r' && inputStr[p] != '.') {
weight = weight * 10 + (inputStr[p] - '0');
p++;
}
int decimal = 0;
if(inputStr[p] == '.'){
p++;
if(inputStr[p] != '\n' && inputStr[p] != '\r'){
decimal = decimal * 10 + (inputStr[p] - '0');
p++;
}
if(inputStr[p] != '\n' && inputStr[p] != '\r'){
decimal = decimal * 10 + (inputStr[p] - '0');
p++;
}else{
decimal = decimal * 10;
}
}
inputTemp[id][i].weight = weight*100 + decimal;
//cout << inputTemp[id][i].start << ","<< inputTemp[id][i].end <<","<< inputTemp[id][i].weight << endl;
while (inputStr[p] == '\n' || inputStr[p] == '\r')
p++;
if(inputTemp[id][i].weight == 0){
inputTemp[id][i].start = 0;
inputTemp[id][i].end = 0;
i--;
continue;
}
dataNum[id]++;
}
pthread_exit(nullptr);
}
inline void readInput(const char *inputPath) {
const int fd = open(inputPath, O_RDONLY, 0666);
assert(fd >= 0);
struct stat st{};
fstat(fd, &st);
inputStrLen = st.st_size;
const int readBitNum = inputStrLen / THREADNUM;
//cout << inputStrLen << endl;
inputStr = (char *) mmap(nullptr, inputStrLen, PROT_READ, MAP_PRIVATE, fd, 0);
assert(inputStr != MAP_FAILED);
readStart[0] = 0;
for (int i = 1; i < THREADNUM; i++) {
int p;
p = max(readBitNum * i, readStart[i - 1] + 1);
while (inputStr[p] != '\n' && p < inputStrLen) {
p++;
}
p++;
readStart[i] = min(p, inputStrLen);
//cout << p << endl;
}
//cout << "read begin" <<endl;
pthread_t threads[THREADNUM];
for (int i = 0; i < THREADNUM; i++) {
const int *j = new int(i);
//cout << *j << endl;
int rc = pthread_create(&threads[*j], nullptr, readLineThread, (void *) j);
#ifdef TEST
if (rc) {
cout << "Error:unable to create thread," << rc << endl;
exit(-1);
}
#endif
}
for (int i = 0; i < THREADNUM; i++) {
pthread_join(threads[i], nullptr);
totalDataNum = totalDataNum + dataNum[i];
dataSum[i] = totalDataNum;
}
}
typedef struct IDMap{
bool isInit = false;
ui val;
ui mapedVal;
}IDMap;
IDMap nodeIDMap[MAXDATANUM];
bool InitMap(IDMap* list, const ui i){
ui id = i & (MAXDATANUM - 1);
if (!list[id].isInit) {
list[id].isInit = true;
list[id].val = i;
return true;
} else {
if (list[id].val == i)
return false;
else{
do{
id++;
if (list[id].val == i)
return false;
} while (list[id].isInit);
list[id].isInit = true;
list[id].val = i;
return true;
}
}
}
void MapSet(IDMap* list,ui i, const ui k){
ui id = i & (MAXDATANUM - 1);
while(list[id].val != i)
id++;
list[id].mapedVal = k;
}
ui MapGet(IDMap* list, const ui i){
ui id = i & (MAXDATANUM - 1);
while(list[id].val != i)
id++;
return list[id].mapedVal;
}
void *treat_thread(void *arg){
const int threadId = *((int *) arg);
const int part = totalDataNum / THREADNUM;
const int front = threadId * part;
const int back = threadId == THREADNUM-1 ? totalDataNum : (threadId + 1) * part;
for (int i = front; i < back; i++) {
//auto iter1 = nodeSet.find(readInputData(i)[0]);
InputFormat *edge = readInputData(i);
ui start = MapGet(nodeIDMap,edge->start);
ui end = MapGet(nodeIDMap,edge->end);
uint64_t weight = edge->weight;
///*
if(start < end){
nodeSizeCount[start] += 16;
}
else{
nodeSizeCount[end]++;
}
// */
pthread_spin_lock(&nodeLock[start][0]);
nodeSet(start, nodeSetHashList)->add(Edge(end, weight));
pthread_spin_unlock(&nodeLock[start][0]);
pthread_spin_lock(&nodeLock[end][1]);
nodeSet(end, inNodeList)->add(Edge(start, weight));
pthread_spin_unlock(&nodeLock[end][1]);
}
}
inline void treatData() {
nodeIDNum = 0;
for (int i = 0; i < totalDataNum; i++) {
const InputFormat *edge = readInputData(i);
const ui start = edge->start;
const ui end = edge->end;
if(InitMap(nodeIDMap,start)){
nodePreList[nodeIDNum] = start;
pthread_spin_init(&nodeLock[nodeIDNum][0], PTHREAD_PROCESS_PRIVATE);
pthread_spin_init(&nodeLock[nodeIDNum][1], PTHREAD_PROCESS_PRIVATE);
nodeIDNum++;
}
if(InitMap(nodeIDMap,end)){
nodePreList[nodeIDNum] = end;
pthread_spin_init(&nodeLock[nodeIDNum][0], PTHREAD_PROCESS_PRIVATE);
pthread_spin_init(&nodeLock[nodeIDNum][1], PTHREAD_PROCESS_PRIVATE);
nodeIDNum++;
}
}
CURTIME(点统计结束);
//memset(nodeSetHashList, 0, sizeof(Node) * nodeIDNum);
//memset(inNodeList, 0, sizeof(Node) * nodeIDNum);
sort(nodePreList,nodePreList + nodeIDNum);
for(int i = 0;i < nodeIDNum;i++){
MapSet(nodeIDMap,nodePreList[i],i);
writeNum(nodePreListStr[i].str,nodePreListStr[i].len, nodePreList[i]);
nodePreListStr[i].add = nodePreListStr[i].len & 7;
}
CURTIME(映射完成);
pthread_t threads[THREADNUM];
//cout << nodeSet.size()<<endl;
for (int j = 1; j < THREADNUM; j++) {
const int *x = new int(j);
pthread_create(&threads[j], nullptr, treat_thread, (void *) x);
}
treat_thread(new int(0));
for (int i = 1; i < THREADNUM; i++) {
pthread_join(threads[i], nullptr);
}
CURTIME(建图完成);
//cout << "treatdata end" <<endl;
for (int i = 0; i < THREADNUM; i++) {
free(inputTemp[i]);
}
}
struct PathList {
Path p;
int next;
};
pthread_spinlock_t lock;
int curGroup = 0;
int groupThread[MAXDATANUM / GROUPSIZE];
int groupDividing[1u<<17];
int groupNum = 1;
#define GEOUP_DIVIDING_THRESH 4096
int totalLen = 0;
void *search_thread(void *arg) {
const int threadId = *((int *) arg);
resultLen[threadId] = 0;
//int begin = threadGap[threadId];
//int end = threadId == THREADNUM - 1 ? nodeIDNum : threadGap[threadId + 1];
//cout << "thread "<< threadId << " working from" << begin << " to " << end << endl;
//ui route[7];//路径
char routeStr[7 * (20)];
int *isThirdNode = (int *) (malloc(sizeof(int) * nodeIDNum));
memset(isThirdNode, -1, nodeIDNum * sizeof(int));
int *pathListHead = (int *) (malloc(sizeof(int) * nodeIDNum));
//unordered_map<ui,int> pathListHead;
struct PathList pathList[120000];
Result *r = result[threadId];
r[0].str = (char *) (malloc(sizeof(char) * 11u * (0u + 1u) * (10000000u * (0u + 1u))));
r[1].str = (char *) (malloc(sizeof(char) * 11u * (1u + 1u) * (10000000u * (1u + 1u))));
r[2].str = (char *) (malloc(sizeof(char) * 11u * (2u + 1u) * (10000000u * (2u + 1u))));
r[3].str = (char *) (malloc(sizeof(char) * 11u * (3u + 1u) * (10000000u * (3u + 1u))));
r[4].str = (char *) (malloc(sizeof(char) * 11u * (4u + 1u) * (10000000u * (4u + 1u))));
r[5].str = (char *) (malloc(sizeof(char) * 11u * (5u + 1u) * (10000000u * (5u + 1u))));
//int groupTotalNum = (nodeIDNum + GROUPSIZE - 1) / GROUPSIZE;
//int oldResultLen = 0;
while(true){
int t;
pthread_spin_lock(&lock);
//oldResultLen = resultLen[threadId];
if(curGroup >= groupNum)
break;
t = curGroup;
groupThread[curGroup] = threadId;
curGroup++;
pthread_spin_unlock(&lock);
#ifdef TEST
assert(t < groupNum);
#endif
const int begin = groupDividing[t];
const int end = t == groupNum-1 ? nodeIDNum : groupDividing[t+1];
for (int id = begin; id < end; id++) {
//route[0] = id;
Node *root = getNodeSet(id);
Node *inRoot = getInNodeSet(id);
if(!root->num || !inRoot->num){
continue;
}
//cout << id <<endl;
int pointer = 0;
//Node *inRoot = &inNodeList[id];
NodeIter xIter(inRoot);
for (Edge *ix = xIter.next(); ix; ix = xIter.next()) {
const Edge x = *ix;
if (x.vertice <= id)
continue;
NodeIter yIter(getInNodeSet(x.vertice));
for (Edge *iy = yIter.next(); iy; iy = yIter.next()) {
const Edge y = *iy;
if (y.vertice <= id || !limit(y.weight, x.weight))
continue;
NodeIter zIter(getInNodeSet(y.vertice));
for (Edge *iz = zIter.next(); iz; iz = zIter.next()) {
const Edge z = *iz;
if (z.vertice <= id || z.vertice == x.vertice || !limit(z.weight, y.weight))
continue;
NodeIter DIter(getInNodeSet(z.vertice));
for (Edge *iD = DIter.next(); iD; iD = DIter.next()){
const Edge D = *iD;
if(D.vertice <= id || D.vertice == x.vertice || D.vertice == y.vertice || !limit(D.weight, z.weight)){
continue;
}
int *head = &pathListHead[D.vertice];
Path p = {x.vertice, y.vertice, z.vertice, x.weight, D.weight};
if (isThirdNode[D.vertice] != id) {
isThirdNode[D.vertice] = id;
pathList[pointer].next = -1;
//cout << z <<endl;
} else {
for (; *head != -1; head = &pathList[*head].next) {
__builtin_prefetch(&pathList[pathList[*head].next], 0);
Path *pl = &pathList[*head].p;
if (p.stp1 < pl->stp1 || (p.stp1 == pl->stp1 && p.num < pl->num)) {
break;
}
}
pathList[pointer].next = *head;
}
pathList[pointer].p = p;
*head = pointer;
pointer++;
__builtin_prefetch(&pathList[pointer], 0);
//cout << pointer <<endl;
}
}
}
}
NodeIter x_iter(root);
for (Edge *ix = x_iter.next(); ix; ix = x_iter.next()) {
const Edge x = *ix;
if (x.vertice < id) continue;
//route[1] = x.vertice;
// 检查是否有5环
if (isThirdNode[x.vertice] == id) {
ui p = 0;
memcpy(routeStr + p, nodePreListStr[id].str, nodePreListStr[id].len + nodePreListStr[id].add);
p += nodePreListStr[id].len;
memcpy(routeStr + p, nodePreListStr[x.vertice].str, nodePreListStr[x.vertice].len + nodePreListStr[x.vertice].add);
p += nodePreListStr[x.vertice].len;
__builtin_prefetch(&r[1], 1);
for (int I = pathListHead[x.vertice]; I != -1; I = pathList[I].next) {
__builtin_prefetch(&pathList[pathList[I].next], 0);
const Path *path = &pathList[I].p;
if (limit(x.weight, path->win) && limit(path->wout, x.weight)) {
__builtin_prefetch(&nodePreListStr[path->stp2], 0);
__builtin_prefetch(&nodePreListStr[path->stp1], 0);
r[2].insert5678(routeStr, p, *path);
resultLen[threadId]++;
}
}
}
NodeIter yIter(getNodeSet(x.vertice));
for (Edge *iy = yIter.next(); iy; iy = yIter.next()) {
const Edge y = *iy;
if (y.vertice <= id || !limit(x.weight, y.weight)) continue;
//route[2] = y.vertice;
// 检查是否有6环
if (isThirdNode[y.vertice] == id) {
ui p = 0;
memcpy(routeStr + p, nodePreListStr[id].str, nodePreListStr[id].len + nodePreListStr[id].add);
p += nodePreListStr[id].len;
memcpy(routeStr + p, nodePreListStr[x.vertice].str, nodePreListStr[x.vertice].len + nodePreListStr[x.vertice].add);
p += nodePreListStr[x.vertice].len;
memcpy(routeStr + p, nodePreListStr[y.vertice].str, nodePreListStr[y.vertice].len + nodePreListStr[y.vertice].add);
p += nodePreListStr[y.vertice].len;
__builtin_prefetch(&r[2], 1);
for (int I = pathListHead[y.vertice]; I != -1; I = pathList[I].next) {
__builtin_prefetch(&pathList[pathList[I].next], 0);
const Path *path = &pathList[I].p;
if (x.vertice != path->stp1 && x.vertice != path->stp2 && x.vertice != path->stp3 &&
limit(y.weight, path->win) && limit(path->wout, x.weight)) {
__builtin_prefetch(&nodePreListStr[path->stp2], 0);
__builtin_prefetch(&nodePreListStr[path->stp1], 0);
r[3].insert5678(routeStr, p, *path);
resultLen[threadId]++;
}
}
}
NodeIter zIter(getNodeSet(y.vertice));
for (Edge *iz = zIter.next(); iz; iz = zIter.next()) {
const Edge z = *iz;
// 检查是是否有3环
if (id == z.vertice && limit(z.weight, x.weight) && limit(y.weight, z.weight)) {
r[0].insert3(id, x.vertice, y.vertice);
resultLen[threadId]++;
continue;
}
if (z.vertice <= id || x.vertice == z.vertice || !limit(y.weight, z.weight)) continue;
//route[3] = z.vertice;
// 检查是否有7环
if (isThirdNode[z.vertice] == id ) {
__builtin_prefetch(&r[3], 1);
ui p = 0;
memcpy(routeStr + p, nodePreListStr[id].str, nodePreListStr[id].len + nodePreListStr[id].add);
p += nodePreListStr[id].len;
memcpy(routeStr + p, nodePreListStr[x.vertice].str, nodePreListStr[x.vertice].len + nodePreListStr[x.vertice].add);
p += nodePreListStr[x.vertice].len;
memcpy(routeStr + p, nodePreListStr[y.vertice].str, nodePreListStr[y.vertice].len + nodePreListStr[y.vertice].add);
p += nodePreListStr[y.vertice].len;
memcpy(routeStr + p, nodePreListStr[z.vertice].str, nodePreListStr[z.vertice].len + nodePreListStr[z.vertice].add);
p += nodePreListStr[z.vertice].len;
for (int I = pathListHead[z.vertice]; I != -1; I = pathList[I].next) {
__builtin_prefetch(&pathList[pathList[I].next], 0);
const Path *path = &pathList[I].p;
if (x.vertice != path->stp1 && x.vertice != path->stp2 && x.vertice != path->stp3 &&
y.vertice != path->stp1 && y.vertice != path->stp2 && y.vertice != path->stp3 &&
limit(z.weight, path->win) && limit(path->wout, x.weight) ) {
__builtin_prefetch(&nodePreListStr[path->stp2], 0);
__builtin_prefetch(&nodePreListStr[path->stp1], 0);
r[4].insert5678(routeStr, p, *path);
resultLen[threadId]++;
}
}
}
NodeIter DIter(getNodeSet(z.vertice));
for (Edge *iD = DIter.next(); iD; iD = DIter.next()) {
const Edge D = *iD;
// 检查是是否有4环
if (id == D.vertice && limit(D.weight, x.weight) && limit(z.weight, D.weight)) {
r[1].insert4(id, x.vertice, y.vertice, z.vertice);
resultLen[threadId]++;
continue;
}
if (D.vertice <= id|| x.vertice == D.vertice || y.vertice == D.vertice || !limit(z.weight, D.weight) || pointer == 0)
continue;
// Node *nodeD = getNodeSet(D);
// 检查是否有8环
if (isThirdNode[D.vertice] == id) {
__builtin_prefetch(&r[4], 1);
//route[4] = D.vertice;
ui p = 0;
memcpy(routeStr + p, nodePreListStr[id].str, nodePreListStr[id].len + nodePreListStr[id].add);
p += nodePreListStr[id].len;
memcpy(routeStr + p, nodePreListStr[x.vertice].str, nodePreListStr[x.vertice].len + nodePreListStr[x.vertice].add);
p += nodePreListStr[x.vertice].len;
memcpy(routeStr + p, nodePreListStr[y.vertice].str, nodePreListStr[y.vertice].len + nodePreListStr[y.vertice].add);
p += nodePreListStr[y.vertice].len;
memcpy(routeStr + p, nodePreListStr[z.vertice].str, nodePreListStr[z.vertice].len + nodePreListStr[z.vertice].add);
p += nodePreListStr[z.vertice].len;
memcpy(routeStr + p, nodePreListStr[D.vertice].str, nodePreListStr[D.vertice].len + nodePreListStr[D.vertice].add);
p += nodePreListStr[D.vertice].len;
for (int I = pathListHead[D.vertice]; I != -1; I = pathList[I].next) {
__builtin_prefetch(&pathList[pathList[I].next], 0);
const Path *path = &pathList[I].p;
if (x.vertice != path->stp1 && x.vertice != path->stp2 && x.vertice != path->stp3 &&
y.vertice != path->stp1 && y.vertice != path->stp2 && y.vertice != path->stp3 &&
z.vertice != path->stp1 && z.vertice != path->stp2 && z.vertice != path->stp3 &&
limit(D.weight, path->win) && limit(path->wout, x.weight)) {
__builtin_prefetch(&nodePreListStr[path->stp2], 0);
__builtin_prefetch(&nodePreListStr[path->stp1], 0);
r[5].insert5678(routeStr, p, *path);
resultLen[threadId]++;
}
}
}
}
}
}
}
}
r[0].newGroup();
r[1].newGroup();
r[2].newGroup();
r[3].newGroup();
r[4].newGroup();
r[5].newGroup();
}
pthread_spin_unlock(&lock);
#ifdef TEST
cout << threadId;
CURTIME(线程结束);
#endif
if (threadId != 0)
pthread_exit(nullptr);
}
char *output;
unsigned int startLoc;
unsigned int pFinalNum;
void *outputResult_thread(void *arg){
int threadId = *((int *) arg);
unsigned int p = startLoc;
for (int i = 0; i < 6; i++){
for(int j=0; j < curGroup; j++){
if(threadId == groupThread[j])
result[threadId][i].outputResult(output, p, threadId);
else
result[groupThread[j]][i].moveForward(p, threadId);
}
}
pFinalNum = p;
#ifdef TEST
cout << threadId;
CURTIME(线程结束);
#endif
}
//# define OUTPUTSIZE (sizeof(char) * 20000000 * ((10+1)*7) + 10)
inline void search() {
#ifdef TEST
cout <<"nodeNum :" << nodeIDNum << endl;
cout <<"edgeNum :" << totalDataNum << endl;
#endif
/*
for(int i=0; i < nodeIDNum; i++){
ui id = i;
Node *node = getNodeSet(id);
if(!node->isInit){
continue;
}
unsigned long n1 = nodeLessInSize[id], n2 = nodeLargerOutSize[id];
unsigned long res = n1*n1*n1 + n2*n2*n2*n2;
totalCountSize += res > COUNTSIZE ? COUNTSIZE : res;
}
//sort(nodeIDList, nodeIDList+nodeSetSize);
*/
int curAddNum = 0;
for (int id = 0; id < nodeIDNum; id++) {
Node *node = getNodeSet(id), *inNode = getInNodeSet(id);
__builtin_prefetch(&nodeSetHashList[id+1], 0);
__builtin_prefetch(&inNodeList[id+1], 0);
if(!node->num){
inNode->num = 0;
inNode->nextList = nullptr;
continue;
}
if(!inNode->num){
node->num = 0;
node->nextList = nullptr;
continue;
}
if(curAddNum > GEOUP_DIVIDING_THRESH){
groupDividing[groupNum++] = id;
curAddNum = 0;
}
curAddNum += nodeSizeCount[id];
//nodeDivideTask(id, i);
nodeSort(*node);
}
/*
for(; gapId < THREADNUM; gapId++){
threadGap[gapId] = nodeIDNum;
}
*/
CURTIME(排序结束);
const int ret = pthread_spin_init(&lock, PTHREAD_PROCESS_PRIVATE);
assert(ret == 0);
pthread_t threads[THREADNUM];
//cout << nodeSet.size()<<endl;
for (int j = 1; j < THREADNUM; j++) {
const int *x = new int(j);
pthread_create(&threads[j], nullptr, search_thread, (void *) x);
}
search_thread(new int(0));
totalLen = resultLen[0];
for (int i = 1; i < THREADNUM; i++) {
pthread_join(threads[i], nullptr);
totalLen += resultLen[i];
}
#ifdef TEST
cout << totalLen << endl;
#endif
CURTIME(search);
}
# define OUTPUTSIZE sizeof(char) * totalLen * (32*3+3)
inline void outputResult(char *outputPath) {
//cout << OUTPUTSIZE << endl;
const int fd = open(outputPath, O_RDWR | O_CREAT, 0666);
#ifdef TEST
assert(fd >= 0);
#endif
int term = ftruncate(fd, OUTPUTSIZE);
#ifdef TEST
assert(term == 0);
#endif
output = (char*) mmap(nullptr, OUTPUTSIZE, PROT_WRITE, MAP_SHARED, fd, 0);
#ifdef TEST
assert(MAP_FAILED != output);
#endif
CURTIME(即将开始循环);
unsigned int p = 0;
writeNum(output, p, totalLen);
output[p-1] = '\n';
startLoc = p;
pthread_t threads[THREADNUM];
for (int j = 1; j < THREADNUM; j++) {
int *x = new int(j);
pthread_create(&threads[j], nullptr, outputResult_thread, (void *) x);
}
outputResult_thread(new int(0));
for (int i = 1; i < THREADNUM; i++) {
pthread_join(threads[i], nullptr);
}
p = pFinalNum;
CURTIME(output解析完成);
int t = munmap(output, OUTPUTSIZE);
#ifdef TEST
assert(t >= 0);
#endif
term = ftruncate(fd, p);
#ifdef TEST
assert(term == 0);
#endif
//int write_result = write(fd, output, p);
//assert(write_result == p);
close(fd);
}
int main() {