File size: 2,164 Bytes
21f3d42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
 * Copyright 2021 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

// Low-level array reading function using std::ifstream.

#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();
}

}  // namespace detail
}  // namespace csrblocksparse

#endif  // LYRA_CODEC_SPARSE_MATMUL_LAYERS_READ_ARRAY_IFSTREAM_H_