Skip to content

Commit

Permalink
refs #6449 Save MD seems work ( tested)
Browse files Browse the repository at this point in the history
It may be not very efficient file mode as one needs to identify the if the buffer for SaveMD make file based is defined correctly.
  • Loading branch information
abuts committed Apr 16, 2013
1 parent dd6d71b commit 97e5561
Show file tree
Hide file tree
Showing 42 changed files with 864 additions and 819 deletions.
4 changes: 2 additions & 2 deletions Code/Mantid/Framework/API/inc/MantidAPI/BoxController.h
Original file line number Diff line number Diff line change
Expand Up @@ -355,8 +355,8 @@ namespace API
IBoxControllerIO * getFileIO()
{return m_fileIO.get();}
/// makes box controller file based by providing class, responsible for fileIO.
void setFileBacked(IBoxControllerIO *newFileIO,const std::string &fileName="");

void setFileBacked(boost::shared_ptr<IBoxControllerIO> newFileIO,const std::string &fileName="");
void clearFileBacked();
//-----------------------------------------------------------------------------------
//BoxCtrlChangesInterface *getChangesList(){return m_ChangesList;}
//void setChangesList(BoxCtrlChangesInterface *pl){m_ChangesList=pl;}
Expand Down
6 changes: 3 additions & 3 deletions Code/Mantid/Framework/API/inc/MantidAPI/IMDEventWorkspace.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,17 @@ namespace API
/// Split all boxes that exceed the split threshold.
virtual void splitAllIfNeeded(Kernel::ThreadScheduler * ts) = 0;

bool fileNeedsUpdating() const;
// bool fileNeedsUpdating() const;

void setFileNeedsUpdating(bool value);
//void setFileNeedsUpdating(bool value);

virtual bool threadSafe() const;

virtual void setCoordinateSystem(const Mantid::API::SpecialCoordinateSystem coordinateSystem) = 0;

protected:
/// Marker set to true when a file-backed workspace needs its back-end file updated (by calling SaveMD(UpdateFileBackEnd=1) )
bool m_fileNeedsUpdating;
//bool m_fileNeedsUpdating;

};

Expand Down
5 changes: 4 additions & 1 deletion Code/Mantid/Framework/API/inc/MantidAPI/IMDNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ class IMDNode
virtual void setFileBacked(const uint64_t /*fileLocation*/,const size_t /*fileSize*/, const bool /*markSaved*/)=0;
/** initiate the structure responsible for swapping the box on HDD if out of memory with default parameters (it does not know its place on HDD and was not saved). */
virtual void setFileBacked()=0;
/// if node was fileBacked, it should clear file-backed information
virtual void clearFileBacked()=0;

/**Save the box at specific disk position using the class, respoinsible for the file IO. */
virtual void saveAt(API::IBoxControllerIO *const /*saver */, uint64_t /*position*/)const=0;
/**Load the additional box data of specified size from the disk location provided using the class, respoinsible for the file IO and append them to the box */
Expand All @@ -63,7 +66,7 @@ class IMDNode
//-------------------------------------------------------------
/// Clear all contained data including precalculated averages.
virtual void clear() = 0;

///@return the type of the event this box contains
virtual std::string getEventType()const =0;
///@return the length of the coordinates (in bytes), the events in the box contain.
Expand Down
4 changes: 3 additions & 1 deletion Code/Mantid/Framework/API/inc/MantidAPI/IMDWorkspace.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,10 @@ namespace Mantid

/// Clear existing masks
virtual void clearMDMasking() = 0;

///
virtual Mantid::API::SpecialCoordinateSystem getSpecialCoordinateSystem() const = 0;
/// if a workspace was filebacked, this should clear file-based status, delete file-based information and close related files.
virtual void clearFileBacked(){};
};

