-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgebra.cpp
1245 lines (1129 loc) · 37.5 KB
/
algebra.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright 2019 Eliza Wszola ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "algebra.h"
//The non-vectorized functions are not really optimized.
//They are here to validate correctness.
//The quantized functions do nothing if HAS_QUANTIZED is disabled
//With HAS_QUANTIZED disabled and main.cpp implemented the way it is
//it should not be possible to call them anyway
uint32_t LINE_SIZE;
uint32_t SPARSE_PIECE_LENGTH;
uint32_t B_THREAD_CHUNK_SIZE;
data_representations data_rep;
void nsleep(uint32_t sleep_time) {
struct timespec tim;
tim.tv_sec = 0;
tim.tv_nsec = (long)sleep_time;
nanosleep(&tim, NULL);
}
#if HAS_HBW
void *b_malloc(size_t size, bool use_hbw) {
void *allocated;
int err;
if (use_hbw)
err = hbw_posix_memalign(&allocated, LINE_SIZE, size);
else
err = posix_memalign(&allocated, LINE_SIZE, size);
if (err) {
std::cout << "Memory allocation error.\n";
return 0;
} else {
return allocated;
}
}
void b_free(void* ptr, bool use_hbw) {
if (use_hbw)
hbw_free(ptr);
else
free(ptr);
}
#else
void *b_malloc(size_t size, bool use_hbw) {
void *allocated;
//ignore use_hbw
int err = posix_memalign(&allocated, LINE_SIZE, size);
if (err) {
std::cout << "Memory allocation error.\n";
return 0;
} else {
return allocated;
}
}
void b_free(void* ptr, bool use_hbw) {
//ignore use_hbw
free(ptr);
}
#endif
void raise_error(std::string error) {
std::string message = "[E] " + error + "\n";
std::cerr << message;
exit(-1);
}
/* ================= MATRIX MEMORY MANAGEMENT ================= */
Matrix allocate_matrix(uint32_t rows, uint32_t columns, bool use_hbw) {
uint64_t size = (uint64_t)rows * columns;
uint32_t row;
uint32_t reals_per_line = LINE_SIZE / sizeof(real);
uint32_t padded_rows;
if (data_rep == QUANTIZED)
padded_rows = (rows + LINE_SIZE - 1) / LINE_SIZE * LINE_SIZE;
else
padded_rows = (rows + reals_per_line - 1)
/ reals_per_line * reals_per_line;
size = (uint64_t)padded_rows * columns;
Matrix mat = {};
mat.columns = columns;
mat.rows = rows;
mat.padded_rows = padded_rows;
mat.data = (real*)b_malloc(size * sizeof(real), use_hbw);
if (!mat.data)
raise_error("Matrix allocation failed.");
return mat;
}
Matrix create_matrix(real *data, uint32_t rows, uint32_t columns,
bool use_hbw) {
Matrix mat = allocate_matrix(rows, columns, use_hbw);
if (!data) {
std::memset(mat.data, 0, sizeof(real) * (uint64_t)rows * columns);
} else {
for (uint32_t i = 0; i < columns; ++i) {
std::memcpy(mat.data + i * (uint64_t)mat.padded_rows,
data + i * (uint64_t)rows, rows * sizeof(real));
for (uint32_t j = rows; j < mat.padded_rows; ++j)
mat.data[i * (uint64_t)mat.padded_rows + j] = 0;
}
}
return mat;
}
Matrix create_matrix(real *data, uint32_t rows, uint32_t columns) {
return create_matrix(data, rows, columns, false);
}
Matrix create_matrix(uint32_t rows, uint32_t columns) {
return create_matrix(nullptr, rows, columns, false);
}
Matrix create_matrix(uint32_t rows, uint32_t columns, bool use_hbw) {
return create_matrix(nullptr, rows, columns, use_hbw);
}
Matrix read_matrix(std::ifstream &x_stream,
uint32_t rows, uint32_t columns, bool use_hbw) {
Matrix mat = allocate_matrix(rows, columns, use_hbw);
std::memset(mat.data, 0, (uint64_t)rows * columns * sizeof(real));
real *x_buffer = new real[rows];
for (uint32_t j = 0; j < columns; ++j) {
x_stream.read(reinterpret_cast<char*>(x_buffer),
std::streamsize(rows * sizeof(real)));
std::memcpy(mat.data + j * (uint64_t)mat.padded_rows, x_buffer,
rows * sizeof(real));
}
delete[] x_buffer;
return mat;
}
void destroy(Matrix mat, bool use_hbw) {
if (mat.data)
b_free(mat.data, use_hbw);
mat.data = nullptr;
}
void destroy(Matrix mat) {
destroy(mat, false);
}
QuantMatrix allocate_matrix_quant(uint32_t rows, uint32_t columns,
bool use_hbw) {
uint32_t row;
uint32_t reals_per_line = LINE_SIZE / sizeof(real);
uint32_t padded_rows = (rows + LINE_SIZE - 1) / LINE_SIZE * LINE_SIZE;
QuantMatrix mat = {};
#if HAS_QUANTIZED
mat.columns = columns;
mat.rows = rows;
mat.padded_rows = padded_rows;
mat.data = (CloverVec*)b_malloc(columns * sizeof(CloverVec), use_hbw);
for (uint32_t i = 0; i < columns; ++i)
new(mat.data + i) CloverVec(padded_rows, use_hbw);
if (!mat.data)
raise_error("Matrix allocation failed.");
for (uint32_t i = 0; i < columns; ++i)
if (!mat.data[i].getData())
raise_error("Matrix allocation failed.");
#endif
return mat;
}
QuantMatrix create_matrix_quant(real *data, uint32_t rows,
uint32_t columns, bool use_hbw) {
QuantMatrix mat = allocate_matrix_quant(rows, columns, use_hbw);
#if HAS_QUANTIZED
if (!data) {
for (uint32_t i = 0; i < columns; ++i)
mat.data[i].clear();
} else {
CloverVector32 buffer_vec(rows);
float *buffer = buffer_vec.getData();
for (uint32_t i=0; i<columns; ++i) {
uint32_t row = i * rows;
std::memcpy(buffer, data + i * rows,
rows * sizeof(float));
mat.data[i].quantize(buffer_vec);
}
}
#endif
return mat;
}
QuantMatrix create_matrix_quant(real *data, uint32_t rows,
uint32_t columns) {
return create_matrix_quant(data, rows, columns, false);
}
QuantMatrix create_matrix_quant(uint32_t rows, uint32_t columns) {
return create_matrix_quant(nullptr, rows, columns, false);
}
QuantMatrix create_matrix_quant(uint32_t rows, uint32_t columns,
bool use_hbw) {
return create_matrix_quant(nullptr, rows, columns, use_hbw);
}
QuantMatrix read_matrix_quant(std::ifstream &x_stream,
uint32_t rows, uint32_t columns, bool use_hbw) {
QuantMatrix mat = allocate_matrix_quant(rows, columns, use_hbw);
#if HAS_QUANTIZED
CloverVector32 buffer_vec(mat.padded_rows);
float *buffer = buffer_vec.getData();
float *buf2 = new float[rows];
for (uint32_t i = 0; i < columns; ++i) {
x_stream.read(reinterpret_cast<char*>(buf2),
std::streamsize(rows * sizeof(float)));
for (uint32_t j = 0; j < rows; ++j)
buffer[j] = buf2[j];
for (uint32_t j = rows; j < mat.padded_rows; ++j)
buffer[j] = 0;
mat.data[i].quantize(buffer_vec);
}
delete[] buf2;
#endif
return mat;
}
void destroy(QuantMatrix mat, bool use_hbw) {
#if HAS_QUANTIZED
if (mat.data) {
for (uint32_t i = 0; i < mat.columns; ++i)
mat.data[i].~CloverVec();
b_free(mat.data, use_hbw);
}
mat.data = nullptr;
#endif
}
void destroy(QuantMatrix mat) {
destroy(mat, false);
}
SparseMatrix allocate_matrix_sparse(uint32_t rows, uint32_t columns,
bool use_hbw) {
SparseMatrix mat = {};
mat.columns = columns;
mat.rows = rows;
mat.column_data = (SparseVector*)b_malloc(sizeof(SparseVector)
* columns, use_hbw);
if (!mat.column_data)
raise_error("Matrix allocation failed.");
return mat;
}
SparseMatrix create_matrix_sparse(uint32_t rows, uint32_t columns,
bool use_hbw) {
SparseMatrix mat = allocate_matrix_sparse(rows, columns, use_hbw);
for (uint32_t j = 0; j < columns; ++j) {
mat.column_data[j].data = nullptr;
mat.column_data[j].length = 0;
mat.column_data[j].nnz = 0;
mat.column_data[j].padded_nnz = 0;
mat.column_data[j].max_idx = 0;
}
return mat;
}
SparseMatrix create_matrix_sparse(uint32_t rows, uint32_t columns) {
return create_matrix_sparse(rows, columns, false);
}
SparseMatrix read_matrix_sparse(std::ifstream &x_stream,
uint32_t rows, uint32_t columns, bool use_hbw) {
SparseMatrix mat = allocate_matrix_sparse(rows, columns, use_hbw);
uint32_t nnz;
uint32_t max_idx;
uint32_t padded_nnz;
uint32_t small_len;
SparsePiece* piece_ptr;
SparsePiece* prev_ptr;
uint32_t* idx = new uint32_t[rows];
float* vals = new float[rows];
for (uint32_t j = 0; j < columns; ++j) {
mat.column_data[j].data = nullptr;
mat.column_data[j].max_idx = 0;
piece_ptr = nullptr;
prev_ptr = nullptr;
nnz = 0;
padded_nnz = 0;
x_stream.read(reinterpret_cast<char*>(&nnz),
std::streamsize(sizeof(uint32_t)));
x_stream.read(reinterpret_cast<char*>(idx),
std::streamsize(nnz * sizeof(uint32_t)));
x_stream.read(reinterpret_cast<char*>(vals),
std::streamsize(nnz * sizeof(float)));
for (uint32_t i = 0; i < nnz; i += SPARSE_PIECE_LENGTH) {
piece_ptr = (SparsePiece*)b_malloc(sizeof(SparsePiece), use_hbw);
if (!piece_ptr)
raise_error("Matrix allocation failed.");
piece_ptr->next = nullptr;
piece_ptr->values = (real*)b_malloc(
SPARSE_PIECE_LENGTH * sizeof(real), use_hbw);
piece_ptr->indices = (uint32_t*)b_malloc(
SPARSE_PIECE_LENGTH * sizeof(uint32_t), use_hbw);
if (!mat.column_data[j].data) {
mat.column_data[j].data = piece_ptr;
} else {
prev_ptr->next = piece_ptr;
}
small_len = std::min((uint32_t)SPARSE_PIECE_LENGTH, nnz - i);
std::memcpy(piece_ptr->values, vals + i,
small_len * sizeof(float));
std::memcpy(piece_ptr->indices, idx + i,
small_len * sizeof(uint32_t));
mat.column_data[j].max_idx = piece_ptr->indices[small_len - 1];
piece_ptr->max_idx = piece_ptr->indices[small_len - 1];
piece_ptr->small_len = small_len;
prev_ptr = piece_ptr;
padded_nnz += SPARSE_PIECE_LENGTH;
if (small_len < SPARSE_PIECE_LENGTH) {
std::memset(piece_ptr->values + small_len, 0,
(SPARSE_PIECE_LENGTH - small_len) * sizeof(float));
std::memset(piece_ptr->indices + small_len, 0,
(SPARSE_PIECE_LENGTH - small_len) * sizeof(uint32_t));
}
if (i + SPARSE_PIECE_LENGTH >= nnz)
max_idx = (small_len > 0
? piece_ptr->indices[small_len - 1] : 0);
}
mat.column_data[j].nnz = nnz;
mat.column_data[j].length = rows;
mat.column_data[j].padded_nnz = padded_nnz;
mat.column_data[j].max_idx = max_idx;
}
delete[] idx;
delete[] vals;
return mat;
}
void destroy(SparseMatrix mat, bool use_hbw) {
if (mat.column_data) {
for (uint32_t j = 0; j < mat.columns; ++j) {
SparsePiece *runner = mat.column_data[j].data;
while (runner) {
SparsePiece* ptr = runner;
runner = runner->next;
b_free(ptr->values, use_hbw);
b_free(ptr->indices, use_hbw);
b_free(ptr, use_hbw);
}
}
b_free(mat.column_data, use_hbw);
}
mat.column_data = nullptr;
}
void destroy(SparseMatrix mat) {
destroy(mat, false);
}
OneSparseMatrix read_matrix_one_sparse(std::ifstream &x_stream,
uint32_t rows, uint32_t columns, bool use_hbw) {
OneSparseMatrix mat = {};
mat.columns = columns;
mat.rows = rows;
mat.column_data = (OneSparseVector*)b_malloc(sizeof(OneSparseVector)
* columns, use_hbw);
if (!mat.column_data)
raise_error("Matrix allocation failed.");
uint32_t nnz;
uint32_t padded_nnz;
uint32_t small_len;
uint32_t* idx;
float* vals;
for (uint32_t j = 0; j < columns; ++j) {
nnz = 0;
padded_nnz = 0;
x_stream.read(reinterpret_cast<char*>(&nnz),
std::streamsize(sizeof(uint32_t)));
padded_nnz = (nnz + 31) / 32 * 32;
idx = (uint32_t*)b_malloc(padded_nnz * sizeof(uint32_t), use_hbw);
vals = (float*)b_malloc(padded_nnz * sizeof(float), use_hbw);
x_stream.read(reinterpret_cast<char*>(idx),
std::streamsize(nnz * sizeof(uint32_t)));
x_stream.read(reinterpret_cast<char*>(vals),
std::streamsize(nnz * sizeof(float)));
if (nnz != padded_nnz) {
std::memset(vals + nnz, 0, (padded_nnz - nnz) * sizeof(float));
std::memset(idx + nnz, 0, (padded_nnz - nnz) * sizeof(uint32_t));
}
mat.column_data[j].nnz = nnz;
mat.column_data[j].length = rows;
mat.column_data[j].padded_nnz = padded_nnz;
mat.column_data[j].max_idx = (nnz > 0 ? idx[nnz - 1] : 0);
mat.column_data[j].indices = idx;
mat.column_data[j].values = vals;
}
return mat;
}
void destroy(OneSparseMatrix mat, bool use_hbw) {
if (mat.column_data) {
for (uint32_t j = 0; j < mat.columns; ++j) {
b_free(mat.column_data[j].values, use_hbw);
b_free(mat.column_data[j].indices, use_hbw);
}
b_free(mat.column_data, use_hbw);
}
mat.column_data = nullptr;
}
void destroy(OneSparseMatrix mat) {
destroy(mat, false);
}
/* ================= VECTOR MEMORY MANAGEMENT ================= */
Vector allocate_vector(uint32_t length, bool use_hbw) {
Vector vec;
if (data_rep == QUANTIZED)
vec.length = (length + LINE_SIZE - 1) / LINE_SIZE * LINE_SIZE;
else
vec.length = length;
vec.data = (real*)b_malloc(vec.length * sizeof(real), use_hbw);
if (!vec.data)
raise_error("Vector allocation failed.");
return vec;
}
Vector create_vector(real *data, uint32_t length, bool use_hbw) {
Vector vec = allocate_vector(length, use_hbw);
if (!data) {
std::memset(vec.data, 0, sizeof(real) * vec.length);
}
else {
std::memcpy(vec.data, data, sizeof(real) * length);
if (vec.length != length)
std::memset(vec.data + length, 0,
sizeof(real) * (vec.length - length));
}
return vec;
}
Vector create_vector(real *data, uint32_t length) {
return create_vector(data, length, false);
}
Vector create_vector(uint32_t length) {
return create_vector(nullptr, length, false);
}
Vector create_vector(uint32_t length, bool use_hbw) {
return create_vector(nullptr, length, use_hbw);
}
Vector read_vector(std::ifstream &y_stream,
uint32_t length, bool use_hbw) {
Vector vec = allocate_vector(length, use_hbw);
y_stream.read(reinterpret_cast<char*>(vec.data),
std::streamsize(length * sizeof(real)));
if (vec.length != length)
std::memset(vec.data + length, 0,
sizeof(real) * (vec.length - length));
return vec;
}
void destroy(Vector vec, bool use_hbw) {
if (!vec.data)
b_free(vec.data, use_hbw);
vec.data = nullptr;
}
void destroy(Vector vec) {
destroy(vec, false);
}
QuantVector allocate_vector_quant(uint32_t length, bool use_hbw) {
QuantVector vec = {};
#if HAS_QUANTIZED
vec.length = (length + LINE_SIZE - 1) / LINE_SIZE * LINE_SIZE;
uint32_t reals_per_line = LINE_SIZE / sizeof(real);
vec.data = (CloverVec*)b_malloc(sizeof(CloverVec), use_hbw);
new(vec.data) CloverVec(length, use_hbw);
if (!vec.data || !vec.data->getData())
raise_error("Vector allocation failed.");
#endif
return vec;
}
QuantVector create_vector_quant(real *data, uint32_t length,
bool use_hbw) {
QuantVector vec = allocate_vector_quant(length, use_hbw);
#if HAS_QUANTIZED
CloverVector32 buffer_vec(length);
float *buffer = buffer_vec.getData();
if (!data) {
vec.data->clear();
} else {
std::memcpy(buffer, data, sizeof(float) * length);
vec.data->quantize(buffer_vec);
}
#endif
return vec;
}
QuantVector create_vector_quant(real *data, uint32_t length) {
return create_vector_quant(data, length, false);
}
QuantVector create_vector_quant(uint32_t length) {
return create_vector_quant(nullptr, length, false);
}
QuantVector create_vector_quant(uint32_t length, bool use_hbw) {
return create_vector_quant(nullptr, length, use_hbw);
}
QuantVector read_vector_quant(std::ifstream &y_stream,
uint32_t length, bool use_hbw) {
QuantVector vec = allocate_vector_quant(length, use_hbw);
#if HAS_QUANTIZED
CloverVector32 buffer_vec(length);
float *buffer = buffer_vec.getData();
float *buf2 = new float[length];
y_stream.read(reinterpret_cast<char*>(buf2),
std::streamsize(length * sizeof(float)));
for (uint32_t j = 0; j < length; ++j)
buffer[j] = buf2[j];
vec.data->quantize(buffer_vec);
delete[] buf2;
#endif
return vec;
}
void destroy(QuantVector vec, bool use_hbw) {
#if HAS_QUANTIZED
if (!vec.data) {
vec.data->~CloverVec();
b_free(vec.data, use_hbw);
}
vec.data = nullptr;
#endif
}
void destroy(QuantVector vec) {
destroy(vec, false);
}
/* ================= GETTERS AND SETTERS ================= */
//Returns a vector containing pointer to a column,
//Modifications to the vector change the original matrix.
Vector get_column(Matrix mat, uint32_t index) {
if (index >= mat.columns)
raise_error("get_column: Matrix column index out of bounds! ("
+ std::to_string(index) + ">="
+ std::to_string(mat.columns) + ")");
Vector vec = {};
if (data_rep == QUANTIZED)
vec.length = mat.padded_rows;
else
vec.length = mat.rows;
vec.data = mat.data + ((uint64_t)mat.padded_rows * index);
return vec;
}
QuantVector get_column(QuantMatrix mat, uint32_t index) {
#if HAS_QUANTIZED
if (index >= mat.columns)
raise_error("get_column: Matrix column index out of bounds! ("
+ std::to_string(index) + ">="
+ std::to_string(mat.columns) + ")");
QuantVector vec = {};
vec.length = mat.padded_rows;
vec.data = mat.data + index;
return vec;
#else
QuantVector vec = {};
return vec;
#endif
}
SparseVector get_column(SparseMatrix mat, uint32_t index) {
if (index >= mat.columns)
raise_error("get_column: Matrix column index out of bounds! ("
+ std::to_string(index) + ">="
+ std::to_string(mat.columns) + ")");
SparseVector vec = mat.column_data[index];
return vec;
}
OneSparseVector get_column(OneSparseMatrix mat, uint32_t index) {
if (index >= mat.columns)
raise_error("get_column: Matrix column index out of bounds! ("
+ std::to_string(index) + ">="
+ std::to_string(mat.columns) + ")");
OneSparseVector vec = mat.column_data[index];
return vec;
}
real get_value(Vector vec, uint32_t index) {
if (index >= vec.length)
raise_error("get_value: Vector index out of bounds! ("
+ std::to_string(index) + ">="
+ std::to_string(vec.length) + ")");
return vec.data[index];
}
real get_value(QuantVector vec, uint32_t index) {
#if HAS_QUANTIZED
if (index >= vec.length)
raise_error("get_value: Vector index out of bounds! ("
+ std::to_string(index) + ">="
+ std::to_string(vec.length) + ")");
return vec.data->get(index);
#else
return 0;
#endif
}
void set_value(Vector &vec, uint32_t index, real value) {
if (index >= vec.length)
raise_error("get_value: Vector index out of bounds! ("
+ std::to_string(index) + ">="
+ std::to_string(vec.length) + ")");
vec.data[index] = value;
}
void set(Vector &vec, Vector tvec) {
if (vec.length != tvec.length)
raise_error("set: Vector lengths do not match! ("
+ std::to_string(vec.length) + "!="
+ std::to_string(tvec.length) + ")");
std::memcpy(vec.data, tvec.data, vec.length * sizeof(real));
}
void set_column(Matrix &mat, uint32_t column, Vector vec) {
if (vec.length != mat.rows)
raise_error("set_column: Vector does not match matrix column! ("
+ std::to_string(vec.length) + "!="
+ std::to_string(mat.rows) + ")");
if (column >= mat.columns)
raise_error("set_column: Matrix column index out of bounds! ("
+ std::to_string(column) + ">="
+ std::to_string(mat.columns) + ")");
std::memcpy(mat.data + (uint64_t)mat.padded_rows * column, vec.data,
vec.length * sizeof(real));
}
void set_column(QuantMatrix &mat, uint32_t column, QuantVector vec) {
#if HAS_QUANTIZED
if (vec.length != mat.padded_rows)
raise_error("set_column: Vector does not match matrix column! ("
+ std::to_string(vec.length) + "!="
+ std::to_string(mat.padded_rows) + ")");
if (column >= mat.columns)
raise_error("set_column: Matrix column index out of bounds! ("
+ std::to_string(column) + ">="
+ std::to_string(mat.columns) + ")");
std::memcpy(mat.data[column].getData(), vec.data->getData(),
vec.data->getValueBytes());
std::memcpy(mat.data[column].getScales(), vec.data->getScales(),
vec.data->getScaleBytes());
#endif
}
//PRE: mat column already has the required pieces
void set_column(SparseMatrix &mat, uint32_t column, SparseVector vec) {
if (vec.length != mat.rows)
raise_error("set_column: Vector does not match matrix column! ("
+ std::to_string(vec.length) + "!="
+ std::to_string(mat.rows) + ")");
if (column >= mat.columns)
raise_error("set_column: Matrix column index out of bounds! ("
+ std::to_string(column) + ">="
+ std::to_string(mat.columns) + ")");
mat.column_data[column].nnz = vec.nnz;
mat.column_data[column].padded_nnz = vec.padded_nnz;
mat.column_data[column].length = vec.length;
mat.column_data[column].max_idx = vec.max_idx;
SparsePiece* source_ptr = vec.data;
SparsePiece* target_ptr = mat.column_data[column].data;
while (source_ptr && target_ptr) {
std::memcpy(target_ptr->values, source_ptr->values,
SPARSE_PIECE_LENGTH * sizeof(real));
std::memcpy(target_ptr->indices, source_ptr->indices,
SPARSE_PIECE_LENGTH * sizeof(uint32_t));
target_ptr->small_len = source_ptr->small_len;
source_ptr = source_ptr->next;
target_ptr = target_ptr->next;
}
if (source_ptr || target_ptr)
raise_error("set_column: Vector pieces do not match the column!");
}
//PRE: mat column already has the required pieces
void set_column(SparseMatrix &mat, uint32_t column,
OneSparseVector vec) {
if (vec.length != mat.rows)
raise_error("set_column: Vector does not match matrix column! ("
+ std::to_string(vec.length) + "!="
+ std::to_string(mat.rows) + ")");
if (column >= mat.columns)
raise_error("set_column: Matrix column index out of bounds! ("
+ std::to_string(column) + ">="
+ std::to_string(mat.columns) + ")");
mat.column_data[column].nnz = vec.nnz;
mat.column_data[column].length = vec.length;
mat.column_data[column].max_idx = vec.max_idx;
SparsePiece* target_ptr = mat.column_data[column].data;
uint32_t source_idx = 0;
uint32_t small_len;
uint32_t padded_nnz = 0;
while (target_ptr && source_idx < vec.nnz) {
small_len = std::min((uint32_t)SPARSE_PIECE_LENGTH,
vec.nnz - source_idx);
std::memcpy(target_ptr->values, vec.values + source_idx,
small_len * sizeof(float));
std::memcpy(target_ptr->indices, vec.indices + source_idx,
small_len * sizeof(uint32_t));
if (small_len != SPARSE_PIECE_LENGTH) {
std::memset(target_ptr->values + small_len, 0,
(SPARSE_PIECE_LENGTH - small_len) * sizeof(float));
std::memset(target_ptr->indices + small_len, 0,
(SPARSE_PIECE_LENGTH - small_len) * sizeof(uint32_t));
}
target_ptr->small_len = small_len;
target_ptr->max_idx = (small_len > 0
? target_ptr->values[small_len - 1] : 0);
target_ptr = target_ptr->next;
source_idx += SPARSE_PIECE_LENGTH;
}
mat.column_data[column].padded_nnz = source_idx;
if (target_ptr || source_idx < vec.nnz)
raise_error("set_column: Vector pieces do not match the column!");
}
/* ================= SCALAR OPERATIONS ================= */
void scalar_multiply(Vector target, Vector vec, real scalar) {
if (target.length != vec.length)
raise_error("scalar_multiply: Vector lengths do not match! ("
+ std::to_string(target.length) + "!="
+ std::to_string(vec.length) + ")");
for (uint32_t i = 0; i < vec.length; ++i)
target.data[i] = vec.data[i] * scalar;
}
void scalar_divide(Vector target, Vector vec, real scalar) {
if (target.length != vec.length)
raise_error("scalar_divide: Vector lengths do not match! ("
+ std::to_string(target.length) + "!="
+ std::to_string(vec.length) + ")");
for (uint32_t i = 0; i < vec.length; ++i)
target.data[i] = vec.data[i] / scalar;
}
real norm_2_squared(Vector vec) {
return dot_product(vec, vec);
}
real norm_1(Vector vec) {
real sum1 = 0;
real sum2 = 0;
real sum3 = 0;
real sum4 = 0;
real sum;
uint32_t len_div_4 = vec.length - (vec.length & 3);
for (uint32_t i = 0; i < len_div_4; i += 4) {
sum1 += std::abs(vec.data[i]);
sum2 += std::abs(vec.data[i+1]);
sum3 += std::abs(vec.data[i+2]);
sum4 += std::abs(vec.data[i+3]);
}
sum = sum1 + sum2 + sum3 + sum4;
for (uint32_t i = len_div_4; i < vec.length; ++i)
sum += std::abs(vec.data[i]);
return sum;
}
real dot_product(Vector vec1, Vector vec2) {
return dot_product(vec1, vec2, 0, vec1.length);
}
real dot_product(Vector vec1, Vector vec2,
uint32_t start, uint32_t end) {
if (vec1.length != vec2.length)
raise_error("dot_product: Vector lengths are not equal! ("
+ std::to_string(vec1.length) + "!="
+ std::to_string(vec2.length) + ")");
if (end > vec1.length)
raise_error("dot_product: Range out of bounds! ("
+ std::to_string(end) + ">" + std::to_string(vec1.length) + ")");
real sum1 = 0;
real sum2 = 0;
real sum3 = 0;
real sum4 = 0;
real sum;
uint32_t end_div_4 = end - ((end - start) & 3);
for (uint32_t i = start; i < end_div_4; i += 4) {
sum1 += vec1.data[i] * vec2.data[i];
sum2 += vec1.data[i+1] * vec2.data[i+1];
sum3 += vec1.data[i+2] * vec2.data[i+2];
sum4 += vec1.data[i+3] * vec2.data[i+3];
}
sum = sum1 + sum2 + sum3 + sum4;
for (uint32_t i = end_div_4; i < end; ++i)
sum += vec1.data[i] * vec2.data[i];
return sum;
}
void scalar_multiply_add(Vector target, Vector vec, real scalar) {
scalar_multiply_add(target, vec, scalar, 0, target.length);
}
void scalar_multiply_add(Vector target, Vector vec, real scalar,
uint32_t start, uint32_t end) {
if (target.length != vec.length)
raise_error("scalar_multiply_add: Vector lengths do not match! ("
+ std::to_string(target.length) + "!="
+ std::to_string(vec.length) + ")");
if (end > vec.length)
raise_error("scalar_multiply_add: Range out of bounds! ("
+ std::to_string(end) + ">" + std::to_string(vec.length) + ")");
for (uint32_t i = start; i < end; ++i)
target.data[i] += vec.data[i] * scalar;
}
real dot_product(SparseVector vec1, Vector vec2) {
return dot_product(vec2, vec1, 0, vec1.length);
}
real dot_product(Vector vec1, SparseVector vec2) {
return dot_product(vec1, vec2, 0, vec1.length);
}
real dot_product(SparseVector vec1, Vector vec2,
uint32_t start, uint32_t end) {
return dot_product(vec2, vec1, start, end);
}
real dot_product(Vector vec1, SparseVector vec2,
uint32_t start, uint32_t end) {
if (vec1.length != vec2.length)
raise_error("dot_product: Vector lengths are not equal! ("
+ std::to_string(vec1.length) + "!="
+ std::to_string(vec2.length) + ")");
if (end > vec1.length)
raise_error("dot_product: Range out of bounds! ("
+ std::to_string(end) + ">" + std::to_string(vec1.length) + ")");
SparsePiece* ptr = vec2.data;
real sum = 0;
uint32_t large_ctr = vec2.nnz;
uint32_t ith_piece = 0;
if (vec2.max_idx >= start && ptr && ptr->indices[0] < end) {
while (ptr && ptr->next && ptr->next->indices[0] <= start) {
ptr = ptr->next;
++ith_piece;
}
while (ptr && ptr->next) {
uint32_t ctr = 0;
uint32_t small_ctr = SPARSE_PIECE_LENGTH;
while (ctr < small_ctr && ptr->indices[ctr] < start)
++ctr;
while (ctr < small_ctr && ptr->indices[ctr] < end) {
sum += vec1.data[ptr->indices[ctr]] * ptr->values[ctr];
++ctr;
}
if (ctr < small_ctr) return sum;
ptr = ptr->next;
++ith_piece;
}
if (ptr) {
uint32_t ctr = 0;
uint32_t small_ctr = large_ctr - ith_piece * SPARSE_PIECE_LENGTH;
while (ctr < small_ctr && ptr->indices[ctr] < start)
++ctr;
while (ctr < small_ctr && ptr->indices[ctr] < end) {
sum += vec1.data[ptr->indices[ctr]] * ptr->values[ctr];
++ctr;
}
}
}
return sum;
}
void scalar_multiply_add(Vector target, SparseVector vec, real scalar) {
scalar_multiply_add(target, vec, scalar, 0, target.length);
}
void scalar_multiply_add(Vector target, SparseVector vec, real scalar,
uint32_t start, uint32_t end) {
if (target.length != vec.length)
raise_error("scalar_multiply_add: Vector lengths do not match! ("
+ std::to_string(target.length) + "!="
+ std::to_string(vec.length) + ")");
if (end > vec.length)
raise_error("scalar_multiply_add: Range out of bounds! ("
+ std::to_string(end) + ">" + std::to_string(vec.length) + ")");
SparsePiece* ptr = vec.data;
uint32_t large_ctr = vec.nnz;
uint32_t ith_piece = 0;
if (vec.max_idx >= start && ptr && ptr->indices[0] < end) {
while (ptr && ptr->next && ptr->next->indices[0] <= start) {
ptr = ptr->next;
++ith_piece;
}
while (ptr && ptr->next) {
uint32_t ctr = 0;
uint32_t small_ctr = SPARSE_PIECE_LENGTH;
while (ctr < small_ctr && ptr->indices[ctr] < start)
++ctr;
while (ctr < small_ctr && ptr->indices[ctr] < end) {
target.data[ptr->indices[ctr]] += ptr->values[ctr] * scalar;
++ctr;
}
if (ctr < small_ctr) return;
ptr = ptr->next;
++ith_piece;
}
if (ptr) {
uint32_t ctr = 0;
uint32_t small_ctr = large_ctr - ith_piece * SPARSE_PIECE_LENGTH;
while (ctr < small_ctr && ptr->indices[ctr] < start)
++ctr;
while (ctr < small_ctr && ptr->indices[ctr] < end) {
target.data[ptr->indices[ctr]] += ptr->values[ctr] * scalar;
++ctr;
}
}
}
}
real norm_2_squared(SparseVector vec) {
SparsePiece* ptr = vec.data;
uint32_t large_ctr = vec.nnz;
uint32_t ith_piece = 0;
real sum = 0;
while (ptr) {
uint32_t ctr = 0;
uint32_t small_ctr = ((ptr->next)
? SPARSE_PIECE_LENGTH
: large_ctr - ith_piece * SPARSE_PIECE_LENGTH);
for (uint32_t ctr = 0; ctr < small_ctr; ++ctr)
sum += ptr->values[ctr] * ptr->values[ctr];
ptr = ptr->next;
++ith_piece;
}
return sum;
}
real dot_product(OneSparseVector vec1, Vector vec2) {
return dot_product(vec2, vec1, 0, vec1.length);
}
real dot_product(Vector vec1, OneSparseVector vec2) {
return dot_product(vec1, vec2, 0, vec1.length);
}
real dot_product(OneSparseVector vec1, Vector vec2,
uint32_t start, uint32_t end) {
return dot_product(vec2, vec1, start, end);
}
real dot_product(Vector vec1, OneSparseVector vec2,
uint32_t start, uint32_t end) {
if (vec1.length != vec2.length)
raise_error("dot_product: Vector lengths are not equal! ("
+ std::to_string(vec1.length) + "!="
+ std::to_string(vec2.length) + ")");
if (end > vec1.length)
raise_error("dot_product: Range out of bounds! ("
+ std::to_string(end) + ">" + std::to_string(vec1.length) + ")");
real sum = 0;
uint32_t i = 0;
while (i < vec2.nnz && vec2.indices[i] < start)
++i;
while (i < vec2.nnz && vec2.indices[i] < end) {
sum += vec2.values[i] * vec1.data[vec2.indices[i]];
++i;
}