-
Notifications
You must be signed in to change notification settings - Fork 3
/
arrays.lisp
1528 lines (1228 loc) · 49.1 KB
/
arrays.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
;;; -*- Mode: LISP; Syntax: Common-lisp; Package: (*SIM-I COMMON-LISP-GLOBAL); Base: 10 -*-
(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.
;;;>
;;;> *+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+
;;; Author: JP Massar.
(defconstant +max-index-pvar-length+ #.(1+ (truncate (log *array-total-size-limit 2))))
(defconstant +max-single-index-pvar-length+ #.(1+ (truncate (log *array-dimension-limit 2))))
(defconstant +max-array-rank-pvar-length+ #.(1+ (truncate (log *array-rank-limit 2))))
;;; Put the names of the ALIASED versions of the array accessor
;;; functions on the property lists of the array accessor function
;;; names.
(eval-when (:compile-toplevel :load-toplevel :execute)
(mapc
#'(lambda (x) (setf (get x 'alias!!-function) (intern (concatenate 'string "ALIASED-" (symbol-name x)) '*SIM-i)))
'(aref!! svref!! bit!! sbit!!)
))
;; array-pvar-p is defined in pvars.lisp
(defun vector-pvar-p (pvar)
(and (array-pvar-p pvar) (eql 1 (array-pvar-rank pvar)))
)
(*defun *map (function-of-pvar-arguments &rest pvars)
(simple-pvar-argument!! &rest pvars)
(safety-check
(new-multiple-pvar-check pvars '*map)
(assert (every #'array-pvar-p pvars) () "Some pvar argument to *map is not an array pvar")
)
(apply 'map nil function-of-pvar-arguments (mapcar 'pvar-array-displaced-array pvars))
)
(defun amap!! (function-of-pvar-arguments &rest pvars)
(simple-pvar-argument!! &rest pvars)
(safety-check
(new-multiple-pvar-check pvars 'amap!!)
(assert (every #'array-pvar-p pvars) () "Some pvar argument to amap!! is not an array pvar")
)
(when (null pvars)
(error "Implementation limitation. You must provide at least one array argument to amap!!")
)
(let ((rank (*array-rank (first pvars)))
(dimensions (*array-dimensions (first pvars)))
)
(safety-check
(dolist (pvar (cdr pvars))
(when (not (equal rank (*array-rank pvar)))
(error "All the arrays given to amap!! do not have the same rank.")
))
(when (not (eql 1 rank))
(dolist (pvar (cdr pvars))
(when (not (equal dimensions (*array-dimensions pvar)))
(error "All the arrays given to amap!! do not have the same dimensions")
))))
(let* ((element-pvar-results-list
(apply 'map 'list function-of-pvar-arguments (mapcar 'pvar-array-displaced-array pvars))
)
(result-array-dimensions
(if (eql 1 rank)
(length element-pvar-results-list)
dimensions
))
(result-array-element-type
(if (not (*or t!!))
'(pvar boolean)
`(pvar ,(type-of (pref (car element-pvar-results-list) (*min (self-address!!)))))
)))
(cond
((equal result-array-element-type '(pvar float))
(setq result-array-element-type '(pvar single-float))
)
((equal result-array-element-type '(pvar complex))
(setq result-array-element-type '(pvar (complex single-float)))
))
(let ((new-array (make-array!! result-array-dimensions :element-type result-array-element-type)))
(let ((displaced-array (pvar-array-displaced-array new-array)))
(do ((j 0 (1+ j)) (pvar-list element-pvar-results-list (cdr pvar-list)))
((null pvar-list))
(*set (aref displaced-array j) (car pvar-list))
))
new-array
))))
(defun make-array!!
(dimensions
&key
(element-type '(pvar t) element-type-provided?)
(initial-element nil)
)
(when (not element-type-provided?)
(error "Currently, the :element-type keyword to make-array!! must be provided")
)
;; Check the dimensions. For now we assume the dimensions
;; must be scalars. Obviously they eventually could be pvars
;; which would give you different sized (but same ranked)
;; arrays in each processor. Different ranked pvars in
;; each processor would require dimensions to be a varying-length
;; vector pvar!
(safety-check
(assert dimensions () "Implementation deficiency: cannot create a *Lisp array with 0 dimensions. Sorry.")
(assert (or (integerp dimensions) (listp dimensions)))
)
(when (numberp dimensions) (setq dimensions (list dimensions)))
(safety-check
(assert (every #'(lambda (x) (and (integerp x) (not (minusp x)))) dimensions) ()
"One of the dimensions provided is not a non-negative integer"
))
;; Check the element type.
(let ((canonical-type (canonical-pvar-type element-type)))
(when (null canonical-type)
(setq canonical-type (list 'pvar element-type))
(setq canonical-type (canonical-pvar-type canonical-type))
(when (null canonical-type)
(error "The element type ~S is not recognizable as nor coercible into a valid *Lisp type specifier" element-type)
))
(when (eq '* (length-pvar-type canonical-type))
(error "Pvar arrays whose elements are of indeterminate size are not currently allowed.")
)
;; Check the initial element. If it is a scalar promote it to a pvar.
(when (and initial-element (not (pvarp initial-element)))
(setq initial-element (!! initial-element))
)
;; Compute the type specifier for an array of this type
;; and allocate one on the stack.
(let ((array-type (make-canonical-pvar-type 'array :element-type (cadr canonical-type) :dimensions dimensions)))
(let ((array-pvar (make-pvar-based-on-canonical-pvar-type :stack array-type)))
(setf (pvar-constant? array-pvar) nil)
(setf (pvar-lvalue? array-pvar) nil)
(setf (pvar-name array-pvar) 'MAKE-ARRAY!!-RETURN)
;; If there is an initial element assign it to each element
;; of the array.
(when initial-element
(*map #'(lambda (element) (*set element initial-element)) array-pvar)
)
array-pvar
))))
;;;;
;;;; ***** AREF!! *****
;;;;
(defvar *value-of-first-active-processor-when-no-processors-active* nil)
(eval-when (:load-toplevel :execute)
(setq *value-of-first-active-processor-when-no-processors-active* (gensym "NO-ACTIVE-PROCESSORS-"))
)
(defun value-of-first-active-processor (pvar &key (return-pvar? nil))
"If there are no active processors
*value-of-first-active-processor-when-no-processors-active*
is explicitly returned.
"
(let ((pvar-array (pvar-array pvar)))
(do-for-selected-processors-internal (j)
(let ((value (aref pvar-array j)))
(return-from value-of-first-active-processor
(if (pvar-p value)
(if return-pvar?
value
(value-of-first-active-processor value)
)
value
))))
*value-of-first-active-processor-when-no-processors-active*
))
(defun check-number-of-indices (pvar pvar-indices function)
(let ((number-of-indices (length pvar-indices))
(rank (array-pvar-rank pvar))
)
(assert (eql rank number-of-indices) ()
"~S: You provided ~D indices, but the pvar array ~S only has ~D dimensions"
function number-of-indices pvar rank
)))
(defun check-that-index-pvars-are-within-bounds (pvar index-pvars function)
(dotimes (j (array-pvar-rank pvar))
(let ((dimension-limit (array-pvar-dimension pvar j))
(index-array (pvar-array (nth j index-pvars)))
)
(with-simple-vectors (index-array)
(declare (type fixnum dimension-limit))
(do-for-selected-processors-internal (i)
(let ((value (aref index-array i)))
(assert (valid-integer-range-exclusive value 0 dimension-limit) ()
"For function ~S, the value, ~S, of index-pvar ~D, in processor ~D, is not within the dimension limit ~D"
function value (nth j index-pvars) i dimension-limit
)))))))
(defmacro with-indices-iterated-over-active-processors
((processor-index-symbol element-pvar-symbol) (pvar index-pvars) &body body)
(assert (and (symbolp processor-index-symbol) (symbolp element-pvar-symbol)))
(let ((index-pvar-arrays-symbol (gensym "INDEX-PVARS-ARRAYS-"))
(pvar-symbol (gensym "PVAR-"))
(number-of-dimensions-symbol (gensym "NUMBER-OF-DIMENSIONS-"))
(single-index-list-symbol (gensym "SINGLE-INDEX-LIST-"))
(array-holding-pvars-symbol (gensym "ARRAY-HOLDING-PVARS-"))
(i-symbol (gensym "I-"))
(index-pvars-symbol (gensym "INDEX-PVARS-"))
)
`(let* ((,index-pvars-symbol ,index-pvars)
(,index-pvar-arrays-symbol (mapcar #'pvar-array ,index-pvars-symbol))
(,pvar-symbol ,pvar)
(,number-of-dimensions-symbol (length ,index-pvars-symbol))
(,single-index-list-symbol (make-list ,number-of-dimensions-symbol))
(,array-holding-pvars-symbol (if (general-pvar-p ,pvar-symbol) nil (pvar-array ,pvar-symbol)))
)
(do-for-selected-processors-internal (,processor-index-symbol)
(dotimes (,i-symbol ,number-of-dimensions-symbol)
(setf (nth ,i-symbol ,single-index-list-symbol) (aref (nth ,i-symbol ,index-pvar-arrays-symbol) ,processor-index-symbol))
)
(let ((,element-pvar-symbol
(if ,array-holding-pvars-symbol
(apply #'aref ,array-holding-pvars-symbol ,single-index-list-symbol)
(apply #'aref (pvar-array (aref (pvar-array ,pvar-symbol) ,processor-index-symbol)) ,single-index-list-symbol)
)))
,@body
)))))
(defun svref!!-internal (vector-pvar index-pvar return-pvar aliased?)
;; If aliased? is true then return-pvar is nil and the actual
;; element pvar is returned. Otherwise the data is copied to
;; return-pvar and return-pvar is returned.
(when (pvar-constant-value index-pvar)
(return-from svref!!-internal
(svref!!-internal-constant-index vector-pvar (pvar-constant-value index-pvar) return-pvar aliased?)
))
(when aliased? (assert (null return-pvar) () "Internal error. Return-pvar provided but aliased? is true"))
;; The complicated case. We retrieve a possibly different
;; element from each processor. We cannot return an
;; alias in this case.
(if aliased?
(error "Internal error. Attempt to alias with non-constant index.")
(let* ((index-array (pvar-array index-pvar))
(return-array (pvar-array return-pvar))
(vector-holding-pvars (pvar-array vector-pvar))
(vector-length (length vector-holding-pvars))
)
(with-simple-vectors (index-array vector-holding-pvars)
(do-for-selected-processors-internal (j)
(let ((index (aref index-array j)))
(assert (array-in-bounds-p vector-holding-pvars index) ()
"The value of the index pvar ~S, in processor ~D, ~D, is not within the array bounds ~S"
vector-pvar j index vector-length
)
(let* ((element-pvar (aref vector-holding-pvars index)))
(if (general-pvar-p element-pvar)
(setf (aref return-array j) (aref (pvar-location element-pvar) j))
(*setf (pref return-pvar j) (pref element-pvar j))
))))
return-pvar
))))
(defun svref!!-internal-constant-index (vector-pvar constant-index return-pvar aliased?)
(when aliased?
(assert (null return-pvar) ()
"Internal error. Return-pvar provided but aliased? is true"
))
(let ((vector-holding-pvars (pvar-array vector-pvar)))
(with-simple-vectors (vector-holding-pvars)
(safety-check
(assert (array-in-bounds-p vector-holding-pvars constant-index) ()
"The value of the constant index pvar ~S, ~D, is not within the array bounds ~S"
vector-pvar constant-index (length vector-holding-pvars)
))
(if aliased?
(svref vector-holding-pvars constant-index)
(progn (let ((temp (aref vector-holding-pvars constant-index)))
(*set return-pvar temp))
return-pvar)
))))
(defun svref!! (vector-pvar index-pvar)
(safety-check
(new-two-pvar-check vector-pvar index-pvar 'svref!!)
(assert (vector-pvar-p vector-pvar))
)
(let ((return-pvar (allocate-temp-pvar-given-canonical-pvar-type (array-pvar-canonical-element-type vector-pvar))))
(setf (pvar-lvalue? return-pvar) t)
(setf (pvar-constant? return-pvar) nil)
(if (pvar-constant-value index-pvar)
(svref!!-internal-constant-index vector-pvar (pvar-constant-value index-pvar) return-pvar nil)
(svref!!-internal vector-pvar index-pvar return-pvar nil)
)
(setf (pvar-lvalue? return-pvar) nil)
return-pvar
))
(defun aliased-svref!! (vector-pvar index-pvar)
;; Used by *SETF and the ALIAS!! mechanism when the index
;; pvar is constant.
(safety-check
(new-pvar-check-lvalue-no-vp-check vector-pvar 'aliased-svref!!)
(new-pvar-check index-pvar 'aliased-svref!!)
(assert (vector-pvar-p vector-pvar))
)
(let (constant-index)
(cond
((setq constant-index (pvar-constant-value index-pvar))
(svref!!-internal-constant-index vector-pvar (pvar-constant-value index-pvar) nil t)
)
((= (setq constant-index (*min index-pvar)) (*max index-pvar))
(svref!!-internal-constant-index vector-pvar constant-index nil t)
)
(t (error "You cannot use alias!! to alias into a vector with non-constant indices"))
)))
(*defun
indirect-setf-svref!! (vector-pvar value-pvar index-pvar)
;; Used by *SETF when the index pvar is not obviously
;; a constant.
(safety-check
(progn
(new-pvar-check-lvalue vector-pvar 'indirect-setf-svref!!)
(new-two-pvar-check index-pvar value-pvar 'indirect-setf-svref!!)
(assert (vector-pvar-p vector-pvar))
))
(cond
;; If the index is really constant, but we couldn't tell
;; at compile time, revert back to the constant setf method.
((pvar-constant? index-pvar)
(let ((single-index (value-of-first-active-processor index-pvar)))
(if (eql single-index *value-of-first-active-processor-when-no-processors-active*)
nil
(*setf (aref!! vector-pvar (!! single-index)) value-pvar)
)))
;; Otherwise, first check the index pvar for invalid indices
(t
(let* ((index-array (pvar-array index-pvar))
(vector-holding-pvars (pvar-array vector-pvar))
(vector-length (length vector-holding-pvars))
)
(do-for-selected-processors-internal (j)
(let ((index (aref index-array j)))
(assert (array-in-bounds-p vector-holding-pvars index) ()
"The value of the index pvar ~S, in processor ~D, ~D, is not within the array bounds ~S"
vector-pvar j index vector-length
)))
(cond
;; The value pvar is a general pvar.
;; do this as efficiently as possible.
((general-pvar-p value-pvar)
(let ((value-array (pvar-array value-pvar)))
(do-for-selected-processors-internal (j)
(let ((index (aref index-array j)))
(let* ((element-pvar (aref vector-holding-pvars index))
(element-pvar-array (pvar-array element-pvar))
(value (aref value-array j))
)
(if (not (pvar-p value))
(setf (aref element-pvar-array j) value)
(*setf (pref element-pvar j) (pref value j))
))))))
;; The value pvar is an array or a structure pvar.
;; Move the elements using PREF.
(t
(do-for-selected-processors-internal (j)
(let* ((index (aref index-array j))
(element-pvar (aref vector-holding-pvars index))
)
(*setf (pref element-pvar j) (pref value-pvar j))
)))
))))
(values)
)
(defun bit!! (bit-pvar &rest index-pvars) (apply 'aref!! bit-pvar index-pvars))
(defun aliased-bit!! (bit-pvar &rest index-pvars) (apply 'aliased-aref!! bit-pvar index-pvars))
(defun sbit!! (bit-pvar &rest index-pvars) (apply 'aref!! bit-pvar index-pvars))
(defun aliased-sbit!! (bit-pvar &rest index-pvars) (apply 'aliased-aref!! bit-pvar index-pvars))
(defun aref!!-internal (array-pvar pvar-indices return-pvar aliased?)
(when aliased? (assert (null return-pvar) () "Internal error. Return-pvar provided but aliased? is true"))
(let ((array-holding-pvars (pvar-array array-pvar))
(all-indices-constants? (every #'pvar-constant-value pvar-indices))
)
(if all-indices-constants?
(cond
((eql 1 (length pvar-indices))
(svref!!-internal-constant-index array-pvar (pvar-constant-value (car pvar-indices)) return-pvar aliased?)
)
(t
(safety-check
(do ((dimension-index 0 (1+ dimension-index))
(index-list pvar-indices (cdr index-list))
)
((null index-list))
(let ((constant-index (pvar-constant-value (car index-list))))
(when (>= constant-index (array-dimension array-holding-pvars dimension-index))
(error "The constant index ~S, accessing dimension ~D of the array pvar ~S, ~@
which has dimensions ~S, is not within those dimensions"
constant-index dimension-index array-pvar (array-dimensions array-holding-pvars)
)))))
(if aliased?
(apply #'aref array-holding-pvars (mapcar #'pvar-constant-value pvar-indices))
(progn
(*set return-pvar (apply #'aref array-holding-pvars (mapcar #'pvar-constant-value pvar-indices)))
return-pvar
))))
(if aliased?
(error "Internal error. Attempt to alias with non-constant index.")
(let ((return-array (pvar-array return-pvar)))
(check-that-index-pvars-are-within-bounds array-pvar pvar-indices 'AREF!!)
(with-indices-iterated-over-active-processors
(j element-pvar) (array-pvar pvar-indices)
(let ((value (aref (pvar-array element-pvar) j)))
(if (not (pvar-p value))
(setf (aref return-array j) value)
(*setf (pref return-pvar j) (pref value j))
)))
return-pvar
))
)))
(defun aref!! (pvar &rest pvar-indices)
(simple-pvar-argument!! pvar &rest pvar-indices)
(safety-check
(new-pvar-check pvar 'aref!!)
(validate-all-pvars pvar-indices 'aref!!)
)
(case-pvar-array
(pvar)
nil
;; Make sure the number of pvar-indices agrees with the rank of
;; pvar-array.
(progn
(safety-check
(check-number-of-indices pvar pvar-indices 'aref!!)
)
;; If there is only 1 dimension, use svref!!
;; Otherwise use the general aref!!-internal
(let ((return-pvar (allocate-temp-pvar-given-canonical-pvar-type (array-pvar-canonical-element-type pvar)))
(length (length pvar-indices))
)
(setf (pvar-lvalue? return-pvar) t)
(setf (pvar-constant? return-pvar) nil)
(cond
((and (eql 1 length) (pvar-constant-value (car pvar-indices)))
(svref!!-internal-constant-index pvar (pvar-constant-value (car pvar-indices)) return-pvar nil)
)
((eql 1 length)
(svref!!-internal pvar (car pvar-indices) return-pvar nil)
)
(t (aref!!-internal pvar pvar-indices return-pvar nil))
)
(setf (pvar-lvalue? return-pvar) nil)
return-pvar
))))
(defun aliased-aref!! (pvar &rest index-pvars)
(simple-pvar-argument!! pvar &rest index-pvars)
(safety-check
(new-pvar-check-lvalue-no-vp-check pvar 'aliased-aref!!)
(validate-all-pvars index-pvars 'aliased-aref!!)
)
(let ((every-pvar-constant? (every #'pvar-constant-value index-pvars)))
(when (not every-pvar-constant?)
(setq every-pvar-constant? (every #'(lambda (pvar) (= (*max pvar) (*min pvar))) index-pvars))
(if every-pvar-constant?
(setq index-pvars (mapcar #'(lambda (pvar) (!! (*max pvar))) index-pvars))
(error "You cannot create an alias for an AREF!! form given non-constant indices.")
))
(case-pvar-array
(pvar)
nil
(aref!!-internal pvar index-pvars nil t)
)))
(defmacro setf-aref!! (pvar value-pvar &rest index-pvars)
(simple-pvar-argument!! value-pvar &rest index-pvars)
(if (eql 1 (length index-pvars))
`(indirect-setf-svref!! ,pvar ,value-pvar ,(car index-pvars))
`(indirect-setf-aref!! ,pvar ,value-pvar ,@index-pvars)
))
(*defun
indirect-setf-aref!! (pvar value-pvar &rest index-pvars)
;; Used by *SETF when the index pvar is not obviously
;; a constant.
(safety-check
(new-two-pvar-check pvar value-pvar 'indirect-setf-aref!!)
(validate-all-pvars index-pvars 'indirect-setf-aref!!)
)
(flet
((internal-indirect-setf-aref!!
()
(safety-check
(check-that-index-pvars-are-within-bounds pvar index-pvars '*SETF-AREF!!)
)
(cond
((general-pvar-p value-pvar)
(with-indices-iterated-over-active-processors
(j element-pvar) (pvar index-pvars)
(let ((value-array (pvar-array value-pvar))
(element-pvar-array (pvar-array element-pvar))
)
(let ((value (aref value-array j)))
(if (not (pvar-p value))
(setf (aref element-pvar-array j) value)
(*setf (pref element-pvar j) (pref value j))
)))))
(t
(with-indices-iterated-over-active-processors
(j element-pvar) (pvar index-pvars)
(*setf (pref element-pvar j) (pref value-pvar j))
)))))
(case-pvar-array
(pvar)
nil
; (internal-indirect-setf-aref!!)
(cond
;; If the indices are really constant, but we couldn't tell
;; at compile time, revert back to the constant setf method.
((every #'pvar-constant? index-pvars)
(let ((single-index-list (mapcar #'value-of-first-active-processor index-pvars)))
(if (member *value-of-first-active-processor-when-no-processors-active* single-index-list)
nil
(*copy-pvar (apply #'aliased-aref!! pvar (mapcar #'!! single-index-list)) value-pvar)
)))
;; Otherwise, first check the index pvars for invalid indices
(t (internal-indirect-setf-aref!!))
)))
(values)
)
; (let* ((index-arrays (mapcar #'pvar-array index-pvars))
; (number-of-dimensions (length index-pvars))
; (single-index-set (make-list number-of-dimensions))
; (array-holding-pvars (pvar-array pvar))
; )
; (do-for-selected-processors-internal (j)
; (dotimes (i number-of-dimensions)
; (setf (nth i single-index-set) (aref (nth i index-arrays) j))
; )
; (assert (apply #'array-in-bounds-p array-holding-pvars single-index-set) ()
; "The values of the index pvars ~S, in processor ~D, ~S, are not within the array bounds ~S"
; index-pvars j single-index-set (array-pvar-dimensions array-holding-pvars)
; ))
;
; (cond
;
; ((general-pvar-p value-pvar)
; (let ((value-array (pvar-array value-pvar)))
; (do-for-selected-processors-internal (j)
; (dotimes (i number-of-dimensions)
; (setf (nth i single-index-set) (aref (nth i index-arrays) j))
; )
; (let* ((element-pvar (apply #'aref array-holding-pvars single-index-set))
; (element-pvar-array (pvar-array element-pvar))
; (value (aref value-array j))
; )
; (if (not (pvar-p value))
; (setf (aref element-pvar-array j) value)
; (*setf (pref element-pvar j) (pref value j))
; )))))
;
; (t
; (do-for-selected-processors-internal (j)
; (dotimes (i number-of-dimensions)
; (setf (nth i single-index-set) (aref (nth i index-arrays) j))
; )
; (let* ((element-pvar (apply #'aref array-holding-pvars single-index-set)))
; (*setf (pref element-pvar j) (pref value-pvar j))
; )))
;
; )))))
;;;; ************************************************************
;;;; ************************************************************
;;; This is cheating, because we *Lisp itself doesn't allow arrays of
;;; element type T, but the simulator doesn't really give a damn.
(defun vector!! (&rest pvars)
(simple-pvar-argument!! &rest pvars)
(validate-all-pvars pvars 'vector!!)
(let ((return-vector-pvar (make-pvar-based-on-canonical-pvar-type :stack `(pvar (array t (,(length pvars)))))))
(setf (pvar-name return-vector-pvar) 'VECTOR!!-RETURN)
(setf (pvar-lvalue? return-vector-pvar) t)
(dotimes (j (length pvars))
(*setf (svref!! return-vector-pvar (!! j)) (nth j pvars))
)
(setf (pvar-lvalue? return-vector-pvar) nil)
return-vector-pvar
))
(*proclaim '(*defun setf-row-major-aref!!))
(defun array!! (dimensions &rest pvars)
(unless (= (reduce #'* dimensions) (length pvars))
(error "Dimensions ~S must have the save size as the number of pvars ~D provided to array!!."
dimensions (length pvars)
))
(let ((result (make-pvar-based-on-canonical-pvar-type :stack `(pvar (array t ,dimensions)))))
(setf (pvar-name result) 'ARRAY!!-RETURN)
(setf (pvar-lvalue? result) t)
(dotimes (j (length pvars))
(*setf (row-major-aref!! result (!! j)) (nth j pvars))
)
(setf (pvar-lvalue? result) nil)
(setf (pvar-array-dimensions result) dimensions)
result
))
(defun typed-vector!! (element-type &rest pvars)
(simple-pvar-argument!! &rest pvars)
(safety-check (validate-all-pvars pvars 'typed-vector!!))
(let ((element-type (valid-pvar-type-p element-type)))
(let ((return-vector-pvar (make-array!! (length pvars) :element-type element-type)))
(setf (pvar-name return-vector-pvar) 'TYPED-VECTOR!!-RETURN)
(setf (pvar-lvalue? return-vector-pvar) t)
(dotimes (j (length pvars))
(*setf (aref!! return-vector-pvar (!! j)) (nth j pvars))
)
(setf (pvar-lvalue? return-vector-pvar) nil)
return-vector-pvar
)))
(defun every-active-processor-of-a-general-pvar-contains-an-array? (general-pvar)
;; Note. If there are no active processors returns T.
(let ((general-pvar-array (pvar-array general-pvar)))
(with-simple-vectors (general-pvar-array)
(do-for-selected-processors-internal (j)
(when (not (array-pvar-p (aref general-pvar-array j)))
(return-from every-active-processor-of-a-general-pvar-contains-an-array? nil)
))))
t
)
(*defun *array-element-type (pvar-array)
"Returns to the front end the element type of pvar-array.
It is an error if pvar-array were to be a general-pvar array
containing different array types.
"
(simple-pvar-argument!! pvar-array)
(safety-check (new-pvar-check pvar-array '*array-element-type))
(case-pvar-array
(pvar-array)
nil
; (if (no-processors-active)
; nil
; (array-pvar-canonical-element-type (get-single-active-array-in-general-pvar pvar-array '*array-element-type))
; )
(array-pvar-canonical-element-type pvar-array)
))
(defun array-rank!! (pvar-array)
"Returns the number of dimensions of pvar-array as a pvar."
(simple-pvar-argument!! pvar-array)
(case-pvar-array
(pvar-array)
nil
(!! (*array-rank pvar-array))
))
(*defun *array-dimension (pvar-array axis-number-scalar)
"Returns to the front end the length of the
axis-number-scalar dimension of pvar-array.
It is an error if pvar-array were to be a
general-pvar array containing different size arrays.
"
(simple-pvar-argument!! pvar-array)
(safety-check
(new-pvar-check pvar-array '*array-dimension)
(assert (and (integerp axis-number-scalar) (not (minusp axis-number-scalar))))
)
(case-pvar-array
(pvar-array)
nil
(progn
(assert (< axis-number-scalar (array-pvar-rank pvar-array)) ()
"There are only ~D dimensions in the pvar array ~S but you asked for dimension ~D"
(array-pvar-rank pvar-array) pvar-array axis-number-scalar
)
(array-pvar-dimension pvar-array axis-number-scalar)
)))
(defun array-dimension!! (pvar-array dimension-pvar)
"Returns the dimension size of a particular dimension in each processor."
(simple-pvar-argument!! (pvar-array dimension-pvar))
(safety-check
(new-pvar-check dimension-pvar 'array-dimension!!)
(assert (*and (integerp!! dimension-pvar)) ()
"The dimension-pvar argument is not everywhere an integer"
)
(assert (*and (and!! (not!! (minusp!! dimension-pvar)) (<!! dimension-pvar (array-rank!! pvar-array)))) ()
"The dimension-pvar argument to array-dimension!! is not everywhere less than the rank of the array"
)
)
(*let (result)
(let ((result-array (pvar-array result))
(dimension-pvar-array (pvar-array dimension-pvar))
)
(case-pvar-array
(pvar-array)
;; A general pvar containing arrays
nil
; (if (no-processors-active)
; (!! 0)
; (progn
; (when (not (every-active-processor-of-a-general-pvar-contains-an-array? pvar-array))
; (error "Not every active processor of pvar ~S contains an array" pvar-array)
; )
; (let ((general-pvar-array (pvar-array pvar-array)))
; (do-for-selected-processors-internal (j)
; (setf (aref result-array j) (array-pvar-dimension (aref general-pvar-array j) (aref dimension-pvar-array j)))
; ))))
;
;; An array pvar
(let ((array-dimensions (array-pvar-dimensions pvar-array)))
(do-for-selected-processors-internal (j)
(setf (aref result-array j) (nth (aref dimension-pvar-array j) array-dimensions))
))
))
result
))
(*defun *array-dimensions (pvar-array)
"Returns to the front end as a list the dimension sizes of pvar-array.
It is an error if pvar-array were to be a general-pvar array
containing different size arrays.
"
(simple-pvar-argument!! pvar-array)
(safety-check (new-pvar-check pvar-array '*array-dimensions))
(case-pvar-array
(pvar-array)
nil
(array-pvar-dimensions pvar-array)
))
(defun array-dimensions!! (pvar-array)
"Returns a vector pvar of length the rank of the array,
the nth element containing the nth dimension size.
"
(simple-pvar-argument!! pvar-array)
(flet
((foo ()
(apply 'typed-vector!!
`(pvar (unsigned-byte ,+max-index-pvar-length+))
(mapcar #'!! (*array-dimensions pvar-array))
)))
(safety-check (new-pvar-check pvar-array 'array-dimensions!!))
(case-pvar-array
(pvar-array)
nil
(foo)
)))
(*defun *array-total-size (pvar-array)
"Returns to the front end the total number of elements in pvar-array.
It is an error if pvar-array were to be a general-pvar array
containing different size arrays.
"
(simple-pvar-argument!! pvar-array)
(safety-check (new-pvar-check pvar-array '*array-total-size))
(case-pvar-array
(pvar-array)
nil
(apply #'* (*array-dimensions pvar-array))
))
(defun array-total-size!! (pvar-array)
(simple-pvar-argument!! pvar-array)
(case-pvar-array
(pvar-array)
nil
(!! (*array-total-size pvar-array))
))
(defun apply-array-function-over-multiple-indices (pvar-array pvar-subscripts scalar-array-function)
(*let (result)
(let* ((number-of-dimensions (length pvar-subscripts))
(single-index-set (make-list number-of-dimensions))
(result-array (pvar-array result))
(index-arrays (mapcar #'pvar-array pvar-subscripts))
)
(case-pvar-array
(pvar-array)
;; General pvar containing array pvars.
nil
; (when (some-processor-active)
; (multiple-value-bind (any-arrays? all-same-rank? all-same-shape?)
; (check-general-pvar-arrays-for-existence-and-same-rank-and-shape pvar-array)
; all-same-shape?
; (when (or (not any-arrays?) (not all-same-rank?))
; (error "The arrays in general pvar ~S are not all the same rank" pvar-array)
; )
; (let ((general-pvar-array (pvar-array pvar-array)))
; (do-for-selected-processors-internal (j)
; (dotimes (i number-of-dimensions)
; (setf (nth i single-index-set) (aref (nth i index-arrays) j))
; )
; (let* ((array-pvar-in-general-pvar (aref general-pvar-array j))
; (array-holding-pvars (pvar-array array-pvar-in-general-pvar))
; )
; (setf (aref result-array j) (apply scalar-array-function array-holding-pvars single-index-set))
; )))))
;
;; An array pvar.
(progn
(assert (eql number-of-dimensions (*array-rank pvar-array)) ()
"You provided ~D subscripts, but the pvar array ~S has only ~D dimensions"
number-of-dimensions pvar-array (*array-rank pvar-array)
)
(let ((array-holding-pvars (pvar-array pvar-array)))
(do-for-selected-processors-internal (j)
(dotimes (i number-of-dimensions)
(setf (nth i single-index-set) (aref (nth i index-arrays) j))
)
(setf (aref result-array j) (apply scalar-array-function array-holding-pvars single-index-set))
)))
))
result
))
(defun array-in-bounds-p!! (pvar-array &rest pvar-subscripts)
"Returns a boolean pvar which is T in every processor
in which pvar-subscripts represents a valid reference
to pvar-array, and NIL otherwise.
"