/// Shared pointer to the IMDWorkspace base class
Expand Down
55 changes: 34 additions & 21 deletions Code/Mantid/Framework/API/src/BoxController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,6 @@ namespace API
// reset the clone file IO controller to avoid dublicated file based operations for different box controllers
return BoxController_sptr(new BoxController(*this));
}
/** makes box controller file based by providing class, responsible for fileIO. The box controller become responsible for the FileIO pointer
*@param newFileIO -- instance of the box controller responsible for the IO;
*@param fileName -- if newFileIO comes without opened file, this is the file name to open for the file based IO operations
*/
void BoxController::setFileBacked(IBoxControllerIO *newFileIO,const std::string &fileName)
{
if(!newFileIO->isOpened())
newFileIO->openFile(fileName,"w");

if(!newFileIO->isOpened())
{
delete newFileIO;
throw(Kernel::Exception::FileError("Can not open target file for filebased box controller ",fileName));
}

// kill old fileIO if any
if(this->m_fileIO) // should happen in destructor anyway but just to be carefull about it
this->m_fileIO->closeFile();

this->m_fileIO = boost::shared_ptr<IBoxControllerIO>(newFileIO);
}

/*Private Copy constructor used in cloning */
BoxController::BoxController(const BoxController & other)
Expand Down Expand Up @@ -197,8 +176,42 @@ namespace API

this->calcNumSplit();
}
/** function clears the file-backed status of the box controller */
void BoxController::clearFileBacked()
{
if(m_fileIO)
{
m_fileIO->flushCache();
// close underlying file
m_fileIO->closeFile();
// decrease the sp counter by one and nullify this instance of sp.
m_fileIO.reset();
}
}
/** makes box controller file based by providing class, responsible for fileIO. The box controller become responsible for the FileIO pointer
*@param newFileIO -- instance of the box controller responsible for the IO;
*@param fileName -- if newFileIO comes without opened file, this is the file name to open for the file based IO operations
*/
void BoxController::setFileBacked( boost::shared_ptr<IBoxControllerIO> newFileIO,const std::string &fileName)
{
if(!newFileIO->isOpened())
newFileIO->openFile(fileName,"w");

if(!newFileIO->isOpened())
{
newFileIO.reset();
throw(Kernel::Exception::FileError("Can not open target file for filebased box controller ",fileName));
}

// kill old fileIO if any
if(this->m_fileIO)// should happen in destructor anyway but just to be carefull about it
{
this->m_fileIO->flushCache();
this->m_fileIO->closeFile();
}

this->m_fileIO = newFileIO;
}


} // namespace Mantid
Expand Down
36 changes: 18 additions & 18 deletions Code/Mantid/Framework/API/src/IMDEventWorkspace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ namespace API
//-----------------------------------------------------------------------------------------------
/** Empty constructor */
IMDEventWorkspace::IMDEventWorkspace()
: IMDWorkspace(),
m_fileNeedsUpdating(false)
: IMDWorkspace()
//m_fileNeedsUpdating(false)
{
}

Expand All @@ -27,22 +27,22 @@ namespace API
}


//-----------------------------------------------------------------------------------------------
/** @return the marker set to true when a file-backed workspace needs its back-end file updated (by calling SaveMD(UpdateFileBackEnd=1) )
*/
bool IMDEventWorkspace::fileNeedsUpdating() const
{
return m_fileNeedsUpdating;
}

//-----------------------------------------------------------------------------------------------
/** Sets the marker set to true when a file-backed workspace needs its back-end file updated (by calling SaveMD(UpdateFileBackEnd=1) )
* @param value :: marker value
*/
void IMDEventWorkspace::setFileNeedsUpdating(bool value)
{
m_fileNeedsUpdating = value;
}
////-----------------------------------------------------------------------------------------------
///** @return the marker set to true when a file-backed workspace needs its back-end file updated (by calling SaveMD(UpdateFileBackEnd=1) )
// */
//bool IMDEventWorkspace::fileNeedsUpdating() const
//{
// return m_fileNeedsUpdating;
//}

