Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // -*- C++ -*- (c) 2010-2011 Jan Kriho <[email protected]>
- #ifndef DIVINE_WRITER_H
- #define DIVINE_WRITER_H
- #include <divine/pma/format.h>
- #include <wibble/test.h>
- #include <fstream>
- #include <list>
- #include <vector>
- #include <string.h>
- namespace divine {
- namespace pma {
- template< typename N >
- struct Writer {
- std::fstream *outfile;
- std::string outfilename;
- bool initialized;
- template< typename C >
- void storeTable( tableDescriptor &tableDesc,
- std::vector< nodeIndexItem > &nodes,
- std::vector< C > &successorTable,
- std::vector< N > &blobs,
- std::vector< std::string > &descs,
- bool seekp = true
- )
- {
- if (seekp) {
- outfile->seekp(tableDesc.offset, std::ios::beg);
- }
- storeNodeIndex( tableDesc, nodes );
- for (int64_t i = 0; i < tableDesc.nodeCount; i++) {
- storeNodeSuccessors( nodes[i], successorTable[i] );
- }
- for (int64_t i = 0; i < tableDesc.nodeCount; i++) {
- storeNode( nodes[i], blobs[i], descs[i] );
- if (!(i & ~((int64_t)0xFF))) { outfile->flush(); }
- }
- outfile->flush();
- }
- template < typename C >
- void storeNodeSuccessors( nodeIndexItem &node,
- C &successors,
- int64_t offset_to = 0,
- bool seekp = false )
- {
- if ( seekp ) {
- outfile->seekp(offset_to, std::ios::beg);
- }
- for (typename C::iterator it = successors.begin(); it != successors.end(); it++) {
- storeInitial(*it, 0, false);
- }
- /*while ( !successors.empty() ) {
- storeInitial( successors.front(), 0, false);
- successors.pop_front();
- }*/
- }
- void storeNode( nodeIndexItem &node,
- N &blob,
- std::string &desc,
- int64_t offset_to = 0,
- bool seekp = false )
- {
- char * array = new char[node.nodeSize + node.descSize];
- int64_t offset = 0;
- assert( blob.valid() );
- assert( blob.size() == node.nodeSize );
- memcpy( array + offset, blob.data(), node.nodeSize );
- offset += node.nodeSize;
- memcpy( array + offset, desc.c_str(), node.descSize );
- offset += node.descSize;
- outfile->write(array, offset);
- delete[] array;
- }
- void storeNodeIndex( tableDescriptor &tableDesc,
- std::vector< nodeIndexItem > &nodes,
- int64_t offset_to = 0,
- bool seekp = false )
- {
- if ( seekp ) {
- outfile->seekp(offset_to, std::ios::beg);
- }
- for ( int i = 0; i < tableDesc.nodeCount; i++ ) {
- storeNodeIndexItem( nodes[i] );
- }
- outfile->flush();
- }
- void storeNodeIndexItem( nodeIndexItem &node, int64_t offset_to = 0, bool seekp = false ) {
- if ( seekp ) {
- outfile->seekp( offset_to, std::ios::beg);
- }
- char array[NODE_INDEX_ITEM_SIZE];
- int offset = 0;
- (*(uint64_t *)((array) + offset)) = htole64(node.succsOffset);
- offset += sizeof(node.succsOffset);
- (*(uint64_t *)((array) + offset)) = htole64(node.dataOffset);
- offset += sizeof(node.dataOffset);
- (*(uint32_t *)((array) + offset)) = htole32(node.nodeSize);
- offset += sizeof(node.nodeSize);
- (*(uint32_t *)((array) + offset)) = htole32(node.descSize);
- offset += sizeof(node.descSize);
- (*(uint16_t *)((array) + offset)) = htole16(node.successors);
- offset += sizeof(node.successors);
- (*(uint16_t *)((array) + offset)) = htole16(node.flags);
- offset += sizeof(node.flags);
- assert(offset == NODE_INDEX_ITEM_SIZE);
- outfile->write( array, offset );
- }
- void storeTableDescriptors ( std::vector< tableDescriptor > &tables, fileHeader &header ) {
- outfile->seekp(TOTAL_HEADER_SIZE + header.initials * NODE_DESCRIPTOR_SIZE);
- if (header.vertexDistribution == None ) {
- storeTableDesc( tables[0], header, 0, false);
- }
- else if (header.vertexDistribution == Hash ) {
- for (int i = 0; i < header.mpiWorkers * header.localWorkers; i++) {
- storeTableDesc( tables[i], header, i, false);
- }
- }
- outfile->flush();
- }
- void storeTableDesc( N & table, fileHeader &header, int id, bool seekp = true ) {
- if ( seekp ) {
- outfile->seekp(TOTAL_HEADER_SIZE +
- header.initials * NODE_DESCRIPTOR_SIZE +
- id * TABLE_DESCRIPTOR_SIZE);
- }
- outfile->write(table.data(), TABLE_DESCRIPTOR_SIZE);
- }
- void storeTableDesc( tableDescriptor &table, fileHeader &header, int id, bool seekp = true ) {
- N array(TABLE_DESCRIPTOR_SIZE);
- int offset = 0;
- offset = table.template write<N>(array, offset);
- storeTableDesc(array, header, id, seekp);
- }
- void storeInitials( std::vector< nodeDescriptor > &initials, fileHeader &header ) {
- outfile->seekp(TOTAL_HEADER_SIZE, std::ios::beg);
- for (int i = 0; i < header.initials; i++) {
- storeInitial(initials[i], i, false);
- }
- outfile->flush();
- }
- void store(N &data, size_t size = 0) {
- assert(data.data() != 0);
- assert_leq(1, data.size());
- if (size == 0) {
- size = data.size();
- }
- outfile->write(data.data(), size);
- }
- void storeInitial(N &initial, int id, bool seekp = true) {
- if ( seekp ) {
- outfile->seekp(TOTAL_HEADER_SIZE + NODE_DESCRIPTOR_SIZE * id, std::ios::beg);
- }
- outfile->write(initial.data(), NODE_DESCRIPTOR_SIZE);
- }
- void storeInitial( nodeDescriptor &initial, int id, bool seekp = true ) {
- N array(NODE_DESCRIPTOR_SIZE);
- int offset = 0;
- offset = initial.template write<N>(array, offset);
- storeInitial(array, id, seekp);
- array.free();
- }
- void storeHeader(N &header) {
- outfile->seekp(0, std::ios::beg);
- outfile->write(header.data(), FILE_HEADER_SIZE + ARCHITECTURE_INFO_SIZE);
- outfile->flush();
- }
- void storeHeader(fileHeader &header, architectureInfo &archinfo) {
- outfile->seekp(0, std::ios::beg);
- N array(FILE_HEADER_SIZE + ARCHITECTURE_INFO_SIZE);
- int offset = 0;
- offset = header.template write<N>(array, offset);
- offset = archinfo.template write<N>(array, offset);
- storeHeader(array);
- //array.free();
- return;
- }
- void init() {
- this->outfile = new std::fstream(this->outfilename.c_str(),
- std::ios::out | std::ios::binary |
- std::ios::trunc);
- this->initialized = true;
- }
- Writer()
- : initialized(false)
- {
- }
- ~Writer() {
- if (initialized) {
- outfile->flush();
- outfile->close();
- delete outfile;
- }
- }
- };
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment