-
Notifications
You must be signed in to change notification settings - Fork 3
/
portable-rank-and-sort.lisp
1048 lines (823 loc) · 35.1 KB
/
portable-rank-and-sort.lisp
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
;;; -*- SYNTAX: COMMON-LISP; MODE: LISP; BASE: 10; PACKAGE: *SIM-I -*-
(in-package :*sim-i)
;;;> *+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+
;;;>
;;;> The Thinking Machines *Lisp Simulator is in the public domain.
;;;> You are free to do whatever you like with it, including but
;;;> not limited to distributing, modifying, and copying.
;;;> Bugs, comments and revisions due to porting can be sent to:
;;;> bug-starlisp@think.com. Other than to Thinking Machines'
;;;> customers, no promise of support is intended or implied.
;;;>
;;;> *+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+
;;; Author: JP Massar.
;;;; Description:
;;;; RANK!!
;;;; This function is just like old RANK!! except that the user may specify
;;;; either a segment-pvar or an axis (dimension) or both. If a segment-pvar alone
;;;; is specified then the ranking is done independently within each
;;;; segment. If an axis alone is specified then the ranking is done
;;;; independently along each row of that dimension. If both are
;;;; specified then the ranking is done independently within each
;;;; segment, the segments all being defined within a single row.
;;;; Example: (rank!! (random!! (!! 10)) '<=!! :segment-pvar (evenp!! (self-address!!)))
;;;; If the first 12 random elements were
;;;;
;;;; 0 2 4 2 1 7 5 3 4 7 8 2
;;;;
;;;; then the result would be
;;;;
;;;; 0 1 1 0 0 1 1 0 0 1 1 0
;;;; NEW-SORT!!
;;;; This function is just like old SORT!! except that the user may specify
;;;; either a segment-pvar or an axis (dimension) or both. See NEW-RANK!!
;;;; above for a discussion of what each specification does. NEW-SORT!!
;;;; also provides the user the ability to sort a pvar given a key
;;;; function. This key function might be, for example, a *DEFSTRUCT
;;;; structure accessor, and the pvar to be sorted would be an instance
;;;; of the *DEFSTRUCT.
;;;; Example: Using the above example except that sorting is being
;;;; done instead of ranking, the result would be:
;;;; 0 2 2 4 1 7 3 5 4 7 2 8
;;;; Example: If AFOO were an instance of a *DEFSTRUCT with slot FOO-A!!,
;;;; then
;;;; (SORT!! AFOO '<=!! :DIMENSION 0 :KEY 'FOO-A!!)
;;;; Would sort AFOO using its A slot as the key, independently along each
;;;; row of dimension 0 (the X dimension).
;;;; (SEGMENTED-SPREAD!! PVAR DIMENSION-CONSTANT SEGMENT-PVAR ELEMENT-PVAR)
;;;; This is similar to SPREAD!!. However, spreading can be done from
;;;; every defined segment, and the value to be spread is designated
;;;; within each segment using the boolean pvar element-pvar.
;;;; Example:
;;;; (segmented-spread!! pvar nil (zerop!! (mod!! (self-address!!) (!! 10))) (random!! (!! 5)))
;;;; This returns a pvar which has either the 0th, 1st, 2nd, 3rd or 4th
;;;; element of each segment defined by (zerop!! (mod!! (self-address!!) (!! 10)))
;;;; (i.e., every 10 processors constitute a segment), as the value
;;;; for the entire segment.
(*proclaim '(ftype (function (t) boolean-pvar) t!!-in-first-active-processor-along-row))
(defun t!!-in-first-active-processor-along-row (dimension-constant)
;; Return a pvar which is 1 in the first active processor
;; of every row along a particular dimension.
;; The trick is to plus scan across the dimension using 1 as
;; the source, and noting which processors still have a 1
;; after the scan.
(*compile-blindly
(*let (result temp)
(declare (type boolean-pvar result))
(declare (type (field-pvar *current-send-address-length*) temp))
(declare (return-pvar-p t))
nil
(*all (*set result nil!!))
(*set temp (scan!! (!! 1) '+!! :dimension dimension-constant))
(*set result (=!! (!! 1) temp))
result
)))
(defvar *stable-rank* nil)
(defun rank!! (pvar predicate &key dimension segment-pvar)
(simple-pvar-argument!! pvar &opt segment-pvar)
(safety-check
(new-vp-pvar-check pvar 'rank!!)
(when segment-pvar (new-vp-pvar-check segment-pvar 'rank!!))
(when (eq predicate (symbol-function '<=!!)) (setq predicate '<=!!))
(when (not (eq predicate '<=!!))
(error "Implementation limitation. Predicate ~S is invalid. Predicate must be <=!!" predicate))
(when dimension
(when (or (not (integerp dimension)) (not (< -1 dimension *number-of-dimensions*)))
(error "You specified dimension ~S, but there are ~D dimensions in the current vp set"
dimension *number-of-dimensions*
))))
(without-void-pvars (pvar)
(internal-rank!! pvar predicate dimension segment-pvar)
))
(defun stable-rank!! (pvar predicate &key dimension segment-pvar)
(let ((*stable-rank* t))
(declare (special *stable-rank*))
(rank!! pvar predicate :dimension dimension :segment-pvar segment-pvar)
))
#+*LISP-HARDWARE
(defun internal-rank!! (pvar predicate dimension segment-pvar)
(case (pvar-type pvar)
(:general (general-rank!! pvar predicate dimension segment-pvar))
;; Turn whatever pvar it is that is to be ranked into an
;; equivalent (from the point of view of ranking) unsigned
;; pvar. Turn the segment pvar into a boolean pvar if it
;; is not a boolean pvar already.
;; finally, dispatch to the proper routine depending on the
;; kind of scan we are doing (segmented vs unsegmented, grid vs cube)
(t
(let ((canonical-value (canonical-value-for-rank pvar))
(canonical-segment (if segment-pvar (canonical-segment segment-pvar) nil))
)
(internal-unsigned-rank!! canonical-value dimension segment-pvar canonical-segment)
))))
#+*LISP-SIMULATOR
(defun internal-rank!! (pvar predicate dimension segment-pvar)
(cond
((*and (and!! (integerp!! pvar) (not!! (minusp!! pvar))))
(let ((canonical-segment (if segment-pvar (canonical-segment segment-pvar) nil))
(canonical-value pvar)
)
(internal-unsigned-rank!! canonical-value dimension segment-pvar canonical-segment)
))
((*and (integerp!! pvar))
(let ((canonical-segment (if segment-pvar (canonical-segment segment-pvar) nil))
(canonical-value (signed-to-ordered-bits!! pvar))
)
(internal-unsigned-rank!! canonical-value dimension segment-pvar canonical-segment)
))
((*and (floatp!! pvar))
(let ((canonical-segment (if segment-pvar (canonical-segment segment-pvar) nil))
(canonical-value (float-to-ordered-bits!! pvar))
)
(internal-unsigned-rank!! canonical-value dimension segment-pvar canonical-segment)
))
(t (general-rank!! pvar predicate dimension segment-pvar))
))
(defun internal-unsigned-rank!! (canonical-value dimension segment-pvar canonical-segment)
(cond
((and (null dimension) (null segment-pvar)) (unsegmented-cube-rank!!-internal canonical-value))
((and (null dimension) segment-pvar) (segmented-cube-rank!!-internal canonical-value canonical-segment))
((and dimension (null segment-pvar)) (grid-rank!!-internal canonical-value dimension))
((and dimension segment-pvar) (segmented-grid-rank!!-internal canonical-value dimension canonical-segment))
))
#+*LISP-HARDWARE
(defun unsegmented-cube-rank!!-internal (field-pvar)
(*compile-blindly
(let ((result (allocate-field-pvar *current-send-address-length*)))
(*set (the (field-pvar *current-send-address-length*) result)
(rank!! (the (field-pvar *) field-pvar) '<=!!))
result
)))
#+*LISP-SIMULATOR
(defun unsegmented-cube-rank!!-internal (pvar)
(let* ((value-and-rank-list nil)
(count 0)
(pvar-array (pvar-array pvar))
(return-pvar (allocate-temp-general-pvar))
(return-array (pvar-array return-pvar))
)
(with-simple-vectors (return-array)
(do-for-selected-processors-internal (j)
(push (cons (aref pvar-array j) j) value-and-rank-list)
)
(setq value-and-rank-list (nreverse value-and-rank-list))
(setq value-and-rank-list
(if *stable-rank*
(stable-sort value-and-rank-list #'< :key #'car)
(sort value-and-rank-list #'<= :key #'car)
))
(do-for-selected-processors-internal (j)
(setf (aref return-array (cdr (car value-and-rank-list)))
count)
(incf count)
(pop value-and-rank-list)
)
return-pvar
)))
(defun segmented-cube-rank!!-internal (source segment)
(segmented-grid-rank!!-internal source nil segment)
)
(defun grid-rank!!-internal (pvar dimension)
;; Make segments out of the rows and then do a segmented grid rank.
(segmented-grid-rank!!-internal pvar dimension (t!!-in-first-active-processor-along-row dimension))
)
(defun segmented-grid-rank!!-internal (pvar dimension segment-pvar)
;; Enumerate the start bits of the segment pvar.
;; This uniquely identifies each segment.
;; Scan out this tag to every processor in the segment.
;; Select every processor, and identify those
;; that were previously selected.
;; Append the enumeration tag and a bit which indicates
;; whether the processor was originally active or not
;; as the high order bits of the source
;; so that source values in the same segment will get
;; ranked next to each other, and so that inactive
;; processors will all have higher rankings per
;; segment than inactive processors.
;; Rank this concatenation across the whole machine.
;; Figure out where the end of each segment is.
;; Finally, scan across the segments forward and
;; backwards to get the minimum rank per segment
;; in every processor of the segment. Then
;; subtract this minimum ranking from the
;; processor's rank value, to produce the ranking
;; relative to the segment.
(*locally
(declare (type (field-pvar *) pvar))
(declare (type boolean-pvar segment-pvar))
(*compile-blindly
(*let (enumeration result min-result css segment-start segment-end pvar-copy)
(declare (type (field-pvar *current-send-address-length*) enumeration result min-result))
(declare (type boolean-pvar css segment-start segment-end))
(declare (type (field-pvar *) pvar-copy))
(declare (return-pvar-p t))
nil
(*all
(*set css nil!!)
(*set segment-start nil!!)
(if dimension
(*set segment-start (zerop!! (self-address-grid!! (!! (the fixnum dimension)))))
(*setf (pref segment-start 0) t)
)
(*set segment-end nil!!)
)
(*set css t!!)
(if (*and (null!! segment-pvar)) ;; if there are no segments selected
(*setf (pref segment-start 0) t) ;; fake one
(*set segment-start segment-pvar));; else use selected segments - WRS 9/18/90
;; make all the integers non-negative
(*set pvar-copy (-!! pvar (!! (*min pvar))))
(*all
(declare (return-pvar-p nil))
(*when (not!! css) (*set pvar-copy (!! 0)))
(*when segment-start (*set enumeration (enumerate!!)))
(*set enumeration
(scan!! enumeration 'copy!! :segment-pvar segment-start :dimension dimension :direction :forward))
(let ((enumerate-size (*integer-length enumeration))
(source-size (*integer-length pvar-copy))
(active-set-tag-size 1)
)
#+*LISP-SIMULATOR
(declare (ignore active-set-tag-size))
(*locally
(declare (type fixnum enumerate-size source-size active-set-tag-size))
(*let (concatenation)
(declare (type (field-pvar (+ enumerate-size active-set-tag-size source-size)) concatenation))
(declare (return-pvar-p nil))
nil
(*set concatenation pvar-copy)
(*set concatenation (deposit-byte!! concatenation (!! source-size) (!! 1) (if!! css (!! 0) (!! 1))))
(let ((temp (1+ source-size)))
(*locally
(declare (type fixnum temp))
(*set concatenation (deposit-byte!! concatenation (!! temp) (!! enumerate-size) enumeration))
))
(*set result (rank!! concatenation '<=!!))
(*set segment-end
(scan!! segment-start 'copy!!
:dimension dimension :direction :backward :segment-pvar t!! :include-self nil))
(if dimension
(*set segment-end
(or!! segment-end (=!! (self-address-grid!! (!! (the fixnum dimension)))
(!! (the fixnum (1- (dimension-size dimension)))))))
(*setf (pref segment-end (1- *number-of-processors-limit*)) t)
)
(*set min-result (scan!! result 'min!! :segment-pvar segment-start :dimension dimension))
(*set min-result (scan!! min-result 'min!! :direction :backward :segment-pvar segment-end :dimension dimension))
(*set result (-!! result min-result))
))))
result
))))
#+*LISP-HARDWARE
(defun general-rank!! (pvar predicate dimension segment-pvar)
;; Make sure the pvar contains only floats and ints.
(*let ((temp-bit (not!! (or!! (integerp!! (the (pvar t) pvar)) (floatp!! (the (pvar t) pvar))))))
(declare (type boolean-pvar temp-bit))
(pvar-error-if
temp-bit
#.(slc::encode-pvar-overflow-tag 'rank!! :other :invalid-type nil)
pvar
))
;; If the pvar contains only floats or ints but not both, then
;; smallest-possible-pvar will make a non-general pvar
;; of the contained type.
(setq pvar (smallest-possible-pvar pvar))
(cond ((not (eq (pvar-type pvar) :general))
(internal-rank!! pvar predicate dimension segment-pvar)
)
(t
;; at this point, I can guarantee that there are both integers and floats in PVAR. There might
;; be precision problems with converting the integers to floats.
(*nocompile
(*let ((temp1 pvar))
(declare (type (float-pvar (pvar-general-mantissa-length pvar)
(pvar-general-exponent-length pvar)) temp1))
(internal-rank!! temp1 predicate dimension segment-pvar)
)))))
#+*LISP-SIMULATOR
(defun general-rank!! (pvar predicate dimension segment-pvar)
;; there are floats and integers.
;; coerce everything to double precision floats and then do the rank.
(safety-check
(assert (*and (and!! (numberp!! pvar) (not!! (complexp!! pvar)))) ()
"You cannot rank a pvar which contains a non-number or a complex number."
))
(rank!! (float!! pvar (!! pi)) predicate :dimension dimension :segment-pvar segment-pvar)
)
(defun canonical-segment (pvar)
#+*LISP-HARDWARE
(case (pvar-type pvar)
(:boolean pvar)
(not!! (null!! pvar))
)
#+*LISP-SIMULATOR
(if (*and (booleanp!! pvar)) pvar (not!! (null!! pvar)))
)
#+*LISP-HARDWARE
(defun canonical-value-for-rank (pvar)
;; We want to return an unsigned pvar which preserves
;; the order of the original pvar. Thus the unsigned pvar
;; can be used for ranking instead of the original one.
(case (pvar-type pvar)
(:field (smallest-possible-field-pvar pvar))
(:signed (signed-to-ordered-bits!! pvar))
(:float (float-to-ordered-bits!! pvar))
(otherwise
(progn
(pvar-error-if t!! #.(slc::encode-pvar-overflow-tag 'rank!! :other :invalid-type nil) pvar)
(!! 0)
))))
(defun smallest-possible-field-pvar (field-pvar)
;; Reduce the pvar to the smallest possible number
;; of bits. Since rank time is proportional to
;; the number of bits this is important.
#+*LISP-HARDWARE
(*compile-blindly
(*locally
(declare (type (field-pvar *) field-pvar))
(let ((size (*integer-length field-pvar)))
(if (eql size (pvar-length field-pvar))
field-pvar
(*let ((result field-pvar))
(declare (type (field-pvar size) result))
(declare (return-pvar-p t))
result
)))))
#+*LISP-SIMULATOR
field-pvar
)
(defun signed-to-ordered-bits!! (signed-pvar)
;; If there are no negative values just convert
;; the pvar to be unsigned. If there are negative
;; values add the negation of the most negative
;; value to all values to produce an unsigned
;; result. Once an unsigned number has been
;; obtained, reduce it to the smallest number of
;; required bits.
(*compile-blindly
(*locally
(declare (type (signed-pvar *) signed-pvar))
(let ((min (*min signed-pvar)))
(if (or (null min) (not (minusp min)))
(*let ((result signed-pvar))
(declare (type (unsigned-pvar (pvar-length signed-pvar)) result))
(declare (return-pvar-p t))
(smallest-possible-field-pvar result)
)
(*let ((result (+!! (!! (the fixnum (- min))) signed-pvar)))
(declare (type (unsigned-pvar (1+ (pvar-length signed-pvar))) result))
(declare (return-pvar-p t))
(smallest-possible-field-pvar result)
))))))
#+*LISP-HARDWARE
(defun float-to-ordered-bits!! (float-pvar)
"Rearrange the bits of a float so that when they
are considered as an unsigned integer, they are ordered
in the same sense that the original floating point numbers
were ordered. This obviously depends on the internal
representation of floats, which for floats is IEEE.
It is an error if the argument float-pvar is not a float pvar."
(let* ((length (pvar-length float-pvar))
(sign-bit-pos (1- length))
)
(*locally
(declare (type (integer 0 256) length sign-bit-pos))
(*let ((bits (taken-as!! (the (float-pvar * *) float-pvar) '(field-pvar length))))
(declare (type (field-pvar length) bits))
nil
;; first, we want to switch the sign bit,
;; since negative is 1 and positive is 0
;; and we want positive numbers to order
;; bigger than negative ones.
(*let ((sign (load-byte!! bits (!! sign-bit-pos) (!! 1)))
(rest (load-byte!! bits (!! 0) (!! sign-bit-pos)))
)
(declare (type (pvar (unsigned-byte 1)) sign))
(declare (type (pvar (unsigned-byte sign-bit-pos)) rest))
(declare (return-pvar-p nil))
nil
(*set sign (if!! (zerop!! sign) (!! 1) (!! 0)))
;; if the float was negative, we want to
;; negate the rest of the bits because the
;; exponent and mantissa are in twos
;; complement.
(*when (zerop!! sign)
(*set rest (load-byte!! (lognot!! rest) (!! 0) (!! sign-bit-pos)))
)
;; Now put the revised sign and other bits back together
;; as a single unsigned integer.
(*set bits sign)
(*set bits (ash!! bits (!! sign-bit-pos)))
(*set bits (+!! bits rest))
)
;; finally, make sure the unsigned integer
;; has the fewest number of bits it needs.
(smallest-possible-field-pvar bits)
))))
#+*LISP-SIMULATOR
(defun float-to-ordered-bits!! (float-pvar)
(*let ((double-float (float!! float-pvar (!! pi)))
result
)
nil
(multiple-value-bind (m e s)
#-KCL
(integer-decode-float most-positive-double-float)
#+KCL
(integer-decode-float (symbol-value 'most-positive-double-float))
(declare (ignore s))
(let ((integer-mantissa-size (+ 5 (integer-length m)))
(integer-exponent-size (+ 5 (integer-length e)))
)
(do-for-selected-processors-internal (j)
nil
(multiple-value-bind (mantissa exponent sign)
(integer-decode-float (pref double-float j))
(*setf (pref result j)
(* sign (dpb exponent (byte integer-exponent-size integer-mantissa-size) mantissa))
))))
result
)))
;;;; ***** END RANK!! ****
(defun test-segmented-cube-rank!! ()
(macrolet
((typed-segmented-cube-rank-test (type initialization-expression)
`(*let ((pvar ,initialization-expression)
(segment-pvar (evenp!! (self-address!!)))
result
supposed-result
)
(declare (type ,type pvar))
(declare (type (field-pvar 8) result supposed-result))
(declare (type boolean-pvar segment-pvar))
(format t "~%Testing segmented cube rank for type ~S" ',type)
(*nocompile (*set result (rank!! pvar '<=!! :segment-pvar segment-pvar)))
(*set supposed-result
(if!! (evenp!! (self-address!!))
(if!! (=!! pvar (pref!! pvar (1+!! (self-address!!))))
(!! 2)
(if!! (<=!! pvar (pref!! pvar (1+!! (self-address!!))))
(!! 0)
(!! 1)
))
(if!! (=!! pvar (pref!! pvar (1-!! (self-address!!))))
(!! 2)
(if!! (>=!! pvar (pref!! pvar (1-!! (self-address!!))))
(!! 1)
(!! 0)
))))
(*when (/=!! result supposed-result)
(*when (/=!! supposed-result (!! 2))
(when (*or t!!)
(print (*min (self-address!!)))
))))))
(typed-segmented-cube-rank-test (field-pvar 8) (random!! (!! 10)))
(typed-segmented-cube-rank-test (signed-pvar 8) (-!! (random!! (!! 10)) (!! 5)))
(typed-segmented-cube-rank-test (float-pvar 23 8) (random!! (!! 100.0)))
))
(defun test-grid-rank!! ()
(assert (*nocompile (*and (=!! (self-address-grid!! (!! 0)) (rank!! (self-address-grid!! (!! 0)) '<=!! :dimension 0)))))
)
(defun test-grid-segmented-cube-rank!! ()
(*let ((pvar (random!! (!! 10)))
(segment-pvar (evenp!! (self-address-grid!! (!! 0))))
result
)
(declare (type (field-pvar 8) pvar result))
(declare (type boolean-pvar segment-pvar))
(*nocompile (*set result (rank!! pvar '<=!! :segment-pvar segment-pvar :dimension 0)))
(*let (supposed-result)
(*set supposed-result
(if!! (evenp!! (self-address-grid!! (!! 0)))
(if!! (=!! pvar (news!! pvar 1 0))
(!! 2)
(if!! (<=!! pvar (news!! pvar 1 0))
(!! 0)
(!! 1)
))
(if!! (=!! pvar (news!! pvar -1 0))
(!! 2)
(if!! (>=!! pvar (news!! pvar -1 0))
(!! 1)
(!! 0)
))))
(assert (*and (or!! (=!! supposed-result (!! 2))
(=!! supposed-result result)
)))
)))
#+*LISP-HARDWARE
(defmacro with-copied-pvar ((old-pvar new-pvar &optional new-pvar-name) &body body)
;; Create a new pvar with the same properties as an old pvar
;; except that it has a different location, and so is a copy.
(assert (and (symbolp old-pvar) (symbolp new-pvar)))
(let ((location-symbol (gensym "PVAR-LOCATION-")))
`(let ((,new-pvar (allocate-field-pvar (pvar-length ,old-pvar))))
(let ((,location-symbol (pvar-location ,new-pvar)))
(copy-pvar-contents ,new-pvar ,old-pvar)
(setf (pvar-location ,new-pvar) ,location-symbol)
,@(if new-pvar-name `((setf (pvar-name ,new-pvar) ',new-pvar-name)))
,@body
))))
#+*LISP-SIMULATOR
(defmacro with-copied-pvar ((old-pvar new-pvar &optional new-pvar-name) &body body)
(assert (and (symbolp old-pvar) (symbolp new-pvar)))
`(*let ((,new-pvar ,old-pvar))
,@(if new-pvar-name `((setf (pvar-name ,new-pvar) ',new-pvar-name)))
,@body
))
;;;; **** START SORT ****
;;;; If key is specified, then the key must be a function of a
;;;; single argument. The KEY is applied to PVAR to obtain
;;;; the object to be used for comparisions. The entire
;;;; PVAR is sorted. The dimension and segment-pvar arguments
;;;; have the obvious semantics.
(defun sort!! (pvar predicate &key dimension segment-pvar key)
(simple-pvar-argument!! pvar &opt segment-pvar)
(safety-check
(new-vp-pvar-check pvar 'sort!!)
(when segment-pvar (new-vp-pvar-check segment-pvar 'sort!!))
(when (eq predicate (symbol-function '<=!!)) (setq predicate '<=!!))
(when (not (eq predicate '<=!!)) (error "Implementation limitation. Predicate ~S must be <=!!" predicate))
(when dimension
(when (or (not (integerp dimension)) (not (< -1 dimension *number-of-dimensions*)))
(error "You specified dimension ~S, but there are ~D dimensions in the current vp set"
dimension *number-of-dimensions*
)))
)
(when (or (eq key 'identity) (eq key #'identity)) (setq key nil))
(without-void-pvars (pvar)
(if (null segment-pvar)
(internal-sort!! pvar predicate dimension segment-pvar key)
(without-void-pvars (segment-pvar)
(let ((canonical-segment (canonical-segment segment-pvar)))
(internal-sort!! pvar predicate dimension canonical-segment key)
))))
)
(defun stable-sort!! (pvar predicate &key dimension segment-pvar key)
(let ((*stable-rank* t))
(sort!! pvar predicate :dimension dimension :segment-pvar segment-pvar :key key)
))
(defun internal-sort!! (pvar predicate dimension segment-pvar key)
(declare (ignore predicate))
(let ((key-pvar (if (null key) pvar (funcall key pvar))))
(cond
((and (null dimension) (null segment-pvar)) (unsegmented-cube-sort!!-internal pvar key-pvar))
((and (null dimension) segment-pvar) (segmented-cube-sort!!-internal pvar segment-pvar key-pvar))
((and dimension (null segment-pvar)) (unsegmented-grid-sort!!-internal pvar dimension key-pvar))
((and dimension segment-pvar) (segmented-grid-sort!!-internal pvar dimension segment-pvar key-pvar))
)))
#+*LISP-SIMULATOR
(eval-when (:compile-toplevel :load-toplevel :execute)
(defmacro the-defined-field (x) x)
)
(defun-wcefi unsegmented-cube-sort!!-internal (pvar key-pvar)
;; If all the processors involved in the sort are contiguous then we can avoid having to do a pack and unpack
(let ((min-self-address (*min (self-address!!)))
(max-self-address (*max (self-address!!))))
(cond
;; if no processors are selected, then just return
((null min-self-address) pvar)
((all-active-processors-contiguous-p min-self-address max-self-address)
(rank-and-send-for-sort!! pvar key-pvar min-self-address)
)
(t
;; The selected processors are not continuous.
(with-copied-pvar (pvar result sort!!-return)
(*let (rank ranked-data-p rendevous)
(declare (type (field-pvar *current-send-address-length*) rank rendevous))
(declare (type boolean-pvar ranked-data-p))
(declare (return-pvar-p nil))
nil
;; First rank the data.
(*nocompile (*set rank (rank!! key-pvar '<=!!)))
(*all (*set ranked-data-p nil!!)) ;clear the bit in all processors
;; Send the data to be sorted to the processor identified
;; by the rank. These processors are the first N processors,
;; where N is the number of active processors.
(*pset :no-collisions
(the-defined-field pvar)
(the-defined-field result)
rank
:notify ranked-data-p
)
;; now have all the selected processors pack their
;; self-addresses into the lower processors in the machine
(*pset :no-collisions (self-address!!) rendevous (enumerate!!))
;; Now each processor numbered 0 through N-1 has an address to
;; send back to (in rendevous) and the data associated with
;; that rank (in result). We now send the reordered data
;; back to the active processors.
(*all
(declare (return-pvar-p nil))
(*when ranked-data-p
(*pset :no-collisions (the-defined-field result) (the-defined-field result) rendevous)
))
)
result
))
)))
(defun all-active-processors-contiguous-p (min-self-address max-self-address)
(*compile-blindly
(not (*let (temp-bit)
(declare (type boolean-pvar temp-bit))
(declare (return-pvar-p nil))
nil
(*all (*set temp-bit nil!!))
(*set temp-bit t!!)
(*all
(declare (return-pvar-p nil))
nil
(*or (and!! (not!! temp-bit)
(<=!! (!! (the fixnum min-self-address))
(self-address!!)
(!! (the fixnum max-self-address))
))))))))
(defun rank-and-send-for-sort!! (data-pvar key-pvar min-self-address)
;; Create a destination pvar of the same type as the data pvar,
;; then rank the key pvar and send the data-pvar via the
;; rank ordering to the destination offset by the min-self-address.
;; By definition all the active data-pvar values are contiguous
;; in cube address space.
(*compile-blindly
(with-copied-pvar (data-pvar result sort!!-return)
(*let (rank)
(declare (type (pvar (unsigned-byte *current-send-address-length*)) rank))
(declare (return-pvar-p nil))
(*nocompile (*set rank (internal-rank!! key-pvar '<=!! nil nil)))
(*pset :no-collisions
(the-defined-field data-pvar)
(the-defined-field result)
(+!! (!! (the fixnum min-self-address)) rank)
))
result
)))
;;;; To do a segmented-cube-sort!!
;;;; 1. Do a segmented cube rank using the key.
;;;; 2. Copy scan the self address of the beginning of
;;;; the segment out to all the segment processors.
;;;; 3. Enumerate the processors in each segment using copy-scan of (!! 1)
;;;; 4. Each processor sends its data to the processor identified
;;;; by the scanned out self address plus the rank value.
;;;; 5. Each processor gets data from the processor identified
;;;; by the scanned out self address plus the enumeration.
(defun segmented-cube-sort!!-internal (pvar segment-pvar key-pvar)
(*compile-blindly
(*locally
(declare (type (field-pvar *) pvar key-pvar))
(declare (type boolean-pvar segment-pvar))
(let ((length #+*LISP-HARDWARE (pvar-length pvar) #+*LISP-SIMULATOR 32))
#+*LISP-SIMULATOR
(declare (ignore length))
(with-copied-pvar (pvar dest sort!!-return)
(*let (rank start-self-address enumeration)
(declare (type (field-pvar *current-send-address-length*) rank start-self-address enumeration))
(*nocompile (*set rank (rank!! key-pvar '<=!! :segment-pvar segment-pvar)))
(*set start-self-address (scan!! (self-address!!) 'copy!! :segment-pvar segment-pvar))
(*set enumeration (1-!! (scan!! (!! 1) '+!! :segment-pvar segment-pvar)))
(*pset :no-collisions (the (field-pvar length) pvar) (the (field-pvar length) dest)
(+!! start-self-address rank)
)
(*set (the (field-pvar length) dest)
(pref!! (the (field-pvar length) dest)
(+!! start-self-address enumeration) :collision-mode :no-collisions))
)
dest
)))))
(defun unsegmented-grid-sort!!-internal (pvar dimension-constant key)
(segmented-grid-sort!!-internal
pvar dimension-constant (t!!-in-first-active-processor-along-row dimension-constant) key
))
(defun segmented-grid-sort!!-internal (pvar dimension-constant segment-pvar key-pvar)
;; This is the same algorithm as segmented-cube-sort!!-internal.
;; In place of a cube address, we use an address object which
;; has the property that it is independent of the number of
;; dimensions in the current Vp Set and a component along
;; a specified dimension can be incremented to obtain a
;; new address using the function address-plus-nth!!, just
;; as a send address can be incremented simply by using addition.
(*compile-blindly
(*locally
(declare (type (field-pvar *) pvar key-pvar))
(declare (type boolean-pvar segment-pvar))
(let ((length #+*LISP-HARDWARE (pvar-length pvar) #+*LISP-SIMULATOR 32))
#+*LISP-SIMULATOR
(declare (ignore length))
(with-copied-pvar (pvar dest sort!!-return)
(*let (rank enumeration start-address-object)
(declare (type (field-pvar *current-send-address-length*) rank enumeration))
(declare (type (pvar address-object) start-address-object))
nil
(*nocompile
(*set rank
(rank!! key-pvar '<=!! :segment-pvar segment-pvar :dimension dimension-constant))
(*set start-address-object
(scan!! (self!!) 'copy!! :segment-pvar segment-pvar :dimension dimension-constant))
)
(*set enumeration
(1-!! (scan!! (!! 1) '+!! :segment-pvar segment-pvar :dimension dimension-constant)))
(progn
(*nocompile
(*pset :no-collisions (the (field-pvar length) pvar) (the (field-pvar length) dest)
(address-plus-nth!! start-address-object rank (!! (the fixnum dimension-constant)))
))
(*set (the (field-pvar length) dest)
(pref!! (the (field-pvar length) dest)
(address-plus-nth!! start-address-object enumeration (!! (the fixnum dimension-constant)))
:collision-mode :no-collisions))
))
dest
)))))
;;;; **** END NEW SORT ****
(*proclaim '(ftype (function (boolean-pvar) boolean-pvar) last-active-processor-in-segment!!))
(defun last-active-processor-in-segment!! (segment-pvar)
(*locally
(declare (type boolean-pvar segment-pvar))
(*let (last-active-processors)
(declare (type boolean-pvar last-active-processors))
(declare (return-pvar-p t))
(let ((last-active-processor (*max (self-address!!))))
(*let (css count last-processor-in-segment)
(declare (type boolean-pvar css last-processor-in-segment))
(declare (type (field-pvar *current-send-address-length*) count))
(declare (return-pvar-p nil))
(*all (*set css nil!!) (*set last-processor-in-segment nil!!))
(*set css t!!)
(*when (and!! segment-pvar (plusp!! (self-address!!)))
(*pset :no-collisions segment-pvar last-processor-in-segment (1-!! (self-address!!)))
)
(*setf (pref last-processor-in-segment last-active-processor) t)
(*all
(*set count (if!! css (!! 1) (!! 0)))
(*set count (scan!! count '+!! :direction :backward :segment-pvar last-processor-in-segment))
(*set last-active-processors (=!! (!! 1) count))
)))
last-active-processors
)))
;(defun-wcefi segmented-reduce-and-spread!! (pvar operator dimension-constant segment-pvar)
;
; (safety-check
; (new-vp-pvar-check pvar 'segmented-reduce-and-spread!!)
; (new-vp-pvar-check segment-pvar 'segmented-reduce-and-spread!!)
; (assert (or (null dimension-constant) (< -1 dimension-constant *number-of-dimensions*)) ()
; "You asked to spread across dimension ~D, but only ~D dimensions exist"
; dimension-constant *number-of-dimensions*
; ))
;
; (without-void-pvars (pvar segment-pvar)
;
; (*let (boolean-segment-pvar last-processor-in-segment-pvar)
; (declare (type boolean-pvar boolean-segment-pvar last-processor-in-segment-pvar))
; (*nocompile (*set boolean-segment-pvar segment-pvar))
; (*set last-processor-in-segment-pvar (last-active-processor-in-segment!! boolean-segment-pvar))
; (scan!!
; (scan!! pvar operator :dimension dimension-constant :
;
;
;;;; Element pvar is a pvar which has a single T value in any
;;;; given segment. It is an error is this is not true.
;;;; The value of pvar singled out by element pvar is spread to
;;;; all the active processors in the segment.
(defun-wcefi segmented-spread!! (pvar dimension-constant segment-pvar element-pvar)
(safety-check
(new-vp-pvar-check pvar 'segmented-spread!!)
(new-vp-pvar-check segment-pvar 'segmented-spread!!)
(new-vp-pvar-check element-pvar 'segmented-spread!!)
)