1
+ /* *
2
+ * @file coo.hxx
3
+ * @author Muhammad Osama (mosama@ucdavis.edu)
4
+ * @brief Interface for Coordinate format.
5
+ * @version 0.1
6
+ * @date 2022-07-21
7
+ *
8
+ * @copyright Copyright (c) 2022
9
+ *
10
+ */
11
+
1
12
#pragma once
2
13
14
+ #include < loops/container/formats.hxx>
15
+ #include < loops/container/detail/convert.hxx>
3
16
#include < loops/container/vector.hxx>
4
17
#include < loops/memory.hxx>
5
18
6
- #include < thrust/execution_policy.h>
7
- #include < thrust/iterator/zip_iterator.h>
8
19
#include < thrust/sort.h>
9
20
#include < thrust/tuple.h>
21
+ #include < thrust/unique.h>
22
+ #include < thrust/execution_policy.h>
23
+ #include < thrust/iterator/zip_iterator.h>
10
24
11
25
namespace loops {
12
26
@@ -30,8 +44,19 @@ struct coo_t {
30
44
vector_t <index_t , space> col_indices; // / J
31
45
vector_t <value_t , space> values; // / V
32
46
47
+ /* *
48
+ * @brief Construct a new coo object with everything initialized to zero.
49
+ *
50
+ */
33
51
coo_t () : rows(0 ), cols(0 ), nnzs(0 ), row_indices(), col_indices(), values() {}
34
52
53
+ /* *
54
+ * @brief Construct a new coo object with the given dimensions.
55
+ *
56
+ * @param r Number of rows.
57
+ * @param c Number of columns.
58
+ * @param nnz Number of non-zero elements.
59
+ */
35
60
coo_t (std::size_t r, std::size_t c, std::size_t nnz)
36
61
: rows(r),
37
62
cols (c),
@@ -40,6 +65,11 @@ struct coo_t {
40
65
col_indices(nnz),
41
66
values(nnz) {}
42
67
68
+ /* *
69
+ * @brief Construct a new coo from another coo object on host/device.
70
+ *
71
+ * @param rhs coo_t<index_t, value_t, rhs_space>
72
+ */
43
73
template <auto rhs_space>
44
74
coo_t (const coo_t <index_t , value_t , rhs_space>& rhs)
45
75
: rows(rhs.rows),
@@ -49,6 +79,28 @@ struct coo_t {
49
79
col_indices(rhs.col_indices),
50
80
values(rhs.values) {}
51
81
82
+ /* *
83
+ * @brief Construct a new coo object from compressed sparse format (CSR).
84
+ *
85
+ * @param csr csr_t<index_t, offset_t, value_t, rhs_space>
86
+ */
87
+ template <auto rhs_space, typename offset_t >
88
+ coo_t (const csr_t <index_t , offset_t , value_t , rhs_space>& csr)
89
+ : rows(csr.rows),
90
+ cols(csr.cols),
91
+ nnzs(csr.nnzs),
92
+ row_indices(csr.nnzs),
93
+ col_indices(csr.col_indices),
94
+ values(csr.values) {
95
+ // / TODO: Do not need this copy for all cases.
96
+ vector_t <offset_t , space> _row_offsets = csr.offsets ;
97
+ detail::offsets_to_indices (_row_offsets, row_indices);
98
+ }
99
+
100
+ /* *
101
+ * @brief Sorts the coordinate matrix by row indices.
102
+ *
103
+ */
52
104
void sort_by_row () {
53
105
auto begin = thrust::make_zip_iterator (
54
106
thrust::make_tuple (row_indices.begin (), col_indices.begin ()));
@@ -57,6 +109,10 @@ struct coo_t {
57
109
sort (begin, end);
58
110
}
59
111
112
+ /* *
113
+ * @brief Sorts the coordinate matrix by column indices.
114
+ *
115
+ */
60
116
void sort_by_column () {
61
117
auto begin = thrust::make_zip_iterator (
62
118
thrust::make_tuple (col_indices.begin (), row_indices.begin ()));
@@ -65,7 +121,44 @@ struct coo_t {
65
121
sort (begin, end);
66
122
}
67
123
124
+ /* *
125
+ * @brief Sorts, removes I,J pairs.
126
+ *
127
+ */
128
+ void remove_duplicates () {
129
+ // Sort by row indices.
130
+ this ->sort_by_row ();
131
+ auto begin = thrust::make_zip_iterator (
132
+ thrust::make_tuple (row_indices.begin (), col_indices.begin ()));
133
+ auto end = thrust::make_zip_iterator (
134
+ thrust::make_tuple (row_indices.end (), col_indices.end ()));
135
+
136
+ // Remove duplicates.
137
+ auto new_it = thrust::unique_by_key (begin, end, values.begin ());
138
+ auto first_it = thrust::get<1 >(new_it);
139
+ nnzs = thrust::distance (values.begin (), first_it);
140
+
141
+ // Resize vectors to new size.
142
+ row_indices.resize (nnzs);
143
+ col_indices.resize (nnzs);
144
+ values.resize (nnzs);
145
+ }
146
+
68
147
private:
148
+ /* *
149
+ * @brief Sorting helper, uses zip iterator as begin and end.
150
+ *
151
+ * @par Example Zip iterator:
152
+ * auto begin = thrust::make_zip_iterator(
153
+ * thrust::make_tuple(col_indices.begin(), row_indices.begin()));
154
+ * auto end = thrust::make_zip_iterator(
155
+ * thrust::make_tuple(col_indices.end(), row_indices.end()));
156
+ *
157
+ * @tparam begin_it_t Begin iterator type.
158
+ * @tparam end_it_t End iterator type.
159
+ * @param begin Begin iterator (zip iterator of row and col indices).
160
+ * @param end End iterator (zip iterator of row and col indices).
161
+ */
69
162
template <typename begin_it_t , typename end_it_t >
70
163
void sort (begin_it_t & begin, end_it_t & end) {
71
164
thrust::sort_by_key (begin, end, values.begin ());
0 commit comments