-
Notifications
You must be signed in to change notification settings - Fork 29
/
mttkrp.c
1931 lines (1593 loc) · 57.4 KB
/
mttkrp.c
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
/******************************************************************************
* INCLUDES
*****************************************************************************/
#include "base.h"
#include "mttkrp.h"
#include "thd_info.h"
#include "tile.h"
#include "util.h"
#include "mutex_pool.h"
/* XXX: this is a memory leak until cpd_ws is added/freed. */
static mutex_pool * pool = NULL;
/**
* @brief Function pointer that performs MTTKRP on a tile of a CSF tree.
*
* @param ct The CSF tensor.
* @param tile_id The tile to process.
* @param mats The matrices.
* @param mode The output mode.
* @param thds Thread structures.
* @param partition A partitioning of the slices in the tensor, to distribute
* to threads. Use the thread ID to decide which slices to
* process. This may be NULL, in that case simply process all
* slices.
*/
typedef void (* csf_mttkrp_func)(
splatt_csf const * const ct,
idx_t const tile_id,
matrix_t ** mats,
idx_t const mode,
thd_info * const thds,
idx_t const * const partition);
/******************************************************************************
* PRIVATE FUNCTIONS
*****************************************************************************/
/**
* @brief Perform a reduction on thread-local MTTKRP outputs.
*
* @param ws MTTKRP workspace containing thread-local outputs.
* @param global_output The global MTTKRP output we are reducing into.
* @param nrows The number of rows in the MTTKRP.
* @param ncols The number of columns in the MTTKRP.
*/
static void p_reduce_privatized(
splatt_mttkrp_ws * const ws,
val_t * const restrict global_output,
idx_t const nrows,
idx_t const ncols)
{
/* Ensure everyone has completed their local MTTKRP. */
#pragma omp barrier
sp_timer_t reduction_timer;
timer_fstart(&reduction_timer);
int const tid = splatt_omp_get_thread_num();
idx_t const num_threads = splatt_omp_get_num_threads();
idx_t const elem_per_thread = (nrows * ncols) / num_threads;
idx_t const start = tid * elem_per_thread;
idx_t const stop = ((idx_t)tid == num_threads-1) ?
(nrows * ncols) : (tid + 1) * elem_per_thread;
/* reduction */
for(idx_t t=0; t < num_threads; ++t){
val_t const * const restrict thread_buf = ws->privatize_buffer[t];
for(idx_t x=start; x < stop; ++x) {
global_output[x] += thread_buf[x];
}
}
timer_stop(&reduction_timer);
#pragma omp master
ws->reduction_time = reduction_timer.seconds;
}
/**
* @brief Map MTTKRP functions onto a (possibly tiled) CSF tensor. This function
* will handle any scheduling required with a partially tiled tensor.
*
* @param tensors An array of CSF representations. tensors[csf_id] is processed.
* @param csf_id Which tensor are we processing?
* @param atomic_func An MTTKRP function which atomically updates the output.
* @param nosync_func An MTTKRP function which does not atomically update.
* @param mats The matrices, with the output stored in mats[MAX_NMODES].
* @param mode Which mode of 'tensors' is the output (not CSF depth).
* @param thds Thread structures.
* @param ws MTTKRP workspace.
*/
static void p_schedule_tiles(
splatt_csf const * const tensors,
idx_t const csf_id,
csf_mttkrp_func atomic_func,
csf_mttkrp_func nosync_func,
matrix_t ** mats,
idx_t const mode,
thd_info * const thds,
splatt_mttkrp_ws * const ws)
{
splatt_csf const * const csf = &(tensors[csf_id]);
idx_t const nmodes = csf->nmodes;
idx_t const depth = nmodes - 1;
idx_t const nrows = mats[mode]->I;
idx_t const ncols = mats[mode]->J;
/* Store old pointer */
val_t * const restrict global_output = mats[MAX_NMODES]->vals;
#pragma omp parallel
{
int const tid = splatt_omp_get_thread_num();
timer_start(&thds[tid].ttime);
idx_t const * const tile_partition = ws->tile_partition[csf_id];
idx_t const * const tree_partition = ws->tree_partition[csf_id];
/*
* We may need to edit mats[MAX_NMODES]->vals, so create a private copy of
* the pointers to edit. (NOT actual factors).
*/
matrix_t * mats_priv[MAX_NMODES+1];
for(idx_t m=0; m < MAX_NMODES; ++m) {
mats_priv[m] = mats[m];
}
/* each thread gets separate structure, but do a shallow copy */
mats_priv[MAX_NMODES] = splatt_malloc(sizeof(**mats_priv));
*(mats_priv[MAX_NMODES]) = *(mats[MAX_NMODES]);
/* Give each thread its own private buffer and overwrite atomic
* function. */
if(ws->is_privatized[mode]) {
/* change (thread-private!) output structure */
memset(ws->privatize_buffer[tid], 0,
nrows * ncols * sizeof(**(ws->privatize_buffer)));
mats_priv[MAX_NMODES]->vals = ws->privatize_buffer[tid];
/* Don't use atomics if we privatized. */
atomic_func = nosync_func;
}
/*
* Distribute tiles to threads in some fashion.
*/
if(csf->ntiles > 1) {
/* We parallelize across tiles, and thus should not distribute within a
* tree. This may change if we instead 'split' tiles across a few
* threads. */
assert(tree_partition == NULL);
/* mode is actually tiled -- avoid synchronization */
if(csf->tile_dims[mode] > 1) {
idx_t tile_id = 0;
/* foreach layer of tiles */
#pragma omp for schedule(dynamic, 1) nowait
for(idx_t t=0; t < csf->tile_dims[mode]; ++t) {
tile_id =
get_next_tileid(TILE_BEGIN, csf->tile_dims, nmodes, mode, t);
while(tile_id != TILE_END) {
nosync_func(csf, tile_id, mats_priv, mode, thds, tree_partition);
tile_id =
get_next_tileid(tile_id, csf->tile_dims, nmodes, mode, t);
}
}
/* tiled, but not this mode. Atomics are still necessary. */
} else {
for(idx_t tile_id = tile_partition[tid];
tile_id < tile_partition[tid+1]; ++tile_id) {
atomic_func(csf, tile_id, mats_priv, mode, thds, tree_partition);
}
}
/*
* Untiled, parallelize within kernel.
*/
} else {
assert(tree_partition != NULL);
atomic_func(csf, 0, mats_priv, mode, thds, tree_partition);
}
timer_stop(&thds[tid].ttime);
/* If we used privatization, perform a reduction. */
if(ws->is_privatized[mode]) {
p_reduce_privatized(ws, global_output, nrows, ncols);
}
splatt_free(mats_priv[MAX_NMODES]);
} /* end omp parallel */
/* restore pointer */
mats[MAX_NMODES]->vals = global_output;
}
/**
* @brief Should a certain mode should be privatized to avoid locks?
*
* @param csf The tensor (just used for dimensions).
* @param mode The mode we are processing.
* @param opts Options, storing the # threads and the threshold.
*
* @return true, if we should privatize.
*/
static bool p_is_privatized(
splatt_csf const * const csf,
idx_t const mode,
double const * const opts)
{
idx_t const length = csf->dims[mode];
idx_t const nthreads = (idx_t) opts[SPLATT_OPTION_NTHREADS];
double const thresh = opts[SPLATT_OPTION_PRIVTHRESH];
/* don't bother if it is not multithreaded. */
if(nthreads == 1) {
return false;
}
return (double)(length * nthreads) <= (thresh * (double)csf->nnz);
}
static inline void p_add_hada_clear(
val_t * const restrict out,
val_t * const restrict a,
val_t const * const restrict b,
idx_t const nfactors)
{
for(idx_t f=0; f < nfactors; ++f) {
out[f] += a[f] * b[f];
a[f] = 0;
}
}
static inline void p_assign_hada(
val_t * const restrict out,
val_t const * const restrict a,
val_t const * const restrict b,
idx_t const nfactors)
{
for(idx_t f=0; f < nfactors; ++f) {
out[f] = a[f] * b[f];
}
}
static inline void p_csf_process_fiber_locked(
val_t * const leafmat,
val_t const * const restrict accumbuf,
idx_t const nfactors,
idx_t const start,
idx_t const end,
idx_t const * const restrict inds,
val_t const * const restrict vals)
{
for(idx_t jj=start; jj < end; ++jj) {
val_t * const restrict leafrow = leafmat + (inds[jj] * nfactors);
val_t const v = vals[jj];
mutex_set_lock(pool, inds[jj]);
for(idx_t f=0; f < nfactors; ++f) {
leafrow[f] += v * accumbuf[f];
}
mutex_unset_lock(pool, inds[jj]);
}
}
static inline void p_csf_process_fiber_nolock(
val_t * const leafmat,
val_t const * const restrict accumbuf,
idx_t const nfactors,
idx_t const start,
idx_t const end,
idx_t const * const restrict inds,
val_t const * const restrict vals)
{
for(idx_t jj=start; jj < end; ++jj) {
val_t * const restrict leafrow = leafmat + (inds[jj] * nfactors);
val_t const v = vals[jj];
for(idx_t f=0; f < nfactors; ++f) {
leafrow[f] += v * accumbuf[f];
}
}
}
static inline void p_csf_process_fiber(
val_t * const restrict accumbuf,
idx_t const nfactors,
val_t const * const leafmat,
idx_t const start,
idx_t const end,
idx_t const * const inds,
val_t const * const vals)
{
/* foreach nnz in fiber */
for(idx_t j=start; j < end; ++j) {
val_t const v = vals[j] ;
val_t const * const restrict row = leafmat + (nfactors * inds[j]);
for(idx_t f=0; f < nfactors; ++f) {
accumbuf[f] += v * row[f];
}
}
}
static inline void p_propagate_up(
val_t * const out,
val_t * const * const buf,
idx_t * const restrict idxstack,
idx_t const init_depth,
idx_t const init_idx,
idx_t const * const * const fp,
idx_t const * const * const fids,
val_t const * const restrict vals,
val_t ** mvals,
idx_t const nmodes,
idx_t const nfactors)
{
/* push initial idx initialize idxstack */
idxstack[init_depth] = init_idx;
for(idx_t m=init_depth+1; m < nmodes; ++m) {
idxstack[m] = fp[m-1][idxstack[m-1]];
}
assert(init_depth < nmodes-1);
/* clear out accumulation buffer */
for(idx_t f=0; f < nfactors; ++f) {
buf[init_depth+1][f] = 0;
}
while(idxstack[init_depth+1] < fp[init_depth][init_idx+1]) {
/* skip to last internal mode */
idx_t depth = nmodes - 2;
/* process all nonzeros [start, end) into buf[depth]*/
idx_t const start = fp[depth][idxstack[depth]];
idx_t const end = fp[depth][idxstack[depth]+1];
p_csf_process_fiber(buf[depth+1], nfactors, mvals[depth+1],
start, end, fids[depth+1], vals);
idxstack[depth+1] = end;
/* exit early if there is no propagation to do... */
if(init_depth == nmodes-2) {
for(idx_t f=0; f < nfactors; ++f) {
out[f] = buf[depth+1][f];
}
return;
}
/* Propagate up until we reach a node with more children to process */
do {
/* propagate result up and clear buffer for next sibling */
val_t const * const restrict fibrow
= mvals[depth] + (fids[depth][idxstack[depth]] * nfactors);
p_add_hada_clear(buf[depth], buf[depth+1], fibrow, nfactors);
++idxstack[depth];
--depth;
} while(depth > init_depth &&
idxstack[depth+1] == fp[depth][idxstack[depth]+1]);
} /* end DFS */
/* copy to out */
for(idx_t f=0; f < nfactors; ++f) {
out[f] = buf[init_depth+1][f];
}
}
static void p_csf_mttkrp_root3_nolock(
splatt_csf const * const ct,
idx_t const tile_id,
matrix_t ** mats,
idx_t const mode,
thd_info * const thds,
idx_t const * const restrict partition)
{
assert(ct->nmodes == 3);
val_t const * const vals = ct->pt[tile_id].vals;
idx_t const * const restrict sptr = ct->pt[tile_id].fptr[0];
idx_t const * const restrict fptr = ct->pt[tile_id].fptr[1];
idx_t const * const restrict sids = ct->pt[tile_id].fids[0];
idx_t const * const restrict fids = ct->pt[tile_id].fids[1];
idx_t const * const restrict inds = ct->pt[tile_id].fids[2];
val_t const * const avals = mats[csf_depth_to_mode(ct, 1)]->vals;
val_t const * const bvals = mats[csf_depth_to_mode(ct, 2)]->vals;
val_t * const ovals = mats[MAX_NMODES]->vals;
idx_t const nfactors = mats[MAX_NMODES]->J;
int const tid = splatt_omp_get_thread_num();
val_t * const restrict accumF = (val_t *) thds[tid].scratch[0];
/* write to output */
val_t * const restrict writeF = (val_t *) thds[tid].scratch[2];
for(idx_t r=0; r < nfactors; ++r) {
writeF[r] = 0.;
}
/* break up loop by partition */
idx_t const nslices = ct->pt[tile_id].nfibs[0];
idx_t const start = (partition != NULL) ? partition[tid] : 0;
idx_t const stop = (partition != NULL) ? partition[tid+1] : nslices;
for(idx_t s=start; s < stop; ++s) {
idx_t const fid = (sids == NULL) ? s : sids[s];
val_t * const restrict mv = ovals + (fid * nfactors);
/* foreach fiber in slice */
for(idx_t f=sptr[s]; f < sptr[s+1]; ++f) {
/* first entry of the fiber is used to initialize accumF */
idx_t const jjfirst = fptr[f];
val_t const vfirst = vals[jjfirst];
val_t const * const restrict bv = bvals + (inds[jjfirst] * nfactors);
for(idx_t r=0; r < nfactors; ++r) {
accumF[r] = vfirst * bv[r];
}
/* foreach nnz in fiber */
for(idx_t jj=fptr[f]+1; jj < fptr[f+1]; ++jj) {
val_t const v = vals[jj];
val_t const * const restrict bv = bvals + (inds[jj] * nfactors);
for(idx_t r=0; r < nfactors; ++r) {
accumF[r] += v * bv[r];
}
}
/* scale inner products by row of A and update to M */
val_t const * const restrict av = avals + (fids[f] * nfactors);
for(idx_t r=0; r < nfactors; ++r) {
writeF[r] += accumF[r] * av[r];
}
} /* foreach fiber */
/* flush to output */
for(idx_t r=0; r < nfactors; ++r) {
mv[r] += writeF[r];
writeF[r] = 0.;
}
} /* foreach slice (tree) */
}
static void p_csf_mttkrp_root3_locked(
splatt_csf const * const ct,
idx_t const tile_id,
matrix_t ** mats,
idx_t const mode,
thd_info * const thds,
idx_t const * const restrict partition)
{
assert(ct->nmodes == 3);
val_t const * const vals = ct->pt[tile_id].vals;
idx_t const * const restrict sptr = ct->pt[tile_id].fptr[0];
idx_t const * const restrict fptr = ct->pt[tile_id].fptr[1];
idx_t const * const restrict sids = ct->pt[tile_id].fids[0];
idx_t const * const restrict fids = ct->pt[tile_id].fids[1];
idx_t const * const restrict inds = ct->pt[tile_id].fids[2];
val_t const * const avals = mats[csf_depth_to_mode(ct, 1)]->vals;
val_t const * const bvals = mats[csf_depth_to_mode(ct, 2)]->vals;
val_t * const ovals = mats[MAX_NMODES]->vals;
idx_t const nfactors = mats[MAX_NMODES]->J;
int const tid = splatt_omp_get_thread_num();
val_t * const restrict accumF = (val_t *) thds[tid].scratch[0];
/* write to output */
val_t * const restrict writeF = (val_t *) thds[tid].scratch[2];
for(idx_t r=0; r < nfactors; ++r) {
writeF[r] = 0.;
}
idx_t const nslices = ct->pt[tile_id].nfibs[0];
idx_t const start = (partition != NULL) ? partition[tid] : 0;
idx_t const stop = (partition != NULL) ? partition[tid+1] : nslices;
for(idx_t s=start; s < stop; ++s) {
/* foreach fiber in slice */
for(idx_t f=sptr[s]; f < sptr[s+1]; ++f) {
/* first entry of the fiber is used to initialize accumF */
idx_t const jjfirst = fptr[f];
val_t const vfirst = vals[jjfirst];
val_t const * const restrict bv = bvals + (inds[jjfirst] * nfactors);
for(idx_t r=0; r < nfactors; ++r) {
accumF[r] = vfirst * bv[r];
}
/* foreach nnz in fiber */
for(idx_t jj=fptr[f]+1; jj < fptr[f+1]; ++jj) {
val_t const v = vals[jj];
val_t const * const restrict bv = bvals + (inds[jj] * nfactors);
for(idx_t r=0; r < nfactors; ++r) {
accumF[r] += v * bv[r];
}
}
/* scale inner products by row of A and update to M */
val_t const * const restrict av = avals + (fids[f] * nfactors);
for(idx_t r=0; r < nfactors; ++r) {
writeF[r] += accumF[r] * av[r];
}
}
idx_t const fid = (sids == NULL) ? s : sids[s];
val_t * const restrict mv = ovals + (fid * nfactors);
/* flush to output */
mutex_set_lock(pool, fid);
for(idx_t r=0; r < nfactors; ++r) {
mv[r] += writeF[r];
writeF[r] = 0.;
}
mutex_unset_lock(pool, fid);
}
}
static void p_csf_mttkrp_intl3_locked(
splatt_csf const * const ct,
idx_t const tile_id,
matrix_t ** mats,
idx_t const mode,
thd_info * const thds,
idx_t const * const restrict partition)
{
assert(ct->nmodes == 3);
val_t const * const vals = ct->pt[tile_id].vals;
idx_t const * const restrict sptr = ct->pt[tile_id].fptr[0];
idx_t const * const restrict fptr = ct->pt[tile_id].fptr[1];
idx_t const * const restrict sids = ct->pt[tile_id].fids[0];
idx_t const * const restrict fids = ct->pt[tile_id].fids[1];
idx_t const * const restrict inds = ct->pt[tile_id].fids[2];
val_t const * const avals = mats[csf_depth_to_mode(ct, 0)]->vals;
val_t const * const bvals = mats[csf_depth_to_mode(ct, 2)]->vals;
val_t * const ovals = mats[MAX_NMODES]->vals;
idx_t const nfactors = mats[MAX_NMODES]->J;
int const tid = splatt_omp_get_thread_num();
val_t * const restrict accumF = (val_t *) thds[tid].scratch[0];
idx_t const nslices = ct->pt[tile_id].nfibs[0];
idx_t const start = (partition != NULL) ? partition[tid] : 0;
idx_t const stop = (partition != NULL) ? partition[tid+1] : nslices;
for(idx_t s=start; s < stop; ++s) {
idx_t const fid = (sids == NULL) ? s : sids[s];
/* root row */
val_t const * const restrict rv = avals + (fid * nfactors);
/* foreach fiber in slice */
for(idx_t f=sptr[s]; f < sptr[s+1]; ++f) {
/* first entry of the fiber is used to initialize accumF */
idx_t const jjfirst = fptr[f];
val_t const vfirst = vals[jjfirst];
val_t const * const restrict bv = bvals + (inds[jjfirst] * nfactors);
for(idx_t r=0; r < nfactors; ++r) {
accumF[r] = vfirst * bv[r];
}
/* foreach nnz in fiber */
for(idx_t jj=fptr[f]+1; jj < fptr[f+1]; ++jj) {
val_t const v = vals[jj];
val_t const * const restrict bv = bvals + (inds[jj] * nfactors);
for(idx_t r=0; r < nfactors; ++r) {
accumF[r] += v * bv[r];
}
}
/* write to fiber row */
val_t * const restrict ov = ovals + (fids[f] * nfactors);
mutex_set_lock(pool, fids[f]);
for(idx_t r=0; r < nfactors; ++r) {
ov[r] += rv[r] * accumF[r];
}
mutex_unset_lock(pool, fids[f]);
}
}
}
static void p_csf_mttkrp_leaf3_locked(
splatt_csf const * const ct,
idx_t const tile_id,
matrix_t ** mats,
idx_t const mode,
thd_info * const thds,
idx_t const * const restrict partition)
{
assert(ct->nmodes == 3);
val_t const * const vals = ct->pt[tile_id].vals;
idx_t const * const restrict sptr = ct->pt[tile_id].fptr[0];
idx_t const * const restrict fptr = ct->pt[tile_id].fptr[1];
idx_t const * const restrict sids = ct->pt[tile_id].fids[0];
idx_t const * const restrict fids = ct->pt[tile_id].fids[1];
idx_t const * const restrict inds = ct->pt[tile_id].fids[2];
val_t const * const avals = mats[csf_depth_to_mode(ct, 0)]->vals;
val_t const * const bvals = mats[csf_depth_to_mode(ct, 1)]->vals;
val_t * const ovals = mats[MAX_NMODES]->vals;
idx_t const nfactors = mats[MAX_NMODES]->J;
int const tid = splatt_omp_get_thread_num();
val_t * const restrict accumF = (val_t *) thds[tid].scratch[0];
idx_t const nslices = ct->pt[tile_id].nfibs[0];
idx_t const start = (partition != NULL) ? partition[tid] : 0;
idx_t const stop = (partition != NULL) ? partition[tid+1] : nslices;
for(idx_t s=start; s < stop; ++s) {
idx_t const fid = (sids == NULL) ? s : sids[s];
/* root row */
val_t const * const restrict rv = avals + (fid * nfactors);
/* foreach fiber in slice */
for(idx_t f=sptr[s]; f < sptr[s+1]; ++f) {
/* fill fiber with hada */
val_t const * const restrict av = bvals + (fids[f] * nfactors);
for(idx_t r=0; r < nfactors; ++r) {
accumF[r] = rv[r] * av[r];
}
/* foreach nnz in fiber, scale with hada and write to ovals */
for(idx_t jj=fptr[f]; jj < fptr[f+1]; ++jj) {
val_t const v = vals[jj];
val_t * const restrict ov = ovals + (inds[jj] * nfactors);
mutex_set_lock(pool, inds[jj]);
for(idx_t r=0; r < nfactors; ++r) {
ov[r] += v * accumF[r];
}
mutex_unset_lock(pool, inds[jj]);
}
}
}
}
static void p_csf_mttkrp_root_nolock(
splatt_csf const * const ct,
idx_t const tile_id,
matrix_t ** mats,
idx_t const mode,
thd_info * const thds,
idx_t const * const restrict partition)
{
/* extract tensor structures */
idx_t const nmodes = ct->nmodes;
val_t const * const vals = ct->pt[tile_id].vals;
/* empty tile, just return */
if(vals == NULL) {
return;
}
if(nmodes == 3) {
p_csf_mttkrp_root3_nolock(ct, tile_id, mats, mode, thds, partition);
return;
}
idx_t const * const * const restrict fp
= (idx_t const * const *) ct->pt[tile_id].fptr;
idx_t const * const * const restrict fids
= (idx_t const * const *) ct->pt[tile_id].fids;
idx_t const nfactors = mats[0]->J;
val_t * mvals[MAX_NMODES];
val_t * buf[MAX_NMODES];
idx_t idxstack[MAX_NMODES];
int const tid = splatt_omp_get_thread_num();
for(idx_t m=0; m < nmodes; ++m) {
mvals[m] = mats[csf_depth_to_mode(ct, m)]->vals;
/* grab the next row of buf from thds */
buf[m] = ((val_t *) thds[tid].scratch[2]) + (nfactors * m);
memset(buf[m], 0, nfactors * sizeof(val_t));
}
val_t * const ovals = mats[MAX_NMODES]->vals;
idx_t const nfibs = ct->pt[tile_id].nfibs[0];
assert(nfibs <= mats[MAX_NMODES]->I);
/* break up loop by partition */
idx_t const start = (partition != NULL) ? partition[tid] : 0;
idx_t const stop = (partition != NULL) ? partition[tid+1] : nfibs;
for(idx_t s=start; s < stop; ++s) {
idx_t const fid = (fids[0] == NULL) ? s : fids[0][s];
assert(fid < mats[MAX_NMODES]->I);
p_propagate_up(buf[0], buf, idxstack, 0, s, fp, fids,
vals, mvals, nmodes, nfactors);
val_t * const restrict orow = ovals + (fid * nfactors);
val_t const * const restrict obuf = buf[0];
mutex_set_lock(pool, fid);
for(idx_t f=0; f < nfactors; ++f) {
orow[f] += obuf[f];
}
mutex_unset_lock(pool, fid);
} /* end foreach outer slice */
}
static void p_csf_mttkrp_root_locked(
splatt_csf const * const ct,
idx_t const tile_id,
matrix_t ** mats,
idx_t const mode,
thd_info * const thds,
idx_t const * const restrict partition)
{
/* extract tensor structures */
idx_t const nmodes = ct->nmodes;
val_t const * const vals = ct->pt[tile_id].vals;
/* empty tile, just return */
if(vals == NULL) {
return;
}
if(nmodes == 3) {
p_csf_mttkrp_root3_locked(ct, tile_id, mats, mode, thds, partition);
return;
}
idx_t const * const * const restrict fp
= (idx_t const * const *) ct->pt[tile_id].fptr;
idx_t const * const * const restrict fids
= (idx_t const * const *) ct->pt[tile_id].fids;
idx_t const nfactors = mats[0]->J;
val_t * mvals[MAX_NMODES];
val_t * buf[MAX_NMODES];
idx_t idxstack[MAX_NMODES];
int const tid = splatt_omp_get_thread_num();
for(idx_t m=0; m < nmodes; ++m) {
mvals[m] = mats[csf_depth_to_mode(ct, m)]->vals;
/* grab the next row of buf from thds */
buf[m] = ((val_t *) thds[tid].scratch[2]) + (nfactors * m);
memset(buf[m], 0, nfactors * sizeof(val_t));
}
val_t * const ovals = mats[MAX_NMODES]->vals;
idx_t const nfibs = ct->pt[tile_id].nfibs[0];
assert(nfibs <= mats[MAX_NMODES]->I);
idx_t const start = (partition != NULL) ? partition[tid] : 0;
idx_t const stop = (partition != NULL) ? partition[tid+1] : nfibs;
for(idx_t s=start; s < stop; ++s) {
idx_t const fid = (fids[0] == NULL) ? s : fids[0][s];
assert(fid < mats[MAX_NMODES]->I);
p_propagate_up(buf[0], buf, idxstack, 0, s, fp, fids,
vals, mvals, nmodes, nfactors);
val_t * const restrict orow = ovals + (fid * nfactors);
val_t const * const restrict obuf = buf[0];
mutex_set_lock(pool, fid);
for(idx_t f=0; f < nfactors; ++f) {
orow[f] += obuf[f];
}
mutex_unset_lock(pool, fid);
} /* end foreach outer slice */
}
static void p_csf_mttkrp_leaf3_nolock(
splatt_csf const * const ct,
idx_t const tile_id,
matrix_t ** mats,
idx_t const mode,
thd_info * const thds,
idx_t const * const partition)
{
assert(ct->nmodes == 3);
val_t const * const vals = ct->pt[tile_id].vals;
idx_t const * const restrict sptr = ct->pt[tile_id].fptr[0];
idx_t const * const restrict fptr = ct->pt[tile_id].fptr[1];
idx_t const * const restrict sids = ct->pt[tile_id].fids[0];
idx_t const * const restrict fids = ct->pt[tile_id].fids[1];
idx_t const * const restrict inds = ct->pt[tile_id].fids[2];
val_t const * const avals = mats[csf_depth_to_mode(ct, 0)]->vals;
val_t const * const bvals = mats[csf_depth_to_mode(ct, 1)]->vals;
val_t * const ovals = mats[MAX_NMODES]->vals;
idx_t const nfactors = mats[MAX_NMODES]->J;
int const tid = splatt_omp_get_thread_num();
val_t * const restrict accumF = (val_t *) thds[tid].scratch[0];
idx_t const nslices = ct->pt[tile_id].nfibs[0];
idx_t const start = (partition != NULL) ? partition[tid] : 0;
idx_t const stop = (partition != NULL) ? partition[tid+1] : nslices;
for(idx_t s=start; s < stop; ++s) {
idx_t const fid = (sids == NULL) ? s : sids[s];
/* root row */
val_t const * const restrict rv = avals + (fid * nfactors);
/* foreach fiber in slice */
for(idx_t f=sptr[s]; f < sptr[s+1]; ++f) {
/* fill fiber with hada */
val_t const * const restrict av = bvals + (fids[f] * nfactors);
for(idx_t r=0; r < nfactors; ++r) {
accumF[r] = rv[r] * av[r];
}
/* foreach nnz in fiber, scale with hada and write to ovals */
for(idx_t jj=fptr[f]; jj < fptr[f+1]; ++jj) {
val_t const v = vals[jj];
val_t * const restrict ov = ovals + (inds[jj] * nfactors);
for(idx_t r=0; r < nfactors; ++r) {
ov[r] += v * accumF[r];
}
}
}
}
}
static void p_csf_mttkrp_leaf_nolock(
splatt_csf const * const ct,
idx_t const tile_id,
matrix_t ** mats,
idx_t const mode,
thd_info * const thds,
idx_t const * const partition)
{
val_t const * const vals = ct->pt[tile_id].vals;
idx_t const nmodes = ct->nmodes;
/* pass empty tiles */
if(vals == NULL) {
return;
}
if(nmodes == 3) {
p_csf_mttkrp_leaf3_nolock(ct, tile_id, mats, mode, thds, partition);
return;
}
/* extract tensor structures */
idx_t const * const * const restrict fp
= (idx_t const * const *) ct->pt[tile_id].fptr;
idx_t const * const * const restrict fids
= (idx_t const * const *) ct->pt[tile_id].fids;
idx_t const nfactors = mats[0]->J;
val_t * mvals[MAX_NMODES];
val_t * buf[MAX_NMODES];
idx_t idxstack[MAX_NMODES];
int const tid = splatt_omp_get_thread_num();
for(idx_t m=0; m < nmodes; ++m) {
mvals[m] = mats[csf_depth_to_mode(ct, m)]->vals;
/* grab the next row of buf from thds */
buf[m] = ((val_t *) thds[tid].scratch[2]) + (nfactors * m);
}
/* foreach outer slice */
idx_t const nouter = ct->pt[tile_id].nfibs[0];
idx_t const start = (partition != NULL) ? partition[tid] : 0;
idx_t const stop = (partition != NULL) ? partition[tid+1] : nouter;
for(idx_t s=start; s < stop; ++s) {
idx_t const fid = (fids[0] == NULL) ? s : fids[0][s];
idxstack[0] = s;
/* clear out stale data */
for(idx_t m=1; m < nmodes-1; ++m) {
idxstack[m] = fp[m-1][idxstack[m-1]];
}
/* first buf will always just be a matrix row */
val_t const * const rootrow = mvals[0] + (fid*nfactors);
val_t * const rootbuf = buf[0];
for(idx_t f=0; f < nfactors; ++f) {
rootbuf[f] = rootrow[f];
}
idx_t depth = 0;
idx_t const outer_end = fp[0][s+1];
while(idxstack[1] < outer_end) {
/* move down to an nnz node */
for(; depth < nmodes-2; ++depth) {
/* propogate buf down */
val_t const * const restrict drow
= mvals[depth+1] + (fids[depth+1][idxstack[depth+1]] * nfactors);
p_assign_hada(buf[depth+1], buf[depth], drow, nfactors);
}
/* process all nonzeros [start, end) */
idx_t const start = fp[depth][idxstack[depth]];
idx_t const end = fp[depth][idxstack[depth]+1];
p_csf_process_fiber_nolock(mats[MAX_NMODES]->vals, buf[depth],
nfactors, start, end, fids[depth+1], vals);
/* now move back up to the next unprocessed child */
do {
++idxstack[depth];
--depth;
} while(depth > 0 && idxstack[depth+1] == fp[depth][idxstack[depth]+1]);
} /* end DFS */
} /* end outer slice loop */
}
static void p_csf_mttkrp_leaf_locked(
splatt_csf const * const ct,
idx_t const tile_id,
matrix_t ** mats,
idx_t const mode,
thd_info * const thds,
idx_t const * const restrict partition)
{
/* extract tensor structures */
val_t const * const vals = ct->pt[tile_id].vals;
idx_t const nmodes = ct->nmodes;
if(vals == NULL) {
return;
}
if(nmodes == 3) {
p_csf_mttkrp_leaf3_locked(ct, tile_id, mats, mode, thds, partition);
return;
}
idx_t const * const * const restrict fp
= (idx_t const * const *) ct->pt[tile_id].fptr;
idx_t const * const * const restrict fids
= (idx_t const * const *) ct->pt[tile_id].fids;
idx_t const nfactors = mats[0]->J;
val_t * mvals[MAX_NMODES];
val_t * buf[MAX_NMODES];
idx_t idxstack[MAX_NMODES];
int const tid = splatt_omp_get_thread_num();
for(idx_t m=0; m < nmodes; ++m) {
mvals[m] = mats[csf_depth_to_mode(ct, m)]->vals;
/* grab the next row of buf from thds */
buf[m] = ((val_t *) thds[tid].scratch[2]) + (nfactors * m);
}
/* foreach outer slice */
idx_t const nslices = ct->pt[tile_id].nfibs[0];
idx_t const start = (partition != NULL) ? partition[tid] : 0;
idx_t const stop = (partition != NULL) ? partition[tid+1] : nslices;
for(idx_t s=start; s < stop; ++s) {
idx_t const fid = (fids[0] == NULL) ? s : fids[0][s];
idxstack[0] = s;
/* clear out stale data */
for(idx_t m=1; m < nmodes-1; ++m) {
idxstack[m] = fp[m-1][idxstack[m-1]];
}
/* first buf will always just be a matrix row */
val_t const * const restrict rootrow = mvals[0] + (fid*nfactors);
val_t * const rootbuf = buf[0];
for(idx_t f=0; f < nfactors; ++f) {