Skip to content

Commit

Permalink
Core-fast initialization: replaced deque with a vector (simpler), and…
Browse files Browse the repository at this point in the history
… clear the used vectors at the end of the function.

I am not quite sure if this is needed, but after this change, I no longer
saw an exception on Micro-Manager shutdown, so it may be helpful.
  • Loading branch information
nicost committed Nov 4, 2024
1 parent f3dba34 commit 4adff8f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
15 changes: 9 additions & 6 deletions MMCore/MMCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -911,7 +911,7 @@ void CMMCore::initializeAllDevicesParallel() throw (CMMError)
std::vector<std::string> devices = deviceManager_->GetDeviceList();
LOG_INFO(coreLogger_) << "Will initialize " << devices.size() << " devices";

std::map<std::shared_ptr<LoadedDeviceAdapter>, std::deque<std::pair<std::shared_ptr<DeviceInstance>, std::string>>> moduleMap;
std::map<std::shared_ptr<LoadedDeviceAdapter>, std::vector<std::pair<std::shared_ptr<DeviceInstance>, std::string>>> moduleMap;
std::vector<std::shared_ptr<DeviceInstance>> ports;

// first round, collect all DeviceAdapters
Expand All @@ -935,7 +935,7 @@ void CMMCore::initializeAllDevicesParallel() throw (CMMError)

if (moduleMap.find(pAdapter) == moduleMap.end())
{
std::deque<std::pair<std::shared_ptr<DeviceInstance>, std::string>> pDevices;
std::vector<std::pair<std::shared_ptr<DeviceInstance>, std::string>> pDevices;
pDevices.push_back(make_pair(pDevice, devices[i]));
moduleMap.insert({ pAdapter, pDevices });
}
Expand All @@ -957,10 +957,10 @@ void CMMCore::initializeAllDevicesParallel() throw (CMMError)

// second round, spin up threads to initialize non-port devices, one thread per module
std::vector<std::future<int>> futures;
std::map<std::shared_ptr<LoadedDeviceAdapter>, std::deque<std::pair<std::shared_ptr<DeviceInstance>, std::string>>>::iterator it;
std::map<std::shared_ptr<LoadedDeviceAdapter>, std::vector<std::pair<std::shared_ptr<DeviceInstance>, std::string>>>::iterator it;
for (it = moduleMap.begin(); it != moduleMap.end(); it++)
{
auto f = std::async(std::launch::async, &CMMCore::initializeDequeOfDevices, this, it->second);
auto f = std::async(std::launch::async, &CMMCore::initializeVectorOfDevices, this, it->second);
futures.push_back(std::move(f));
}
for (auto& f : futures) {
Expand All @@ -972,7 +972,7 @@ void CMMCore::initializeAllDevicesParallel() throw (CMMError)
// assign default roles syncronously
for (it = moduleMap.begin(); it != moduleMap.end(); it++)
{
std::deque<std::pair<std::shared_ptr<DeviceInstance>, std::string>> pDevices = it->second;
std::vector<std::pair<std::shared_ptr<DeviceInstance>, std::string>> pDevices = it->second;
for (int i = 0; i < pDevices.size(); i++)
{
assignDefaultRole(pDevices[i].first);
Expand All @@ -981,14 +981,17 @@ void CMMCore::initializeAllDevicesParallel() throw (CMMError)
LOG_INFO(coreLogger_) << "Finished initializing " << devices.size() << " devices";

updateCoreProperties();
// not sure if this cleanup is needed, but should not hurt:
moduleMap.clear();
ports.clear();
}


/**
* This helper function is executed by a single thread, allowing initializeAllDevices to operate multi-threaded.
* All devices are supposed to originate from the same device adapter
*/
int CMMCore::initializeDequeOfDevices(std::deque<std::pair<std::shared_ptr<DeviceInstance>, std::string>> pDevices) {
int CMMCore::initializeVectorOfDevices(std::vector<std::pair<std::shared_ptr<DeviceInstance>, std::string>> pDevices) {
for (int i = 0; i < pDevices.size(); i++) {
std::shared_ptr<DeviceInstance> pDevice = pDevices[i].first;

Expand Down
2 changes: 1 addition & 1 deletion MMCore/MMCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ class CMMCore
void loadSystemConfigurationImpl(const char* fileName) throw (CMMError);
void initializeAllDevicesSerial() throw (CMMError);
void initializeAllDevicesParallel() throw (CMMError);
int initializeDequeOfDevices(std::deque<std::pair<std::shared_ptr<DeviceInstance>, std::string>> pDevices);
int initializeVectorOfDevices(std::vector<std::pair<std::shared_ptr<DeviceInstance>, std::string>> pDevices);
};

#if defined(__GNUC__) && !defined(__clang__)
Expand Down

0 comments on commit 4adff8f

Please sign in to comment.