Distributed Ranges
Loading...
Searching...
No Matches
dense_matrix.hpp
1// SPDX-FileCopyrightText: Intel Corporation
2//
3// SPDX-License-Identifier: BSD-3-Clause
4
5#pragma once
6
7#include <iterator>
8
9#include <dr/detail/index.hpp>
10#include <dr/detail/iterator_adaptor.hpp>
11#include <dr/detail/matrix_entry.hpp>
12#include <dr/sp/views/dense_column_view.hpp>
13#include <dr/sp/views/dense_matrix_iterator.hpp>
14#include <dr/sp/views/dense_row_view.hpp>
15
16namespace dr::sp {
17
18template <typename T, typename Allocator = std::allocator<T>>
20public:
21 using size_type = std::size_t;
22 using difference_type = std::ptrdiff_t;
23
24 using allocator_type = Allocator;
25
26 using scalar_pointer = typename std::allocator_traits<Allocator>::pointer;
27
28 using scalar_reference = std::iter_reference_t<scalar_pointer>;
30
31 using key_type = dr::index<>;
32 using map_type = T;
33
35
37 : allocator_(Allocator()), shape_(shape), ld_(shape[1]) {
38 data_ = allocator_.allocate(shape_[0] * shape_[1]);
39 }
40
41 dense_matrix(key_type shape, std::size_t ld)
42 requires(std::is_default_constructible_v<Allocator>)
43 : allocator_(Allocator()), shape_(shape), ld_(ld) {
44 data_ = allocator_.allocate(shape_[0] * ld_);
45 }
46
47 dense_matrix(key_type shape, std::size_t ld, const Allocator &alloc)
48 : allocator_(alloc), shape_(shape), ld_(ld) {
49 data_ = allocator_.allocate(shape_[0] * ld_);
50 }
51
53 : allocator_(other.allocator_), data_(other.data_), shape_(other.shape_),
54 ld_(other.ld_) {
55 other.null_data_();
56 }
57
58 dense_matrix &operator=(dense_matrix &&other) {
59 deallocate_storage_();
60 allocator_ = other.allocator_;
61 data_ = other.data_;
62 shape_ = other.shape_;
63 ld_ = other.ld_;
64
65 other.null_data_();
66 }
67
68 dense_matrix(const dense_matrix &other) = delete;
69 dense_matrix &operator=(const dense_matrix &other) = delete;
70
71 ~dense_matrix() { deallocate_storage_(); }
72
73 key_type shape() const noexcept { return shape_; }
74
75 size_type size() const noexcept { return shape()[0] * shape()[1]; }
76
77 scalar_reference operator[](key_type idx) const {
78 return data_[idx[0] * ld_ + idx[1]];
79 }
80
81 iterator begin() const {
82 return iterator(data_, key_type{0, 0}, shape_, ld_);
83 }
84
85 iterator end() const {
86 return iterator(data_, key_type{shape_[0], 0}, shape_, ld_);
87 }
88
89 auto row(size_type row_index) const {
90 // return dense_matrix_row_view(data_ + row_index * ld_, row_index,
91 // shape()[1]);
92 auto row_elements = rng::views::iota(size_type(0), size_type(shape()[1]));
93 scalar_pointer data = data_ + row_index * ld_;
94
95 return row_elements | rng::views::transform([=](auto column_index) {
96 return reference(key_type(row_index, column_index),
97 data[column_index]);
98 });
99 }
100
101 auto column(size_type column_index) const {
102 // return dense_matrix_column_view(data_ + column_index, column_index,
103 // shape()[0], ld_);
104 auto column_elements =
105 rng::views::iota(size_type(0), size_type(shape()[0]));
106 scalar_pointer data = data_ + column_index;
107 size_type ld = ld_;
108
109 return column_elements | rng::views::transform([=](auto row_index) {
110 return reference(key_type(row_index, column_index),
111 data[row_index * ld]);
112 });
113 }
114
115 scalar_pointer data() const { return data_; }
116
117 size_type ld() const { return ld_; }
118
119 /*
120 auto local() const {
121 }
122 */
123
124private:
125 void deallocate_storage_() {
126 if (data_ != nullptr) {
127 allocator_.deallocate(data_, shape_[0] * ld_);
128 }
129 }
130
131 void null_data_() {
132 data_ = nullptr;
133 shape_ = {0, 0};
134 ld_ = 0;
135 }
136
137 allocator_type allocator_;
138 scalar_pointer data_;
139 key_type shape_;
140 size_type ld_;
141};
142
143} // namespace dr::sp
Definition: index.hpp:34
Definition: iterator_adaptor.hpp:23
Definition: matrix_entry.hpp:115
Definition: dense_matrix.hpp:19