File: mmio.h

package info (click to toggle)
pytorch 1.7.1-7
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 80,340 kB
  • sloc: cpp: 670,830; python: 343,991; ansic: 67,845; asm: 5,503; sh: 2,924; java: 2,888; xml: 266; makefile: 244; ruby: 148; yacc: 144; objc: 51; lex: 44
file content (53 lines) | stat: -rw-r--r-- 1,254 bytes parent folder | download
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
#pragma once

#include <cstdio>
#include <set>
#include <string>
#include <type_traits>

namespace caffe2 {

template <typename T>
void StoreMatrixInMatrixMarketFormat(
    int m,
    int n,
    const T* a,
    const std::string& matrix_name) {
  using namespace std;
  static set<string> dumped_matrix_names;

  string name(matrix_name);
  string::size_type pos = name.rfind('/');
  if (pos != string::npos) {
    name = name.substr(pos + 1);
  }
  if (dumped_matrix_names.find(name) == dumped_matrix_names.end()) {
    dumped_matrix_names.insert(name);

    FILE* fp = fopen((matrix_name + ".mtx").c_str(), "w");
    if (!fp) {
      return;
    }

    if (is_integral<T>::value) {
      fprintf(fp, "%%%%MatrixMarket matrix array integer general\n");
    } else {
      fprintf(fp, "%%%%MatrixMarket matrix array real general\n");
    }
    fprintf(fp, "%d %d\n", m, n);
    // matrix market array format uses column-major order
    for (int j = 0; j < n; ++j) {
      for (int i = 0; i < m; ++i) {
        if (is_integral<T>::value) {
          fprintf(fp, "%d\n", static_cast<int>(a[j * m + i]));
        } else {
          fprintf(fp, "%f\n", static_cast<float>(a[j * m + i]));
        }
      }
    }

    fclose(fp);
  }
}

} // namespace caffe2