Skip to content

Commit

Permalink
Merge pull request The-OpenROAD-Project#4251 from QuantamHD/odb_compress
Browse files Browse the repository at this point in the history
odb: Migrate DB methods to generic iostreams
  • Loading branch information
maliberty authored Nov 13, 2023
2 parents b9bbde6 + faec82c commit 2d83b71
Show file tree
Hide file tree
Showing 13 changed files with 101 additions and 135 deletions.
12 changes: 7 additions & 5 deletions src/OpenRoad.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

#include "ord/OpenRoad.hh"

#include <fstream>
#include <iostream>
#include <thread>
#ifdef ENABLE_PYTHON3
Expand Down Expand Up @@ -484,11 +485,12 @@ void OpenRoad::readDb(const char* filename)

void OpenRoad::writeDb(const char* filename)
{
FILE* stream = fopen(filename, "w");
if (stream) {
db_->write(stream);
fclose(stream);
}
std::ofstream stream;
stream.exceptions(std::ifstream::failbit | std::ifstream::badbit
| std::ios::eofbit);
stream.open(filename, std::ios::binary);

db_->write(stream);
}

void OpenRoad::diffDbs(const char* filename1,
Expand Down
2 changes: 2 additions & 0 deletions src/drt/src/distributed/paUpdate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

#include "distributed/paUpdate.h"

#include <fstream>

#include "distributed/frArchive.h"
#include "serialization.h"
using namespace fr;
Expand Down
1 change: 1 addition & 0 deletions src/drt/src/pa/FlexPA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <boost/io/ios_state.hpp>
#include <boost/serialization/export.hpp>
#include <chrono>
#include <fstream>
#include <iostream>
#include <sstream>

Expand Down
1 change: 1 addition & 0 deletions src/drt/src/pa/FlexPA_prep.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <omp.h>

#include <chrono>
#include <fstream>
#include <iostream>
#include <sstream>

Expand Down
36 changes: 18 additions & 18 deletions src/odb/include/odb/db.h
Original file line number Diff line number Diff line change
Expand Up @@ -367,31 +367,31 @@ class dbDatabase : public dbObject
/// WARNING: This function destroys the data currently in the database.
/// Throws ZIOError..
///
void read(std::ifstream& f);
void read(std::istream& f);

///
/// Write a database to this stream.
/// Throws ZIOError..
///
void write(FILE* file);
void write(std::ostream& file);

/// Throws ZIOError..
void writeTech(FILE* file);
void writeLib(FILE* file, dbLib* lib);
void writeLibs(FILE* file);
void writeBlock(FILE* file, dbBlock* block);
void writeChip(FILE* file);
void writeWires(FILE* file, dbBlock* block);
void writeNets(FILE* file, dbBlock* block);
void writeParasitics(FILE* file, dbBlock* block);
void readTech(std::ifstream& f);
void readLib(std::ifstream& f, dbLib*);
void readLibs(std::ifstream& f);
void readBlock(std::ifstream& f, dbBlock* block);
void readWires(std::ifstream& f, dbBlock* block);
void readNets(std::ifstream& f, dbBlock* block);
void readParasitics(std::ifstream& f, dbBlock* block);
void readChip(std::ifstream& f);
void writeTech(std::ostream& file);
void writeLib(std::ostream& file, dbLib* lib);
void writeLibs(std::ostream& file);
void writeBlock(std::ostream& file, dbBlock* block);
void writeChip(std::ostream& file);
void writeWires(std::ostream& file, dbBlock* block);
void writeNets(std::ostream& file, dbBlock* block);
void writeParasitics(std::ostream& file, dbBlock* block);
void readTech(std::istream& f);
void readLib(std::istream& f, dbLib*);
void readLibs(std::istream& f);
void readBlock(std::istream& f, dbBlock* block);
void readWires(std::istream& f, dbBlock* block);
void readNets(std::istream& f, dbBlock* block);
void readParasitics(std::istream& f, dbBlock* block);
void readChip(std::istream& f);

///
/// ECO - The following methods implement a simple ECO mechanism for capturing
Expand Down
85 changes: 25 additions & 60 deletions src/odb/include/odb/dbStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
#include <array>
#include <cstdint>
#include <cstring>
#include <fstream>
#include <istream>
#include <ostream>
#include <string>

#include "ZException.h"
Expand All @@ -51,18 +52,21 @@ class _dbDatabase;
class dbOStream
{
_dbDatabase* _db;
FILE* _f;
std::ostream& _f;
double _lef_area_factor;
double _lef_dist_factor;

void write_error()
// By default values are written as their string ("255" vs 0xFF)
// representations when using the << stream method. In dbOstream we are
// primarly writing the byte representation which the below accomplishes.
template <typename T>
void writeValueAsBytes(T type)
{
throw ZException("write failed on database stream; system io error: (%s)",
strerror(ferror(_f)));
_f.write(reinterpret_cast<char*>(&type), sizeof(T));
}

public:
dbOStream(_dbDatabase* db, FILE* f);
dbOStream(_dbDatabase* db, std::ostream& f);

_dbDatabase* getDatabase() { return _db; }

Expand All @@ -74,100 +78,67 @@ class dbOStream

dbOStream& operator<<(char c)
{
int n = fwrite(&c, sizeof(c), 1, _f);
if (n != 1) {
write_error();
}
writeValueAsBytes(c);
return *this;
}

dbOStream& operator<<(unsigned char c)
{
int n = fwrite(&c, sizeof(c), 1, _f);
if (n != 1) {
write_error();
}
writeValueAsBytes(c);
return *this;
}

dbOStream& operator<<(int16_t c)
{
int n = fwrite(&c, sizeof(c), 1, _f);
if (n != 1) {
write_error();
}
writeValueAsBytes(c);
return *this;
}

dbOStream& operator<<(uint16_t c)
{
int n = fwrite(&c, sizeof(c), 1, _f);
if (n != 1) {
write_error();
}
writeValueAsBytes(c);
return *this;
}

dbOStream& operator<<(int c)
{
int n = fwrite(&c, sizeof(c), 1, _f);
if (n != 1) {
write_error();
}
writeValueAsBytes(c);
return *this;
}

dbOStream& operator<<(uint64_t c)
{
int n = fwrite(&c, sizeof(c), 1, _f);
if (n != 1) {
write_error();
}
writeValueAsBytes(c);
return *this;
}

dbOStream& operator<<(unsigned int c)
{
int n = fwrite(&c, sizeof(c), 1, _f);
if (n != 1) {
write_error();
}
writeValueAsBytes(c);
return *this;
}

dbOStream& operator<<(int8_t c)
{
int n = fwrite(&c, sizeof(c), 1, _f);
if (n != 1) {
write_error();
}
writeValueAsBytes(c);
return *this;
}

dbOStream& operator<<(float c)
{
int n = fwrite(&c, sizeof(c), 1, _f);
if (n != 1) {
write_error();
}
writeValueAsBytes(c);
return *this;
}

dbOStream& operator<<(double c)
{
int n = fwrite(&c, sizeof(c), 1, _f);
if (n != 1) {
write_error();
}
writeValueAsBytes(c);
return *this;
}

dbOStream& operator<<(long double c)
{
int n = fwrite(&c, sizeof(c), 1, _f);
if (n != 1) {
write_error();
}
writeValueAsBytes(c);
return *this;
}

Expand All @@ -178,21 +149,15 @@ class dbOStream
} else {
int l = strlen(c) + 1;
*this << l;
int n = fwrite(c, l, 1, _f);
if (n != 1) {
write_error();
}
_f.write(c, l);
}

return *this;
}

