template <typename T>
slate::Matrix<T> read_binary(std::string& input_file,
int64_t* n_ptr,
int64_t nb = 100,
int64_t p = 0,
int64_t q = 0,
bool transpose = false)
{
// Input file is of form filename.bin
// finename.bin stores a binary file of floats
// Matrix dimensions stored as rows cols in filename.dim
// Need to remove .bin and replace with .dim
std::string dim_file = input_file.substr(0, input_file.find_last_of(".")) + ".dim";
// Get the number of rows and columns to allocate memory
std::ifstream file(dim_file);
std::string line;
int64_t rows = 0;
int64_t cols = 0;
std::getline(file, line);
std::istringstream iss(line);
iss >> rows >> cols;
// Set n_ptr to final number of rows
if (transpose) {
*n_ptr = cols;
} else {
*n_ptr = rows;
}
// Close .dim file
file.close();
// Open binary file
std::ifstream bin_file(input_file, std::ios::binary);
// Allocate memory for the matrix
slate::Matrix<T> X( rows, cols, nb, p, q, MPI_COMM_WORLD );
X.insertLocalTiles();
// Read in the data
for (int64_t j = 0; j < X.nt(); ++j) {
for (int64_t i = 0; i < X.mt(); ++i) {
if (X.tileIsLocal( i, j )) {
try {
auto Atile = X( i, j );
auto A = Atile.data();
int64_t start_row = i * nb;
int64_t start_col = j * nb;
int64_t end_row = std::min(start_row + nb, rows);
int64_t end_col = std::min(start_col + nb, cols);
auto n = end_row - start_row;
auto m = end_col - start_col;
for (int64_t k = 0; k < n; ++k) {
bin_file.seekg(( cols * (start_row + k) + start_col ) * sizeof(float));
bin_file.read((char*)&A[k*m], m * sizeof(float));
}
}
catch (...) {
// ignore missing tiles
}
// Reset file
bin_file.clear();
bin_file.seekg(0, std::ios::beg);
}
}
}
// Close binary file
bin_file.close();
// If transpose, return the transpose
if (transpose) {
return slate::transpose( X );
} else {
return X;
}
}