Arcane  4.2.1.0
Developer documentation
Loading...
Searching...
No Matches
MatrixMarkerToBinary.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 <string>
21#include <complex>
22
23#include "arccore/alina/AlinaUtils.h"
24#include "arccore/alina/ValueTypeComplex.h"
25#include "arccore/alina/IO.h"
26
27#include "arccore/common/internal/ProgramOptions.h"
28
29using namespace Arcane;
30namespace io = Alina::IO;
31namespace po = Arcane::ProgramOptions;
32using Alina::precondition;
33
34//---------------------------------------------------------------------------
35template <class T>
36void convert(Alina::IO::mm_reader& ifile, const std::string& ofile)
37{
38 std::ofstream f(ofile, std::ios::binary);
39 precondition(f, "Failed to open output file for writing.");
40
41 if (ifile.is_sparse()) {
42 size_t rows, cols;
43 std::vector<ptrdiff_t> ptr, col;
44 std::vector<T> val;
45
46 std::tie(rows, cols) = ifile(ptr, col, val);
47
48 precondition(io::write(f, rows), "File I/O error.");
49 precondition(io::write(f, ptr), "File I/O error.");
50 precondition(io::write(f, col), "File I/O error.");
51 precondition(io::write(f, val), "File I/O error.");
52
53 std::cout
54 << "Wrote " << rows << " by " << cols << " sparse matrix, "
55 << ptr.back() << " nonzeros" << std::endl;
56 }
57 else {
58 size_t rows, cols;
59 std::vector<T> val;
60
61 std::tie(rows, cols) = ifile(val);
62
63 precondition(io::write(f, rows), "File I/O error.");
64 precondition(io::write(f, cols), "File I/O error.");
65 precondition(io::write(f, val), "File I/O error.");
66
67 std::cout
68 << "Wrote " << rows << " by " << cols << " dense matrix"
69 << std::endl;
70 }
71}
72
73//---------------------------------------------------------------------------
74int main(int argc, char* argv[])
75{
76 po::options_description desc("Options");
77
78 desc.add_options()("help,h", "Show this help.")("input,i", po::value<std::string>()->required(),
79 "Input matrix in the MatrixMarket format.")("output,o", po::value<std::string>()->required(),
80 "Output binary file.");
81
83 po::store(po::parse_command_line(argc, argv, desc), vm);
84
85 if (vm.count("help")) {
86 std::cout << desc << std::endl;
87 return 0;
88 }
89
90 po::notify(vm);
91
92 io::mm_reader read(vm["input"].as<std::string>());
93 precondition(!read.is_integer(), "Integer matrices are not supported!");
94
95 if (read.is_complex()) {
96 convert<std::complex<double>>(read, vm["output"].as<std::string>());
97 }
98 else {
99 convert<double>(read, vm["output"].as<std::string>());
100 }
101}
Matrix market reader.
Definition IO.h:54
bool is_sparse() const
Matrix in the file is sparse.
Definition IO.h:127
Describes a set of command-line options.
Stores parsed option values.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --