-
Notifications
You must be signed in to change notification settings - Fork 20
/
tensors.hpp
2764 lines (2427 loc) · 80.9 KB
/
tensors.hpp
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
#pragma once
#include "build_info.hpp"
#ifdef ASGARD_USE_CUDA
#include <cuda_runtime.h>
#endif
#include "lib_dispatch.hpp"
#include "tools.hpp"
#include <memory>
#include <string>
#include <vector>
/* tolerance for answer comparisons */
#define TOL std::numeric_limits<P>::epsilon() * 2
// allows a private member function to declare via its parameter list who from
// outside the class is allowed to call it. you must hold an "access badge".
template<typename badge_holder>
class access_badge
{
friend badge_holder;
access_badge(){};
};
// used to suppress warnings in unused variables
auto const ignore = [](auto ignored) { (void)ignored; };
enum class mem_type
{
owner,
view,
const_view
};
template<mem_type mem>
using enable_for_owner = std::enable_if_t<mem == mem_type::owner>;
template<mem_type mem>
using enable_for_all_views =
std::enable_if_t<mem == mem_type::view || mem == mem_type::const_view>;
// enable only for const views
template<mem_type mem>
using enable_for_const_view = std::enable_if_t<mem == mem_type::const_view>;
// enable only for nonconst views
template<mem_type mem>
using enable_for_view = std::enable_if_t<mem == mem_type::view>;
// disable for const views
template<mem_type mem>
using disable_for_const_view =
std::enable_if_t<mem == mem_type::owner || mem == mem_type::view>;
template<resource resrc>
using enable_for_host = std::enable_if_t<resrc == resource::host>;
template<resource resrc>
using enable_for_device = std::enable_if_t<resrc == resource::device>;
// resource arguments allow developers to select host (CPU only) or device
// (accelerator if enabled, fall back to host) allocation for tensors. device
// tensors have a restricted API - most member functions are disabled. the fast
// math component is designed to allow BLAS on host and device tensors.
// mem_type arguments allow for the selection of owner or view (read/write
// window into underlying owner memory) semantics.
// device owners can be constructed with no-arg, size, or
// initializer list constructors.
//
// device owners are allocated in accelerator DRAM when
// the appropriate build option is set, with allocation
// falling back to CPU RAM otherwise.
//
// additionally, device owners can be transfer constructed from a
// host owner or copy/move constructed from another device owner.
//
// host owners can be created with any of the below constructors.
//
// device views can only be created from a device owner, and host
// views can only be constructor from host owners
namespace fk
{
// forward declarations
template<typename P, mem_type mem = mem_type::owner,
resource resrc = resource::host> // default to be an owner only on host
class vector;
template<typename P, mem_type mem = mem_type::owner,
resource resrc = resource::host>
class matrix;
template<typename P, mem_type mem, resource resrc>
class vector
{
// all types of vectors are mutual friends
template<typename, mem_type, resource>
friend class vector;
public:
template<mem_type m_ = mem, typename = enable_for_owner<m_>>
vector();
template<mem_type m_ = mem, typename = enable_for_owner<m_>>
explicit vector(int const size);
template<mem_type m_ = mem, typename = enable_for_owner<m_>>
vector(std::initializer_list<P> list);
template<mem_type m_ = mem, typename = enable_for_owner<m_>,
resource r_ = resrc, typename = enable_for_host<r_>>
vector(std::vector<P> const &);
template<mem_type m_ = mem, typename = enable_for_owner<m_>>
vector(fk::matrix<P, mem_type::owner, resrc> const &);
template<mem_type m_ = mem, typename = enable_for_view<m_>, mem_type omem,
mem_type om_ = omem, typename = disable_for_const_view<om_>>
explicit vector(fk::vector<P, omem, resrc> &vec, int const start_index,
int const stop_index);
template<mem_type m_ = mem, typename = enable_for_const_view<m_>,
mem_type omem>
explicit vector(fk::vector<P, omem, resrc> const &vec, int const start_index,
int const stop_index);
// overloads for default case - whole vector
template<mem_type m_ = mem, typename = enable_for_view<m_>, mem_type omem,
mem_type om_ = omem, typename = disable_for_const_view<om_>>
explicit vector(fk::vector<P, omem, resrc> &owner);
template<mem_type m_ = mem, typename = enable_for_const_view<m_>,
mem_type omem>
explicit vector(fk::vector<P, omem, resrc> const &owner);
// create vector view from matrix
// const view version
template<mem_type m_ = mem, typename = enable_for_const_view<m_>,
mem_type omem>
explicit vector(fk::matrix<P, omem, resrc> const &source, int const col_index,
int const row_start, int const row_stop);
// modifiable view version
template<mem_type m_ = mem, typename = enable_for_view<m_>, mem_type omem,
mem_type om_ = omem, typename = disable_for_const_view<om_>>
explicit vector(fk::matrix<P, omem, resrc> &source, int const col_index,
int const row_start, int const row_stop);
~vector();
// constructor/assignment (required to be same to same T==T)
vector(vector<P, mem, resrc> const &);
// cannot be templated per C++ spec 12.8
// instead of disabling w/ sfinae for const_view,
// static assert added to definition
vector<P, mem, resrc> &operator=(vector<P, mem, resrc> const &);
// move constructor/assignment (required to be same to same)
vector(vector<P, mem, resrc> &&);
// as with copy assignment, static assert added
// to definition to prevent assignment into
// const views
vector<P, mem, resrc> &operator=(vector<P, mem, resrc> &&);
// copy construct owner from view values
template<mem_type omem, mem_type m_ = mem, typename = enable_for_owner<m_>,
mem_type m__ = omem, typename = enable_for_all_views<m__>>
explicit vector(vector<P, omem, resrc> const &);
// assignment owner <-> view
template<mem_type omem, mem_type m_ = mem,
typename = disable_for_const_view<m_>>
vector<P, mem, resrc> &operator=(vector<P, omem, resrc> const &);
// converting constructor/assignment overloads
template<typename PP, mem_type omem, mem_type m_ = mem,
typename = enable_for_owner<m_>, resource r_ = resrc,
typename = enable_for_host<r_>>
explicit vector(vector<PP, omem> const &);
template<typename PP, mem_type omem, mem_type m_ = mem,
typename = disable_for_const_view<m_>, resource r_ = resrc,
typename = enable_for_host<r_>>
vector<P, mem> &operator=(vector<PP, omem> const &);
// device transfer
// host to device, new vector
template<resource r_ = resrc, typename = enable_for_host<r_>>
vector<P, mem_type::owner, resource::device> clone_onto_device() const;
// host to device copy
template<mem_type omem, mem_type m_ = mem,
typename = disable_for_const_view<m_>, resource r_ = resrc,
typename = enable_for_device<r_>>
vector<P, mem, resrc> &transfer_from(vector<P, omem, resource::host> const &);
// device to host, new vector
template<resource r_ = resrc, typename = enable_for_device<r_>>
vector<P, mem_type::owner, resource::host> clone_onto_host() const;
// device to host copy
template<mem_type omem, mem_type m_ = mem,
typename = disable_for_const_view<m_>, resource r_ = resrc,
typename = enable_for_host<r_>>
vector<P, mem, resrc> &
transfer_from(vector<P, omem, resource::device> const &);
//
// copy out of std::vector
//
template<mem_type m_ = mem, typename = disable_for_const_view<m_>,
resource r_ = resrc, typename = enable_for_host<r_>>
vector<P, mem> &operator=(std::vector<P> const &);
//
// copy into std::vector
//
template<resource r_ = resrc, typename = enable_for_host<r_>>
std::vector<P> to_std() const;
//
// subscripting operators
//
template<mem_type m_ = mem, typename = disable_for_const_view<m_>,
resource r_ = resrc, typename = enable_for_host<r_>>
P &operator()(int const);
template<resource r_ = resrc, typename = enable_for_host<r_>>
P operator()(int const) const;
//
// comparison operators
//
template<mem_type omem, resource r_ = resrc, typename = enable_for_host<r_>>
bool operator==(vector<P, omem> const &) const;
template<mem_type omem, resource r_ = resrc, typename = enable_for_host<r_>>
bool operator!=(vector<P, omem> const &) const;
template<mem_type omem, resource r_ = resrc, typename = enable_for_host<r_>>
bool operator<(vector<P, omem> const &) const;
//
// math operators
//
template<mem_type omem, resource r_ = resrc, typename = enable_for_host<r_>>
vector<P> operator+(vector<P, omem> const &right) const;
template<mem_type omem, resource r_ = resrc, typename = enable_for_host<r_>>
vector<P> operator-(vector<P, omem> const &right) const;
template<mem_type omem, resource r_ = resrc, typename = enable_for_host<r_>>
P operator*(vector<P, omem> const &)const;
template<mem_type omem, resource r_ = resrc, typename = enable_for_host<r_>>
vector<P> operator*(matrix<P, omem> const &)const;
template<resource r_ = resrc, typename = enable_for_host<r_>>
vector<P> operator*(P const) const;
template<mem_type omem, resource r_ = resrc, typename = enable_for_host<r_>>
vector<P> single_column_kron(vector<P, omem> const &) const;
template<mem_type m_ = mem, typename = disable_for_const_view<m_>,
resource r_ = resrc, typename = enable_for_host<r_>>
vector<P, mem> &scale(P const x);
//
// basic queries to private data
//
int size() const { return size_; }
// just get a pointer. cannot deref/assign. for e.g. blas
// use subscript operators for general purpose access
// this can be offsetted for views
P *data(int const elem = 0) const { return &data_[elem]; }
// this is to allow specific other types to access the private ref counter of
// owners - specifically, we want to allow a matrix<view> to be made from a
// vector<owner/view>
std::shared_ptr<int>
get_ref_count(access_badge<matrix<P, mem_type::view, resrc>> const)
{
return ref_count_;
}
std::shared_ptr<int> get_ref_count(
access_badge<matrix<P, mem_type::const_view, resrc>> const) const
{
return ref_count_;
}
//
// utility functions
//
template<resource r_ = resrc, typename = enable_for_host<r_>>
void print(std::string const label = "") const;
template<resource r_ = resrc, typename = enable_for_host<r_>>
void dump_to_octave(char const *) const;
template<mem_type m_ = mem, typename = enable_for_owner<m_>>
fk::vector<P, mem_type::owner, resrc> &resize(int const size = 0);
template<mem_type omem, mem_type m_ = mem,
typename = disable_for_const_view<m_>, resource r_ = resrc,
typename = enable_for_host<r_>>
vector<P, mem> &set_subvector(int const, vector<P, omem> const);
template<resource r_ = resrc, typename = enable_for_host<r_>>
vector<P> extract(int const, int const) const;
template<mem_type omem, mem_type m_ = mem, typename = enable_for_owner<m_>,
resource r_ = resrc, typename = enable_for_host<r_>>
vector<P, mem> &concat(vector<P, omem> const &right);
typedef P *iterator;
typedef const P *const_iterator;
template<mem_type m_ = mem, typename = disable_for_const_view<m_>>
iterator begin()
{
return data();
}
template<mem_type m_ = mem, typename = disable_for_const_view<m_>>
iterator end()
{
return data() + size();
}
const_iterator begin() const { return data(); }
const_iterator end() const { return data() + size(); }
template<mem_type m_ = mem, typename = enable_for_owner<m_>>
int get_num_views() const;
private:
// const/nonconst view constructors delegate to this private constructor
// delegated is a dummy variable to enable resolution
template<mem_type m_ = mem, typename = enable_for_all_views<m_>,
mem_type omem>
explicit vector(fk::vector<P, omem, resrc> const &vec, int const start_index,
int const stop_index, bool const delegated);
// vector view from matrix constructors (both const/nonconst) delegate
// to this private constructor, also with a dummy variable
template<mem_type omem, mem_type m_ = mem,
typename = enable_for_all_views<m_>>
explicit vector(fk::matrix<P, omem, resrc> const &source,
std::shared_ptr<int> source_ref_count, int const column_index,
int const row_start, int const row_stop);
P *data_; //< pointer to elements
int size_; //< dimension
std::shared_ptr<int> ref_count_ = nullptr;
};
template<typename P, mem_type mem, resource resrc>
class matrix
{
template<typename, mem_type, resource>
friend class matrix; // so that views can access owner sharedptr/rows
// template on pointer/ref type to get iterator and const iterator
template<typename T, typename R>
class matrix_iterator; // forward declaration for custom iterator; defined
// out of line
public:
template<mem_type m_ = mem, typename = enable_for_owner<m_>>
matrix();
template<mem_type m_ = mem, typename = enable_for_owner<m_>>
matrix(int rows, int cols);
template<mem_type m_ = mem, typename = enable_for_owner<m_>>
matrix(std::initializer_list<std::initializer_list<P>> list);
// create const view
template<mem_type m_ = mem, typename = enable_for_const_view<m_>,
mem_type omem>
explicit matrix(fk::matrix<P, omem, resrc> const &owner, int const start_row,
int const stop_row, int const start_col, int const stop_col);
// create modifiable view
template<mem_type m_ = mem, typename = enable_for_view<m_>, mem_type omem,
mem_type om_ = omem, typename = disable_for_const_view<om_>>
explicit matrix(fk::matrix<P, omem, resrc> &owner, int const start_row,
int const stop_row, int const start_col, int const stop_col);
// overloads for default case - whole matrix
template<mem_type m_ = mem, typename = enable_for_const_view<m_>,
mem_type omem>
explicit matrix(fk::matrix<P, omem, resrc> const &owner);
template<mem_type m_ = mem, typename = enable_for_view<m_>, mem_type omem,
mem_type om_ = omem, typename = disable_for_const_view<om_>>
explicit matrix(fk::matrix<P, omem, resrc> &owner);
// create matrix view from vector
// const view version
template<mem_type m_ = mem, typename = enable_for_const_view<m_>,
mem_type omem>
explicit matrix(fk::vector<P, omem, resrc> const &source, int const num_rows,
int const num_cols, int const start_index = 0);
// modifiable view version
template<mem_type m_ = mem, typename = enable_for_view<m_>, mem_type omem,
mem_type om_ = omem, typename = disable_for_const_view<om_>>
explicit matrix(fk::vector<P, omem, resrc> &source, int const num_rows,
int const num_cols, int const start_index = 0);
~matrix();
// copy constructor/assign
matrix(matrix<P, mem, resrc> const &);
matrix<P, mem, resrc> &operator=(matrix<P, mem, resrc> const &);
// copy construct owner from view values
template<mem_type omem, mem_type m_ = mem, typename = enable_for_owner<m_>,
mem_type m__ = omem, typename = enable_for_all_views<m__>>
explicit matrix(matrix<P, omem, resrc> const &);
// assignment owner <-> view
template<mem_type omem, mem_type m_ = mem,
typename = disable_for_const_view<m_>>
matrix<P, mem, resrc> &operator=(matrix<P, omem, resrc> const &);
// converting construction/assign
template<typename PP, mem_type omem, mem_type m_ = mem,
typename = enable_for_owner<m_>, resource r_ = resrc,
typename = enable_for_host<r_>>
explicit matrix(matrix<PP, omem> const &);
template<typename PP, mem_type omem, mem_type m_ = mem,
typename = disable_for_const_view<m_>, resource r_ = resrc,
typename = enable_for_host<r_>>
matrix<P, mem> &operator=(matrix<PP, omem> const &);
// host to device, new matrix
template<resource r_ = resrc, typename = enable_for_host<r_>>
fk::matrix<P, mem_type::owner, resource::device> clone_onto_device() const;
// host to device copy
template<mem_type omem, resource r_ = resrc, typename = enable_for_device<r_>>
matrix<P, mem, resrc> &transfer_from(matrix<P, omem, resource::host> const &);
// device to host, new matrix
template<resource r_ = resrc, typename = enable_for_device<r_>>
fk::matrix<P, mem_type::owner, resource::host> clone_onto_host() const;
// device to host copy
template<mem_type omem, resource r_ = resrc, typename = enable_for_host<r_>>
matrix<P, mem, resrc> &
transfer_from(matrix<P, omem, resource::device> const &);
// move constructor/assign
matrix(matrix<P, mem, resrc> &&);
matrix<P, mem, resrc> &operator=(matrix<P, mem, resrc> &&);
//
// copy out of fk::vector
//
template<mem_type omem, mem_type m_ = mem,
typename = disable_for_const_view<m_>, resource r_ = resrc,
typename = enable_for_host<r_>>
matrix<P, mem> &operator=(fk::vector<P, omem> const &);
//
// subscripting operators
//
template<mem_type m_ = mem, typename = disable_for_const_view<m_>,
resource r_ = resrc, typename = enable_for_host<r_>>
P &operator()(int const, int const);
template<resource r_ = resrc, typename = enable_for_host<r_>>
P operator()(int const, int const) const;
//
// comparison operators
//
template<mem_type omem, resource r_ = resrc, typename = enable_for_host<r_>>
bool operator==(matrix<P, omem> const &) const;
template<mem_type omem, resource r_ = resrc, typename = enable_for_host<r_>>
bool operator!=(matrix<P, omem> const &) const;
template<mem_type omem, resource r_ = resrc, typename = enable_for_host<r_>>
bool operator<(matrix<P, omem> const &) const;
//
// math operators
//
template<resource r_ = resrc, typename = enable_for_host<r_>>
matrix<P> operator*(P const) const;
template<mem_type omem, resource r_ = resrc, typename = enable_for_host<r_>>
vector<P> operator*(vector<P, omem> const &)const;
template<mem_type omem, resource r_ = resrc, typename = enable_for_host<r_>>
matrix<P> operator*(matrix<P, omem> const &)const;
template<mem_type omem, resource r_ = resrc, typename = enable_for_host<r_>>
matrix<P> operator+(matrix<P, omem> const &) const;
template<mem_type omem, resource r_ = resrc, typename = enable_for_host<r_>>
matrix<P> operator-(matrix<P, omem> const &) const;
template<mem_type m_ = mem, typename = enable_for_owner<m_>,
resource r_ = resrc, typename = enable_for_host<r_>>
matrix<P, mem, resrc> &transpose();
template<mem_type m_ = mem, typename = enable_for_owner<m_>,
resource r_ = resrc, typename = enable_for_host<r_>>
matrix<P, mem, resrc> &ip_transpose();
template<mem_type omem, resource r_ = resrc, typename = enable_for_host<r_>>
matrix<P> kron(matrix<P, omem> const &) const;
template<typename U = P,
typename = std::enable_if_t<std::is_floating_point<U>::value &&
std::is_same<P, U>::value>,
mem_type m_ = mem, typename = disable_for_const_view<m_>,
resource r_ = resrc, typename = enable_for_host<r_>>
matrix<P, mem> &invert();
template<typename U = P,
typename = std::enable_if_t<std::is_floating_point<U>::value &&
std::is_same<P, U>::value>,
resource r_ = resrc, typename = enable_for_host<r_>>
P determinant() const;
//
// basic queries to private data
//
int nrows() const { return nrows_; }
int ncols() const { return ncols_; }
// for owners: stride == nrows
// for views: stride == owner's nrows
int stride() const { return stride_; }
int size() const { return nrows() * ncols(); }
// just get a pointer. cannot deref/assign. for e.g. blas
// use subscript operators for general purpose access
P *data(int const i = 0, int const j = 0) const
{
// return &data_[i * stride() + j]; // row-major
return &data_[j * stride() + i]; // column-major
}
//
// utility functions
//
template<mem_type omem, mem_type m_ = mem,
typename = disable_for_const_view<m_>, resource r_ = resrc,
typename = enable_for_host<r_>>
matrix<P, mem> &update_col(int const, fk::vector<P, omem> const &);
template<mem_type m_ = mem, typename = disable_for_const_view<m_>,
resource r_ = resrc, typename = enable_for_host<r_>>
matrix<P, mem> &update_col(int const, std::vector<P> const &);
template<mem_type omem, mem_type m_ = mem,
typename = disable_for_const_view<m_>, resource r_ = resrc,
typename = enable_for_host<r_>>
matrix<P, mem> &update_row(int const, fk::vector<P, omem> const &);
template<mem_type m_ = mem, typename = disable_for_const_view<m_>,
resource r_ = resrc, typename = enable_for_host<r_>>
matrix<P, mem> &update_row(int const, std::vector<P> const &);
template<mem_type m_ = mem, typename = enable_for_owner<m_>>
matrix<P, mem_type::owner, resrc> &clear_and_resize(int const, int const);
template<mem_type omem, mem_type m_ = mem,
typename = disable_for_const_view<m_>, resource r_ = resrc,
typename = enable_for_host<r_>>
matrix<P, mem> &set_submatrix(int const row_idx, int const col_idx,
fk::matrix<P, omem> const &submatrix);
template<resource r_ = resrc, typename = enable_for_host<r_>>
matrix<P> extract_submatrix(int const row_idx, int const col_idx,
int const num_rows, int const num_cols) const;
template<resource r_ = resrc, typename = enable_for_host<r_>>
void print(std::string const label = "") const;
template<resource r_ = resrc, typename = enable_for_host<r_>>
void dump_to_octave(char const *name) const;
template<mem_type m_ = mem, typename = enable_for_owner<m_>>
int get_num_views() const;
// this is to allow specific other types to access the private ref counter of
// owners - specifically, we want to allow a vector<view> to be made from a
// matrix<owner/view>
std::shared_ptr<int>
get_ref_count(access_badge<vector<P, mem_type::view, resrc>> const)
{
return ref_count_;
}
std::shared_ptr<int> get_ref_count(
access_badge<vector<P, mem_type::const_view, resrc>> const) const
{
return ref_count_;
}
using iterator = matrix_iterator<P *, P &>;
using const_iterator = matrix_iterator<P const *, P const &>;
template<mem_type m_ = mem, typename = disable_for_const_view<m_>>
iterator begin()
{
return iterator(data(), stride(), nrows());
}
template<mem_type m_ = mem, typename = disable_for_const_view<m_>>
iterator end()
{
return iterator(data() + stride() * ncols(), stride(), nrows());
}
const_iterator begin() const
{
return const_iterator(data(), stride(), nrows());
}
const_iterator end() const
{
return const_iterator(data() + stride() * ncols(), stride(), nrows());
}
private:
// matrix view constructors (both const/nonconst) delegate to this private
// constructor delegated is a dummy variable to assist in overload resolution
template<mem_type m_ = mem, typename = enable_for_all_views<m_>,
mem_type omem>
explicit matrix(fk::matrix<P, omem, resrc> const &owner, int const start_row,
int const stop_row, int const start_col, int const stop_col,
bool const delegated);
// matrix view from vector owner constructors (both const/nonconst) delegate
// to this private constructor, also with a dummy variable
template<mem_type omem, mem_type m_ = mem,
typename = enable_for_all_views<m_>>
explicit matrix(fk::vector<P, omem, resrc> const &source,
std::shared_ptr<int> source_ref_count, int const num_rows,
int const num_cols, int const start_index);
P *data_; //< pointer to elements
int nrows_; //< row dimension
int ncols_; //< column dimension
int stride_; //< leading dimension;
// number of elements in memory between successive matrix
// elements in a row
std::shared_ptr<int> ref_count_ = nullptr;
};
//-----------------------------------------------------------------------------
//
// device allocation and transfer helpers
//
//-----------------------------------------------------------------------------
template<typename P>
inline void
allocate_device(P *&ptr, int const num_elems, bool const initialize = true)
{
#ifdef ASGARD_USE_CUDA
auto success = cudaMalloc((void **)&ptr, num_elems * sizeof(P));
assert(success == cudaSuccess);
if (num_elems > 0)
{
expect(ptr != nullptr);
}
if (initialize)
{
success = cudaMemset((void *)ptr, 0, num_elems * sizeof(P));
expect(success == cudaSuccess);
}
#else
if (initialize)
{
ptr = new P[num_elems]();
}
else
{
ptr = new P[num_elems];
}
#endif
}
template<typename P>
inline void delete_device(P *const ptr)
{
#ifdef ASGARD_USE_CUDA
auto const success = cudaFree(ptr);
// the device runtime may be unloaded at process shut down
// (when static storage duration destructors are called)
// returning a cudartUnloading error code.
expect((success == cudaSuccess) || (success == cudaErrorCudartUnloading));
#else
delete[] ptr;
#endif
}
template<typename P>
inline void
copy_on_device(P *const dest, P const *const source, int const num_elems)
{
#ifdef ASGARD_USE_CUDA
auto const success =
cudaMemcpy(dest, source, num_elems * sizeof(P), cudaMemcpyDeviceToDevice);
expect(success == cudaSuccess);
#else
std::copy(source, source + num_elems, dest);
#endif
}
template<typename P>
inline void
copy_to_device(P *const dest, P const *const source, int const num_elems)
{
#ifdef ASGARD_USE_CUDA
auto const success =
cudaMemcpy(dest, source, num_elems * sizeof(P), cudaMemcpyHostToDevice);
expect(success == cudaSuccess);
#else
std::copy(source, source + num_elems, dest);
#endif
}
template<typename P>
inline void
copy_to_host(P *const dest, P const *const source, int const num_elems)
{
#ifdef ASGARD_USE_CUDA
auto const success =
cudaMemcpy(dest, source, num_elems * sizeof(P), cudaMemcpyDeviceToHost);
expect(success == cudaSuccess);
#else
std::copy(source, source + num_elems, dest);
#endif
}
template<typename P, mem_type mem, mem_type omem>
inline void
copy_matrix_on_device(fk::matrix<P, mem, resource::device> &dest,
fk::matrix<P, omem, resource::device> const &source)
{
expect(source.nrows() == dest.nrows());
expect(source.ncols() == dest.ncols());
#ifdef ASGARD_USE_CUDA
auto const success =
cudaMemcpy2D(dest.data(), dest.stride() * sizeof(P), source.data(),
source.stride() * sizeof(P), source.nrows() * sizeof(P),
source.ncols(), cudaMemcpyDeviceToDevice);
expect(success == 0);
#else
std::copy(source.begin(), source.end(), dest.begin());
#endif
}
template<typename P, mem_type mem, mem_type omem, mem_type m_ = mem,
typename = disable_for_const_view<m_>>
inline void
copy_matrix_to_device(fk::matrix<P, mem, resource::device> &dest,
fk::matrix<P, omem, resource::host> const &source)
{
expect(source.nrows() == dest.nrows());
expect(source.ncols() == dest.ncols());
#ifdef ASGARD_USE_CUDA
auto const success =
cudaMemcpy2D(dest.data(), dest.stride() * sizeof(P), source.data(),
source.stride() * sizeof(P), source.nrows() * sizeof(P),
source.ncols(), cudaMemcpyHostToDevice);
expect(success == 0);
#else
std::copy(source.begin(), source.end(), dest.begin());
#endif
}
template<typename P, mem_type mem, mem_type omem, mem_type m_ = mem,
typename = disable_for_const_view<m_>>
inline void
copy_matrix_to_host(fk::matrix<P, mem, resource::host> &dest,
fk::matrix<P, omem, resource::device> const &source)
{
expect(source.nrows() == dest.nrows());
expect(source.ncols() == dest.ncols());
#ifdef ASGARD_USE_CUDA
auto const success =
cudaMemcpy2D(dest.data(), dest.stride() * sizeof(P), source.data(),
source.stride() * sizeof(P), source.nrows() * sizeof(P),
source.ncols(), cudaMemcpyDeviceToHost);
expect(success == 0);
#else
std::copy(source.begin(), source.end(), dest.begin());
#endif
}
} // namespace fk
//
// This would otherwise be the start of the tensors.cpp, if we were still doing
// the explicit instantiations
//
#include <algorithm>
#include <cmath>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
//-----------------------------------------------------------------------------
//
// fk::vector class implementation starts here
//
//-----------------------------------------------------------------------------
template<typename P, mem_type mem, resource resrc>
template<mem_type, typename>
fk::vector<P, mem, resrc>::vector()
: data_{nullptr}, size_{0}, ref_count_{std::make_shared<int>(0)}
{}
// right now, initializing with zero for e.g. passing in answer vectors to blas
// but this is probably slower if needing to declare in a perf. critical region
template<typename P, mem_type mem, resource resrc>
template<mem_type, typename>
fk::vector<P, mem, resrc>::vector(int const size)
: size_{size}, ref_count_{std::make_shared<int>(0)}
{
expect(size >= 0);
if constexpr (resrc == resource::host)
{
data_ = new P[size_]();
}
else
{
allocate_device(data_, size_);
}
}
// can also do this with variadic template constructor for constness
// https://stackoverflow.com/a/5549918
// but possibly this is "too clever" for our needs right now
template<typename P, mem_type mem, resource resrc>
template<mem_type, typename>
fk::vector<P, mem, resrc>::vector(std::initializer_list<P> list)
: size_{static_cast<int>(list.size())}, ref_count_{std::make_shared<int>(0)}
{
if constexpr (resrc == resource::host)
{
data_ = new P[size_]();
std::copy(list.begin(), list.end(), data_);
}
else
{
allocate_device(data_, size_);
copy_to_device(data_, list.begin(), size_);
}
}
template<typename P, mem_type mem, resource resrc>
template<mem_type, typename, resource, typename>
fk::vector<P, mem, resrc>::vector(std::vector<P> const &v)
: data_{new P[v.size()]}, size_{static_cast<int>(v.size())},
ref_count_{std::make_shared<int>(0)}
{
std::copy(v.begin(), v.end(), data_);
}
//
// matrix conversion constructor linearizes the matrix, i.e. stacks the columns
// of the matrix into a single vector
//
template<typename P, mem_type mem, resource resrc>
template<mem_type, typename>
fk::vector<P, mem, resrc>::vector(
fk::matrix<P, mem_type::owner, resrc> const &mat)
: ref_count_{std::make_shared<int>(0)}
{
size_ = mat.size();
if ((*this).size() == 0)
{
if constexpr (resrc == resource::host)
{
delete[] data_;
}
else
{
delete_device(data_);
}
data_ = nullptr;
}
else
{
if constexpr (resrc == resource::host)
{
data_ = new P[mat.size()]();
int i = 0;
for (auto const &elem : mat)
{
(*this)(i++) = elem;
}
}
else
{
allocate_device(data_, size_);
copy_on_device(data_, mat.data(), mat.size());
}
}
}
// vector view constructor given a start and stop index
// modifiable view version - delegates to private constructor
template<typename P, mem_type mem, resource resrc>
template<mem_type, typename, mem_type omem, mem_type, typename>
fk::vector<P, mem, resrc>::vector(fk::vector<P, omem, resrc> &vec,
int const start_index, int const stop_index)
: vector(vec, start_index, stop_index, true)
{}
// const view version - delegates to private constructor
template<typename P, mem_type mem, resource resrc>
template<mem_type, typename, mem_type omem>
fk::vector<P, mem, resrc>::vector(fk::vector<P, omem, resrc> const &vec,
int const start_index, int const stop_index)
: vector(vec, start_index, stop_index, true)
{}
// delegating constructor to extract view from owner. overload for default case
// of viewing the entire owner
// const view version
template<typename P, mem_type mem, resource resrc>
template<mem_type, typename, mem_type omem>
fk::vector<P, mem, resrc>::vector(fk::vector<P, omem, resrc> const &a)
: vector(a, 0, std::max(0, a.size() - 1), true)
{}
// modifiable view version
template<typename P, mem_type mem, resource resrc>
template<mem_type, typename, mem_type omem, mem_type, typename>
fk::vector<P, mem, resrc>::vector(fk::vector<P, omem, resrc> &a)
: vector(a, 0, std::max(0, a.size() - 1), true)
{}
// create vector view of an existing matrix
// const version - delegates to private constructor
template<typename P, mem_type mem, resource resrc>
template<mem_type, typename, mem_type omem>
fk::vector<P, mem, resrc>::vector(fk::matrix<P, omem, resrc> const &source,
int const column_index, int const row_start,
int const row_stop)
: vector(source, source.get_ref_count({}), column_index, row_start,
row_stop)
{}
// modifiable view version - delegates to private constructor
template<typename P, mem_type mem, resource resrc>
template<mem_type, typename, mem_type omem, mem_type, typename>
fk::vector<P, mem, resrc>::vector(fk::matrix<P, omem, resrc> &source,
int const column_index, int const row_start,
int const row_stop)
: vector(source, source.get_ref_count({}), column_index, row_start,
row_stop)
{}
template<typename P, mem_type mem, resource resrc>
fk::vector<P, mem, resrc>::~vector()
{
if constexpr (mem == mem_type::owner)
{
expect(ref_count_.use_count() == 1);
if constexpr (resrc == resource::host)
{
delete[] data_;
}
else
{
delete_device(data_);
}
}
}
//
// vector copy constructor for like types (like types only)
//
template<typename P, mem_type mem, resource resrc>
fk::vector<P, mem, resrc>::vector(vector<P, mem, resrc> const &a)
: size_{a.size_}
{
if constexpr (mem == mem_type::owner)
{
ref_count_ = std::make_shared<int>(0);
if constexpr (resrc == resource::host)
{
data_ = new P[a.size()];
std::memcpy(data_, a.data(), a.size() * sizeof(P));
}
else
{
allocate_device(data_, a.size());
copy_on_device(data_, a.data(), a.size());
}
}
else
{
data_ = a.data();
ref_count_ = a.ref_count_;
}
}
//
// vector copy assignment
// this can probably be optimized better. see:
// http://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom
//
template<typename P, mem_type mem, resource resrc>
fk::vector<P, mem, resrc> &fk::vector<P, mem, resrc>::
operator=(vector<P, mem, resrc> const &a)