ddemidov / vexcl
1
#ifndef TESTS_RANDOM_MATRIX_HPP
2
#define TESTS_RANDOM_MATRIX_HPP
3

4
#include <vector>
5
#include <set>
6
#include "random_vector.hpp"
7

8
template <typename RT, typename CT, typename VT>
9
void random_matrix(size_t n, size_t m, size_t nnz_per_row,
10
        std::vector<RT> &row,
11
        std::vector<CT> &col,
12
        std::vector<VT> &val
13
        )
14
{
15 6
    row.clear();
16 6
    col.clear();
17

18 6
    row.reserve(n + 1);
19 6
    col.reserve(nnz_per_row * n);
20

21 6
    std::default_random_engine rng( std::rand() );
22 6
    std::uniform_int_distribution<size_t> random_width(0, nnz_per_row - 1);
23 6
    std::uniform_int_distribution<size_t> random_column(0, m - 1);
24

25 6
    row.push_back(0);
26 6
    for(size_t k = 0; k < n; k++) {
27 6
        size_t width = random_width(rng);
28

29 6
        std::set<CT> cs;
30 6
        while(cs.size() < width)
31 6
            cs.insert(static_cast<CT>(random_column(rng)));
32

33 6
        for(auto c = cs.begin(); c != cs.end(); c++)
34 6
            col.push_back(*c);
35

36 6
        row.push_back(static_cast<RT>(col.size()));
37
    }
38

39 6
    random_vector<VT>( col.size() ).swap(val);
40
}
41

42
#endif

Read our documentation on viewing source code .

Loading