|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef LYRA_CODEC_SPARSE_MATMUL_LAYERS_READ_ARRAY_IFSTREAM_H_ |
|
#define LYRA_CODEC_SPARSE_MATMUL_LAYERS_READ_ARRAY_IFSTREAM_H_ |
|
|
|
#include <cstdint> |
|
#include <fstream> |
|
#include <sstream> |
|
#include <string> |
|
|
|
#include "absl/status/status.h" |
|
#include "absl/strings/substitute.h" |
|
#include "include/ghc/filesystem.hpp" |
|
|
|
namespace csrblocksparse { |
|
namespace detail { |
|
|
|
template <typename T> |
|
absl::Status ReadArrayIfstream(const std::string& file_name, |
|
const std::string& path, std::vector<T>* array, |
|
int64_t* length) { |
|
ghc::filesystem::path complete_path(path); |
|
complete_path /= file_name; |
|
std::ifstream in_stream(complete_path.u8string(), std::ios::binary); |
|
if (!in_stream.is_open()) { |
|
return absl::UnknownError( |
|
absl::Substitute("Error opening $0", complete_path.string())); |
|
} |
|
|
|
std::stringstream buffer; |
|
buffer << in_stream.rdbuf(); |
|
if (buffer.str().empty()) { |
|
LOG(ERROR) << "File " << complete_path << " was empty."; |
|
return absl::UnknownError( |
|
absl::Substitute("File $0 was empty", complete_path.string())); |
|
} |
|
std::string contents = buffer.str(); |
|
*length = contents.length(); |
|
int64_t elem = (*length + sizeof(T) - 1) / sizeof(T); |
|
array->resize(elem); |
|
std::move(contents.begin(), contents.end(), |
|
reinterpret_cast<char*>(array->data())); |
|
|
|
return absl::OkStatus(); |
|
} |
|
|
|
} |
|
} |
|
|
|
#endif |
|
|