Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export compressed model file #4305

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions ortools/base/gzipfile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,51 @@ File* GZipFileReader(const absl::string_view name, File* file,
LOG(INFO) << "not implemented";
return nullptr;
}

absl::Status WriteToGzipFile(const absl::string_view filename,
const absl::string_view contents) {
// 1 for fastest compression.
gzFile gz_file = gzopen(filename.data(), "w1");
if (gz_file == Z_NULL) {
return {absl::StatusCode::kInternal, "Unable to open file"};
}

// have to write in chunks, otherwise fails to compress big files.
constexpr size_t chunk = 1024 * 1024; // 1 MB chunk size.
size_t remaining = contents.size();
const char* chunk_start_point = contents.data();

while (remaining > 0) {
size_t current_chunk = std::min(chunk, remaining);
size_t written = gzwrite(gz_file, chunk_start_point, current_chunk);
if (written != current_chunk) {
int err_no = 0;
absl::string_view error_message = gzerror(gz_file, &err_no);
gzclose(gz_file);
return {
absl::StatusCode::kInternal,
absl::StrCat(
"Error while writing chunk to compressed file:",
error_message
)
};
}

chunk_start_point += current_chunk;
remaining -= current_chunk;
}

if (const int status = gzclose(gz_file); status != Z_OK) {
int err_no;
absl::string_view error_message = gzerror(gz_file, &err_no);
return {
absl::StatusCode::kInternal,
absl::StrCat(
"Unable to close the file:",
error_message
)
};
}

return absl::OkStatus();
}
6 changes: 6 additions & 0 deletions ortools/base/gzipfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#ifndef OR_TOOLS_BASE_GZIPFILE_H_
#define OR_TOOLS_BASE_GZIPFILE_H_

#include "absl/status/status.h"
#include "absl/strings/string_view.h"
#include "ortools/base/basictypes.h" // for Ownership enum
#include "zlib.h" // for Z_DEFAULT_COMPRESSION
Expand Down Expand Up @@ -65,4 +66,9 @@ inline File* GZipFileReader(absl::string_view name, File* compressed_file,
AppendedStreams::kConcatenateStreams);
}

// Write contents into a gzip compressed file with the lowest compression level
// which is the fastest.
absl::Status WriteToGzipFile(absl::string_view filename,
absl::string_view contents);

#endif // OR_TOOLS_BASE_GZIPFILE_H_
5 changes: 3 additions & 2 deletions ortools/linear_solver/csharp/linear_solver.i
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ CONVERT_VECTOR(operations_research::MPVariable, MPVariable)
%unignore operations_research::MPSolver::ExportModelAsLpFormat(bool);
%unignore operations_research::MPSolver::ExportModelAsMpsFormat(bool, bool);
%unignore operations_research::MPSolver::WriteModelToMpsFile(
const std::string& filename, bool, bool);
const std::string& filename, bool, bool, bool);
%unignore operations_research::MPSolver::SetHint(
const std::vector<operations_research::MPVariable*>&,
const std::vector<double>&);
Expand All @@ -190,9 +190,10 @@ CONVERT_VECTOR(operations_research::MPVariable, MPVariable)
}

bool WriteModelToMpsFile(const std::string& filename, bool fixed_format,
bool obfuscated) {
bool obfuscated, bool compress=false) {
operations_research::MPModelExportOptions options;
options.obfuscate = obfuscated;
options.use_gzip_compression = compress;
operations_research::MPModelProto model;
$self->ExportModelToProto(&model);
return WriteModelToMpsFile(filename, model, options).ok();
Expand Down
3 changes: 2 additions & 1 deletion ortools/linear_solver/java/linear_solver.i
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,10 @@ PROTO2_RETURN(
* Write the loaded model to file in MPS format.
*/
bool writeModelToMpsFile(const std::string& filename, bool fixed_format,
bool obfuscate) {
bool obfuscate, bool compress=false) {
operations_research::MPModelExportOptions options;
options.obfuscate = obfuscate;
options.use_gzip_compression = compress;
operations_research::MPModelProto model;
$self->ExportModelToProto(&model);
return WriteModelToMpsFile(filename, model, options).ok();
Expand Down
6 changes: 6 additions & 0 deletions ortools/linear_solver/model_exporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "absl/strings/str_format.h"
#include "absl/strings/string_view.h"
#include "ortools/base/helpers.h"
#include "ortools/base/gzipfile.h"
#include "ortools/base/logging.h"
#include "ortools/base/options.h"
#include "ortools/base/status_macros.h"
Expand Down Expand Up @@ -253,6 +254,11 @@ absl::Status WriteModelToMpsFile(absl::string_view filename,
const MPModelExportOptions& options) {
ASSIGN_OR_RETURN(std::string mps_data,
ExportModelAsMpsFormat(model, options));

if (options.use_gzip_compression) {
return WriteToGzipFile(filename, mps_data);
}

return file::SetContents(filename, mps_data, file::Defaults());
}

Expand Down
6 changes: 6 additions & 0 deletions ortools/linear_solver/model_exporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ struct MPModelExportOptions {
* was chosen so that SCIP can read the files.
*/
int max_line_length = 10000;

/**
* For .mps files only. Decides whether to use gzip compression when exporting
* models to files.
*/
bool use_gzip_compression = false;
};

/**
Expand Down
3 changes: 2 additions & 1 deletion ortools/linear_solver/python/linear_solver.i
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,10 @@ from ortools.linear_solver.python.linear_solver_natural_api import VariableExpr
}

bool WriteModelToMpsFile(const std::string& filename, bool fixed_format,
bool obfuscate) {
bool obfuscate, bool compress=false) {
operations_research::MPModelExportOptions options;
options.obfuscate = obfuscate;
options.use_gzip_compression = compress;
operations_research::MPModelProto model;
$self->ExportModelToProto(&model);
return WriteModelToMpsFile(filename, model, options).ok();
Expand Down