From 59a081a04e2e22dcc1d77cd636e2619d5be59d6a Mon Sep 17 00:00:00 2001 From: Carl Pearson Date: Fri, 30 Jul 2021 17:42:09 -0600 Subject: [PATCH] matrix market reader post --- content/post/20210730-mm/index.md | 105 ++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 content/post/20210730-mm/index.md diff --git a/content/post/20210730-mm/index.md b/content/post/20210730-mm/index.md new file mode 100644 index 0000000..54f423f --- /dev/null +++ b/content/post/20210730-mm/index.md @@ -0,0 +1,105 @@ ++++ +title = "Single-header C++ Matrix Market Reader" +date = 2021-07-30T00:00:00 +lastmod = 2021-07-30T00:00:00 +draft = false + +# Authors. Comma separated list, e.g. `["Bob Smith", "David Jones"]`. +authors = ["Carl Pearson"] + +tags = ["sparse"] + +summary = "GPLv3 single-header C++11 Matrix Market Reader" + +# Projects (optional). +# Associate this post with one or more of your projects. +# Simply enter your project's folder or file name without extension. +# E.g. `projects = ["deep-learning"]` references +# `content/project/deep-learning/index.md`. +# Otherwise, set `projects = []`. +projects = [] + +# Featured image +# To use, add an image named `featured.jpg/png` to your project's folder. +[image] + # Caption (optional) + caption = "" + + # Focal point (optional) + # Options: Smart, Center, TopLeft, Top, TopRight, Left, Right, BottomLeft, Bottom, BottomRight + focal_point = "Center" + + # Show image only in page previews? + preview_only = true + + +categories = [] + +# Set captions for image gallery. + + ++++ + +I'm continually re-writing some C++ code to read [Matrix Market files](https://math.nist.gov/MatrixMarket/formats.html), a common format for distributing matrices. +I finally got around to writing something that I should be able to reuse. + +The code is [available on github](github.com/cwpearson/matrix-market). + +An example follows: + +```c++ +#include "mm/mm.hpp" + +int main(int argc, char **argv) +{ + if (argc < 2) + { + std::cerr << "Usage: " << argv[0] << " \n"; + exit(EXIT_FAILURE); + } + const std::string path = argv[1]; + + { + std::cout << "with Ordinal=int, Scalar=float, Offset=size_t" << std::endl; + typedef int Ordinal; + typedef float Scalar; + typedef MtxReader reader_t; + typedef typename reader_t::coo_type coo_t; + typedef typename coo_t::entry_type entry_t; + + // read matrix as coo + reader_t reader(path); + coo_t coo = reader.read_coo(); + + // non-zeros, rows, cols + std::cout << coo.nnz() << std::endl; // size_t + std::cout << coo.num_rows() << "," << coo.num_cols() << std::endl; // int + + // first entry + entry_t e = coo.entries[0]; + std::cout << e.i << "," << e.j << std::endl; // int, int + std::cout << e.e << std::endl; // float + } + + { + std::cout << "with Ordinal=int64_t, Scalar=std::complex, Offset=int" << std::endl; + typedef MtxReader, int> reader_t; + typedef typename reader_t::coo_type coo_t; + typedef typename coo_t::entry_type entry_t; + + // read matrix as coo + reader_t reader(path); + coo_t coo = reader.read_coo(); + + // non-zeros, rows, cols + std::cout << coo.nnz() << std::endl; // int + std::cout << coo.num_rows() << "," << coo.num_cols() << std::endl; // complex + + // first entry + entry_t e = coo.entries[0]; + std::cout << e.i << "," << e.j << std::endl; // int64_t + std::cout << e.e << std::endl; // complex + } + return 0; +} +``` \ No newline at end of file