////-----------------------------------------------------------------------------------------------
///** Sets the marker set to true when a file-backed workspace needs its back-end file updated (by calling SaveMD(UpdateFileBackEnd=1) )
// * @param value :: marker value
// */
//void IMDEventWorkspace::setFileNeedsUpdating(bool value)
//{
// m_fileNeedsUpdating = value;
//}

//-----------------------------------------------------------------------------------------------
/** Is the workspace thread-safe. For MDEventWorkspaces, this means operations
Expand Down
28 changes: 24 additions & 4 deletions Code/Mantid/Framework/API/test/BoxControllerTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,15 +207,17 @@ class BoxControllerTest : public CxxTest::TestSuite
a->setSplitInto(10);
a->setMaxDepth(10);
a->setMaxId(123456);
TS_ASSERT_THROWS_NOTHING(a->setFileBacked(new MantidTestHelpers::BoxControllerDummyIO(a),"fakeFile"));
boost::shared_ptr<IBoxControllerIO> pS(new MantidTestHelpers::BoxControllerDummyIO(a));
TS_ASSERT_THROWS_NOTHING(a->setFileBacked(pS,"fakeFile"));
TS_ASSERT(a->isFileBacked());

BoxController_sptr b = a->clone();
// Check that settings are the same but BC are different
compareBoxControllers(*a, *b);

TS_ASSERT(!b->isFileBacked());
TS_ASSERT_THROWS_NOTHING(b->setFileBacked(new MantidTestHelpers::BoxControllerDummyIO(b),"fakeFile2"));
boost::shared_ptr<IBoxControllerIO> pS2(new MantidTestHelpers::BoxControllerDummyIO(b));
TS_ASSERT_THROWS_NOTHING(b->setFileBacked(pS2,"fakeFile2"));

// Check that settings are the same but BC are different
compareBoxControllers(*a, *b);
Expand All @@ -228,8 +230,8 @@ class BoxControllerTest : public CxxTest::TestSuite
void test_MRU_access()
{
BoxController_sptr a = boost::shared_ptr<BoxController>(new BoxController(2));

a->setFileBacked(new MantidTestHelpers::BoxControllerDummyIO(a),"existingFakeFile");
boost::shared_ptr<IBoxControllerIO> pS(new MantidTestHelpers::BoxControllerDummyIO(a));
a->setFileBacked(pS,"existingFakeFile");
DiskBuffer * dbuf = a->getFileIO();

// Set the cache parameters
Expand All @@ -246,6 +248,24 @@ class BoxControllerTest : public CxxTest::TestSuite
TS_ASSERT_EQUALS(0, box_controller.getMaxId());
}

void test_openCloseFileBacked()
{
BoxController_sptr a = boost::shared_ptr<BoxController>(new BoxController(2));
TS_ASSERT(!a->isFileBacked());

boost::shared_ptr<IBoxControllerIO> pS(new MantidTestHelpers::BoxControllerDummyIO(a));
TS_ASSERT_THROWS_NOTHING(a->setFileBacked(pS,"fakeFile"));

TSM_ASSERT("Box controller should have open faked file",pS->isOpened());
std::string fileName = pS->getFileName();
TSM_ASSERT_EQUALS("Box controller file should be named as requested ",0,fileName.compare(fileName.size()-8,8,std::string("fakeFile")));
TS_ASSERT(a->isFileBacked());

TS_ASSERT_THROWS_NOTHING(a->clearFileBacked());
TS_ASSERT(!a->isFileBacked());
TSM_ASSERT("Box controller should now close the faked file",!pS->isOpened());
}


};

Expand Down
1 change: 1 addition & 0 deletions Code/Mantid/Framework/API/test/CompositeFunctionTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class CompositeFunctionTest_MocMatrixWorkspace : public MatrixWorkspace
void init(const size_t &,const size_t &,const size_t &) { }
void generateHistogram(const std::size_t , const MantidVec& , MantidVec& , MantidVec& , bool ) const { }

void clearFileBacked(){};
private:
std::vector<CompositeFunctionTest_MocSpectrum> m_spectra;
size_t m_blocksize;
Expand Down
4 changes: 4 additions & 0 deletions Code/Mantid/Framework/Kernel/src/ISaveable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,12 @@ namespace Kernel
void ISaveable::saveAt(uint64_t newPos, uint64_t newSize)
{
m_setter.lock();
// load old contents
this->load();
// set new position, derived by the disk buffer
m_fileIndexStart= newPos;
m_fileNumEvents = newSize;
// save in the new location
this->save();
this->clearDataFromMemory();
m_setter.unlock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ namespace MDAlgorithms
/// Algorithm's category for identification
virtual const std::string category() const { return "MDAlgorithms";}

protected: // for testing
void saveExperimentInfos(::NeXus::File * const file, API::IMDEventWorkspace_const_sptr ws);
private:
/// Sets documentation strings for this algorithm
virtual void initDocs();
Expand Down
6 changes: 3 additions & 3 deletions Code/Mantid/Framework/MDAlgorithms/src/BinMD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,9 +324,9 @@ namespace MDAlgorithms
{
BoxController_sptr bc = ws->getBoxController();
// store exisiting write buffer size for the future
uint64_t writeBufSize =bc->getDiskBuffer().getWriteBufferSize();
//uint64_t writeBufSize =bc->getDiskBuffer().getWriteBufferSize();
// and disable write buffer (if any) for input MD Events for this algorithm purposes;
bc->setCacheParameters(1,0);
//bc->setCacheParameters(1,0);


// Cache some data to speed up accessing them a bit
Expand Down Expand Up @@ -440,7 +440,7 @@ namespace MDAlgorithms
}

// return the size of the input workspace write buffer to its initial value
bc->setCacheParameters(sizeof(MDE),writeBufSize);
//bc->setCacheParameters(sizeof(MDE),writeBufSize);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ namespace MDAlgorithms
if (!bc) throw std::runtime_error("Error with InputWorkspace: no BoxController!");
if (bc->isFileBacked())
{
if (ws->fileNeedsUpdating())
//if (ws->fileNeedsUpdating())
{
// Data was modified! You need to save first.
g_log.notice() << "InputWorkspace's file-backend being updated. " << std::endl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ namespace MDAlgorithms
API::IMDNode* box2 = boxes2[j];

this->compare( box1->getID(), box2->getID(), "Boxes have different ID" );
this->compare( box1->getDepth(), box2->getDepth(), "Boxes are at a different depth" );
this->compare(size_t(box1->getDepth()), size_t(box2->getDepth()), "Boxes are at a different depth" );
this->compare( box1->getNumChildren(), box2->getNumChildren(), "Boxes do not have the same number of children");
for (size_t i=0; i<box1->getNumChildren(); i++)
this->compare( box1->getChild(i)->getID(), box2->getChild(i)->getID(), "Child of boxes do not match IDs" );
Expand Down
2 changes: 1 addition & 1 deletion Code/Mantid/Framework/MDAlgorithms/src/DivideMD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ namespace MDAlgorithms
// Recalculate the totals
ws->refreshCache();
// Mark file-backed workspace as dirty
ws->setFileNeedsUpdating(true);
//ws->setFileNeedsUpdating(true);
}


Expand Down
2 changes: 1 addition & 1 deletion Code/Mantid/Framework/MDAlgorithms/src/FakeMDEventData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ namespace MDAlgorithms
CALL_MDEVENT_FUNCTION(this->addFakeUniformData, in_ws)

// Mark that events were added, so the file back end (if any) needs updating
in_ws->setFileNeedsUpdating(true);
//in_ws->setFileNeedsUpdating(true);
}

/**
Expand Down

0 comments on commit 97e5561

Please sign in to comment.