public bool ChangeModel(IModelChanges changes)
        {
            if (!m_shouldShowNewLearningTask)
            {
                return(false);
            }

            if (CurrentWorld != null)
            {
                changes.RemoveNode(CurrentWorld.World);
            }
            if (Curriculum.IsLast())
            {
                // stop execution
                if (CurrentLearningTask != null)
                {
                    CurriculumFinished(this, new SchoolEventArgs(CurrentLearningTask));
                    LearningTaskFinished(this, new SchoolEventArgs(CurrentLearningTask));
                }
                CurrentLearningTask = null;
                if (Owner.SimulationHandler.CanPause)
                {
                    Owner.SimulationHandler.PauseSimulation();
                }
                return(false);
            }
            if (CurrentLearningTask == null)
            {
                CurriculumStarting(this, EventArgs.Empty);
            }

            if (CurrentLearningTask != null)
            {
                LearningTaskFinished(this, new SchoolEventArgs(CurrentLearningTask));
            }
            CurrentLearningTask = Curriculum.GetNext();
            LearningTaskNew(this, new SchoolEventArgs(CurrentLearningTask));

            CurrentWorld = (IWorldAdapter)Owner.CreateNode(CurrentLearningTask.RequiredWorldType);
            if (CurrentWorld.CopyDataThroughCPU != CopyDataThroughCPU)  // to avoid setting the value when simulation is not stopped (which caused assert error in setter)
            {
                CurrentWorld.CopyDataThroughCPU = CopyDataThroughCPU;
            }
            CurrentWorld.World.EnableDefaultTasks();
            changes.AddNode(CurrentWorld.World);
            changes.AddNode(this);

            m_shouldShowNewLearningTask = false;
            m_isAfterChangeModelExecute = true;
            m_isAfterChangeModelInit    = true;
            return(true);
        }
Exemple #2
0
        private void SetupAfterModelChange(IModelChanges modelChanges, List <MyNode> changersActivated)
        {
            // Clean up memory.
            IterateNodes(modelChanges.RemovedNodes, DestroyNode);

            // This must happen before UpdateMemoryModel() because some nodes touch kernels in UpdateMemoryBlocks().
            MyKernelFactory.Instance.SetCurrent(0);

            // Refresh topological ordering.
            List <MyNode> orderedNodes = MySimulationHandler.OrderNetworkNodes(m_project.Network);

            // Update the whole memory model.
            // TODO(HonzaS): This may break things, check.
            // We'll need to forbid changing of count after the simulation has started with the exception of added nodes.
            // However, the added nodes may lead to reallocation of blocks - deal with it.
            bool updatesNotConverged = UpdateMemoryModel(m_project, orderedNodes);

            Validator.ClearValidation();

            // Validate new nodes.
            IterateNodes(modelChanges.AddedNodes, ValidateNode);

            Validator.AssertError(!updatesNotConverged, m_project.Network, "Possible infinite loop in memory block sizes.");

            if (!Validator.ValidationSucessfull)
            {
                throw new InvalidOperationException("Validation failed for the changed model.");
            }

            // Reschedule.
            Schedule(m_project, modelChanges.AddedNodes);

            // Init nodes
            IterateNodes(modelChanges.AddedNodes, InitNode);

            // Allocate memory
            IEnumerable <MyWorkingNode> nodesToAllocate =
                modelChanges.AddedNodes.Where(node => MyMemoryManager.Instance.IsRegistered(node));

            IterateNodes(nodesToAllocate, AllocateNode);

            foreach (MyNode node in changersActivated)
            {
                EmitModelChanged(node);
            }
        }
        private void SetupAfterModelChange(IModelChanges modelChanges, List<MyNode> changersActivated)
        {
            // Clean up memory.
            IterateNodes(modelChanges.RemovedNodes, DestroyNode);

            // This must happen before UpdateMemoryModel() because some nodes touch kernels in UpdateMemoryBlocks().
            MyKernelFactory.Instance.SetCurrent(0);

            // Refresh topological ordering.
            List<MyNode> orderedNodes = MySimulationHandler.OrderNetworkNodes(m_project.Network);

            // Update the whole memory model.
            // TODO(HonzaS): This may break things, check.
            // We'll need to forbid changing of count after the simulation has started with the exception of added nodes.
            // However, the added nodes may lead to reallocation of blocks - deal with it.
            bool updatesNotConverged = UpdateMemoryModel(m_project, orderedNodes);

            Validator.ClearValidation();

            // Validate new nodes.
            IterateNodes(modelChanges.AddedNodes, ValidateNode);

            Validator.AssertError(!updatesNotConverged, m_project.Network, "Possible infinite loop in memory block sizes.");

            if (!Validator.ValidationSucessfull)
                throw new InvalidOperationException("Validation failed for the changed model.");

            // Reschedule.
            Schedule(m_project, modelChanges.AddedNodes);

            // Init nodes
            IterateNodes(modelChanges.AddedNodes, InitNode);

            // Allocate memory
            IEnumerable<MyWorkingNode> nodesToAllocate =
                modelChanges.AddedNodes.Where(node => MyMemoryManager.Instance.IsRegistered(node));
            IterateNodes(nodesToAllocate, AllocateNode);

            foreach (MyNode node in changersActivated)
                EmitModelChanged(node);
        }