8#include <dr/views/csr_matrix_view.hpp>
11#include <unordered_set>
17template <
typename T>
struct uniform_distribution {
18 using type = std::uniform_int_distribution<T>;
21template <std::
floating_po
int T>
struct uniform_distribution<T> {
22 using type = std::uniform_real_distribution<T>;
26using uniform_distribution_t =
typename uniform_distribution<T>::type;
29 template <std::
integral I>
30 inline std::size_t operator()(
const std::pair<I, I> &v)
const {
31 return v.first * 31 + v.second;
40template <
typename T =
float, std::
integral I = std::
size_t>
41auto generate_random_csr(
dr::index<I> shape,
double density = 0.01,
42 unsigned int seed = 0) {
44 assert(density >= 0.0 && density < 1.0);
46 std::unordered_set<std::pair<I, I>, pair_hash> tuples{};
47 std::vector<std::pair<std::pair<I, I>, T>> entries;
48 std::size_t nnz = density * shape[0] * shape[1];
51 std::mt19937 gen(seed);
52 std::uniform_int_distribution<I> row(0, shape[0] - 1);
53 std::uniform_int_distribution<I> column(0, shape[1] - 1);
55 uniform_distribution_t<T> value_gen(0, 1);
57 while (tuples.size() < nnz) {
60 if (tuples.find({i, j}) == tuples.end()) {
61 T value = value_gen(gen);
62 tuples.insert({i, j});
63 entries.push_back({{i, j}, value});
66 T *values =
new T[nnz];
67 I *rowptr =
new I[shape[0] + 1];
68 I *colind =
new I[nnz];
74 std::sort(entries.begin(), entries.end());
75 for (
auto iter = entries.begin(); iter != entries.end(); ++iter) {
76 auto &&[index, value] = *iter;
77 auto &&[i, j] = index;
83 if (r + 1 > shape[0]) {
98 for (; r < shape[0]; r++) {
108template <
typename T =
float, std::
integral I = std::
size_t>
109auto generate_band_csr(I size, std::size_t up_band = 3,
110 std::size_t down_band = 3) {
111 std::size_t nnz = (1 + up_band + down_band) * size -
112 (up_band * (up_band + 1) / 2) -
113 (down_band * (down_band + 1) / 2);
115 T *values =
new T[nnz];
116 I *rowptr =
new I[size + 1];
117 I *colind =
new I[nnz];
123 for (
auto i = 0; i < size; i++) {
124 for (
auto j = std::max(
static_cast<long long>(i) -
125 static_cast<long long>(down_band),
126 static_cast<long long>(0));
129 colind[c] =
static_cast<I
>(j);
135 for (
auto j = i + 1; j <= i + up_band; j++) {
147 for (; r < size; r++) {
Definition: csr_matrix_view.hpp:126