Distributed Ranges
Loading...
Searching...
No Matches
matrix_io.hpp
1// SPDX-FileCopyrightText: Intel Corporation
2//
3// SPDX-License-Identifier: BSD-3-Clause
4
5#pragma once
6
7#include <algorithm>
8#include <cassert>
9#include <fstream>
10#include <sstream>
11#include <string>
12#include <tuple>
13#include <vector>
14
15#include <dr/detail/matrix_io.hpp>
16#include <dr/views/csr_matrix_view.hpp>
17
18namespace dr::sp {
19
20template <typename T, typename I>
21auto create_distributed(dr::views::csr_matrix_view<T, I> local_mat,
22 const matrix_partition &partition) {
23 dr::sp::sparse_matrix<T, I> a(local_mat.shape(), partition);
24
25 std::vector<dr::views::csr_matrix_view<T, I>> views;
26 std::vector<sycl::event> events;
27 views.reserve(a.grid_shape()[0] * a.grid_shape()[1]);
28
29 for (I i = 0; i < a.grid_shape()[0]; i++) {
30 for (I j = 0; j < a.grid_shape()[1]; j++) {
31 auto &&tile = a.tile({i, j});
32 dr::index<I> row_bounds(i * a.tile_shape()[0],
33 i * a.tile_shape()[0] + tile.shape()[0]);
34 dr::index<I> column_bounds(j * a.tile_shape()[1],
35 j * a.tile_shape()[1] + tile.shape()[1]);
36
37 auto local_submat = local_mat.submatrix(row_bounds, column_bounds);
38
39 auto submatrix_shape = dr::index<I>(row_bounds[1] - row_bounds[0],
40 column_bounds[1] - column_bounds[0]);
41
42 auto copied_submat = dr::__detail::convert_to_csr(
43 local_submat, submatrix_shape, rng::distance(local_submat),
44 std::allocator<T>{});
45
46 auto e = a.copy_tile_async({i, j}, copied_submat);
47
48 views.push_back(copied_submat);
49 events.push_back(e);
50 }
51 }
52 __detail::wait(events);
53
54 for (auto &&view : views) {
55 dr::__detail::destroy_csr_matrix_view(view, std::allocator<T>{});
56 }
57
58 return a;
59}
60
61template <typename T, typename I>
62auto create_distributed(dr::views::csr_matrix_view<T, I> local_mat) {
63 return create_distributed(
64 local_mat, dr::sp::block_cyclic({dr::sp::tile::div, dr::sp::tile::div},
65 {dr::sp::nprocs(), 1}));
66}
67
68template <typename T, typename I = std::size_t>
69auto mmread(std::string file_path, const matrix_partition &partition,
70 bool one_indexed = true) {
71 auto local_mat = read_csr<T, I>(file_path, one_indexed);
72
73 auto a = create_distributed(local_mat, partition);
74
75 dr::__detail::destroy_csr_matrix_view(local_mat, std::allocator<T>{});
76
77 return a;
78}
79
80template <typename T, typename I = std::size_t>
81auto mmread(std::string file_path, bool one_indexed = true) {
82 return mmread<T, I>(file_path, dr::sp::row_cyclic(), one_indexed);
83}
84
85} // namespace dr::sp
Definition: index.hpp:34
Definition: matrix_partition.hpp:34
Definition: sparse_matrix.hpp:135
Definition: csr_matrix_view.hpp:126