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