forked from vermaseren/form
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort.c
5118 lines (4997 loc) · 136 KB
/
sort.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
/** @file sort.c
*
* Contains the sort routines.
* We distinguish levels of sorting.
* The ground level is the sorting of terms in an expression.
* When a term has functions, the arguments can contain terms that need
* sorting, which this then done by raising the level. This can happen
* recursively. NewSort and EndSort automatically raise and lower the
* level. Because the ground level does some special things, sometimes
* we have to raise immediately to the second level skipping the ground level.
*
* Special routines for the parallel sorting are in the file threads.c
* Also the sorting of terms in polynomials is special but most of that is
* controlled by changing the address of the compare routine. Other routines
* relevant for adding rational polynomials are in the file polynito.c
*/
/* #[ License : */
/*
* Copyright (C) 1984-2023 J.A.M. Vermaseren
* When using this file you are requested to refer to the publication
* J.A.M.Vermaseren "New features of FORM" math-ph/0010025
* This is considered a matter of courtesy as the development was paid
* for by FOM the Dutch physics granting agency and we would like to
* be able to track its scientific use to convince FOM of its value
* for the community.
*
* This file is part of FORM.
*
* FORM is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* FORM is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along
* with FORM. If not, see <http://www.gnu.org/licenses/>.
*/
/* #] License : */
/*
#[ Includes : sort.c
Sort routines according to new conventions (25-jun-1997).
This is more object oriented.
The active sort is indicated by AT.SS which should agree with
AN.FunSorts[AR.sLevel];
#define GZIPDEBUG
*/
#define NEWSPLITMERGE
/* Comment to turn off Timsort in SplitMerge for debugging: */
#define NEWSPLITMERGETIMSORT
/* During SplitMerge, print pointer array state on entry. Very spammy, for debugging. */
/* #define SPLITMERGEDEBUG */
/* Debug printing for GarbHand */
/* #define TESTGARB */
#include "form3.h"
#ifdef WITHPTHREADS
UBYTE THRbuf[100];
#endif
#ifdef WITHSTATS
extern LONG numwrites;
extern LONG numreads;
extern LONG numseeks;
extern LONG nummallocs;
extern LONG numfrees;
#endif
LONG numcompares;
/*
#] Includes :
#[ SortUtilities :
#[ WriteStats : VOID WriteStats(lspace,par,checkLogType)
*/
char *toterms[] = { " ", " >>", "-->" };
/**
* Writes the statistics.
*
* @param plspace The size in bytes currently occupied
* @param par
* par = 0 = STATSSPLITMERGE after a splitmerge.
* par = 1 = STATSMERGETOFILE after merge to sortfile.
* par = 2 = STATSPOSTSORT after the sort
* @param checkLogType == CHECKLOGTYPE: The output should not
* go to the log file if AM.LogType is 1.
*
* current expression is to be found in AR.CurExpr.
* terms are in S->TermsLeft.
* S->GenTerms.
*/
VOID WriteStats(POSITION *plspace, WORD par, WORD checkLogType)
{
GETIDENTITY
LONG millitime, y = 0x7FFFFFFFL >> 1;
WORD timepart;
SORTING *S;
POSITION pp;
int use_wtime;
if ( AT.SS == AT.S0 && AC.StatsFlag ) {
#ifdef WITHPTHREADS
if ( AC.ThreadStats == 0 && identity > 0 ) return;
#elif defined(WITHMPI)
if ( AC.OldParallelStats ) return;
if ( ! AC.ProcessStats && PF.me != MASTER ) return;
#endif
if ( Expressions == 0 ) return;
if ( par == STATSSPLITMERGE ) {
if ( AC.ShortStatsMax == 0 ) return;
AR.ShortSortCount++;
if ( AR.ShortSortCount < AC.ShortStatsMax ) return;
}
AR.ShortSortCount = 0;
S = AT.SS;
MLOCK(ErrorMessageLock);
/* If the statistics should not go to the log file, temporarily hide the
* LogHandle. This must be done within the ErrorMessageLock region to
* avoid a data race between threads. */
const WORD oldLogHandle = AC.LogHandle;
if ( checkLogType && AM.LogType ) {
AC.LogHandle = -1;
}
if ( AC.ShortStats ) {}
else {
#ifdef WITHPTHREADS
if ( identity > 0 ) {
MesPrint(" Thread %d reporting",identity);
}
else {
MesPrint("");
}
#elif defined(WITHMPI)
if ( PF.me != MASTER ) {
MesPrint(" Process %d reporting",PF.me);
}
else {
MesPrint("");
}
#else
MesPrint("");
#endif
}
/*
* We define WTimeStatsFlag as a flag to print the wall-clock time on
* the *master*, not in workers. This can be confusing in thread
* statistics when short statistics is used. Technically,
* TimeWallClock() is not thread-safe in TFORM.
*/
use_wtime = AC.WTimeStatsFlag;
#if defined(WITHPTHREADS)
if ( use_wtime && identity > 0 ) use_wtime = 0;
#elif defined(WITHMPI)
if ( use_wtime && PF.me != MASTER ) use_wtime = 0;
#endif
millitime = use_wtime ? TimeWallClock(1) * 10 : TimeCPU(1);
timepart = (WORD)(millitime%1000);
millitime /= 1000;
timepart /= 10;
if ( AC.ShortStats ) {
#if defined(WITHPTHREADS) || defined(WITHMPI)
#ifdef WITHPTHREADS
if ( identity > 0 ) {
#else
if ( PF.me != MASTER ) {
const int identity = PF.me;
#endif
if ( par == STATSSPLITMERGE || par == STATSPOSTSORT ) {
SETBASEPOSITION(pp,y);
if ( ISLESSPOS(*plspace,pp) ) {
MesPrint("%d: %7l.%2is %8l>%10l%3s%10l:%10p %s %s",identity,
millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
/*
MesPrint("%d: %14s %17s %7l.%2is %8l>%10l%3s%10l:%10p",identity,
EXPRNAME(AR.CurExpr),AC.Commercial,millitime,timepart,
AN.ninterms,S->GenTerms,toterms[par],S->TermsLeft,plspace);
*/
}
else {
y = 1000000000L;
SETBASEPOSITION(pp,y);
MULPOS(pp,100);
if ( ISLESSPOS(*plspace,pp) ) {
MesPrint("%d: %7l.%2is %8l>%10l%3s%10l:%11p %s %s",identity,
millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
}
else {
MULPOS(pp,10);
if ( ISLESSPOS(*plspace,pp) ) {
MesPrint("%d: %7l.%2is %8l>%10l%3s%10l:%12p %s %s",identity,
millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
}
else {
MULPOS(pp,10);
if ( ISLESSPOS(*plspace,pp) ) {
MesPrint("%d: %7l.%2is %8l>%10l%3s%10l:%13p %s %s",identity,
millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
}
else {
MULPOS(pp,10);
if ( ISLESSPOS(*plspace,pp) ) {
MesPrint("%d: %7l.%2is %8l>%10l%3s%10l:%14p %s %s",identity,
millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
}
else {
MULPOS(pp,10);
if ( ISLESSPOS(*plspace,pp) ) {
MesPrint("%d: %7l.%2is %8l>%10l%3s%10l:%15p %s %s",identity,
millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
}
else {
MULPOS(pp,10);
if ( ISLESSPOS(*plspace,pp) ) {
MesPrint("%d: %7l.%2is %8l>%10l%3s%10l:%16p %s %s",identity,
millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
}
else {
MULPOS(pp,10);
if ( ISLESSPOS(*plspace,pp) ) {
MesPrint("%d: %7l.%2is %8l>%10l%3s%10l:%17p %s %s",identity,
millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
}
} } } } }
}
}
}
else if ( par == STATSMERGETOFILE ) {
SETBASEPOSITION(pp,y);
if ( ISLESSPOS(*plspace,pp) ) {
MesPrint("%d: %7l.%2is %10l:%10p",identity,millitime,timepart,
S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
}
else {
y = 1000000000L;
SETBASEPOSITION(pp,y);
MULPOS(pp,100);
if ( ISLESSPOS(*plspace,pp) ) {
MesPrint("%d: %7l.%2is %10l:%11p",identity,millitime,timepart,
S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
}
else {
MULPOS(pp,10);
if ( ISLESSPOS(*plspace,pp) ) {
MesPrint("%d: %7l.%2is %10l:%12p",identity,millitime,timepart,
S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
}
else {
MULPOS(pp,10);
if ( ISLESSPOS(*plspace,pp) ) {
MesPrint("%d: %7l.%2is %10l:%13p",identity,millitime,timepart,
S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
}
else {
MULPOS(pp,10);
if ( ISLESSPOS(*plspace,pp) ) {
MesPrint("%d: %7l.%2is %10l:%14p",identity,millitime,timepart,
S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
}
else {
MULPOS(pp,10);
if ( ISLESSPOS(*plspace,pp) ) {
MesPrint("%d: %7l.%2is %10l:%15p",identity,millitime,timepart,
S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
}
else {
MULPOS(pp,10);
if ( ISLESSPOS(*plspace,pp) ) {
MesPrint("%d: %7l.%2is %10l:%16p",identity,millitime,timepart,
S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
}
else {
MULPOS(pp,10);
if ( ISLESSPOS(*plspace,pp) ) {
MesPrint("%d: %7l.%2is %10l:%17p",identity,millitime,timepart,
S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
}
} } } } }
}
}
} } else
#endif
{
if ( par == STATSSPLITMERGE || par == STATSPOSTSORT ) {
SETBASEPOSITION(pp,y);
if ( ISLESSPOS(*plspace,pp) ) {
MesPrint("%7l.%2is %8l>%10l%3s%10l:%10p %s %s",
millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
/*
MesPrint("%14s %17s %7l.%2is %8l>%10l%3s%10l:%10p",
EXPRNAME(AR.CurExpr),AC.Commercial,millitime,timepart,
AN.ninterms,S->GenTerms,toterms[par],S->TermsLeft,plspace);
*/
}
else {
y = 1000000000L;
SETBASEPOSITION(pp,y);
MULPOS(pp,100);
if ( ISLESSPOS(*plspace,pp) ) {
MesPrint("%7l.%2is %8l>%10l%3s%10l:%11p %s %s",
millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
}
else {
MULPOS(pp,10);
if ( ISLESSPOS(*plspace,pp) ) {
MesPrint("%7l.%2is %8l>%10l%3s%10l:%12p %s %s",
millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
}
else {
MULPOS(pp,10);
if ( ISLESSPOS(*plspace,pp) ) {
MesPrint("%7l.%2is %8l>%10l%3s%10l:%13p %s %s",
millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
}
else {
MULPOS(pp,10);
if ( ISLESSPOS(*plspace,pp) ) {
MesPrint("%7l.%2is %8l>%10l%3s%10l:%14p %s %s",
millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
}
else {
MULPOS(pp,10);
if ( ISLESSPOS(*plspace,pp) ) {
MesPrint("%7l.%2is %8l>%10l%3s%10l:%15p %s %s",
millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
}
else {
MULPOS(pp,10);
if ( ISLESSPOS(*plspace,pp) ) {
MesPrint("%7l.%2is %8l>%10l%3s%10l:%16p %s %s",
millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
}
else {
MULPOS(pp,10);
if ( ISLESSPOS(*plspace,pp) ) {
MesPrint("%7l.%2is %8l>%10l%3s%10l:%17p %s %s",
millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
}
} } } } }
}
}
}
else if ( par == STATSMERGETOFILE ) {
SETBASEPOSITION(pp,y);
if ( ISLESSPOS(*plspace,pp) ) {
MesPrint("%7l.%2is %10l:%10p",millitime,timepart,
S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
}
else {
y = 1000000000L;
SETBASEPOSITION(pp,y);
MULPOS(pp,100);
if ( ISLESSPOS(*plspace,pp) ) {
MesPrint("%7l.%2is %10l:%11p",millitime,timepart,
S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
}
else {
MULPOS(pp,10);
if ( ISLESSPOS(*plspace,pp) ) {
MesPrint("%7l.%2is %10l:%12p",millitime,timepart,
S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
}
else {
MULPOS(pp,10);
if ( ISLESSPOS(*plspace,pp) ) {
MesPrint("%7l.%2is %10l:%13p",millitime,timepart,
S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
}
else {
MULPOS(pp,10);
if ( ISLESSPOS(*plspace,pp) ) {
MesPrint("%7l.%2is %10l:%14p",millitime,timepart,
S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
}
else {
MULPOS(pp,10);
if ( ISLESSPOS(*plspace,pp) ) {
MesPrint("%7l.%2is %10l:%15p",millitime,timepart,
S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
}
else {
MULPOS(pp,10);
if ( ISLESSPOS(*plspace,pp) ) {
MesPrint("%7l.%2is %10l:%16p",millitime,timepart,
S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
}
else {
MULPOS(pp,10);
if ( ISLESSPOS(*plspace,pp) ) {
MesPrint("%7l.%2is %10l:%17p",millitime,timepart,
S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
}
} } } } }
}
}
}
} }
else {
if ( par == STATSMERGETOFILE ) {
if ( use_wtime ) {
MesPrint("WTime = %7l.%2i sec",millitime,timepart);
}
else {
MesPrint("Time = %7l.%2i sec",millitime,timepart);
}
}
else {
#if ( BITSINLONG > 32 )
if ( S->GenTerms >= 10000000000L ) {
if ( use_wtime ) {
MesPrint("WTime = %7l.%2i sec Generated terms = %16l",
millitime,timepart,S->GenTerms);
}
else {
MesPrint("Time = %7l.%2i sec Generated terms = %16l",
millitime,timepart,S->GenTerms);
}
}
else {
if ( use_wtime ) {
MesPrint("WTime = %7l.%2i sec Generated terms = %10l",
millitime,timepart,S->GenTerms);
}
else {
MesPrint("Time = %7l.%2i sec Generated terms = %10l",
millitime,timepart,S->GenTerms);
}
}
#else
if ( use_wtime ) {
MesPrint("WTime = %7l.%2i sec Generated terms = %10l",
millitime,timepart,S->GenTerms);
}
else {
MesPrint("Time = %7l.%2i sec Generated terms = %10l",
millitime,timepart,S->GenTerms);
}
#endif
}
#if ( BITSINLONG > 32 )
if ( par == STATSSPLITMERGE )
if ( S->TermsLeft >= 10000000000L ) {
MesPrint("%16s%8l Terms %s = %16l",EXPRNAME(AR.CurExpr),
AN.ninterms,FG.swmes[par],S->TermsLeft);
}
else {
MesPrint("%16s%8l Terms %s = %10l",EXPRNAME(AR.CurExpr),
AN.ninterms,FG.swmes[par],S->TermsLeft);
}
else {
if ( S->TermsLeft >= 10000000000L ) {
#ifdef WITHPTHREADS
if ( identity > 0 && par == STATSPOSTSORT ) {
MesPrint("%16s Terms in thread = %16l",
EXPRNAME(AR.CurExpr),S->TermsLeft);
}
else
#elif defined(WITHMPI)
if ( PF.me != MASTER && par == STATSPOSTSORT ) {
MesPrint("%16s Terms in process= %16l",
EXPRNAME(AR.CurExpr),S->TermsLeft);
}
else
#endif
{
MesPrint("%16s Terms %s = %16l",
EXPRNAME(AR.CurExpr),FG.swmes[par],S->TermsLeft);
}
}
else {
#ifdef WITHPTHREADS
if ( identity > 0 && par == STATSPOSTSORT ) {
MesPrint("%16s Terms in thread = %10l",
EXPRNAME(AR.CurExpr),S->TermsLeft);
}
else
#elif defined(WITHMPI)
if ( PF.me != MASTER && par == STATSPOSTSORT ) {
MesPrint("%16s Terms in process= %10l",
EXPRNAME(AR.CurExpr),S->TermsLeft);
}
else
#endif
{
MesPrint("%16s Terms %s = %10l",
EXPRNAME(AR.CurExpr),FG.swmes[par],S->TermsLeft);
}
}
}
#else
if ( par == STATSSPLITMERGE )
MesPrint("%16s%8l Terms %s = %10l",EXPRNAME(AR.CurExpr),
AN.ninterms,FG.swmes[par],S->TermsLeft);
else {
#ifdef WITHPTHREADS
if ( identity > 0 && par == STATSPOSTSORT ) {
MesPrint("%16s Terms in thread = %10l",
EXPRNAME(AR.CurExpr),S->TermsLeft);
}
else
#elif defined(WITHMPI)
if ( PF.me != MASTER && par == STATSPOSTSORT ) {
MesPrint("%16s Terms in process= %10l",
EXPRNAME(AR.CurExpr),S->TermsLeft);
}
else
#endif
{
MesPrint("%16s Terms %s = %10l",
EXPRNAME(AR.CurExpr),FG.swmes[par],S->TermsLeft);
}
}
#endif
SETBASEPOSITION(pp,y);
if ( ISLESSPOS(*plspace,pp) ) {
MesPrint("%24s Bytes used = %10p",AC.Commercial,plspace);
}
else {
y = 1000000000L;
SETBASEPOSITION(pp,y);
MULPOS(pp,100);
if ( ISLESSPOS(*plspace,pp) ) {
MesPrint("%24s Bytes used =%11p",AC.Commercial,plspace);
}
else {
MULPOS(pp,10);
if ( ISLESSPOS(*plspace,pp) ) {
MesPrint("%24s Bytes used =%12p",AC.Commercial,plspace);
}
else {
MULPOS(pp,10);
if ( ISLESSPOS(*plspace,pp) ) {
MesPrint("%24s Bytes used =%13p",AC.Commercial,plspace);
}
else {
MULPOS(pp,10);
if ( ISLESSPOS(*plspace,pp) ) {
MesPrint("%24s Bytes used =%14p",AC.Commercial,plspace);
}
else {
MULPOS(pp,10);
if ( ISLESSPOS(*plspace,pp) ) {
MesPrint("%24s Bytes used =%15p",AC.Commercial,plspace);
}
else {
MULPOS(pp,10);
if ( ISLESSPOS(*plspace,pp) ) {
MesPrint("%24s Bytes used =%16p",AC.Commercial,plspace);
}
else {
MULPOS(pp,10);
if ( ISLESSPOS(*plspace,pp) ) {
MesPrint("%24s Bytes used=%17p",AC.Commercial,plspace);
}
} } } } }
}
} }
#ifdef WITHSTATS
MesPrint("Total number of writes: %l, reads: %l, seeks, %l"
,numwrites,numreads,numseeks);
MesPrint("Total number of mallocs: %l, frees: %l"
,nummallocs,numfrees);
#endif
/* Put back the original LogHandle, it was changed if the statistics were
* not printed in the log file. */
AC.LogHandle = oldLogHandle;
MUNLOCK(ErrorMessageLock);
}
}
/*
#] WriteStats :
#[ NewSort : WORD NewSort()
*/
/**
* Starts a new sort.
* At the lowest level this is a 'main sort' with the struct according
* to the parameters in S0. At higher levels this is a sort for
* functions, subroutines or dollars.
* We prepare the arrays and structs.
*
* @return Regular convention (OK -> 0)
*/
WORD NewSort(PHEAD0)
{
GETBIDENTITY
SORTING *S, **newFS;
int i, newsize;
if ( AN.SoScratC == 0 )
AN.SoScratC = (UWORD *)Malloc1(2*(AM.MaxTal+2)*sizeof(UWORD),"NewSort");
AR.sLevel++;
if ( AR.sLevel >= AN.NumFunSorts ) {
if ( AN.NumFunSorts == 0 ) newsize = 100;
else newsize = 2*AN.NumFunSorts;
newFS = (SORTING **)Malloc1((newsize+1)*sizeof(SORTING *),"FunSort pointers");
for ( i = 0; i < AN.NumFunSorts; i++ ) newFS[i] = AN.FunSorts[i];
for ( ; i <= newsize; i++ ) newFS[i] = 0;
if ( AN.FunSorts ) M_free(AN.FunSorts,"FunSort pointers");
AN.FunSorts = newFS; AN.NumFunSorts = newsize;
}
if ( AR.sLevel == 0 ) {
numcompares = 0;
AN.FunSorts[0] = AT.S0;
if ( AR.PolyFun == 0 ) { AT.S0->PolyFlag = 0; }
else if ( AR.PolyFunType == 1 ) { AT.S0->PolyFlag = 1; }
else if ( AR.PolyFunType == 2 ) {
if ( AR.PolyFunExp == 2
|| AR.PolyFunExp == 3 ) AT.S0->PolyFlag = 1;
else AT.S0->PolyFlag = 2;
}
AR.ShortSortCount = 0;
}
else {
if ( AN.FunSorts[AR.sLevel] == 0 ) {
AN.FunSorts[AR.sLevel] = AllocSort(
AM.SLargeSize,AM.SSmallSize,AM.SSmallEsize,AM.STermsInSmall
,AM.SMaxPatches,AM.SMaxFpatches,AM.SIOsize);
}
AN.FunSorts[AR.sLevel]->PolyFlag = 0;
}
AT.SS = S = AN.FunSorts[AR.sLevel];
S->sFill = S->sBuffer;
S->lFill = S->lBuffer;
S->lPatch = 0;
S->fPatchN = 0;
S->GenTerms = S->TermsLeft = S->GenSpace = S->SpaceLeft = 0;
S->PoinFill = S->sPointer;
*S->PoinFill = S->sFill;
if ( AR.sLevel > 0 ) { S->PolyWise = 0; }
PUTZERO(S->SizeInFile[0]); PUTZERO(S->SizeInFile[1]); PUTZERO(S->SizeInFile[2]);
S->sTerms = 0;
PUTZERO(S->file.POposition);
S->stage4 = 0;
if ( AR.sLevel > AN.MaxFunSorts ) AN.MaxFunSorts = AR.sLevel;
/*
The next variable is for the staged sort only.
It should be treated differently
PUTZERO(AN.OldPosOut);
*/
return(0);
}
/*
#] NewSort :
#[ EndSort : WORD EndSort(PHEAD buffer,par)
*/
/**
* Finishes a sort.
* At AR.sLevel == 0 the output is to the regular output stream.
* When AR.sLevel > 0, the parameter par determines the actual output.
* The AR.sLevel will be popped.
* All ongoing stages are finished and if the sortfile is open
* it is closed.
* The statistics are printed when AR.sLevel == 0
* par == 0 Output to the buffer.
* par == 1 Sort for function arguments.
* The output will be copied into the buffer.
* It is assumed that this is in the WorkSpace.
* par == 2 Sort for $-variable. We return the address of the buffer
* that contains the output in buffer (treated like WORD **).
* We first catch the output in a file (unless we can
* intercept things after the small buffer has been sorted)
* Then we read from the file into a buffer.
* Only when par == 0 data compression can be attempted at AT.SS==AT.S0.
*
* @param buffer buffer for output when needed
* @param par See above
* @return If negative: error. If positive: number of words in output.
*/
LONG EndSort(PHEAD WORD *buffer, int par)
{
MesPrint("EndSort par %d", par);
GETBIDENTITY
SORTING *S = AT.SS;
WORD j, **ss, *to, *t;
LONG sSpace, over, tover, spare, retval = 0, jj;
POSITION position, pp;
off_t lSpace;
FILEHANDLE *fout = 0, *oldoutfile = 0, *newout = 0;
if ( AM.exitflag && AR.sLevel == 0 ) return(0);
#ifdef WITHMPI
if( (retval = PF_EndSort()) > 0){
oldoutfile = AR.outfile;
retval = 0;
goto RetRetval;
}
else if(retval < 0){
retval = -1;
goto RetRetval;
}
/* PF_EndSort returned 0: for S != AM.S0 and slaves still do the regular sort */
#endif /* WITHMPI */
oldoutfile = AR.outfile;
/* PolyFlag repair action
if ( S == AT.S0 ) {
if ( AR.PolyFun == 0 ) { S->PolyFlag = 0; }
else if ( AR.PolyFunType == 1 ) { S->PolyFlag = 1; }
else if ( AR.PolyFunType == 2 ) {
if ( AR.PolyFunExp == 2
|| AR.PolyFunExp == 3 ) S->PolyFlag = 1;
else S->PolyFlag = 2;
}
S->PolyWise = 0;
}
else {
S->PolyFlag = S->PolyWise = 0;
}
*/
S->PolyWise = 0;
*(S->PoinFill) = 0;
#ifdef SPLITTIME
PrintTime((UBYTE *)"EndSort, before SplitMerge");
#endif
S->sPointer[SplitMerge(BHEAD S->sPointer,S->sTerms)] = 0;
#ifdef SPLITTIME
PrintTime((UBYTE *)"Endsort, after SplitMerge");
#endif
sSpace = 0;
tover = over = S->sTerms;
ss = S->sPointer;
if ( over >= 0 ) {
if ( S->lPatch > 0 || S->file.handle >= 0 ) {
ss[over] = 0;
MesPrint("EndSort call ComPress");
sSpace = ComPress(ss,&spare);
S->TermsLeft -= over - spare;
if ( par == 1 ) { AR.outfile = newout = AllocFileHandle(0,(char *)0); }
}
else if ( S != AT.S0 ) {
ss[over] = 0;
if ( par == 2 ) {
sSpace = 3;
while ( ( t = *ss++ ) != 0 ) { sSpace += *t; }
if ( AN.tryterm > 0 && ( (sSpace+1)*sizeof(WORD) < (size_t)(AM.MaxTer) ) ) {
to = TermMalloc("$-sort space");
}
else {
LONG allocsp = sSpace+1;
if ( allocsp < MINALLOC ) allocsp = MINALLOC;
allocsp = ((allocsp+7)/8)*8;
to = (WORD *)Malloc1(allocsp*sizeof(WORD),"$-sort space");
if ( AN.tryterm > 0 ) AN.tryterm = 0;
}
*((WORD **)buffer) = to;
ss = S->sPointer;
while ( ( t = *ss++ ) != 0 ) {
j = *t; while ( --j >= 0 ) *to++ = *t++;
}
*to = 0;
retval = sSpace + 1;
}
else {
to = buffer;
sSpace = 0;
while ( ( t = *ss++ ) != 0 ) {
j = *t;
if ( ( sSpace += j ) > AM.MaxTer/((LONG)sizeof(WORD)) ) {
MLOCK(ErrorMessageLock);
MesPrint("Sorted function argument too long.");
MUNLOCK(ErrorMessageLock);
retval = -1; goto RetRetval;
}
while ( --j >= 0 ) *to++ = *t++;
}
*to = 0;
}
goto RetRetval;
}
else {
POSITION oldpos;
if ( S == AT.S0 ) {
fout = AR.outfile;
*AR.CompressPointer = 0;
SeekScratch(AR.outfile,&position);
}
else {
fout = &(S->file);
PUTZERO(position);
}
oldpos = position;
S->TermsLeft = 0;
/*
Here we can go directly to the output.
*/
#ifdef WITHZLIB
{ int oldgzipCompress = AR.gzipCompress;
AR.gzipCompress = 0;
#endif
if ( tover > 0 ) {
ss = S->sPointer;
while ( ( t = *ss++ ) != 0 ) {
if ( *t ) S->TermsLeft++;
#ifdef WITHPTHREADS
if ( AS.MasterSort && ( fout == AR.outfile ) ) { PutToMaster(BHEAD t); }
else
#endif
if ( PutOut(BHEAD t,&position,fout,1) < 0 ) {
retval = -1; goto RetRetval;
}
}
}
#ifdef WITHPTHREADS
if ( AS.MasterSort && ( fout == AR.outfile ) ) { PutToMaster(BHEAD 0); }
else
#endif
if ( FlushOut(&position,fout,1) ) {
retval = -1; goto RetRetval;
}
#ifdef WITHZLIB
AR.gzipCompress = oldgzipCompress;
}
#endif
#ifdef WITHPTHREADS
if ( AS.MasterSort && ( fout == AR.outfile ) ) goto RetRetval;
#endif
#ifdef WITHMPI
if ( PF.me != MASTER && PF.exprtodo < 0 ) goto RetRetval;
#endif
DIFPOS(oldpos,position,oldpos);
S->SpaceLeft = BASEPOSITION(oldpos);
WriteStats(&oldpos,STATSPOSTSORT,NOCHECKLOGTYPE);
pp = oldpos;
goto RetRetval;
}
}
else if ( par == 1 && newout == 0 ) { AR.outfile = newout = AllocFileHandle(0,(char *)0); }
sSpace++;
lSpace = sSpace + (S->lFill - S->lBuffer) - (LONG)S->lPatch*(AM.MaxTer/sizeof(WORD));
/* Note wrt MaxTer and lPatch: each patch starts with space for decompression */
/* Not needed if only large buffer, but needed when using files (?) */
SETBASEPOSITION(pp,lSpace);
MULPOS(pp,sizeof(WORD));
if ( S->file.handle >= 0 ) {
ADD2POS(pp,S->fPatches[S->fPatchN]);
}
if ( S == AT.S0 ) {
if ( S->lPatch > 0 || S->file.handle >= 0 ) {
WriteStats(&pp,STATSSPLITMERGE,CHECKLOGTYPE);
}
}
if ( par == 2 ) { AR.outfile = newout = AllocFileHandle(0,(char *)0); }
if ( S->lPatch > 0 ) {
if ( ( S->lPatch >= S->MaxPatches ) ||
( ( (WORD *)(((UBYTE *)(S->lFill + sSpace)) + 2*AM.MaxTer) ) >= S->lTop ) ) {
/*
The large buffer is too full. Merge and write it
*/
#ifdef GZIPDEBUG
MLOCK(ErrorMessageLock);
MesPrint("%w EndSort: lPatch = %d, MaxPatches = %d,lFill = %x, sSpace = %ld, MaxTer = %d, lTop = %x"
,S->lPatch,S->MaxPatches,S->lFill,sSpace,AM.MaxTer/sizeof(WORD),S->lTop);
MUNLOCK(ErrorMessageLock);
#endif
if ( MergePatches(1) ) {
MLOCK(ErrorMessageLock);
MesCall("EndSort");
MUNLOCK(ErrorMessageLock);
retval = -1; goto RetRetval;
}
S->lPatch = 0;
pp = S->SizeInFile[1];
MULPOS(pp,sizeof(WORD));
#ifndef WITHPTHREADS
if ( S == AT.S0 )
#endif
{
POSITION pppp;
SETBASEPOSITION(pppp,0);
SeekFile(S->file.handle,&pppp,SEEK_CUR);
SeekFile(S->file.handle,&pp,SEEK_END);
SeekFile(S->file.handle,&pppp,SEEK_SET);
WriteStats(&pp,STATSMERGETOFILE,CHECKLOGTYPE);
UpdateMaxSize();
}
}
else {
S->Patches[S->lPatch++] = S->lFill;
to = (WORD *)(((UBYTE *)(S->lFill)) + AM.MaxTer);
if ( tover > 0 ) {
ss = S->sPointer;
while ( ( t = *ss++ ) != 0 ) {
j = *t;
if ( j < 0 ) j = t[1] + 2;
while ( --j >= 0 ) *to++ = *t++;
}
}
*to++ = 0;
S->lFill = to;
if ( S->file.handle < 0 ) {
if ( MergePatches(2) ) {
MLOCK(ErrorMessageLock);
MesCall("EndSort");
MUNLOCK(ErrorMessageLock);
retval = -1; goto RetRetval;
}
if ( S == AT.S0 ) {
pp = S->SizeInFile[2];
MULPOS(pp,sizeof(WORD));
#ifdef WITHPTHREADS
if ( AS.MasterSort && ( fout == AR.outfile ) ) goto RetRetval;
#endif
WriteStats(&pp,STATSPOSTSORT,NOCHECKLOGTYPE);
UpdateMaxSize();
}
else {
if ( par == 2 && newout->handle >= 0 ) {
POSITION zeropos;
PUTZERO(zeropos);
#ifdef ALLLOCK
LOCK(newout->pthreadslock);
#endif
SeekFile(newout->handle,&zeropos,SEEK_SET);
to = (WORD *)Malloc1(BASEPOSITION(newout->filesize)+sizeof(WORD)*2
,"$-buffer reading");
if ( AN.tryterm > 0 ) AN.tryterm = 0;
if ( ( retval = ReadFile(newout->handle,(UBYTE *)to,BASEPOSITION(newout->filesize)) ) !=
BASEPOSITION(newout->filesize) ) {
MLOCK(ErrorMessageLock);
MesPrint("Error reading information for $ variable");
MUNLOCK(ErrorMessageLock);
M_free(to,"$-buffer reading");
retval = -1;
}
else {
*((WORD **)buffer) = to;
retval /= sizeof(WORD);
}
#ifdef ALLLOCK
UNLOCK(newout->pthreadslock);
#endif
}
else if ( newout->handle >= 0 ) {
/*
We land here if par == 1 (function arg sort) and PutOut has created a file.
This means that the term is larger than SIOsize, which we ensure is at least
as large as MaxTermSize in setfile.c. Thus we know already that the term won't fit.
*/
TooLarge:
MLOCK(ErrorMessageLock);
MesPrint("(1)Output should fit inside a single term. Increase MaxTermSize?");
MesCall("EndSort");
MUNLOCK(ErrorMessageLock);
retval = -1; goto RetRetval;
}
else {
t = newout->PObuffer;
if ( par == 2 ) {
jj = newout->POfill - t;
if ( AN.tryterm > 0 && ( (jj+2)*sizeof(WORD) < (size_t)(AM.MaxTer) ) ) {
to = TermMalloc("$-sort space");
}
else {
LONG allocsp = jj+2;
if ( allocsp < MINALLOC ) allocsp = MINALLOC;
allocsp = ((allocsp+7)/8)*8;
to = (WORD *)Malloc1(allocsp*sizeof(WORD),"$-sort space");
if ( AN.tryterm > 0 ) AN.tryterm = 0;
}
*((WORD **)buffer) = to;
NCOPY(to,t,jj);
}
else {
j = newout->POfill - t;
to = buffer;