Arcane  4.2.3.0
Developer documentation
Loading...
Searching...
No Matches
DistributedTranspose.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/*---------------------------------------------------------------------------*/
9/*
10 * This file is based on the work on AMGCL library (version march 2026)
11 * which can be found at https://github.com/ddemidov/amgcl.
12 *
13 * Copyright (c) 2012-2022 Denis Demidov <dennis.demidov@gmail.com>
14 * SPDX-License-Identifier: MIT
15 */
16/*---------------------------------------------------------------------------*/
17/*---------------------------------------------------------------------------*/
18
19#include <iostream>
20#include <vector>
21
22#include "arccore/alina/BuiltinBackend.h"
23#include "arccore/alina/Adapters.h"
24#include "arccore/alina/DistributedMatrix.h"
25#include "arccore/alina/IO.h"
26#include "arccore/alina/Profiler.h"
27
28using namespace Arcane;
29
30int main(int argc, char* argv[])
31{
32 auto mpm = MessagePassing::Mpi::StandaloneMpiMessagePassingMng::createAndInitializeMPI(&argc, &argv, MPI_THREAD_MULTIPLE);
34
35 int n = 16;
36 int chunk_len = (n + comm.size - 1) / comm.size;
37 int chunk_beg = std::min(n, chunk_len * comm.rank);
38 int chunk_end = std::min(n, chunk_len * (comm.rank + 1));
39 int chunk = chunk_end - chunk_beg;
40
41 std::vector<int> ptr;
42 ptr.reserve(chunk + 1);
43 ptr.push_back(0);
44 std::vector<int> col;
45 col.reserve(chunk * 4);
46 std::vector<double> val;
47 val.reserve(chunk * 4);
48
49 for (int i = 0, j = chunk_beg; i < chunk; ++i, ++j) {
50 if (j > 0) {
51 col.push_back(j - 1);
52 val.push_back(-1);
53 }
54
55 col.push_back(j);
56 val.push_back(2);
57
58 if (j + 1 < n) {
59 col.push_back(j + 1);
60 val.push_back(-1);
61 }
62
63 if (j + 5 < n) {
64 col.push_back(j + 5);
65 val.push_back(-0.1);
66 }
67
68 ptr.push_back(col.size());
69 }
70
71 typedef Alina::BuiltinBackend<double> Backend;
73
74 Matrix A(comm, std::tie(chunk, ptr, col, val), chunk);
75
76 {
77 std::ostringstream fname;
78 fname << "A_loc_" << comm.rank << ".mtx";
79 Alina::IO::mm_write(fname.str(), *A.local());
80 }
81
82 {
83 std::ostringstream fname;
84 fname << "A_rem_" << comm.rank << ".mtx";
85 Alina::IO::mm_write(fname.str(), *A.remote());
86 }
87
88 auto B = transpose(A);
89
90 {
91 std::ostringstream fname;
92 fname << "B_loc_" << comm.rank << ".mtx";
93 Alina::IO::mm_write(fname.str(), *B->local());
94 }
95
96 {
97 std::ostringstream fname;
98 fname << "B_rem_" << comm.rank << ".mtx";
99 Alina::IO::mm_write(fname.str(), *B->remote());
100 }
101}
Distributed Matrix using message passing.
Matrix class, to be used by user.
static Ref< IMessagePassingMng > createAndInitializeMPI(int *argc, char **argv[], int wanted_thread_level)
Initialize MPI and creates a manager associated with the MPI_WORLD_COMM communicator.
__host__ __device__ Real3x3 transpose(const Real3x3 &t)
Transpose the matrix.
Definition MathUtils.h:265
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Convenience wrapper around MPI_Comm.