File: mm2bin.cpp

package info (click to toggle)
amgcl 1.4.4-1
  • links: PTS, VCS
  • area: contrib
  • in suites: sid
  • size: 5,676 kB
  • sloc: cpp: 34,043; python: 747; pascal: 258; f90: 196; makefile: 20
file content (82 lines) | stat: -rw-r--r-- 2,546 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
#include <string>
#include <complex>

#include <boost/program_options.hpp>
#include <amgcl/util.hpp>
#include <amgcl/value_type/complex.hpp>
#include <amgcl/io/mm.hpp>
#include <amgcl/io/binary.hpp>

namespace io = amgcl::io;
namespace po = boost::program_options;
using amgcl::precondition;

//---------------------------------------------------------------------------
template <class T>
void convert(amgcl::io::mm_reader &ifile, const std::string &ofile) {
    std::ofstream f(ofile, std::ios::binary);
    precondition(f, "Failed to open output file for writing.");

    if (ifile.is_sparse()) {
        size_t rows, cols;
        std::vector<ptrdiff_t> ptr, col;
        std::vector<T> val;

        std::tie(rows, cols) = ifile(ptr, col, val);

        precondition(io::write(f, rows), "File I/O error.");
        precondition(io::write(f, ptr),  "File I/O error.");
        precondition(io::write(f, col),  "File I/O error.");
        precondition(io::write(f, val),  "File I/O error.");

        std::cout
            << "Wrote " << rows << " by " << cols << " sparse matrix, "
            << ptr.back() << " nonzeros" << std::endl;
    } else {
        size_t rows, cols;
        std::vector<T> val;

        std::tie(rows, cols) = ifile(val);

        precondition(io::write(f, rows), "File I/O error.");
        precondition(io::write(f, cols), "File I/O error.");
        precondition(io::write(f, val),  "File I/O error.");

        std::cout
            << "Wrote " << rows << " by " << cols << " dense matrix"
            << std::endl;
    }
}

//---------------------------------------------------------------------------
int main(int argc, char *argv[]) {
    po::options_description desc("Options");

    desc.add_options()
        ("help,h", "Show this help.")
        ("input,i", po::value<std::string>()->required(),
             "Input matrix in the MatrixMarket format.")
        ("output,o", po::value<std::string>()->required(),
             "Output binary file.")
        ;

    po::variables_map vm;
    po::store(po::parse_command_line(argc, argv, desc), vm);

    if (vm.count("help")) {
        std::cout << desc << std::endl;
        return 0;
    }

    po::notify(vm);

    io::mm_reader read(vm["input"].as<std::string>());
    precondition(!read.is_integer(), "Integer matrices are not supported!");

    if (read.is_complex()) {
        convert<std::complex<double>>(read, vm["output"].as<std::string>());
    } else {
        convert<double>(read, vm["output"].as<std::string>());
    }
}