pytorch/caffe2/quantization/server/mmio.h
Richard Barnes 1622546050 use irange for loops (#70248)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/70248

Modified loops in files under fbsource/fbcode/caffe2/ from the format
```
for(TYPE var=x0;var<x_max;x++)
```
to the format
```
for(const auto var: irange(xmax))
```

This was achieved by running r-barnes's loop upgrader script (D28874212) with some modification to exclude all files under /torch/jit and a number of reversions or unused variable suppression warnings added by hand.

Test Plan: Sandcastle

Reviewed By: malfet

Differential Revision: D32813863

fbshipit-source-id: 527244b4a2b220fdfe7f17dee3599603f492a2ca
2022-01-06 23:14:29 -08:00

55 lines
1.3 KiB
C++

#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 (const auto j : c10::irange(n)) {
for (const auto i : c10::irange(m)) {
if (is_integral<T>::value) {
// NOLINTNEXTLINE(clang-analyzer-core.NullDereference)
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