Exemple #1
0
 private void init(int number, bool previewMode, EntropySettings entropySettings)
 {
     dimensions              = ChaosSettings_implant.getDimensions();
     sampleCount             = number;
     pMode                   = previewMode;
     entropySettings_implant = entropySettings;
 }
    private Results_implant entropyEval_implant(ChaosSettings_implant currentJobSettings)
    {
        // UI handler has already applied 'accuracy' to the resolution and angular resolution values.

        ChaosEngine_implant currentJobEngine = new(currentJobSettings, commonVars.getImplantSimulationSettings(), commonVars.getImplantSettings());

        Results_implant evalResults = new();

        if (!currentJobEngine.isValid())
        {
            return(evalResults);
        }

        evalResults.setResult(currentJobEngine.getResult());
        evalResults.setMin(currentJobEngine.getMin());
        evalResults.setMax(currentJobEngine.getMax());
        evalResults.resistWidthVar  = currentJobSettings.getValue(ChaosSettings_implant.properties.resistCDVar);
        evalResults.resistHeightVar = currentJobSettings.getValue(ChaosSettings_implant.properties.resistHeightVar);
        evalResults.resistCRRVar    = currentJobSettings.getValue(ChaosSettings_implant.properties.resistTopCRRVar);
        evalResults.tiltVar         = currentJobSettings.getValue(ChaosSettings_implant.properties.tiltVar);
        evalResults.twistVar        = currentJobSettings.getValue(ChaosSettings_implant.properties.twistVar);
        evalResults.setResistShapes(commonVars.getColors(), currentJobEngine.getGeom(), currentJobEngine.getBGGeom(), currentJobEngine.getShadow(), currentJobEngine.getMinShadow(), currentJobEngine.getMaxShadow());
        evalResults.setValid(currentJobEngine.isValid());
        return(evalResults);
    }
    private void entropyRunCore_implant_singleThread(bool previewMode, int numberOfCases)
    {
        const int numberOfResultsFields = 1;
        // Let's sample this
        const int sampleRate = 100;

        setSampler_implant(numberOfCases, previewMode);
        sampler_implant.sample(false);
        sw.Start();

        implantResultPackage = new SimResultPackage(ref varianceContext.implantPreviewLock, numberOfCases, numberOfResultsFields);
        sw.Reset();
        for (int i = 0; i < numberOfCases; i++)
        {
            sw.Start();
            currentProgress = i + 1;
            try
            {
                // Get the results from the implant calc engine.
                ChaosSettings_implant cs = sampler_implant.getSample(i);
                implantResultPackage.Add(entropyEval_implant(cs));

                if (numberOfCases == 1 || currentProgress % sampleRate == 0)
                {
                    // Update the preview configuration.
                    if (implantResultPackage.getImplantResult(i).isValid())
                    {
                        if (numberOfCases > 1)
                        {
                            try
                            {
                                resultString = implantResultPackage.getMeanAndStdDev();
                            }
                            catch (Exception)
                            {
                                // Non-critical if an exception is raised. Ignore and carry on.
                            }
                        }
                        else
                        {
                            // Need to workaround some oddness here. The .ToString() calls below seemed to turn "0.0" into blanks.
                            string tmp = Convert.ToDouble(implantResultPackage.getImplantResult(i).getResult()).ToString("0.##");
                            resultString += tmp + ",";

                            tmp           = Convert.ToDouble(implantResultPackage.getImplantResult(i).getMin()).ToString("0.##");
                            resultString += tmp + ",";

                            tmp           = Convert.ToDouble(implantResultPackage.getImplantResult(i).getMax()).ToString("0.##");
                            resultString += tmp;
                        }
                        updateImplantSimUIFunc?.Invoke(false, implantResultPackage, resultString);
                    }
                    // Force redraw. We could use the progress bar to repaint, though.
                    // Note that this is an ugly hack to also ensure we collect stop button presses.
                    forceImplantRepaintFunc?.Invoke();
                }
            }
            catch (Exception)
            {
                // Reject the case - something happened.
            }
            // Nudge progress bar.
            if (numberOfCases > 1)
            {
                timeOfFlight_implant(sw.Elapsed.TotalSeconds);
                stepProgress?.Invoke();
            }

            // Check if user cancelled.
            if (abortRunFunc == null)
            {
                continue;
            }

            abortRunFunc();
            if (!commonVars.runAbort)
            {
                continue;
            }

            sw.Stop();
            implantResultPackage.setState(false);
            commonVars.runAbort = false; // reset state to allow user to abort save of results.
            break;
        }

        implantResultPackage.setRunTime(sw.Elapsed.TotalSeconds);
        sw.Stop();
    }
    private void entropyRunCore_implant_multipleThread(bool previewMode, int numberOfCases)
    {
        setSampler_implant(numberOfCases, previewMode);
        sampler.sample(true, false);

        const int numberOfResultsFields = 1;

        implantResultPackage = new SimResultPackage(ref varianceContext.implantPreviewLock, numberOfCases, numberOfResultsFields);

        multithreadWarningFunc?.Invoke();

        // Set up timers for the UI refresh
        commonVars.m_timer = new Timer {
            AutoReset = true, Interval = CentralProperties.timer_interval
        };
        updateImplantSimUIMTFunc?.Invoke();
        commonVars.m_timer.Start();

        // Set our parallel task options based on user settings.
        ParallelOptions po = new();
        // Attempt at parallelism.
        CancellationTokenSource cancelSource      = new();
        CancellationToken       cancellationToken = cancelSource.Token;

        switch (varianceContext.numberOfThreads)
        {
        case -1:
            po.MaxDegreeOfParallelism = commonVars.HTCount;
            break;

        default:
            po.MaxDegreeOfParallelism = varianceContext.numberOfThreads;
            break;
        }

        if (commonVars.getImplantSettings_nonSim().getInt(EntropySettings_nonSim.properties_i.greedy) == 0)
        {
            if (po.MaxDegreeOfParallelism > 1) // avoid setting to 0
            {
                po.MaxDegreeOfParallelism -= 1;
            }
        }

        try
        {
            Parallel.For(0, numberOfCases, po, (i, loopState) =>
            {
                try
                {
                    ChaosSettings_implant cs      = sampler_implant.getSample(i);
                    Results_implant currentResult = entropyEval_implant(cs);

                    if (currentResult.isValid())     // only update if result is valid.
                    {
                        try
                        {
                            {
                                if (commonVars.getImplantSettings_nonSim().getInt(EntropySettings_nonSim.properties_i.external) == 1 && baseFileName != "")
                                {
                                    switch (commonVars.getImplantSettings_nonSim().getInt(EntropySettings_nonSim.properties_i.externalType))
                                    {
                                    case (int)CommonVars.external_Type.svg:
                                        writeSVG(currentResult, i, numberOfCases);
                                        break;

                                    default:
                                        writeLayout_implant(currentResult, i, numberOfCases, commonVars.getImplantSettings_nonSim().getInt(EntropySettings_nonSim.properties_i.externalType));
                                        break;
                                    }
                                }
                                implantResultPackage.Add(currentResult);
                            }
                        }
                        catch (Exception)
                        {
                        }
                    }
                    Interlocked.Increment(ref currentProgress);

                    forceImplantRepaintFunc?.Invoke();

                    abortRunFuncMT?.Invoke(implantResultPackage, cancelSource, cancellationToken);
                }
                catch (OperationCanceledException)
                {
                    implantResultPackage.setState(false);
                    // ReSharper disable once AccessToDisposedClosure
                    commonVars.m_timer.Stop();
                    commonVars.runAbort = false;     // reset state to allow user to abort save of results.
                    sw.Stop();
                    loopState.Stop();
                }
            }
                         );
        }
        catch (OperationCanceledException)
        {
            implantResultPackage.setState(false);
            commonVars.runAbort = false; // reset state to allow user to abort save of results.
            commonVars.m_timer.Stop();
            sw.Stop();
        }
        catch (AggregateException)
        {
            commonVars.m_timer.Stop();
            sw.Stop();
        }
        catch (Exception)
        {
        }
        commonVars.m_timer.Stop();
        commonVars.m_timer.Dispose();
        implantResultPackage.setRunTime(swTime);
        sw.Stop();
        sw.Reset();
    }