Alien  1.3.0
Developer documentation
Loading...
Searching...
No Matches
SuiteSparseArchiveSystemReader.h
1/*
2 * Copyright 2020 IFPEN-CEA
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * SPDX-License-Identifier: Apache-2.0
17 */
18
19#pragma once
20
21#include <arccore/base/ArccoreGlobal.h>
22#include <alien/import_export/Reader.h>
23
24#include <string>
25#include <archive.h>
26#include <archive_entry.h>
27
28namespace Alien
29{
30class Matrix;
31class Vector;
32
33class ALIEN_EXPORT SuiteSparseArchiveSystemReader
34{
35 public:
36 SuiteSparseArchiveSystemReader() = delete;
37 SuiteSparseArchiveSystemReader(SuiteSparseArchiveSystemReader const&) = delete;
38 SuiteSparseArchiveSystemReader& operator=(SuiteSparseArchiveSystemReader const&) = delete;
39
40 explicit SuiteSparseArchiveSystemReader(std::string const& filename);
41 ~SuiteSparseArchiveSystemReader();
42
43 template <typename MatrixT>
44 void readMatrix(MatrixT& A);
45
46 template <typename VectorT>
47 void readVector(VectorT& rhs);
48
49 private:
50 std::string m_filename;
51 archive* m_archive = nullptr;
52};
53
54template <typename MatrixT>
55void SuiteSparseArchiveSystemReader::readMatrix(MatrixT& A)
56{
57 m_archive = archive_read_new();
58 archive_read_support_filter_gzip(m_archive);
59 archive_read_support_format_tar(m_archive);
60
61 auto r = archive_read_open_filename(m_archive, m_filename.c_str(), 10240);
62 if (r != ARCHIVE_OK) {
63 throw FatalErrorException(A_FUNCINFO, "Open archive " + m_filename);
64 }
65
66 // look for matrix in archive
67 bool matrix_found = false;
68 archive_entry* entry = nullptr;
69 auto pos = m_filename.find_last_of('/');
70 std::string matrix_name(m_filename, pos + 1, m_filename.size() - pos - 1 - 7); // .tar.gz
71
72 while (archive_read_next_header(m_archive, &entry) == ARCHIVE_OK) {
73 //std::cout << "entry name :" << archive_entry_pathname(entry) << "\n";
74 if (archive_entry_pathname(entry) == std::string(matrix_name + "/" + matrix_name + ".mtx")) {
75 matrix_found = true;
76 break;
77 }
78 archive_read_data_skip(m_archive);
79 }
80
81 if (!matrix_found) {
82 throw FatalErrorException(A_FUNCINFO, "Matrix not found in " + m_filename);
83 }
84
85 LibArchiveReader reader(m_archive);
86 loadMMMatrixFromReader<MatrixT, LibArchiveReader>(A, reader);
87
88 r = archive_read_close(m_archive);
89 if (r != ARCHIVE_OK) {
90 throw FatalErrorException(A_FUNCINFO, "Close archive " + m_filename);
91 }
92
93 r = archive_free(m_archive);
94 if (r != ARCHIVE_OK) {
95 throw FatalErrorException(A_FUNCINFO, "Free archive");
96 }
97}
98
99template <typename VectorT>
100void SuiteSparseArchiveSystemReader::readVector(VectorT& rhs)
101{
102 m_archive = archive_read_new();
103 archive_read_support_filter_gzip(m_archive);
104 archive_read_support_format_tar(m_archive);
105
106 auto r = archive_read_open_filename(m_archive, m_filename.c_str(), 10240);
107 if (r != ARCHIVE_OK) {
108 throw FatalErrorException(A_FUNCINFO, "Open archive " + m_filename);
109 }
110
111 bool vector_found = false;
112 archive_entry* entry = nullptr;
113 auto pos = m_filename.find_last_of('/');
114 std::string matrix_name(m_filename, pos + 1, m_filename.size() - pos - 1 - 7); // .tar.gz
115
116 while (archive_read_next_header(m_archive, &entry) == ARCHIVE_OK) {
117 //std::cout << "entry name :" << archive_entry_pathname(entry) << "\n";
118 if (archive_entry_pathname(entry) == std::string(matrix_name + "/" + matrix_name + "_b.mtx")) {
119 vector_found = true;
120 break;
121 }
122 archive_read_data_skip(m_archive);
123 }
124
125 if (vector_found) // vector is not always present
126 {
127 LibArchiveReader reader(m_archive);
128 loadMMRhsFromReader<VectorT, LibArchiveReader>(rhs, reader);
129 }
130
131 r = archive_read_close(m_archive);
132 if (r != ARCHIVE_OK) {
133 throw FatalErrorException(A_FUNCINFO, "Close archive " + m_filename);
134 }
135
136 r = archive_free(m_archive);
137 if (r != ARCHIVE_OK) {
138 throw FatalErrorException(A_FUNCINFO, "Free archive");
139 }
140}
141
142} // namespace Alien
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Definition BackEnd.h:17