// // Copyright (c) Microsoft. All rights reserved. // Licensed under the MIT license. See LICENSE.md file in the project root for full license information. // #pragma once #include #include #include #include "CUDATimer.h" #include #include #include extern bool g_useMemorySwapping; extern bool g_shareNodeValueMatrices; namespace Microsoft { namespace MSR { namespace CNTK { // forward declarations class ComputationNodeBase; class FrameRange; template class SwapInAction; template class SwapOutAction; template class SwapAction; template class Matrix; template class SwapManager { private: std::unordered_map*, SwapInAction*> m_buffer2SwapIn; std::unordered_map*, SwapOutAction*> m_buffer2SwapOut; std::unordered_map*> > m_node2ForwardSwapOut; std::unordered_map*> > m_node2BackwardSwapin; std::unordered_map*> > m_node2BackwardFree; // singleton constructor std::unordered_map m_node2Timestep; std::unordered_map m_timeStep2Node; CUDATimer m_timer; void CleanUp(); float m_minFreeMemory; float m_freed; float m_swappedOut; float m_swappedIn; bool m_wasForward; public: SwapManager(); ~SwapManager(){}; float FreeGPUMemoryInGB(); // this is called BEFORE a ForwardProp / BackpropTo method call void BeginSynchronizeState(ComputationNodeBase *node, bool isForward, bool isTraining); // this is called AFTER a ForwardProp / BackpropTo method call void EndSynchronizeState(ComputationNodeBase *node, bool isForward, bool isTraining); void SwapOutNodes(ComputationNodeBase* node, bool isForward, bool isTraining, int n); bool m_useMemorySwapping; void ClearActionsAndTheirMemory(); void InitializeSwapping(std::unordered_map*> > forwardSwapOutNodes2matrices, std::unordered_map*> > backwardSwapInNodes2matrices, std::unordered_map*> > lastBackwardNodes2matrices); }; }}}