Skip to content

Commit

Permalink
Merge branch 'feature/9357_eliminate_managedworkspaces' into develop
Browse files Browse the repository at this point in the history
Conflicts:
	Code/Mantid/Framework/DataObjects/test/ManagedHistogram1DTest.h
Re #9357.
  • Loading branch information
RussellTaylor committed Apr 29, 2014
2 parents 100c140 + c595e7f commit fcbee20
Show file tree
Hide file tree
Showing 32 changed files with 15 additions and 2,752 deletions.
2 changes: 0 additions & 2 deletions Code/Mantid/Framework/API/inc/MantidAPI/ISpectrum.h
Expand Up @@ -46,8 +46,6 @@ namespace API
*/
class DLLExport ISpectrum
{
friend class ManagedDataBlock2D;

public:
ISpectrum();
ISpectrum(const specid_t specNo);
Expand Down
2 changes: 0 additions & 2 deletions Code/Mantid/Framework/API/inc/MantidAPI/MemoryManager.h
Expand Up @@ -53,8 +53,6 @@ namespace Mantid
public:
/// Returns available physical memory in the system in KB.
MemoryInfo getMemoryInfo();
/// Returns true if there is not sufficient memory for a full Workspace2D.
bool goForManagedWorkspace(std::size_t NVectors,std::size_t XLength,std::size_t YLength);
/// Release memory back to the system if we linked againsed tcmalloc
void releaseFreeMemory();
/// Release memory back to the system if we linked againsed tcmalloc and are above this much use
Expand Down
74 changes: 0 additions & 74 deletions Code/Mantid/Framework/API/src/MemoryManager.cpp
Expand Up @@ -39,8 +39,6 @@ MemoryManagerImpl::~MemoryManagerImpl()
{
}



MemoryInfo MemoryManagerImpl::getMemoryInfo()
{
Kernel::MemoryStats mem_stats;
Expand All @@ -51,78 +49,6 @@ MemoryInfo MemoryManagerImpl::getMemoryInfo()
return info;
}

/** Decides if a ManagedWorkspace2D sould be created for the current memory conditions
and workspace parameters NVectors, XLength,and YLength.
@param NVectors :: the number of vectors
@param XLength :: the size of the X vector
@param YLength :: the size of the Y vector
@return true is managed workspace is needed
*/
bool MemoryManagerImpl::goForManagedWorkspace(std::size_t NVectors, std::size_t XLength, std::size_t YLength)
{
int AlwaysInMemory;// Check for disabling flag
if (Kernel::ConfigService::Instance().getValue("ManagedWorkspace.AlwaysInMemory", AlwaysInMemory)
&& AlwaysInMemory)
return false;

// check potential size to create and determine trigger
int availPercent;
if (!Kernel::ConfigService::Instance().getValue("ManagedWorkspace.LowerMemoryLimit", availPercent))
{
// Default to 40% if missing
availPercent = 40;
}
if (availPercent > 150)
{
g_log.warning("ManagedWorkspace.LowerMemoryLimit is not allowed to be greater than 150%.");
availPercent = 150;
}
if (availPercent < 0)
{
g_log.warning("Negative value for ManagedWorkspace.LowerMemoryLimit. Setting to 0.");
availPercent = 0;
}
if (availPercent > 90)
{
g_log.warning("ManagedWorkspace.LowerMemoryLimit is greater than 90%. Danger of memory errors.");
}
MemoryInfo mi = getMemoryInfo();
size_t triggerSize = mi.availMemory / 100 * availPercent / sizeof(double);
// Avoid int overflow
size_t wsSize = 0;
if (NVectors > 1024)
wsSize = NVectors / 1024 * (YLength * 2 + XLength);
else if (YLength * 2 + XLength > 1024)
wsSize = (YLength * 2 + XLength) / 1024 * NVectors;
else
wsSize = NVectors * (YLength * 2 + XLength) / 1024;

// g_log.debug() << "Requested memory: " << (wsSize * sizeof(double))/1024 << " MB. " << std::endl;
// g_log.debug() << "Available memory: " << mi.availMemory << " KB.\n";
// g_log.debug() << "MWS trigger memory: " << triggerSize * sizeof(double) << " KB.\n";

bool goManaged = (wsSize > triggerSize);
// If we're on the cusp of going managed, add in the reserved but unused memory
if( goManaged )
{
// This is called separately as on some systems it is an expensive calculation.
// See Kernel/src/Memory.cpp - reservedMem() for more details
Kernel::MemoryStats mem_stats;
const size_t reserved = mem_stats.reservedMem();
// g_log.debug() << "Windows - Adding reserved but unused memory of " << reserved << " KB\n";
mi.availMemory += reserved;
triggerSize += reserved / 100 * availPercent / sizeof(double);
goManaged = (wsSize > triggerSize);

g_log.debug() << "Requested memory: " << (wsSize * sizeof(double))/1024 << " MB." << std::endl;
g_log.debug() << "Available memory: " << (mi.availMemory)/1024 << " MB." << std::endl;
g_log.debug() << "ManagedWS trigger memory: " << (triggerSize * sizeof(double))/1024 << " MB." << std::endl;
}

return goManaged;
}


/** Release any free memory back to the system.
* Calling this could help the system avoid going into swap.
* NOTE: This only works if you linked against tcmalloc.
Expand Down
43 changes: 4 additions & 39 deletions Code/Mantid/Framework/API/src/WorkspaceFactory.cpp
Expand Up @@ -149,9 +149,6 @@ void WorkspaceFactoryImpl::initializeFromParent(const MatrixWorkspace_const_sptr
}

/** Creates a new instance of the class with the given name, and allocates memory for the arrays
* where it creates and initialises either a Workspace2D or a ManagedWorkspace2D
* according to the size requested and the value of the configuration parameter
* ManagedWorkspace.LowerMemoryLimit (default 40% of available physical memory) Workspace2D only.
* @param className The name of the class you wish to create
* @param NVectors The number of vectors/histograms/detectors in the workspace
* @param XLength The number of X data points/bin boundaries in each vector (must all be the same)
Expand All @@ -163,46 +160,14 @@ void WorkspaceFactoryImpl::initializeFromParent(const MatrixWorkspace_const_sptr
MatrixWorkspace_sptr WorkspaceFactoryImpl::create(const std::string& className, const size_t& NVectors,
const size_t& XLength, const size_t& YLength) const
{
MatrixWorkspace_sptr ws;

// Creates a managed workspace if over the trigger size and a 2D workspace is being requested.
// Otherwise calls the vanilla create method.
bool is2D = className.find("2D") != std::string::npos;
if ( MemoryManager::Instance().goForManagedWorkspace(static_cast<size_t>(NVectors), static_cast<size_t>(XLength),
static_cast<size_t>(YLength)) && is2D )
{
// check if there is enough memory for 100 data blocks
int blockMemory;
if ( ! Kernel::ConfigService::Instance().getValue("ManagedWorkspace.DataBlockSize", blockMemory)
|| blockMemory <= 0 )
{
// default to 1MB if property not found
blockMemory = 1024*1024;
}

MemoryInfo mi = MemoryManager::Instance().getMemoryInfo();
if ( static_cast<unsigned int>(blockMemory)*100/1024 > mi.availMemory )
{
throw std::runtime_error("There is not enough memory to allocate the workspace");
}

ws = boost::dynamic_pointer_cast<MatrixWorkspace>(this->create("ManagedWorkspace2D"));
g_log.information("Created a ManagedWorkspace2D");
}
else
{
// No need for a Managed Workspace
if ( is2D && ( className.substr(0,7) == "Managed" ))
ws = boost::dynamic_pointer_cast<MatrixWorkspace>(this->create("Workspace2D"));
else
ws = boost::dynamic_pointer_cast<MatrixWorkspace>(this->create(className));
}
MatrixWorkspace_sptr ws = boost::dynamic_pointer_cast<MatrixWorkspace>(this->create(className));

if (!ws)
{
g_log.error("Workspace was not created");
throw std::runtime_error("Workspace was not created");
g_log.error("Workspace was not created");
throw std::runtime_error("Workspace was not created");
}

ws->initialize(NVectors,XLength,YLength);
return ws;
}
Expand Down
16 changes: 0 additions & 16 deletions Code/Mantid/Framework/API/test/WorkspaceFactoryTest.h
Expand Up @@ -41,13 +41,6 @@ class WorkspaceFactoryTest : public CxxTest::TestSuite
std::vector<size_t> size;
};

class ManagedWorkspace2DTest: public Workspace2DTest
{
public:
const std::string id() const {return "ManagedWorkspace2DTest";}
size_t getNumberHistograms() const { return 2;}
};

class NotInFactory : public WorkspaceTester
{
public:
Expand All @@ -60,15 +53,6 @@ class WorkspaceFactoryTest : public CxxTest::TestSuite
{
WorkspaceFactory::Instance().subscribe<Workspace1DTest>("Workspace1DTest");
WorkspaceFactory::Instance().subscribe<Workspace2DTest>("Workspace2DTest");
try
{
WorkspaceFactory::Instance().subscribe<ManagedWorkspace2DTest>("ManagedWorkspace2DTest");
}
catch (std::runtime_error&)
{
// In theory, we shouldn't have the 'real' ManagedWorkspace2D when running this test, but
// in reality we do so need catch the error from trying to subscribe again
}
}

void testReturnType()
Expand Down
Expand Up @@ -80,11 +80,6 @@ namespace Mantid
void validateWorkspaceSizes( bool bexcludeMonitors ,bool bseparateMonitors,
const int64_t normalwsSpecs,const int64_t monitorwsSpecs);

/// This method is useful for separating or excluding monitors from the output workspace
void separateOrexcludeMonitors(DataObjects::Workspace2D_sptr localWorkspace,
bool binclude,bool bexclude,bool bseparate,
int64_t numberOfSpectra,const std::string &fileName);

/// creates output workspace, monitors excluded from this workspace
void excludeMonitors(FILE* file,const int& period,const std::vector<specid_t>& monitorList,
DataObjects::Workspace2D_sptr ws_sptr);
Expand Down
87 changes: 0 additions & 87 deletions Code/Mantid/Framework/DataHandling/src/LoadRaw3.cpp
Expand Up @@ -573,92 +573,5 @@ namespace Mantid
return bMonitor;
}

/** This method separates/excludes monitors from output workspace and creates a separate workspace for monitors
* THIS METHOD IS ONLY CALLED BY THE goManagedRaw METHOD ABOVE AND NOT IN THE GENERAL CASE
* @param localWorkspace :: shared pointer to workspace
* @param binclude :: boolean variable for including monitors
* @param bexclude :: boolean variable for excluding monitors
* @param bseparate :: boolean variable for separating the monitor workspace from output workspace
* @param m_numberOfSpectra :: number of spectra
* @param fileName :: raw file name
*/
void LoadRaw3::separateOrexcludeMonitors(DataObjects::Workspace2D_sptr localWorkspace,
bool binclude,bool bexclude,bool bseparate,
int64_t m_numberOfSpectra,const std::string &fileName)
{
(void) binclude; // Avoid compiler warning

std::vector<specid_t> monitorwsList;
DataObjects::Workspace2D_sptr monitorWorkspace;
FILE *file(NULL);
std::vector<specid_t> monitorSpecList = getmonitorSpectrumList(SpectrumDetectorMapping(localWorkspace.get()));
if (bseparate && !monitorSpecList.empty())
{
Property *ws = getProperty("OutputWorkspace");
std::string localWSName = ws->value();
std::string monitorWSName = localWSName + "_monitors";

declareProperty(new WorkspaceProperty<Workspace> ("MonitorWorkspace", monitorWSName,
Direction::Output));
//create monitor workspace
const int64_t nMons(static_cast<int64_t>(monitorSpecList.size()));
monitorWorkspace = boost::dynamic_pointer_cast<DataObjects::Workspace2D>(
WorkspaceFactory::Instance().create(localWorkspace,nMons,m_lengthIn,m_lengthIn-1));

setProperty("MonitorWorkspace", boost::dynamic_pointer_cast<Workspace>(monitorWorkspace));
file =openRawFile(fileName);
ioRaw(file,true );
}
// Now check whether there is more than one time regime in use
m_noTimeRegimes =getNumberofTimeRegimes();
// Get the time channel array(s) and store in a vector inside a shared pointer
m_timeChannelsVec = getTimeChannels(m_noTimeRegimes,m_lengthIn);
//read raw file
if (bseparate && !monitorSpecList.empty())
{
readData(file, 0);
}
int64_t monitorwsIndex = 0;
for (specid_t i = 0; i < m_numberOfSpectra; ++i)
{
int64_t histToRead = i + 1;
if (bseparate && !monitorSpecList.empty())
{
if (!readData(file, histToRead))
{
throw std::runtime_error("Error reading raw file");
}
}
if ((bseparate && !monitorSpecList.empty()) || bexclude)
{
if (isMonitor(monitorSpecList, static_cast<specid_t>(i) + 1))
{
spec2index_map wsIndexmap;
SpectraAxis* axis = dynamic_cast<SpectraAxis*>(localWorkspace->getAxis(1));
axis->getSpectraIndexMap(wsIndexmap);
spec2index_map::const_iterator wsItr;
wsItr = wsIndexmap.find(static_cast<specid_t>(i + 1));
if (wsItr != wsIndexmap.end())
monitorwsList.push_back(static_cast<specid_t>(wsItr->second));
if (bseparate)
{
monitorWorkspace->getSpectrum(monitorwsIndex)->setSpectrumNo(i+1);
setWorkspaceData(monitorWorkspace, m_timeChannelsVec, monitorwsIndex, i + 1, m_noTimeRegimes,m_lengthIn,1);
++monitorwsIndex;
}
}
}

}
if ((bseparate && !monitorwsList.empty()) || bexclude)
{
localWorkspace->setMonitorList(monitorwsList);
if (bseparate)
{
fclose(file);
}
}
}

} // namespace DataHandling
} // namespace Mantid
Expand Up @@ -6,7 +6,6 @@
#include "MantidAPI/WorkspaceFactory.h"
#include "MantidDataHandling/LoadInstrumentFromRaw.h"
#include "MantidDataHandling/LoadMappingTable.h"
#include "MantidDataObjects/ManagedWorkspace2D.h"
#include "MantidKernel/ConfigService.h"
#include "MantidKernel/TimeSeriesProperty.h"
#include <cxxtest/TestSuite.h>
Expand All @@ -16,7 +15,6 @@ using namespace Mantid;
using namespace Mantid::API;
using namespace Mantid::Kernel;
using namespace Mantid::DataHandling;
using namespace Mantid::DataObjects;

class LoadMappingTableTest : public CxxTest::TestSuite
{
Expand Down
Expand Up @@ -16,7 +16,6 @@

#include "MantidDataHandling/LoadMuonNexus1.h"
#include "MantidAPI/WorkspaceFactory.h"
#include "MantidDataObjects/ManagedWorkspace2D.h"
#include "MantidAPI/AnalysisDataService.h"
#include "MantidAPI/FrameworkManager.h"
#include "MantidKernel/ConfigService.h"
Expand Down
Expand Up @@ -13,7 +13,6 @@

#include "MantidDataHandling/LoadMuonNexus2.h"
#include "MantidAPI/WorkspaceFactory.h"
#include "MantidDataObjects/ManagedWorkspace2D.h"
#include "MantidAPI/AnalysisDataService.h"
#include "MantidAPI/FrameworkManager.h"
#include "MantidKernel/ConfigService.h"
Expand Down

0 comments on commit fcbee20

Please sign in to comment.