Skip to content

Commit

Permalink
Refs #10370 Added method to MDBox to reserve memory for events.
Browse files Browse the repository at this point in the history
MergeMDFiles uses this method to reserve memory for events in an MDBox object. Removed reserve in loadAndAddFrom method on MDBox (could this have a detrimental effect on LoadMD?).
  • Loading branch information
ianbush committed Oct 15, 2014
1 parent e189c59 commit bede8f8
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 11 deletions.
1 change: 1 addition & 0 deletions Code/Mantid/Framework/API/inc/MantidAPI/IMDNode.h
Expand Up @@ -71,6 +71,7 @@ class IMDNode
*@param loadFileData -- if true, the data on HDD and not yet in memory are loaded into memory before deleting fileBacked information,
if false, all on HDD contents are discarded, which can break the data integrity (used by destructor) */
virtual void clearFileBacked(bool loadFileData)=0;
virtual void reserveMemoryForLoad(uint64_t)=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;
Expand Down
19 changes: 13 additions & 6 deletions Code/Mantid/Framework/MDAlgorithms/src/MergeMDFiles.cpp
Expand Up @@ -159,17 +159,24 @@ namespace MDAlgorithms
TargetBox->clear();

uint64_t nBoxEvents(0);
std::vector<size_t> numFileEvents(m_EventLoader.size());

for (size_t iw=0; iw<this->m_EventLoader.size(); iw++)
{
size_t ID = TargetBox->getID();
numFileEvents[iw] = static_cast<size_t>(m_fileComponentsStructure[iw].getEventIndex()[2*ID+1]);
nBoxEvents += numFileEvents[iw];
}

uint64_t fileLocation = m_fileComponentsStructure[iw].getEventIndex()[2*ID+0];
size_t numFileEvents = static_cast<size_t>(m_fileComponentsStructure[iw].getEventIndex()[2*ID+1]);
if(numFileEvents==0)continue;
//TODO: it is possible to avoid the reallocation of the memory at each load
TargetBox->loadAndAddFrom(m_EventLoader[iw],fileLocation,numFileEvents);
// At this point memory required is known, so it is reserved all in one go
TargetBox->reserveMemoryForLoad(nBoxEvents);

nBoxEvents += numFileEvents;
for (size_t iw=0; iw<this->m_EventLoader.size(); iw++)
{
size_t ID = TargetBox->getID();
uint64_t fileLocation = m_fileComponentsStructure[iw].getEventIndex()[2*ID+0];
if(numFileEvents[iw]==0) continue;
TargetBox->loadAndAddFrom(m_EventLoader[iw],fileLocation,numFileEvents[iw]);
}

return nBoxEvents;
Expand Down
1 change: 1 addition & 0 deletions Code/Mantid/Framework/MDEvents/inc/MantidMDEvents/MDBox.h
Expand Up @@ -68,6 +68,7 @@ namespace MDEvents
//-----------------------------------------------------------------------------------------------
virtual void saveAt(API::IBoxControllerIO *const /* */, uint64_t /*position*/)const;
virtual void loadAndAddFrom(API::IBoxControllerIO *const /* */, uint64_t /*position*/, size_t /* Size */);
virtual void reserveMemoryForLoad(uint64_t /* Size */);
/**drop events data from memory but keep averages (and file-backed info) */
void clearDataFromMemory();
/** */
Expand Down
2 changes: 2 additions & 0 deletions Code/Mantid/Framework/MDEvents/inc/MantidMDEvents/MDGridBox.h
Expand Up @@ -67,6 +67,8 @@ namespace MDEvents
/**Load the box data of specified size from the disk location provided using the class, respoinsible for the file IO. */
virtual void loadAndAddFrom(API::IBoxControllerIO *const /* */, uint64_t /*position*/, size_t /* Size */)
{/*Not directly loadable */}
virtual void reserveMemoryForLoad(uint64_t /* Size */)
{/*Not directly loadable */}
//-------------------------------------------------------------------------------------------------------

/** Uses the cached value of points stored in the grid box
Expand Down
18 changes: 13 additions & 5 deletions Code/Mantid/Framework/MDEvents/src/MDBox.cpp
Expand Up @@ -900,9 +900,20 @@ namespace MDEvents
FileSaver->saveBlock(TabledData,position);

}

/**
* Reserve all the memory required for loading in one step.
*
* @param size -- number of events to reserve for
*/
TMDE(
void MDBox)::reserveMemoryForLoad(uint64_t size)
{
this->data.reserve(size);
}

/**Load the box data of specified size from the disk location provided using the class, respoinsible for the file IO and append them to exisiting events
* Clear events vector first if overwriting the exisitng events is necessary. The efficiency would be higher if jentle cleaning occurs (the size of event data vector
is nullified but memory still allocated)
* Clear events vector first if overwriting the exisitng events is necessary.
*
* @param FileSaver -- the pointer to the class, responsible for file IO
* @param filePosition -- the place in the direct access file, where necessary data are located
Expand All @@ -923,9 +934,6 @@ namespace MDEvents
std::vector<coord_t> TableData;
FileSaver->loadBlock(TableData,filePosition,nEvents);

// convert loaded events to data;
size_t nCurrentEvents = data.size();
this->data.reserve(nCurrentEvents+nEvents);
// convert data to events appending new events to existing
MDE::dataToEvents(TableData,data,false);

Expand Down
1 change: 1 addition & 0 deletions Code/Mantid/Framework/MDEvents/test/MDBoxBaseTest.h
Expand Up @@ -49,6 +49,7 @@ class MDBoxBaseTester : public MDBoxBase<MDE,nd>
void setFileBacked(){};
void saveAt(API::IBoxControllerIO *const /* */, uint64_t /*position*/)const{/*Not saveable */};
void loadAndAddFrom(API::IBoxControllerIO *const /* */, uint64_t /*position*/, size_t /* Size */){};
void reserveMemoryForLoad(uint64_t /* Size */){};
// regardless of what is actually instantiated, base tester would call itself gridbox
bool isBox()const{return false;}

Expand Down

0 comments on commit bede8f8

Please sign in to comment.