-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithms_powermethod.c
More file actions
5267 lines (4414 loc) · 152 KB
/
algorithms_powermethod.c
File metadata and controls
5267 lines (4414 loc) · 152 KB
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
#include <algorithms.h>
#include <rand_utils.h>
#include <file_utils.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <mkl.h>
// #include <blis.h>
#include <gen_utils.h>
#include <smmintrin.h>
#include <immintrin.h>
#include <gen_data.h>
// #define SWITCHMODE
/////////////////////////////////////////
// Loops:
// 1) go over all powers in the method
// 2) go over all blocks
// 3) go over all dimensions (!) essentially tvLooped
// PROBLEM:
// Idea of the algorithm(!)
// Result are just temporary storages, the result is actually the vector(!!!)
// #define SINGLEBLOCK
// #define PRINT_AUX
// #define DEBUG_ENV
// #define NORMALIZE
// #define SINGLEBLOCK
// #define SINGLEVECTORUP
// #define PRINT_AUX
#define VECTOR_START 0
#if (TEST_ENV==1) // VERIFICATION MODE
#define NORMALIZE // MUST INCLUDE if want no errors of type 0.00000 different from 0.00000
// #define NORM_TRICK
// #define RESETS_ENABLE // Old requirement, now we revised our methods by using BETA parameter of BLAS
// #define SINGLEVECTORUP
// #define SINGLEBLOCK
#else // BENCHMARKING MODE
#define NORMALIZE // MUST INCLUDE
// #define NORM_TRICK
// #define SINGLEBLOCK
// #define SINGLEVECTORUP
#endif
inline size_t
block_inc(size_t * const counters, const size_t * const thresholds, const size_t init_offset) {
size_t offset = init_offset;
while ( offset<=init_offset && (++counters[offset] == thresholds[offset])) {
counters[offset--] = 0;
// ALT IMPLEMENTATION
//if (offset == -1) {
//break;
//}
}
return offset;
}
inline void
block_inc_fast(short * const counters, const int mode_size, const size_t dim_minus_one) {
size_t offset = dim_minus_one;
while ( offset<=dim_minus_one && (++counters[offset] == mode_size)) {
counters[offset--] = 0;
}
}
inline int ipow(int base, uint8_t exp) {
static const uint8_t highest_bit_set[] = {
0, 1, 2, 2, 3, 3, 3, 3,
4, 4, 4, 4, 4, 4, 4, 4,
5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5,
6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 255, // anything past 63 is a guaranteed overflow with base > 1
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
};
uint64_t result = 1;
switch (highest_bit_set[exp]) {
case 255: // we use 255 as an overflow marker and return 0 on overflow/underflow
if (base == 1) {
return 1;
}
if (base == -1) {
return 1 - 2 * (exp & 1);
}
return 0;
case 6:
if (exp & 1) result *= base;
exp >>= 1;
base *= base;
case 5:
if (exp & 1) result *= base;
exp >>= 1;
base *= base;
case 4:
if (exp & 1) result *= base;
exp >>= 1;
base *= base;
case 3:
if (exp & 1) result *= base;
exp >>= 1;
base *= base;
case 2:
if (exp & 1) result *= base;
exp >>= 1;
base *= base;
case 1:
if (exp & 1) result *= base;
default:
return result;
}
}
// Potential optimizations:
// reset result should be of smaller size? what size? sometimes its okay NOT to reset the result, LOL
// okay anyway changing this wont make sense (anymore); Correct (same reuslt) in both cases
void
pmModel(struct tensor_storage * restrict tensor, struct lin_storage * restrict * restrict vector_array,
struct tensor_storage * result_1, struct tensor_storage * result_2, const int iters) {
size_t resets = 0;
size_t total_memory = 0;
// printf("vectors zies are:\n");
// for(int d=0;d<tensor->dim;++d) {
// printf("sizeis%zu\n", vector_array[d]->size);
// }
#ifdef DEBUG_ENV
printf("tvModel\n");
#endif
int save_dim = tensor->dim;
int dim = tensor->dim;
struct tensor_storage * restrict input;
struct lin_storage * restrict output;
size_t mode_size = tensor->layout[0];
for (int iter=0; iter<iters; ++iter) {
for (int vector_up=VECTOR_START; vector_up<dim; ++vector_up) {
#ifdef DEBUG_ENV
printf("vector_up=%d, \n", vector_up);
#endif
// in/out set for iterations == 0(!)
int iterations = 0;
input = tensor;
output = &result_1->lin;
// printf("YES ITS ME\n");
// print_to_console(result_1->lin.data, result_1->lin.size);
// quick check with reverse mode
// it's very interesitng but if we move forward along modes its' always the same mode
size_t run_mode = 0;
// #ifdef SWITCHMODE
// for (int mode=0; mode<=dim-1; ++mode) {
// #else
for (int mode=dim-1; mode>=0; --mode) {
// #endif
// for (int mode=dim-1; mode>=0; --mode) {
if (mode == vector_up) continue;
// WOW AMAZING ONE LINER (!) to solve my problems
// #ifdef SWITCHMODE
// run_mode = mode-iterations;
// #else
run_mode = mode;
// #endif
#ifdef DEBUG_ENV
printf("compute with mode %d\n", mode);
#endif
if (iterations == dim-2) {
// printf("Output is a vector!\n");
output = vector_array[vector_up];
reset_array(vector_array[vector_up]->data, vector_array[vector_up]->size, 0.0);
}
input->dim = dim-iterations;
// printf("input %zu / vector %zu\n", input->lin.size, vector_array[0]->size);
output->size = input->lin.size / vector_array[0]->size;
#ifdef DEBUG_ENV
if (tensor->lin.size > 100) {
printf("tensor_ptr (limited to 100 out of %zu):", tensor->lin.size);
print_to_console(input->lin.data, 100);
} else {
printf("tensor_ptr (of size %zu):", tensor->lin.size);
print_to_console(input->lin.data, tensor->lin.size);
}
if (mode_size > 100) {
printf("vector (limited to 100 out of %zu):", mode_size);
print_to_console(vector_array[mode]->data, 100);
} else {
printf("vector (of size %zu):", mode_size);
print_to_console(vector_array[mode]->data, mode_size);
}
if (output->size > 100) {
printf("output(before) (limited to 100 out of %zu):", output->size);
print_to_console(output->data, 100);
} else {
printf("output(before) (of size %zu):", output->size);
print_to_console(output->data, output->size);
}
#endif
// size_t el=0;
// for (size_t k=0; k<mode_size; ++k) {
// for (size_t n22=0; n22<(tensor->lin.size/mode_size);++n22){
// printf("mulnumber %zu\n",el);
// output->data[k] += input->lin.data[el++]*vector_array[mode]->data[n22];
// }
// }
tvm_vector_major_BLAS_col_mode(input, vector_array[mode], output, run_mode);
if (vector_up == 0) total_memory += input->lin.size + input->layout[mode] + output->size;
#ifdef DEBUG_ENV
if (output->size > 100) {
printf("output(after) (limited to 100 out of %zu):", output->size);
print_to_console(output->data, 100);
} else {
printf("output(after) (of size %zu):", output->size);
print_to_console(output->data, output->size);
}
#endif
++iterations;
if (iterations % 2 == 0) {
output = &result_1->lin;
reset_array(result_1->lin.data, result_1->lin.size, 0);
#ifdef PRINT_AUX
resets += result_1->lin.size;
#endif
input = result_2;
} else {
output = &result_2->lin;
#ifdef PRINT_AUX
resets += result_2->lin.size;
#endif
reset_array(result_2->lin.data, result_2->lin.size, 0);
input = result_1;
}
}
#ifdef NORMALIZE
// printf("norm, ");
(void) normalize(vector_array[vector_up], mode_size);
#endif
#ifdef PRINT_AUX
printf("Resets (model): %zu\n", resets);
#endif
#ifdef SINGLEVECTORUP
break;
#endif
}
}
printf("total_memory touched is %zu\n", total_memory);
tensor->dim = save_dim;
result_2->dim = save_dim - 1;
result_1->dim = save_dim - 1;
}
// taco code to generate this:
void
pmTaco5(const struct tensor_storage * restrict tensor, struct lin_storage * restrict * restrict vector_array,
struct lin_storage * restrict result_1, struct lin_storage * restrict result_2, const int iters) {
const int mode_size = tensor->layout[0];
for (int32_t iA = 0; iA < mode_size; iA++) {
double tj = 0.0;
for (int32_t jA = 0; jA < mode_size; jA++) {
int32_t pA2 = iA * mode_size + jA;
double tk = 0.0;
for (int32_t kA = 0; kA < mode_size; kA++) {
int32_t pA3 = pA2 * mode_size + kA;
double tl = 0.0;
for (int32_t lA = 0; lA < mode_size; lA++) {
int32_t pA4 = pA3 * mode_size + lA;
double tm = 0.0;
for (int32_t mA = 0; mA < mode_size; mA++) {
int32_t pA5 = pA4 * mode_size + mA;
tm += tensor->lin.data[pA5] * vector_array[1]->data[jA] * vector_array[2]->data[kA] * vector_array[3]->data[lA] * vector_array[4]->data[mA];
}
tl += tm;
}
tk += tl;
}
tj += tk;
}
vector_array[0]->data[iA] = tj;
}
(void) normalize(vector_array[0], mode_size);
for (int32_t pb = 0; pb < mode_size; pb++) {
vector_array[1]->data[pb] = 0.0;
}
for (int32_t iA = 0; iA < mode_size; iA++) {
double ti = vector_array[0]->data[iA];
for (int32_t jA = 0; jA < mode_size; jA++) {
int32_t pA2 = iA * mode_size + jA;
double tk = 0.0;
for (int32_t kA = 0; kA < mode_size; kA++) {
int32_t pA3 = pA2 * mode_size + kA;
double tl = 0.0;
for (int32_t lA = 0; lA < mode_size; lA++) {
int32_t pA4 = pA3 * mode_size + lA;
double tm = 0.0;
for (int32_t mA = 0; mA < mode_size; mA++) {
int32_t pA5 = pA4 * mode_size + mA;
tm += tensor->lin.data[pA5] * ti * vector_array[2]->data[kA] * vector_array[3]->data[lA] * vector_array[4]->data[mA];
}
tl += tm;
}
tk += tl;
}
vector_array[1]->data[jA] = vector_array[1]->data[jA] + tk;
}
}
(void) normalize(vector_array[1], mode_size);
// Generated by the Tensor Algebra Compiler (tensor-compiler.org)
for (int32_t pc = 0; pc < mode_size; pc++) {
vector_array[2]->data[pc] = 0.0;
}
for (int32_t iA = 0; iA < mode_size; iA++) {
double ti = vector_array[0]->data[iA];
for (int32_t jA = 0; jA < mode_size; jA++) {
int32_t pA2 = iA * mode_size + jA;
double tj = ti;
double tj0 = vector_array[1]->data[jA];
for (int32_t kA = 0; kA < mode_size; kA++) {
int32_t pA3 = pA2 * mode_size + kA;
double tl = 0.0;
for (int32_t lA = 0; lA < mode_size; lA++) {
int32_t pA4 = pA3 * mode_size + lA;
double tm = 0.0;
for (int32_t mA = 0; mA < mode_size; mA++) {
int32_t pA5 = pA4 * mode_size + mA;
tm += tensor->lin.data[pA5] * tj * tj0 * vector_array[3]->data[lA] * vector_array[4]->data[mA];
}
tl += tm;
}
vector_array[2]->data[kA] = vector_array[2]->data[kA] + tl;
}
}
}
(void) normalize(vector_array[2], mode_size);
// Generated by the Tensor Algebra Compiler (tensor-compiler.org)
for (int32_t pd = 0; pd < mode_size; pd++) {
vector_array[3]->data[pd] = 0.0;
}
for (int32_t iA = 0; iA < mode_size; iA++) {
double ti = vector_array[0]->data[iA];
for (int32_t jA = 0; jA < mode_size; jA++) {
int32_t pA2 = iA * mode_size + jA;
double tj = ti;
double tj0 = vector_array[1]->data[jA];
for (int32_t kA = 0; kA < mode_size; kA++) {
int32_t pA3 = pA2 * mode_size + kA;
double tk = tj;
double tk0 = tj0;
double tk1 = vector_array[2]->data[kA];
for (int32_t lA = 0; lA < mode_size; lA++) {
int32_t pA4 = pA3 * mode_size + lA;
double tm = 0.0;
for (int32_t mA = 0; mA < mode_size; mA++) {
int32_t pA5 = pA4 * mode_size + mA;
tm += tensor->lin.data[pA5] * tk * tk0 * tk1 * vector_array[4]->data[mA];
}
vector_array[3]->data[lA] = vector_array[3]->data[lA] + tm;
}
}
}
}
(void) normalize(vector_array[3], mode_size);
// Generated by the Tensor Algebra Compiler (tensor-compiler.org)
for (int32_t pe = 0; pe < mode_size; pe++) {
vector_array[4]->data[pe] = 0.0;
}
for (int32_t iA = 0; iA < mode_size; iA++) {
double ti = vector_array[0]->data[iA];
for (int32_t jA = 0; jA < mode_size; jA++) {
int32_t pA2 = iA * mode_size + jA;
double tj = ti;
double tj0 = vector_array[1]->data[jA];
for (int32_t kA = 0; kA < mode_size; kA++) {
int32_t pA3 = pA2 * mode_size + kA;
double tk = tj;
double tk0 = tj0;
double tk1 = vector_array[2]->data[kA];
for (int32_t lA = 0; lA < mode_size; lA++) {
int32_t pA4 = pA3 * mode_size + lA;
double tl = tk;
double tl0 = tk0;
double tl1 = tk1;
double tl2 = vector_array[3]->data[lA];
for (int32_t mA = 0; mA < mode_size; mA++) {
int32_t pA5 = pA4 * mode_size + mA;
vector_array[4]->data[mA] = vector_array[4]->data[mA] + tensor->lin.data[pA5] * tl * tl0 * tl1 * tl2;
}
}
}
}
}
(void) normalize(vector_array[4], mode_size);
}
void
pmTaco10(const struct tensor_storage * restrict tensor, struct lin_storage * restrict * restrict vector_array,
struct lin_storage * restrict result_1, struct lin_storage * restrict result_2, const int iters) {
const int mode_size = tensor->layout[0]; // FIXED (WRONG)
size_t vector_ids [10] = {};
int counter = 0;
for (int i=0; i<10; ++i) {
if (counter == VECTOR_START) {
vector_ids[0] = counter;
} else {
vector_ids[i] = counter;
}
++counter;
}
print_to_console_sizet(vector_ids, 10);
// Generated by the Tensor Algebra Compiler (tensor-compiler.org)
for (int iA = 0; iA < mode_size; iA++) {
double tj = 0.0;
for (int jA = 0; jA < mode_size; jA++) {
int pA2 = iA * mode_size + jA;
double tk = 0.0;
for (int kA = 0; kA < mode_size; kA++) {
int pA3 = pA2 * mode_size + kA;
double tl = 0.0;
for (int lA = 0; lA < mode_size; lA++) {
int pA4 = pA3 * mode_size + lA;
double tm = 0.0;
for (int mA = 0; mA < mode_size; mA++) {
int pA5 = pA4 * mode_size + mA;
double tn = 0.0;
for (int nA = 0; nA < mode_size; nA++) {
int pA6 = pA5 * mode_size + nA;
double to = 0.0;
for (int oA = 0; oA < mode_size; oA++) {
int pA7 = pA6 * mode_size + oA;
double tp = 0.0;
for (int pA = 0; pA < mode_size; pA++) {
int pA8 = pA7 * mode_size + pA;
double tr = 0.0;
for (int rA = 0; rA < mode_size; rA++) {
int pA9 = pA8 * mode_size + rA;
double ts = 0.0;
for (int sA = 0; sA < mode_size; sA++) {
int pA10 = pA9 * mode_size + sA;
ts += tensor->lin.data[pA10] * vector_array[vector_ids[1]]->data[jA] * vector_array[vector_ids[2]]->data[kA] * vector_array[vector_ids[3]]->data[lA] * vector_array[vector_ids[4]]->data[mA] * vector_array[vector_ids[5]]->data[nA] *
vector_array[vector_ids[6]]->data[oA] * vector_array[vector_ids[7]]->data[pA] * vector_array[vector_ids[8]]->data[rA] * vector_array[vector_ids[9]]->data[sA];
}
tr += ts;
}
tp += tr;
}
to += tp;
}
tn += to;
}
tm += tn;
}
tl += tm;
}
tk += tl;
}
tj += tk;
}
vector_array[0]->data[iA] = tj;
}
(void) normalize(vector_array[0], mode_size);
}
void
pmLooped(const struct tensor_storage * restrict tensor, struct lin_storage * restrict * restrict vector_array,
struct lin_storage * restrict result_1, struct lin_storage * restrict result_2, const int iters) {
// size_t total_memory = 0;
#ifdef DEBUG_ENV
printf("tvLooped - result sizes are %zu and %zu\n", result_1->size, result_2->size);
#endif
size_t resets = 0;
int dim = tensor->dim;
size_t right_size[dim];
size_t left_size[dim];
left_size[0] = 1;
right_size[dim-1] = 1;
for (int d=1; d<dim; ++d) {
left_size[d] = left_size[d-1] * tensor->layout[d];
// printf("we have incremented left size d(%d) to %zu be from elft size d-1(%d)= %zu\n", d,left_size[d], d-1, left_size[d-1]);
}
for (int d=dim-2; d>=0; --d) {
right_size[d] = right_size[d+1] * tensor->layout[d];
// printf("we have incremented right size d(%d) to %zu be from right size d+1(%d)= %zu\n", d,right_size[d], d+1, right_size[d+1]);
}
const MKL_INT mode_size = tensor->layout[0]; // FIXED (WRONG)
const MKL_INT incx = 1;
const MKL_INT incy = 1;
const MKL_INT n2 = tensor->lin.size / mode_size;
double alpha = 1;
// double sum = 0;
double beta = 0; // we always write from scratch (!)
for (int j=0; j<iters; ++j) {
#ifdef DEBUG_ENV
printf("\niteration %d\n", j);
#endif
// vector up is the one being produced
for (int vector_up=VECTOR_START; vector_up<dim; ++vector_up) {
#ifdef DEBUG_ENV
printf("vector_up=%d, ", vector_up);
#endif
size_t divisor = 1;
// in/out set for iterations == 0(!)
int iterations = 0;
const double * restrict input = tensor->lin.data;
double * restrict output = result_1->data;
const double * restrict vector;
#ifdef RESETS_ENABLE
reset_array(output, n2, 0.0);
#endif
#ifdef PRINT_AUX
resets += n2;
#endif
// reset_array(result_2->data, n2, 0.0);
// printf("at this point result2 is cleared;");
size_t n3 = n2;
// double beta = 1;
int temp_dim = dim;
for (int mode=dim-1; mode>=0; --mode) {
if (mode == vector_up) continue;
#ifdef DEBUG_ENV
printf("compute with mode %d\n", mode);
#endif
vector = vector_array[mode]->data;
if (iterations == dim-2) {
output = vector_array[vector_up]->data;
// beta = 0;
// reset_array(vector_array[vector_up]->data, vector_array[vector_up]->size, 0.0);
// printf("Output is now a vector pointer vector up = %d\n", vector_up);
}
// printf("iteration ==================== %zu\n", iterations);
#ifdef DEBUG_ENV
if (tensor->lin.size > 100) {
printf("tensor_ptr (limited to 100 out of %zu):", tensor->lin.size);
print_to_console(input, 100);
} else {
printf("tensor_ptr (of size %zu):", tensor->lin.size);
print_to_console(input, tensor->lin.size);
}
if (mode_size > 100) {
printf("vector (limited to 100 out of %zu):", mode_size);
print_to_console(vector, 100);
} else {
printf("vector (of size %zu):", mode_size);
print_to_console(vector, mode_size);
}
if (n3 > 100) {
printf("output (limited to 100 out of %zu):", n3);
print_to_console(output, 100);
} else {
printf("output (of size %zu):", n3);
print_to_console(output, n3);
}
#endif
size_t fixed_right_size = right_size[mode] / divisor;
if (mode != --temp_dim) {
#ifdef DEBUG_ENV
printf("left hand side: %zu, right_size: %zu (times mode size %zu)\n", left_size[mode], fixed_right_size, mode_size);
#endif
for (size_t i=0; i<left_size[mode]; ++i) {
const double * restrict next = input + i*mode_size*fixed_right_size;
double * restrict next_result = output + i*fixed_right_size;
cblas_dgemv(
CblasColMajor,
CblasNoTrans,
fixed_right_size, mode_size,
alpha,
next, fixed_right_size,
vector, incx,
beta,
next_result, incy);
}
// if (vector_up == 0) total_memory += ((left_size[mode]*mode_size*fixed_right_size) + mode_size + (fixed_right_size*left_size[mode]));
} else {
// HERE? SHOULD WE UPDATE n2????
#ifdef DEBUG_ENV
printf("right hand side: n3=%d, mode_size=%d\n", n3, mode_size);
#endif
cblas_dgemv(
CblasRowMajor,
CblasNoTrans,
n3, mode_size,
alpha,
input, mode_size,
vector, incx,
beta,
output, incy);
// if (vector_up == 0) total_memory += ((mode_size*n3) + mode_size + (n3));
}
#ifdef DEBUG_ENV
if (n3 > 100) {
printf("output resultant (limited to 100 out of %zu):", n3);
print_to_console(output, 100);
} else {
printf("output resultant (of size %zu):", n3);
print_to_console(output, n3);
}
#endif
// Super important finding: we have to clear all of it (its its the last step(!)
// In all other steps: just clear n3 or less
if (++iterations == dim-1) {
break;
}
// We have one more iteration to go - clear the UPCOMING buffer (!)
if (iterations % 2 == 0) {
output = result_1->data;
// printf("at this point result1 is cleared\n");
// reset_array(result_1->data, n3, 0.0);
input = result_2->data;
} else {
output = result_2->data;
// reset_array(result_2->data, n3, 0.0);
// printf("at this point result2 is cleared\n");
input = result_1->data;
}
divisor *= mode_size;
n3 = n2 / divisor;
#ifdef RESETS_ENABLE
reset_array(output, n3, 0.0);
#endif
#ifdef PRINT_AUX
resets += n3;
#endif
}
// double temp_sum = 0;
#ifdef NORMALIZE
(void) normalize(vector_array[vector_up], mode_size);
#endif
// printf("temp_sum =%f\n", temp_sum);
// printf("temp_sum is %f, sum is %f\n", temp_sum, sum);
// sum += temp_sum;
// printf("after addition, sum is %f\n", sum);
#ifdef SINGLEVECTORUP
break;
#endif
}
// printf("sum is %f\n", sum);
}
#ifdef PRINT_AUX
printf("Resets: %zu\n", resets);
#endif
// printf("total_memory touched is %zu\n", total_memory);
}
void
pmLoopedSingle(const struct tensor_storage * restrict tensor, struct lin_storage * restrict * restrict vector_array,
struct lin_storage * restrict result_1, struct lin_storage * restrict result_2, const int iters) {
#ifdef DEBUG_ENV
printf("tvLooped - result sizes are %zu and %zu\n", result_1->size, result_2->size);
#endif
size_t resets = 0;
int dim = tensor->dim;
size_t right_size[dim];
size_t left_size[dim];
left_size[0] = 1;
right_size[dim-1] = 1;
for (int d=1; d<dim; ++d) {
left_size[d] = left_size[d-1] * tensor->layout[d];
// printf("we have incremented left size d(%d) to %zu be from elft size d-1(%d)= %zu\n", d,left_size[d], d-1, left_size[d-1]);
}
for (int d=dim-2; d>=0; --d) {
right_size[d] = right_size[d+1] * tensor->layout[d];
// printf("we have incremented right size d(%d) to %zu be from right size d+1(%d)= %zu\n", d,right_size[d], d+1, right_size[d+1]);
}
const MKL_INT mode_size = tensor->layout[0]; // FIXED (WRONG)
const MKL_INT incx = 1;
const MKL_INT incy = 1;
const MKL_INT n2 = tensor->lin.size / mode_size;
double alpha = 1;
double beta = 0; // we always write from scratch (!)
for (int j=0; j<iters; ++j) {
#ifdef DEBUG_ENV
printf("\niteration %d\n", j);
#endif
for (int vector_up=VECTOR_START; vector_up<dim; ++vector_up) {
#ifdef DEBUG_ENV
printf("vector_up=%d, ", vector_up);
#endif
size_t divisor = 1;
int iterations = 0;
const double * restrict input = tensor->lin.data;
double * restrict output = result_1->data;
const double * restrict vector;
#ifdef RESETS_ENABLE
reset_array(output, n2, 0.0);
#endif
#ifdef PRINT_AUX
resets += n2;
#endif
int right_mode = dim-1;
for (int iterations = 0; iterations < dim-1; ++iterations) {
if (iterations == dim-2) {
output = vector_array[vector_up]->data;
}
if (iterations < vector_up) {
vector = vector_array[iterations]->data;
#ifdef DEBUG_ENV
printf("mode multiplied with is %d, ", iterations);
printf("left hand side multiplication:, right size is the follwoing %zu\n", right_size[iterations]);
printf("tensor_ptr: ");
print_to_console(input, mode_size);
// printf("Mul this block with vector[%d]\n", mode);
printf("vector: ");
print_to_console(vector, mode_size);
printf("output (JUST BEFORE): ");
print_to_console(output, mode_size);
#endif
// left_kernel[iterations](input, vector, output, NULL, NULL, NULL);
cblas_dgemv(
CblasColMajor,
CblasNoTrans,
right_size[iterations], mode_size,
alpha,
input, right_size[iterations],
vector, incx,
beta,
output, incy);
} else {
vector = vector_array[right_mode--]->data;
#ifdef DEBUG_ENV
printf("mode multiplied with is %d, ", right_mode+1);
printf("right hand side multiplication:, right size is the follwoing %zu\n", right_size[iterations]);
printf("tensor_ptr: ");
print_to_console(input, mode_size);
// printf("Mul this block with vector[%d]\n", mode);
printf("vector: ");
print_to_console(vector, mode_size);
printf("output (JUST BEFORE): ");
print_to_console(output, mode_size);
#endif
// right_kernel[iterations](vector, input, output, NULL, NULL, NULL);
cblas_dgemv(
CblasRowMajor,
CblasNoTrans,
right_size[iterations], mode_size,
alpha,
input, mode_size,
vector, incx,
beta,
output, incy);
}
#ifdef DEBUG_ENV
printf("output (4 el): ");
print_to_console(output, 4);
#endif
if (iterations % 2 == 0) {
output = result_2->data;
input = result_1->data;
} else {
output = result_1->data;
input = result_2->data;
}
#ifdef PRINT_AUX
executions += (right_size[iterations]*mode_size);
resets += right_size[iterations];
#endif
// sum += output[el];
#ifdef RESETS_ENABLE
reset_array(output, right_size[iterations], 0.0);
#endif
}
#ifdef NORMALIZE
(void) normalize(vector_array[vector_up], mode_size);
#endif
#ifdef SINGLEVECTORUP
break;
#endif
}
}
#ifdef PRINT_AUX
printf("Resets: %zu\n", resets);
#endif
}
void
pmLoopedSingleMvs(const struct tensor_storage * restrict tensor, struct lin_storage * restrict * restrict vector_array,
struct lin_storage * restrict result_1, struct lin_storage * restrict result_2, const int iters) {
#ifdef DEBUG_ENV
printf("tvLooped - result sizes are %zu and %zu\n", result_1->size, result_2->size);
#endif
size_t resets = 0;
int dim = tensor->dim;
size_t right_size[dim];
size_t left_size[dim];
left_size[0] = 1;
right_size[dim-1] = 1;
for (int d=1; d<dim; ++d) {
left_size[d] = left_size[d-1] * tensor->layout[d];
// printf("we have incremented left size d(%d) to %zu be from elft size d-1(%d)= %zu\n", d,left_size[d], d-1, left_size[d-1]);
}
for (int d=dim-2; d>=0; --d) {
right_size[d] = right_size[d+1] * tensor->layout[d];
// printf("we have incremented right size d(%d) to %zu be from right size d+1(%d)= %zu\n", d,right_size[d], d+1, right_size[d+1]);
}
const MKL_INT mode_size = tensor->layout[0]; // FIXED (WRONG)
const MKL_INT incx = 1;
const MKL_INT incy = 1;
const MKL_INT n2 = tensor->lin.size / mode_size;
double alpha = 1;
double beta = 0; // we always write from scratch (!)
for (int j=0; j<iters; ++j) {
#ifdef DEBUG_ENV
printf("\niteration %d\n", j);
#endif
for (int vector_up=VECTOR_START; vector_up<dim; ++vector_up) {
#ifdef DEBUG_ENV
printf("vector_up=%d, ", vector_up);
#endif
size_t divisor = 1;
int iterations = 0;
const double * restrict input = tensor->lin.data;
double * restrict output = result_1->data;
const double * restrict vector;
#ifdef RESETS_ENABLE
reset_array(output, n2, 0.0);
#endif
#ifdef PRINT_AUX
resets += n2;
#endif
int right_mode = dim-1; // literally a mode to countdown from
int left_mode = 0;
for (int iterations = 0; iterations < dim-1; ++iterations) {
if (iterations == dim-2) {
output = vector_array[vector_up]->data;
}
if (right_mode > vector_up) {
vector = vector_array[right_mode--]->data;
#ifdef DEBUG_ENV
printf("mode multiplied with is %d, ", right_mode+1);
printf("right hand side multiplication:, right size is the follwoing %zu\n", right_size[iterations]);
printf("tensor_ptr: ");
print_to_console(input, mode_size);
// printf("Mul this block with vector[%d]\n", mode);
printf("vector: ");
print_to_console(vector, mode_size);
printf("output (JUST BEFORE): ");
print_to_console(output, mode_size);
#endif
// right_kernel[iterations](vector, input, output, NULL, NULL, NULL);
cblas_dgemv(
CblasRowMajor,
CblasNoTrans,
right_size[iterations], mode_size,
alpha,
input, mode_size,
vector, incx,
beta,
output, incy);
} else {
vector = vector_array[left_mode++]->data;
#ifdef DEBUG_ENV
printf("mode multiplied with is %d, ", iterations);
printf("left hand side multiplication:, right size is the follwoing %zu\n", right_size[iterations]);
printf("tensor_ptr: ");
print_to_console(input, mode_size);
// printf("Mul this block with vector[%d]\n", mode);
printf("vector: ");
print_to_console(vector, mode_size);
printf("output (JUST BEFORE): ");
print_to_console(output, mode_size);
#endif
// left_kernel[iterations](input, vector, output, NULL, NULL, NULL);
cblas_dgemv(
CblasColMajor,
CblasNoTrans,
right_size[iterations], mode_size,
alpha,
input, right_size[iterations],
vector, incx,
beta,
output, incy);
}
#ifdef DEBUG_ENV
printf("output (4 el): ");
print_to_console(output, 4);
#endif
if (iterations % 2 == 0) {
output = result_2->data;
input = result_1->data;
} else {
output = result_1->data;
input = result_2->data;