From 4adff8fc2a244a777222e46958f9bc0fdf2cecc3 Mon Sep 17 00:00:00 2001 From: Nico Stuurman Date: Mon, 4 Nov 2024 11:25:14 -0800 Subject: [PATCH] Core-fast initialization: replaced deque with a vector (simpler), and 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. --- MMCore/MMCore.cpp | 15 +++++++++------ MMCore/MMCore.h | 2 +- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/MMCore/MMCore.cpp b/MMCore/MMCore.cpp index 95caa1062..161726a20 100644 --- a/MMCore/MMCore.cpp +++ b/MMCore/MMCore.cpp @@ -911,7 +911,7 @@ void CMMCore::initializeAllDevicesParallel() throw (CMMError) std::vector devices = deviceManager_->GetDeviceList(); LOG_INFO(coreLogger_) << "Will initialize " << devices.size() << " devices"; - std::map, std::deque, std::string>>> moduleMap; + std::map, std::vector, std::string>>> moduleMap; std::vector> ports; // first round, collect all DeviceAdapters @@ -935,7 +935,7 @@ void CMMCore::initializeAllDevicesParallel() throw (CMMError) if (moduleMap.find(pAdapter) == moduleMap.end()) { - std::deque, std::string>> pDevices; + std::vector, std::string>> pDevices; pDevices.push_back(make_pair(pDevice, devices[i])); moduleMap.insert({ pAdapter, pDevices }); } @@ -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> futures; - std::map, std::deque, std::string>>>::iterator it; + std::map, std::vector, 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) { @@ -972,7 +972,7 @@ void CMMCore::initializeAllDevicesParallel() throw (CMMError) // assign default roles syncronously for (it = moduleMap.begin(); it != moduleMap.end(); it++) { - std::deque, std::string>> pDevices = it->second; + std::vector, std::string>> pDevices = it->second; for (int i = 0; i < pDevices.size(); i++) { assignDefaultRole(pDevices[i].first); @@ -981,6 +981,9 @@ 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(); } @@ -988,7 +991,7 @@ void CMMCore::initializeAllDevicesParallel() throw (CMMError) * 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::string>> pDevices) { +int CMMCore::initializeVectorOfDevices(std::vector, std::string>> pDevices) { for (int i = 0; i < pDevices.size(); i++) { std::shared_ptr pDevice = pDevices[i].first; diff --git a/MMCore/MMCore.h b/MMCore/MMCore.h index c8601a620..6889614e0 100644 --- a/MMCore/MMCore.h +++ b/MMCore/MMCore.h @@ -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::string>> pDevices); + int initializeVectorOfDevices(std::vector, std::string>> pDevices); }; #if defined(__GNUC__) && !defined(__clang__)