dbOStream& operator<<(dbObjectType c)
{
int n = fwrite(&c, sizeof(c), 1, _f);
if (n != 1) {
write_error();
}
writeValueAsBytes(c);
return *this;
}

Expand Down Expand Up @@ -251,13 +216,13 @@ class dbOStream

class dbIStream
{
std::ifstream& _f;
std::istream& _f;
_dbDatabase* _db;
double _lef_area_factor;
double _lef_dist_factor;

public:
dbIStream(_dbDatabase* db, std::ifstream& f);
dbIStream(_dbDatabase* db, std::istream& f);

_dbDatabase* getDatabase() { return _db; }

Expand Down
13 changes: 1 addition & 12 deletions src/odb/src/db/dbBlock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3351,24 +3351,13 @@ void dbBlock::writeDb(char* filename, int allNode)
dbname = fmt::format("{}.remote.{}.db", filename, getpid());
} else
dbname = fmt::format("{}.db", filename);
FILE* file = fopen(dbname.c_str(), "wb");
std::ofstream file(dbname, std::ios::binary);
if (!file) {
getImpl()->getLogger()->warn(
utl::ODB, 19, "Can not open file {} to write!", dbname);
return;
}
int io_bufsize = 65536;
char* buffer = (char*) malloc(io_bufsize);
if (buffer == nullptr) {
getImpl()->getLogger()->warn(
utl::ODB, 20, "Memory allocation failed for io buffer");
fclose(file);
return;
}
setvbuf(file, buffer, _IOFBF, io_bufsize);
getDataBase()->write(file);
free((void*) buffer);
fclose(file);
if (block->_journal) {
debugPrint(getImpl()->getLogger(),
utl::ODB,
Expand Down
Loading

0 comments on commit 2d83b71

Please sign in to comment.