Fish - FiberLib for VISH 0.3
Fish - The Fiber Bundle API for the Vish Visualization Shell
Macros | Functions
051-CopyBoundFragments.cpp File Reference

[← Previous Example] [Next Example → 060-RegularFragmentTopology.cpp ]. More...

#include <fiber/bundle/Bundle.hpp>
#include <fiber/finit/FinitAPI.h>
#include <fiber/field/ArrayRef.hpp>
#include <fiber/field/UniformCartesianArray.hpp>
#include <memcore/MemUsage.hpp>

Macros

#define Verbose(X)   AppVerbose("RUN",X)
 

Functions

void check_file_save ()
 
void check_file_copy_by_creator_copy ()
 
void check_file_copy_by_data_copy ()
 
int main ()
 

Detailed Description

[← Previous Example] [Next Example → 060-RegularFragmentTopology.cpp ].

In FiberLib Tutorial

#include <fiber/bundle/Bundle.hpp>
#include <fiber/finit/FinitAPI.h>
#include <fiber/field/ArrayRef.hpp>
#include <fiber/field/UniformCartesianArray.hpp>
#include <memcore/MemUsage.hpp>
using namespace Fiber;
using namespace Eagle::PhysicalSpace;
#undef Verbose
#define Verbose(X) AppVerbose("RUN",X)
static constexpr int
FRAGMENTS_PER_FILE = 6;
static constexpr bool
CREATE_INPUT_FILE = true,
CHECK_FILE_SAVE = false,
CHECK_FILE_COPY_BY_CREATOR_COPY = true,
CHECK_FILE_COPY_BY_DATA_COPY = false;
static constexpr int kB = 1024, MB = kB*kB;
static constexpr memsize_t MEMCORE_CACHESIZE = 8*MB;
void check_file_save()
{
puts("##################### COPY FILE ######################");fflush(stdout);
RefPtr<Cache>&theGlobalCache = Cache::MemCache();
// only for checking that data pointers indeed become null once file is closed.
{
BP.load("InputFile.f5");
BP->iterate_timeslices( true, [&](double t, const Slice&S)
{
Verbose(0) << " T=" << t;
S.iterate_grids( [&](const GridID&GId, const Grid&G)
{
Verbose(0) << " Grid " << GId.Name();
G.iterate_all( [&](const Skeleton&S,
const Representer&theRepresenter, const Representation&R,
const FieldID&theFieldID, const Field&theField,
{
Verbose(0) << " Field " << theFieldID.Name();
if (CAB)
{
RefPtr<MemBase> Pos = CAB->create();
Verbose(0) << "* File Creator created: *** RAM: "
<< " Cache: "
<< double(theGlobalCache->totalCost())/MB << "MB";
}
return true;
} );
return true;
} );
return true;
}
);
Verbose(0) << "* File loaded: *** RAM: " << getRelativeMemUsageMiB()
<< "MB Cache: " << double(theGlobalCache->totalCost())/MB << "MB"
<< " -- file has been COMPLETELY loaded at this time, thus full available RAM is used here by now -- "
<< " - if file is larger than RAM, then some data have been forgotten again, and will be required to be loaded again when saving the Output file. - "
;
BP->save("SavedInputFile.f5");
Verbose(0) << "* SAVED, file open: *** RAM: " << getRelativeMemUsageMiB()
<< "MB Cache: " << double(theGlobalCache->totalCost())/MB << "MB";
}
}
void check_file_copy_by_creator_copy()
{
puts("##################### COPY FILE VIA CREATOR COPY ######################");fflush(stdout);
{
inBP.load("InputFile.f5");
(*outBP).bindToNew("InputFileBoundCopy.f5");
inBP->iterate_timeslices( true, [&](double t, const Slice&S)
{
Verbose(0) << " T=" << t;
S.iterate_grids( [&](const GridID&GId, const Grid&G)
{
Verbose(0) << " Grid " << GId.Name();
G.iterate_all( [&](const Skeleton&S,
const Representer&theRepresenter, const Representation&R,
const FieldID&theFieldID, const Field&theField,
{
Assert(CAB);
outBP[t]["Newly Created Grid"].makeCartesianRepresentation(3);
OutField->setCreatorByName( CAB, FragmentID::Name(fid ));
Verbose(0) << "Field [" << theFieldID.Name() << "] [" << FragmentID::Name(fid ) << "]"
<< "* File Creator created: *** RAM: "
<< "MB Cache: " << Cache::MemCache()->totalCostMiB() << "MB"
// << " Creator is " << CAB
;
return true;
} );
return true;
} );
return true;
}
);
Verbose(0) << "* File loaded: *** RAM: " << getRelativeMemUsageMiB()
<< "MB Cache: " << Cache::MemCache()->totalCostMiB() << "MB"
<< " -- NO file data have been loaded at this time already, thus NO RAM has been used here by now -- "
;
Assert( Cache::MemCache()->totalCostMiB() == 0.0 );
} // < ------- ALL file data reading and writing takes place here, at the Destructor of the Bundle objects!
Verbose(0) << "* File has been copied, now RAM usage is " << getRelativeMemUsageMiB()
<< "MB Cache: " << Cache::MemCache()->totalCostMiB() << "MB";
Assert( Cache::MemCache()->totalCostMiB() == 0.0 );
}
void check_file_copy_by_data_copy()
{
puts("##################### COPY FILE VIA DATA COPY ######################");fflush(stdout);
{
inBP.load("InputFile.f5");
(*outBP).bindToNew("InputFileViaDataCopy.f5");
inBP->iterate_timeslices( true, [&](double t, const Slice&S)
{
Verbose(0) << " T=" << t;
S.iterate_grids( [&](const GridID&GId, const Grid&G)
{
Verbose(0) << " Grid " << GId.Name();
G.iterate_all( [&](const Skeleton&S,
const Representer&theRepresenter, const Representation&R,
const FieldID&theFieldID, const Field&theField,
{
Assert(CAB);
outBP[t]["Data Copied Grid"].makeCartesianRepresentation(3);
#if 0
OutField->setPersistentDataByName( CAB->create()->copy(), FragmentID::Name(fid ));
#else
OutField->setPersistentDataByName( CAB->create(), FragmentID::Name(fid ));
#endif
Verbose(0) << "Field [" << theFieldID.Name() << "] [" << FragmentID::Name(fid ) << "]"
<< " Persistent Creator created: *** RAM: "
<< "MB Cache: " << Cache::MemCache()->totalCostMiB() << "MB"
// << " Creator is " << CAB
;
return true;
} );
return true;
} );
return true;
}
);
Verbose(0) << "* File loaded: *** RAM: " << getRelativeMemUsageMiB()
<< "MB Cache: " << Cache::MemCache()->totalCostMiB() << "MB"
<< " -- file has been PARTIALLY loaded at this time, thus some RAM is used here by now -- "
<< " - if file is larger than RAM, then some data have already been saved to the new file and forgotten by now; they do not need to be loaded again for saving. - "
;
Assert( Cache::MemCache()->totalCostMiB() != 0.0 );
}
Verbose(0) << "* File has been copied, now RAM usage is " << getRelativeMemUsageMiB()
<< "MB Cache: " << Cache::MemCache()->totalCostMiB() << "MB";
Assert( Cache::MemCache()->totalCostMiB() == 0.0 );
}
int main()
{
// Initialize I/O layers (only for static binaries)
Finit();
RefPtr<Cache>&theGlobalCache = Cache::MemCache();
theGlobalCache->setMaxCost( MEMCORE_CACHESIZE );
Verbose(0) << "* Initial *** RAM: " << getRelativeMemUsageMiB()
<< "MB Cache: " << double(theGlobalCache->totalCost())/MB << "MB";
/*
Create an input file
*/
if (CREATE_INPUT_FILE)
{
//
// Create a bundle object with a grid at T=1.0, named "DemoGrid", in
// a three-dimensional Cartesian representation, using the default
// coordinate system.
//
Representation&myCartesianRepresentation = BP[1.0]["DemoGrid"].makeCartesianRepresentation(3);
BP->bindToNew("InputFile.f5");
//
// Define the size of the grid to be used henceforth
//
const MultiIndex<3> GridDims = MIndex(64,64,64);
//
// Create a coordinate field and fill it with values
//
RefPtr<Field>&Positions = myCartesianRepresentation[ FIBER_POSITIONS ];
Assert(Positions);
for(int i=0; i<FRAGMENTS_PER_FILE; i++)
{
//
// Creates a MemArray with a file-write creator, defining
// the type of the creator at this state.
//
for(auto I : GridDims)
{
// just some more or less arbitrary formula, values do not matter
P.x() = I[0]*0.1;
P.y() = I[1]*0.2;
P.z() = I[2]*0.3;
}
CoordinateCreator = Positions->createCreator(Coordinates, "Some Test Fragment " + std:: to_string(i) ) )
{
}
}
Verbose(0) << "**** RAM: " << double( getMemUsage() - InitialRAM )/MB
<< " Cache: " << double(theGlobalCache->totalCost())/MB << "MB";
}
Verbose(0) << "*** Input File created, closed, cleaned up -RAM: " << double( getMemUsage() - InitialRAM )/MB
<< " Cache: " << double(theGlobalCache->totalCost())/MB << "MB";
Assert( theGlobalCache->totalCost() == 0);
if (CHECK_FILE_SAVE)
check_file_save();
if (CHECK_FILE_COPY_BY_CREATOR_COPY)
check_file_copy_by_creator_copy();
if (CHECK_FILE_COPY_BY_DATA_COPY)
check_file_copy_by_data_copy();
return 0;
}
Convenience class that implements a pointer to a Bundle object but adds some useful member funtions t...
Definition Bundle.hpp:779
int save(const string &url, const RefPtr< LoaderProgress > &SaveProgress=nullptr, const RefPtrStorageTransformations &ST=nullptr)
Definition Bundle.cpp:1142
An iterator with an optional DataCreator, which is just a class to intercept creation of data along a...
Definition CreativeIterator.hpp:34
Identifier for Fields within a Grid.
Definition FieldID.hpp:53
A Field is a collection of CreativeArrayBase reference pointers which are accessed via FragmentID obj...
Definition Field.hpp:245
A grid identifier.
Definition GridID.hpp:29
A Grid is a set of Skeleton objects, each of them accessed via some unique SkeletonID object.
Definition Grid.hpp:60
A Representation is a set of Field objects, each of them accessed via some FieldID identifier.
Definition Representation.hpp:101
A Skeleton is a set of Representation object, each of them accessed by an Representer object.
Definition Skeleton.hpp:102
Information per time slice, mainly a set of Grid objects that are accessed via GridID objects.
Definition Slice.hpp:36
int iterate_grids(const Functor &F) const
Lambda iterator for bundles, provides Slices: @eode iterate_grids( [](const GridID&GId,...
Definition Slice.hpp:197
MemSizeConfig< sizeof(void *)>::memsize_t memsize_t
Given a fragmented field of curvilinear coordinates, (3D array of coordinates), build a uniform Grid ...
Definition FAQ.dox:2
unsigned long long getMemUsage()
double getRelativeMemUsageMiB()
unsigned long long setInitialMemUsage()
string to_string(const Eagle::FixedArray< ElementType, N > &A, const char *OpenBrace="{", const char *CloseBrace="}", const char *Separator=",")