-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspartns.lisp
1697 lines (1500 loc) · 67.2 KB
/
spartns.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
;; This software is Copyright (c) Jeronimo Pellegrini, 2008.
;; You have the rights to distribute
;; and use this software as governed by the terms
;; of the Lisp Lesser GNU Public License
;; (http://opensource.franz.com/preamble.html),
;; known as the LLGPL.
(in-package :spartns)
;;;--- SPECIAL VARIABLES ---;;;
(eval-when (:compile-toplevel :load-toplevel :execute)
(defvar *representation-schemes* (make-hash-table :size 20 :test 'eq)
"This variable holds a hashtable that maps names (symbols)
to representation schemes for sparse vectors. These are structures
that map fixnums onto data of any type.")
(defvar *spartn-schemes* (make-hash-table)
"This variable holds a hashtable which maps names onto spartn schemes. "))
;;;--- DEFSCHEME ---;;;
(defun get-scheme (name)
"Retrieves a representation scheme given its name, which should be a symbol. If the scheme
is not registered, this function returns NIL.
(spartns::get-scheme 'spartns:cvector)
==>
#<HASH-TABLE :TEST EQ :COUNT 10 {10036CF001}>
T
The hashtable returned is the representation scheme (see the function DEFSCHEME)."
(check-type name symbol)
(gethash name *representation-schemes* nil))
(defun remove-scheme (name)
"Removes a representation scheme from the database."
(check-type name symbol)
(remhash name *representation-schemes*))
(defun expand-scheme (function scheme subst-list)
"Expands the code from a representation scheme, substituting parameters.
For example, if a scheme with the name 'cvector has this function registered:
'spartns::get --> '(get-cvec data index sparse-element)
Then expand-scheme will work like this:
(expand-scheme 'spartns::get 'cvector '((data d) (index i) (sparse-element sp)))
=> '(get-cvec d i sp)"
(let ((scheme-hash (gethash scheme *representation-schemes*)))
(when (null scheme-hash)
(cerror "Ignore this error" (format nil "Can't expand scheme ~a (it's not in the schemes database)." scheme)))
(subst-all subst-list (gethash function scheme-hash))))
(defmacro defscheme (&key
name
type
make
get
set
delete
traverse
pack
capacity
count)
"Adds a new representation scheme.
- name is the name of the scheme (a symbol)
- type is the Common Lisp type of an instance of this scheme. For example, if your scheme
represents the mapping as a hashtable, then type is HASH-TABLE. If it is represented as a
Lisp list, then it is LIST
All other parameters (make, get, set, delete, traverse, capacity, count) should contain the
Lisp code necessary to perform these actions:
- make: the code necessary to create an instance of this scheme. If your code contains the
symbols SIZE and ELEMENT-TYPE, they will be substituted for the actual values;
- get: code necessary to access one element. Symbols that are substituted: DATA, INDEX,
SPARSE-ELEMENT. It should return TWO VALUES:
+ the retrieved element (the sparse element, if the index was not found);
+ either T or NIL, depending on wether the element was found or not.
For example, (IF found (VALUES element T)
(VALUES sparse NIL));
- set: code to set one position. Substituted symbols: DATA, INDEX, VALUE, ELEMENT-TYPE
- delete: code to remove one element. Substituted symbols: DATA, INDEX, SPARSE-ELEMENT,
ELEMENT-TYPE;
- traverse: code to traverse the structure, binding variables to the index and value while
executing a body of code. Substituted symbols: INDEX, VALUE, DATA, ELEMENT-TYPE, BODY;
- pack: code that packs the container, releasing non-used memory;
- capacity: code that returns the maximum number of elements that the instance can handle.
Substituted symbols: DATA;
- count: code to return the actual number of non-zero elements. Substituted symbols: DATA.
The macro will return the name of the newly created scheme.
For example:
(defscheme :name hash
:type HASH-TABLE
:make (let () (declare (ignore element-type)) (make-hash-table :size size))
:get (gethash index data sparse-element)
:set (setf (gethash index data) value)
:delete (remhash index data)
:traverse (do-traverse-hash (index value data) body)
:pack (values) ; we don't write code for shrinking a hashtable!
:capacity (let () (declare (ignore data)) most-positive-fixnum)
:count (hash-table-count data))
In the example, the make function ignores ELEMENT-TYPE (because hashtables ignore them).
However, you should NOT try to make the code ignore symbols by using, for example,
(let () (declare (ignore element-type)) (make-hash-table :size size))
because the element-type inside DECLARE will be substituted.
Since the make function returns an object of type HASH-TABLE, then this is the value passed to
the type parameter.
The capacity code always returns most-positive-fixnum, since hashtables can grow."
(check-type name symbol)
(dolist (parameter (list spartns::make
spartns::get
spartns::set
spartns::delete
spartns::traverse
spartns::pack
spartns::capacity
spartns::count))
(check-type parameter list))
(eval-when (:compile-toplevel :load-toplevel :execute)
(let ((new-scheme (gensym)))
`(progn
(let ((,new-scheme (make-hash-table :size 7 :test 'eq)))
(setf (gethash 'spartns::name ,new-scheme) ',name
(gethash 'spartns::type ,new-scheme) ',type
(gethash 'spartns::make ,new-scheme) ',make
(gethash 'spartns::get ,new-scheme) ',get
(gethash 'spartns::set ,new-scheme) ',set
(gethash 'spartns::delete ,new-scheme) ',delete
(gethash 'spartns::traverse ,new-scheme) ',traverse
(gethash 'spartns::pack ,new-scheme) ',pack
(gethash 'spartns::capacity ,new-scheme) ',capacity
(gethash 'spartns::count ,new-scheme) ',count)
(setf (gethash ',name *representation-schemes*) ,new-scheme))))))
;;;--- SCHEME: PATRICIA ---;;;
;; Currently being developed
#+ nil (defscheme :name patricia
:type ???
:make ()
:get (patricia::find data index)
:set ()
:delete (patricia::delete data index)
:traverse (patricia::traverse (index value data) body)
:pack (values)
:capacity (values most-positive-fixnum) ; no limit
:count (patricia::patricia-tree-count data))
;;;--- SCHEME: AVL ---;;;
;; Currently being developed
#+ nil (defscheme :name avl
:type avl::avl-tree
:make (avl::make-avl-tree)
:get (multiple-value-bind (v f) (avl::avl-find data index sparse-element)
(values (the element-type v)
(the boolean f)))
:set (avl::avl-insert data index value)
:delete (avl::avl-insert data index sparse-element) ; cheating...
:traverse (avl::avl-in-order (index value data) body)
:pack (values) ; can't pack an AVL tree
:capacity (values most-positive-fixnum) ; no limit
:count (avl::avl-size data))
;;;--- SCHEME: HASH ---;;;
;; We will add a representation scheme based on hashtables.
;; Poplog does not allow zero as :size to MAKE-HASH-TABLE,
;; so we use (max 1 size) for it. However, I'd like it to be
;; zero for all others (it saves memory -- for example, Clisp
;; seems to allocate a completely empty hashtable is this
;; case). So that's why we have implementation specific code
;; there.
(defscheme :name hash
:type hash-table
:make (make-hash-table :size #+poplog (max 1 size)
#-poplog
#+gcl (max 1 size)
#-gcl
size
:test #'eq)
:get #+xcl (gethash index data sparse-element)
#-xcl (multiple-value-bind (v f) (gethash index data)
(values (the element-type (if f v sparse-element))
(the boolean f)))
:set (setf (gethash (the fixnum index) data) (the element-type value))
:delete (remhash index data)
:traverse #|loop for index fixnum being the hash-keys of data do
(when (and (>= index start)
(or (< end 0)
(<= index end)))
(symbol-macrolet ((value (gethash index data)))
body)))|#
(maphash #'(lambda (index value) ; value will be overriden
(declare (type fixnum index))
(when (and (>= index start)
(or (< end 0)
(<= index end)))
(symbol-macrolet ((value (gethash index data)))
body)))
data)
:pack (values) ; can't pack a hashtable -- yet!
:capacity (values most-positive-fixnum)
:count (hash-table-count data))
;;;--- SCHEME: ARRAY ---;;;
;; An array-structure has an array of ELEMENT-TYPE for the data and a bit vector
;; for representing sparsity: when the bit is 0, the element is not present; when it
;; is one, it is present
(eval-when (:compile-toplevel :load-toplevel :execute)
(defstruct (array-structure (:type vector))
(values (make-array 0) :type simple-array)
(active (make-array 0 :element-type '(mod 2)) :type (simple-array (mod 2) (*)))
(max-capacity 0 :type fixnum)
(count 0 :type fixnum)))
;; We will add a representation scheme based on arrays:
(defscheme :name array
:type (simple-vector 4)
:make (make-array-structure
:values (make-array size
:element-type (quote element-type)
:initial-element (the element-type sparse-element))
:active (make-array size
:element-type (quote (mod 2))
:initial-element 0)
:max-capacity size)
:get (values
(aref (the (simple-array element-type (*)) (array-structure-values data))
#-openmcl index
#+openmcl (coerce index 'fixnum))
(test-equal (aref (the (simple-array (mod 2) (*)) (array-structure-active data)) index)
1))
:set (progn (setf (aref (the (simple-array element-type (*)) (array-structure-values data)) index)
(the element-type value)
(aref (the (simple-array (mod 2) (*)) (array-structure-active data)) index)
1)
(incf (array-structure-count data))
(values))
:delete (progn
(setf (aref
(the (simple-array element-type (*)) (array-structure-values data)) index)
(the element-type sparse-element))
(setf (aref
(the (simple-array (mod 2) (*)) (array-structure-active data)) index)
0)
(decf (array-structure-count data)))
:traverse (when (<= start (1- (length data)))
(loop for index fixnum
from start
to (if (< end 0)
(1- (length data))
(min end (1- (length data)))) do
(when (test-equal 1 (aref (the (simple-array (mod 2) (*))
(array-structure-active data))
index))
(symbol-macrolet ((value (aref (the (simple-array element-type (*))
(array-structure-values data))
index)))
body))))
:pack (values) ; can't pack an ordinary array
:capacity (array-structure-max-capacity data)
:count (array-structure-count data))
;;;--- SCHEME: CVECTOR ---;;;
;;; * values is an array holding the nonzero elements of the sparse-array;
;;; * index is an array of fixnums where the indices are stored;
;;; * max-capacity is the maximum number of elements that the cvector can handle
;;; (it is the size of the internal arrays values and index);
;;; * count is the number of elements actually stored, which may be less than
;;; max-capacity.
(eval-when (:compile-toplevel :load-toplevel :execute)
(defstruct (cvector (:type vector))
(values (make-array 0) :type simple-array)
(index (make-array 0 :element-type 'fixnum) :type (simple-array fixnum (*)))
(max-capacity 0 :type fixnum)
(count 0 :type fixnum)))
;;; The operations on cvectors are defined as macros because we need to generate
;;; code for different data types. If we switch to dynamic typing, array traversals
;;; can be more than 5 times slower.
;; Indices are set to -1 when the cvector is created.
(defmacro make-new-cvector (size &key (element-type t) (sparse-element 0))
"Makes a new (empty) cvector with maximum capacity equal to size.
The element-type parameter can be used to make the internal representation more efficient.
The default element-type is T."
`(make-cvector
:values (make-array ,size :element-type ',element-type :initial-element ,sparse-element)
:index (make-array ,size :element-type 'fixnum :initial-element -1)
:max-capacity ,size))
(defmacro get-cvec (cv index sparse-element element-type test-equal)
"Retrieves an element from a cvector, given its index.
If the element is found, returns (values element T).
If the element is not in the vector, returns (values sparse-element NIL) instead."
(jp-utils:with-gensyms (pos found)
`(multiple-value-bind (,pos ,found)
(binary-search ,index ; fast
(cvector-index ,cv)
; :element-type fixnum
:end (the fixnum (1- (cvector-count ,cv)))
:not-found t)
(declare (type fixnum ,pos)
(type boolean ,found))
(if (not ,found)
(values (the ,element-type ,sparse-element) nil)
(let ((result (aref (the (simple-array ,element-type (*))(cvector-values ,cv)) ,pos)))
(values (the ,element-type result)
(the boolean (not (the boolean (,test-equal result ,sparse-element))))))))))
(defmacro resize-cvec (cv element-type new-size)
"Resizes a cvector, adjusting the arrays for values and indices"
`(setf
(cvector-values ,cv) (robust-adjust-array (cvector-values ,cv) ,new-size :element-type ',element-type)
(cvector-index ,cv) (robust-adjust-array (cvector-index ,cv) ,new-size :element-type 'fixnum)
(cvector-max-capacity ,cv) ,new-size))
(defmacro pack-cvec (cv element-type)
"Packs a cvector so cvector-count will be the same as cvector-max-capacity."
`(resize-cvec ,cv ,element-type (cvector-count ,cv)))
(declaim (inline bsearch-fixnum/index))
;(pprint (macroexpand '
(jp-utils:def-nonrec-binary-search bsearch-fixnum/index :element-type fixnum :not-found t)
; ))
(compile 'bsearch-fixnum/index)
(defmacro set-cvec (cv index sparse-element element-type value resize-function)
"Sets an element in a cvector, given its index.
If the index is not in the vector, it is added, unless there is no more space left,
in which case an error is signaled."
(declare (ignore sparse-element))
(jp-utils:with-gensyms (pos found idx)
(let ((data-vector-type `(simple-array ,element-type (*))))
`(multiple-value-bind (,pos ,found)
#| (nonrec-binary-search
,index
(cvector-index ,cv)
:not-found t
:start 0
:end (the fixnum (1- (cvector-count ,cv))))|#
(bsearch-fixnum/index
,index
(cvector-index ,cv)
0
(the fixnum (1- (cvector-count ,cv))))
(declare (type fixnum ,pos)
(type boolean ,found))
(unless ,found
(when (>= (cvector-count ,cv) (cvector-max-capacity ,cv))
(resize-cvec ,cv ,element-type
(the fixnum ;(+ (cvector-max-capacity ,cv)
(funcall #',resize-function (cvector-max-capacity ,cv)))))
(loop for ,idx fixnum from (cvector-count ,cv) downto (the fixnum (1+ ,pos)) do
(setf (aref (the ,data-vector-type (cvector-values ,cv)) ,idx)
(aref (the ,data-vector-type (cvector-values ,cv)) (1- ,idx)))
(setf (aref (the (simple-array fixnum (*)) (cvector-index ,cv)) ,idx)
(aref (the (simple-array fixnum (*)) (cvector-index ,cv)) (1- ,idx))))
(incf (cvector-count ,cv)))
(setf (aref (the ,data-vector-type (cvector-values ,cv)) ,pos)
(the ,element-type ,value))
(setf (aref (the (simple-array fixnum (*)) (cvector-index ,cv)) ,pos)
(the fixnum ,index))
(values)))))
(defmacro do-traverse-cvector ((index value cvec &key (element-type t) (start 0) (end -1) (setf t)) &body body)
"Traverses a cvector, binding variables to the index and value.
element-type is used in type declarations inside the code; start and end are used
for indices; setf is a flag that makes the method use SYMBOL-MACROLET instead of
LET for the value binding (it is on by default).
An example follows (although the macro expansion is a bit convoluted):
(macroexpand-1 '(spartns::do-traverse-cvector (i v d) b))
==>
(WHEN (<= 0 (AREF (CVECTOR-INDEX D) (1- (CVECTOR-COUNT D))))
(LET ((#:FIXED-START-3597
(MULTIPLE-VALUE-BIND (#:POS-3599 #:FOUND-3600)
(BINARY-SEARCH 0 (CVECTOR-INDEX D) :END
(THE FIXNUM (1- (CVECTOR-COUNT D))) :NOT-FOUND T)
#:POS-3599))
(#:FIXED-END-3598
(IF (< -1 0)
(1- (CVECTOR-COUNT D))
(MULTIPLE-VALUE-BIND (#:POS-3599 #:FOUND-3600)
(BINARY-SEARCH -1 (CVECTOR-INDEX D) :END
(THE FIXNUM (1- (CVECTOR-COUNT D))) :NOT-FOUND
T)
(IF #:FOUND-3600
#:POS-3599
(- #:POS-3599 1))))))
(LOOP FOR #:I-3596 FIXNUM FROM #:FIXED-START-3597 TO #:FIXED-END-3598
DO (LET ((I (AREF (CVECTOR-INDEX D) #:I-3596)))
(SYMBOL-MACROLET ((V
(AREF
(THE (SIMPLE-ARRAY T (*))
(CVECTOR-VALUES D))
#:I-3596)))
(DECLARE (TYPE T V)
(TYPE FIXNUM I))
B)))))
T"
;; FIXME: start and end should be indices, and we should search those indices
;; and then traverse only that part of the cvector.
(jp-utils:with-gensyms (i fixed-start fixed-end pos found)
(let ((the-let (if setf
'symbol-macrolet
'let)))
;; Don't traverse if start > largest stored index:
`(when (<= ,start (aref (cvector-index ,cvec) (1- (cvector-count ,cvec))))
;; Now find start and end positions of given indices:
(let ((,fixed-start
(multiple-value-bind (,pos ,found)
(binary-search ,start
(cvector-index ,cvec)
; :element-type fixnum
:end (the fixnum (1- (cvector-count ,cvec)))
:not-found t)
,pos))
(,fixed-end (if (< ,end 0)
(1- (cvector-count ,cvec))
(multiple-value-bind (,pos ,found)
(binary-search ,end ; fast
(cvector-index ,cvec)
; :element-type fixnum
:end (the fixnum (1- (cvector-count ,cvec)))
:not-found t)
(if ,found ,pos (- ,pos 1))))))
;; And traverse!
(loop for ,i fixnum from ,fixed-start to ,fixed-end do
(let ((,index (aref (cvector-index ,cvec) ,i)))
(,the-let ((,value (aref (the (simple-array ,element-type (*))
(cvector-values ,cvec)) ,i)))
#-gcl (declare (type ,element-type ,value)
(type fixnum ,index))
,@body))))))))
;; We will add a representation scheme that uses cvectors:
(defscheme :name cvector
:type (simple-vector 4)
:make (make-new-cvector (the fixnum size)
:element-type element-type
:sparse-element sparse-element)
:get (get-cvec data index sparse-element element-type test-equal)
:set (set-cvec data index sparse-element element-type value resize-function)
:delete (set-cvec data index sparse-element element-type sparse-element
(lambda (x)
(declare (ignore x)) 0))
:traverse (do-traverse-cvector (index value data :start start
:end end
:element-type element-type)
body)
:pack (pack-cvec data element-type)
:capacity (cvector-max-capacity data)
:count (cvector-count data))
;;;--- AUXILIARY SPARTN FUNCTIONS AND MACROS ---;;;
;;; These functions and macros are used by the macros that define the functions
;;; which will operate on sparse tensors.
(defun sparse-element-for-next-dim (rep-list empty-dim-map sparse-element)
"Returns the sparse element to be used in the next dimension: either the
sparse-element for the tensor (if this is the last dimension) or an empty
map using next dimension's representation."
(if (null (cdr rep-list))
sparse-element
(gethash (second rep-list) empty-dim-map)))
(defun chained-make (&key rep-list non-zero-list element-type sparse-element empty-dim-map)
"This function will return the code necessary to create the representation of one
of the dimensions of the sparse tensor."
(let ((type-next-dim (if (null (cdr rep-list))
element-type
(gethash 'type (get-scheme (second rep-list))))))
(expand-scheme 'spartns::make (car rep-list) `((size (car ,non-zero-list))
(element-type ,type-next-dim)
(sparse-element ,(sparse-element-for-next-dim
rep-list
empty-dim-map
sparse-element))))))
(defun chained-pack (&key data rep-list element-type)
(if (null (cdr rep-list))
`(progn ,(expand-scheme 'spartns::pack (car rep-list) `((data ,data)
(element-type ,element-type))))
(let ((type (gethash 'type (get-scheme (second rep-list)))))
(jp-utils:with-gensyms (trav-index trav-value)
`(progn
,(expand-scheme 'spartns::pack (car rep-list) `((data ,data)
(element-type ,type)))
,(expand-scheme 'spartns::traverse (car rep-list) `((data ,data)
(index ,trav-index)
(value ,trav-value)
(element-type ,type)
(start 0)
(end -1)
(body ,(chained-pack :data trav-value
:rep-list (cdr rep-list)
:element-type element-type)))))))))
(defun chained-get (&key data rep-list index-list element-type sparse-element block-name test-equal empty-dim-map)
"This function will return the code necessary to access one element in
a sparse tensor, given the chained representation scheme.
- data will be inserted as is in the code. It is the container where the tensor is stored.
- rep-list MUST be a list of the representation schemes, and it CANNOT be a variable,
because it will be used in macroexpansion time.
- index-list is inserted as is, and will be later replaced by the list of indices.
- element-type is inserted as is. It is the type of the stored elements.
- sparse-element is inserted as is. It is the value of the sparse element.
- block-name is just a name for the block of code. A wrapper function will use gensym
for this.
This function should be used by a macro that creates the real function for accessing the
tensor."
(jp-utils:with-gensyms (value found)
(let* ((test (if (null (second rep-list))
test-equal
'eql))
(sparse (sparse-element-for-next-dim rep-list empty-dim-map sparse-element))
(type (if (null (second rep-list))
element-type
(gethash 'type (get-scheme (second rep-list)))))
(get-body (expand-scheme 'spartns::get (car rep-list) `((data ,data)
(test-equal ,test)
(index (the fixnum ,(car index-list)))
(sparse-element ,sparse)
(element-type ,type)))))
`(progn (multiple-value-bind (,value ,found) ,get-body
(declare (type boolean ,found))
(if ,found
,(if (null (second rep-list)) ; last dimension, return:
`(return-from ,block-name (the ,element-type ,value))
(chained-get :data value ; not last, recurse!
:rep-list (cdr rep-list)
:index-list (cdr index-list)
:element-type element-type
:sparse-element sparse-element
:block-name block-name
:test-equal test-equal
:empty-dim-map empty-dim-map))
(return-from ,block-name (the ,element-type ,sparse-element))))))))
(defun create-next-dim (value data rep-list index-list non-zero-list element-type sparse-element resize-function)
"This function is used by chained-set to create a new structure when necessary."
(jp-utils:with-gensyms (new-structure)
(let* ((next-rep (second rep-list))
(third-rep (third rep-list))
(second-type (if (null next-rep)
element-type
(gethash 'type (get-scheme next-rep))))
(third-type (if (null third-rep) ; either elements (final dim) or indices
element-type
(gethash 'type (get-scheme third-rep))))
(third-sparse (if (null third-rep)
sparse-element
(expand-scheme 'spartns::make third-rep `((size 0)
(element-type fixnum)
(sparse-element 0)))))
(make-body (expand-scheme 'spartns::make next-rep `((size ,(second non-zero-list))
(element-type ,third-type)
(sparse-element ,third-sparse)))))
`(let ((,new-structure ,make-body))
(setf ,value ,new-structure)
,(expand-scheme 'spartns::set (car rep-list) `((data ,data)
(index (the fixnum ,(car index-list)))
(value ,value)
(element-type ,second-type)
(resize-function ,resize-function)))))))
(defun chained-set (&key
data
rep-list
index-list
new-value
non-zero-list
element-type
sparse-element
block-name
test-equal
empty-dim-map
resize-function)
"This function will return the code necessary to store one element in
a sparse tensor, given the chained representation scheme.
- data will be inserted as is in the code. It is the container where the tensor is stored.
- rep-list MUST be a list of the representation schemes, and it CANNOT be a variable,
because it will be used in macroexpansion time.
- index-list is inserted as is, and will be later replaced by the list of indices.
- new-value will be inserted as is. It is the value to be stored in the tensor.
- element-type is inserted as is. It is the type of the stored elements.
- sparse-element is inserted as is. It is the value of the sparse element.
- block-name is just a name for the block of code. A wrapper function will use gensym
for this."
(jp-utils:with-gensyms (value found)
(let* ((test (if (null (second rep-list))
test-equal
'eql))
(next-rep (second rep-list))
(sparse (sparse-element-for-next-dim rep-list empty-dim-map sparse-element))
(type (if (null next-rep)
element-type
(gethash 'type (get-scheme next-rep))))
(get-body (expand-scheme 'spartns::get (car rep-list) `((data ,data)
(test-equal ,test)
(index (the fixnum ,(car index-list)))
(sparse-element ,sparse)
(element-type ,type))))
(set-body (expand-scheme 'spartns::set (car rep-list) `((data ,data)
(index (the fixnum ,(car index-list)))
(value ,new-value)
(element-type ,type)
(resize-function ,resize-function)))))
(if (null next-rep) ; last dimension, set it:
set-body
`(multiple-value-bind (,value ,found) ,get-body
(declare (type boolean ,found)) ; not last... create next if necessary, then recurse:
(unless ,found
,(create-next-dim value data rep-list index-list non-zero-list element-type sparse-element resize-function))
,(chained-set :data value ; not last, recurse!
:rep-list (cdr rep-list)
:index-list (cdr index-list)
:new-value new-value
:non-zero-list (cdr non-zero-list)
:element-type element-type
:sparse-element sparse-element
:block-name block-name
:test-equal test-equal
:empty-dim-map empty-dim-map
:resize-function resize-function))))))
(defun chained-delete (&key data rep-list index-list element-type sparse-element block-name test-equal empty-dim-map)
"This function will return the code necessary to delete one element from
a sparse tensor, given the chained representation scheme.
- data will be inserted as is in the code. It is the container where the tensor is stored.
- rep-list MUST be a list of the representation schemes, and it CANNOT be a variable,
because it will be used in macroexpansion time.
- index-list is inserted as is, and will be later replaced by the list of indices.
- element-type is inserted as is. It is the type of the stored elements.
- sparse-element is inserted as is. It is the value of the sparse element.
- block-name is just a name for the block of code. A wrapper function will use gensym
for this."
(jp-utils:with-gensyms (value found)
(let* ((test (if (null (second rep-list))
test-equal
'eql))
(sparse (sparse-element-for-next-dim rep-list empty-dim-map sparse-element))
(type (if (null (second rep-list))
element-type
(gethash 'type (get-scheme (second rep-list)))))
(get-body (expand-scheme 'spartns::get (car rep-list) `((data ,data)
(test-equal ,test)
(index (the fixnum ,(car index-list)))
(sparse-element ,sparse)
(element-type ,type))))
(delete-body (expand-scheme 'spartns::delete (car rep-list) `((data ,data)
(index (the fixnum ,(car index-list)))
(sparse-element ,sparse)
(element-type ,element-type)))))
`(multiple-value-bind (,value ,found) ,get-body
(declare (type boolean ,found)
(ignorable ,value))
(if ,found
,(if (null (second rep-list)) ; last dimension, return:
`(progn
,delete-body
(return-from ,block-name (values)))
(chained-delete :data value ; not last, recurse!
:rep-list (cdr rep-list)
:index-list (cdr index-list)
:element-type element-type
:sparse-element sparse-element
:block-name block-name
:test-equal test-equal
:empty-dim-map empty-dim-map))
(return-from ,block-name (values)))))))
(defun traverse-spartn-aux (index-list
value
data
element-type
sparse-element
rep-list
test-equal
dont-bind
empty-dim-map
body)
"This is an auxiliary function that is used by the spartn-generic-traverse macro.
It will produce the code to traverse a sparse tensor, given the representation
scheme.
dont-bind is a list of indices not to bind."
(declare (optimize (debug 3)))
(when (null rep-list)
(return-from traverse-spartn-aux body))
(let* ((the-value (if (null (cdr rep-list)) ; either a new var, or the var given to be bound:
value
(gensym "NEW-VAR-")))
(test (if (null (second rep-list))
test-equal
'eql))
(type (if (null (second rep-list))
element-type
(gethash 'type (get-scheme (second rep-list))))))
(jp-utils:with-gensyms (the-start the-end new-index)
(let ((start 0)
(end -1)
(final-index (car index-list)))
(when (member (car index-list) dont-bind)
(setf start (car index-list)
end (car index-list)
final-index new-index))
;; start and end could be set to "index", so they'll be mixed up. Here
;; we split them:
`(let ((,the-start ,start)
(,the-end ,end))
,(expand-scheme 'spartns::traverse
(car rep-list)
`((data ,data)
(test-equal ,test)
(index ,final-index)
(value ,the-value)
(element-type ,type)
(sparse-element ,sparse-element)
(start ,the-start)
(end ,the-end)
(body ,(traverse-spartn-aux (cdr index-list)
value
the-value
element-type
sparse-element
(cdr rep-list)
test-equal
dont-bind
empty-dim-map
body)))))))))
(defmacro spartn-generic-traverse ((index-list value data)
rep-list
element-type
sparse-element
test-equal
dont-bind
&body body)
"Traverses a sparse tensor, given:
- index-list -- a list of symbols (that will be bound to the indices)
- value -- a symbol to be bound to the value
- data -- the data (of course)
- rep-list -- the list of representation schemes
- element-type -- the element type (we DECLARE its type for efficiency)
- sparse-element -- the sparse element
- test-equal -- the test to be used for equality
- dont-bind -- a list of indices not to bind (these indices must be defined, and their value will be used.
- body -- commands to be executed in each iteration, usig the bound symbols described above"
(multiple-value-bind (make-empty-dims empty-dim-map)
(make-empty-dimensions rep-list)
(append make-empty-dims
(list (traverse-spartn-aux index-list
value
data
element-type
sparse-element
rep-list
test-equal
dont-bind
empty-dim-map
`(progn ,@body))))))
;;;--- SPARTN FUNCTION GENERATORS ---;;;
;;; These functions will generate functions to operate on Spartn structures.
(defun def-spartn-get (fun-name rep-list type sparse-element test-equal empty-dim-map
&key (declare '(optimize (speed 3) (safety 1))) (def NIL))
"Creates a function to get elements in a tensor."
;; (assert (eql (type-of sparse-element) type))
(check-type rep-list list)
(jp-utils:with-gensyms (block-name data)
(let* ((index-list (loop repeat (length rep-list) collect (gensym "INDEX-")))
(definition `(,fun-name (,data ,@index-list)
,(format nil "This function retrieves an element from a sparse tensor. The element type is
~a, the sparse element is ~a and the tensor is represented using the scheme ~a." type sparse-element rep-list)
(declare ,declare)
(declare (type ,(gethash 'spartns::type (get-scheme (car rep-list)))
,data)
(type fixnum ,@index-list))
(block ,block-name
,(chained-get :data data
:rep-list rep-list
:index-list index-list
:element-type type
:sparse-element sparse-element
:block-name block-name
:test-equal test-equal
:empty-dim-map empty-dim-map)))))
(if (null def)
definition
(append (list 'defun) definition)))))
(defun def-spartn-pack (fun-name rep-list type &key (def NIL))
"Creates a function that packs a sparse tensor."
(check-type rep-list list)
(jp-utils:with-gensyms (data)
(let ((definition `(,fun-name (,data)
,(format nil "This function packs a sparse tensor with element type ~a and representation scheme ~a."
type
rep-list)
,(chained-pack :data data :rep-list rep-list :element-type type))))
(if (null def)
definition
(append (list 'defun) definition)))))
(defun def-spartn-set (fun-name rep-list type sparse-element non-zero-list test-equal empty-dim-map resize-function
&key (declare '(optimize (speed 3) (safety 1))) (def NIL))
"Creates a function to set elements in a tensor."
;; (assert (eql (type-of sparse-element) type))
(check-type rep-list list)
(jp-utils:with-gensyms (block-name data value)
(let* ((index-list (loop repeat (length rep-list) collect (gensym "INDEX-")))
(definition `(,fun-name (,data ,@index-list ,value)
,(format nil "This function sets an element on a sparse tensor. The element type is
~a, the sparse element is ~a and the tensor is represented using the scheme ~a." type sparse-element rep-list)
(declare ,declare)
(block ,block-name
,(chained-set :data data
:rep-list rep-list
:index-list index-list
:new-value value
:non-zero-list non-zero-list
:element-type type
:sparse-element sparse-element
:block-name block-name
:test-equal test-equal
:resize-function resize-function
:empty-dim-map empty-dim-map)))))
;; If we don't return (values), CMUCL complains about not knowing the return type:
(if (null def)
(append definition (list '(values)))
(append (list 'defun) definition (list '(values)))))))
(defun def-spartn-delete (fun-name rep-list type sparse-element test-equal empty-dim-map
&key (declare '(optimize (speed 3) (safety 1))) (def NIL))
"Creates a function to delete elements from a tensor."
;; (assert (eql (type-of sparse-element) type))
(check-type rep-list list)
(jp-utils:with-gensyms (block-name data)
(let* ((index-list (loop repeat (length rep-list) collect (gensym "INDEX-")))
(definition `(,fun-name (,data ,@index-list)
,(format nil "This function deletes an element from a sparse tensor. The element type is
~a, the sparse element is ~a and the tensor is represented using the scheme ~a." type sparse-element rep-list)
(declare ,declare)
(block ,block-name
,(chained-delete :data data
:rep-list rep-list
:index-list index-list
:element-type type
:sparse-element sparse-element
:block-name block-name
:test-equal test-equal
:empty-dim-map empty-dim-map)))))
;; If we don't return (values), CMUCL complains about not knowing the return type:
(if (null def)
(append definition (list '(values)))
(append (list 'defun) definition (list '(values)))))))
(defun def-spartn-make (fun-name rep-list type sparse non-zero-list empty-dim-map
&key (declare '(optimize (speed 3) (safety 1))) (def NIL))
"Creates a function to initialize a tensor."
(check-type rep-list list)
(check-type non-zero-list list)
(let ((definition `(,fun-name (&key (non-zeros ',non-zero-list))
,(format nil "This function creates a sparse tensor with element type ~a and representation scheme ~a."
type
rep-list)
(declare ,declare)
,(chained-make :rep-list rep-list
:non-zero-list 'non-zeros
:element-type type
:sparse-element sparse
:empty-dim-map empty-dim-map))))
(if (null def)
definition
(append (list 'defun) definition))))
;; I tried not using LIST and using only quasiquotation, and I went nuts.
;; But it works now:
(defun def-spartn-traverse (mac-name rep-list type sparse test-equal &key (def NIL))
"Creates a macro for traversing one type of spartn, given the representation scheme (as a list), the element
type, the sparse element and the desired name for the new macro.
For example:
(def-spartn-traverse do-hhd (hash hash) double-float 0d0 #'=)
This will create a macro DO-HHD which can be used like this:
(do-hhd ((i j) val data)
(format t \"The value at (~a, ~a) is ~a\" i j val))"
(jp-utils:with-gensyms (value data)
(let* ((index-list (loop repeat (length rep-list) collect (gensym)))
(definition `(,mac-name ((,index-list ,value ,data &key (dont-bind NIL)) &body body)
(list 'spartn-generic-traverse
(list (list ,@index-list)
,value
,data)
',rep-list
',type
',sparse
',test-equal
dont-bind
(push 'progn body)))))
(if (null def)
definition
(append (list 'defmacro) definition)))))
(defun def-spartn-copy (fun-name scheme-name &key (declare '(optimize (speed 3) (safety 1))) (def NIL))
"Creates a fuction that copies a spartn.
Examples:
(def-spartn-copy 'my-copy-function 'hash)
==>
(MY-COPY-FUNCTION (#:DATA-817) \"Copies a sparse tensor of type HASH\"
(DECLARE (OPTIMIZE (SPEED 3) (SAFETY 1)))
(SPARTN-CONVERT #:DATA-817 :FROM HASH :TO HASH :DESTRUCTIVE NIL))
(def-spartn-copy 'my-copy-function 'hash :def t)
==>
(DEFUN MY-COPY-FUNCTION (#:DATA-820)
\"Copies a sparse tensor of type HASH\"
(DECLARE (OPTIMIZE (SPEED 3) (SAFETY 1)))
(SPARTN-CONVERT #:DATA-820 :FROM HASH :TO HASH :DESTRUCTIVE NIL))"
(declare (optimize (debug 3)))
(check-type scheme-name symbol)
(jp-utils:with-gensyms (data)
(let ((definition `(,fun-name (,data)
,(format nil "Copies a sparse tensor of type ~a" scheme-name)
(declare ,declare) ;; XCL gets confused here
(spartns:spartn-convert ,data
:from ,scheme-name
:to ,scheme-name
:destructive nil))))
(if (null def)
definition
(append (list 'defun) definition)))))
;;;--- SPARTNS ---;;;
(eval-when (:compile-toplevel :load-toplevel :execute)
(defstruct spartn-scheme
representation
non-zero
element-type
sparse-element
declare
test-equal
resize-function))
(defun define-spartn-functions (name spartn-scheme empty-dim-map empty-dim-defs &key (def nil) (declare nil))
"Returns the code for the functions used with a spartn scheme. This is used by defspartn and w/spartns.
If DEF is T, then this function will generate top-level code to define the functions:
(DEFUN get-X (tensor index) ...)
If DEF is NIL, then only the names, lambda-lists and bodies are generated:
(get-X (tensor index) ...)
The first form is used by defspartn (which is supposed to be used as a top-leval form), and the second
is used by w/spartns (which will use LABELS to create local functions)."
(let ((make-fun-name (format-symbol "MAKE-~a" name))
(get-fun-name (format-symbol "GET-~a" name))
(set-fun-name (format-symbol "SET-~a" name))