Fish - FiberLib for VISH 0.3
Fish - The Fiber Bundle API for the Vish Visualization Shell
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)

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.
WeakPtr<MemBase> PositionsTracer;
{
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();
PositionsTracer = Pos;
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";
Assert( PositionsTracer );
}
Assert( !PositionsTracer );
}
void check_file_copy_by_creator_copy()
{
puts("##################### COPY FILE VIA CREATOR COPY ######################");fflush(stdout);
WeakPtr<MemBase> PositionsTracer;
{
BundlePtr inBP;
inBP.load("InputFile.f5");
BundlePtr outBP;
(*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,
const RefPtr<CreativeArrayBase>&CAB, const RefPtr<FragmentID>&fid)
{
Assert(CAB);
Representation&myCartesianRepresentation =
outBP[t]["Newly Created Grid"].makeCartesianRepresentation(3);
RefPtr<Field>&OutField = myCartesianRepresentation[ theFieldID.Name() ];
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);
WeakPtr<MemBase> PositionsTracer;
{
BundlePtr inBP;
inBP.load("InputFile.f5");
BundlePtr outBP;
(*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,
const RefPtr<CreativeArrayBase>&CAB, const RefPtr<FragmentID>&fid)
{
Assert(CAB);
Representation&myCartesianRepresentation =
outBP[t]["Data Copied Grid"].makeCartesianRepresentation(3);
RefPtr<Field>&OutField = myCartesianRepresentation[ theFieldID.Name() ];
#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();
auto InitialRAM = getMemUsage();
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.
//
ArrayRef<point, 3> Coordinates(GridDims);
for(auto I : GridDims)
{
point&P = Coordinates[ I ];
// 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;
}
if (MemBase::Creator_t
CoordinateCreator = Positions->createCreator(Coordinates, "Some Test Fragment " + std:: to_string(i) ) )
{
CoordinateCreator->touch();
}
}
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;
}
int main()
Demonstrates minimal usage of the FiberLib Create Bundle, insert a time slice, and safe it to a file.
Definition 010-SimpleSave.cpp:13
An array reference class, which is a convenience class for reference pointers to multidimensional mem...
Definition ArrayRef.hpp:28
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
Identifier for Fields within a Grid.
Definition FieldID.hpp:53
const string & Name() const
Get the textual description.
Definition FieldID.hpp:86
A Field is a collection of CreativeArrayBase reference pointers which are accessed via FragmentID obj...
Definition Field.hpp:245
const string & Name() const
Text ID of the fragment.
Definition FragmentID.hpp:65
A grid identifier.
Definition GridID.hpp:29
const string & Name() const
Get the name associated with this Grid ID.
Definition GridID.hpp:131
A Grid is a set of Skeleton objects, each of them accessed via some unique SkeletonID object.
Definition Grid.hpp:60
int iterate_all(const Functor &F) const
Lambda functor iteration over all data.
Definition SkeletonMap.hpp:260
A multidimensional index that is automatically a lower-dimensional index via recursion.
Definition MultiIndex.hpp:449
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
static RefPtr< Cache, CacheBase > & MemCache()
MemSizeConfig< sizeof(void *)>::memsize_t memsize_t
StrongPtr< Object, ObjectBase > RefPtr
Given a fragmented field of curvilinear coordinates, (3D array of coordinates), build a uniform Grid ...
Definition FAQ.dox:2
WeakPtr< BaseSpace > Representer
The type used to reference other objects, which is the anchor of representations of a Skeleton.
Definition RepresentationMap.hpp:28
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=",")

Macro Definition Documentation

◆ Verbose

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