Exemplo n.º 1
0
        public ParallelLoop(int fromInclusive, int toExclusive, ParallelPriolity priolity, LoopDelegate parallelFunction)
        {
            this.StartIndex = fromInclusive;
            this.EndIndex   = toExclusive;
            this.func       = parallelFunction;

            //코어16 : Low4, Normal8, High14
            //코어8 : Low2 Normal4 High6
            //코어6 : Low2 Normal3 High5
            //코어4 : Low1 Normal2 High3
            //코어2 : Low1 Normal1 High2
            switch (priolity)
            {
            case ParallelPriolity.Low:
                ThreadCount = ThreadCount_Low;
                break;

            case ParallelPriolity.Normal:
                ThreadCount = ThreadCount_Normal;
                break;

            case ParallelPriolity.High:
                ThreadCount = ThreadCount_High;
                break;

            case ParallelPriolity.Full:
                ThreadCount = ThreadCount_Full;
                break;
            }

            Init();
        }
Exemplo n.º 2
0
    public delegate void LoopDelegate <T0>(EntityData entityData, ref T0 component0); // genvariadic delegate

    public unsafe void Loop <T0>(LoopDelegate <T0> loopAction)                        // genvariadic function
        where T0 : unmanaged                                                          // genvariadic duplicate
    {
        var flags = stackalloc Flags[]
        {
            GetComponentFlag <T0>() // genvariadic duplicate ,
        };

        var   typeQty        = 1; // genvariadic quantity
        Flags archetypeFlags = Flags.Join(flags, typeQty);

        var matchingPools = new List <KeyValuePair <Flags, ArchetypePool> >();

        foreach (var pools in archetypePools_)
        {
            if (pools.Key.Contains(archetypeFlags))
            {
                matchingPools.Add(pools);
            }
        }

        //loop all pools and entities (todo MT)
        foreach (var matchingPool in matchingPools)
        {
            var comp0buffer = matchingPool.Value.GetComponentBuffer <T0>(flags[0]); // genvariadic duplicate

            for (int i = 0; i < matchingPool.Value.Count; i++)
            {
                loopAction(new EntityData(matchingPool.Key, i),
                           ref comp0buffer[i] // genvariadic duplicate ,
                           );
            }
        }
    }
Exemplo n.º 3
0
            public SingleTask(int threadIndex, ParallelLoop owner, LoopDelegate func)
            {
                this.ThreadIndex = threadIndex;
                this.Owner       = owner;
                this.func        = func;

                FindCheckRange();
            }
Exemplo n.º 4
0
        /// <summary>
        /// A parallel version of a standard for loop.
        /// </summary>
        /// <remarks>
        /// Parallel.For is similar to a standard for loop starting at index i = start and running as long as i is less than end.
        /// loopbody is called once in each iteration and index i is incremented by one after each iteration. <p/>
        /// GrainSize specifies a reasonable number of iterations in each task. If the number of iterations is larger than grainSize, data
        /// is split in two and handled separately.
        /// Adjusting the grain size can lead to better performance, but the optimal grain
        /// size depends on the problem at hand. Increasing the grain size will decrease the amount of tasks,
        /// and thus decrease the overhead of setting up tasks. But a small amount of tasks might introduce some load balancing problems,
        /// if no tasks are available for free processors. Decreasing the grain size will increase the amount of tasks and thereby ensure
        /// better load balancing, but on the other hand it will also increase the overhead of setting up tasks. Use
        /// Parallel.For(int start, int end, LoopDelegate loopbody) at first and once your code is working, experiment with
        /// the grain size.<p/>
        /// Parallel.For catches any uncaught exception raised inside the tasks. Once the exception is caught Parallel.For cancels all
        /// running tasks that it has started and throws a Jibu.CancelException, containing the original exception. Jibu cancels all
        /// tasks but it doesn't just brutally stop them - the user defined code must detect and handle the cancellation. For more information on cancellation see Jibu.Task.Cancel and Jibu.CancelException
        /// </remarks>
        /// <exception cref="Jibu.CancelException">If one of the Tasks is cancelled, Parallel.For cancels the remaining Tasks
        /// and throws a Jibu.CancelException.</exception>
        /// <exception cref="System.ArgumentOutOfRangeException">Is thrown if the grain size is less than 1.</exception>
        /// <param name="start">First index, which is incremented by one until it equals end.</param>
        /// <param name="end">Last index, which is not included in the loop. The For loop runs as long as start is less than end.</param>
        /// <param name="grainSize">A reasonable number of iterations in each task</param>
        /// <param name="loopbody">A delegate containing the work. Loopbody is executed once for each iteration.</param>
        // <example>
        // - Parallel For Example -
        // <code><include ForExample/ForExample.cs></code>
        // </example>
        public static void For(int start, int end, int grainSize, LoopDelegate loopbody)
        {
            if (grainSize < 1)
            {
                throw new ArgumentOutOfRangeException("Grain size cannot be less than 1.");
            }

            new DelegateAsync(delegate { ForTask(start, end, grainSize, loopbody); }).WaitFor();
        }
Exemplo n.º 5
0
        static void Main(string[] args)
        {
            var PH = new PharmacyForm();

            Application.Run(PH);
            //FlashCard.Get1FC(PH);
            if (args.Length == 0)
            {
                //MneumonicSystem.Phonetic();
                //TypingGame.TypingGameStart();
                //WorkingMemoryGame.Play(10, 3, true);//keep constant at this value for accurate stats
                //Arithmetic.Game(10, 2, 11, 20);

                //Console.WriteLine("Do a plank or prone y");
                //Timer();
                //FlashCard.FlashCards();
                //if scores increase run a rnd p**n file
            }
            else if (args[0] == "fc")
            {
                new Deck("").DisplayAllFC();
            }
            else if (args[0] == "tg")
            {
                TypingGame.TypingGameStart();
            }
            else if (args[0] == "ph")
            {
                MneumonicSystem.Phonetic();
            }
            else if (args[0] == "read")
            {
                LoopDelegate ld = new LoopDelegate(OneRandRead);

                InfRounds(ld);
            }
            else if (args[0] == "wm")
            {
                WorkingMemoryGame.Play(10, 2, true);//keep constant at this value for accurate stats
            }
            else if (args[0] == "celeb")
            {
                MneumonicSystem.Celebrity();
            }
            else if (args[0] == "ag")
            {
                Arithmetic.Game(10, 2, 4, 20);
            }
            else if (args[0] == "timer")
            {
                Timer();
            }
            else if (args[0] == "test")
            {
                TestP();
            }
        }
Exemplo n.º 6
0
        public static WDThreadPattern fromMethods(uint msToWaitForNormalTermination,
                                                  LoopDelegate onLoop, Action onStop = null, Action onStart = null)
        {
            WDThreadPattern t = new WDThreadPattern(msToWaitForNormalTermination);

            t.OnLoop  = onLoop;
            t.OnStart = onStart;
            t.OnStop  = onStop;
            return(t);
        }
Exemplo n.º 7
0
    public IEnumerator LoopEnum(float timeBetweenLoops, LoopDelegate loopable)
    {
        yield return(new WaitForSeconds(timeBetweenLoops));

        if (loopable())
        {
            yield return(StartCoroutine(LoopEnum(timeBetweenLoops, loopable)));
        }

        yield break;
    }
Exemplo n.º 8
0
        /// <summary>
        /// 스레드를 분할해 함수를 병렬로 실행합니다.
        /// </summary>
        /// <param name="parallelFunction">void (int startIndex, int endIndex) 형식의 병렬 함수</param>
        /// <param name="LoopRange">총 루프 횟수</param>
        /// <param name="priolity">분할 갯수</param>
        public ParallelLoop(int fromInclusive, int toExclusive, int threadCount, LoopDelegate parallelFunction, bool clipProcessorCount = true)
        {
            this.func       = parallelFunction;
            this.StartIndex = fromInclusive;
            this.EndIndex   = toExclusive;
            int coreCount = Environment.ProcessorCount;

            ThreadCount = clipProcessorCount ? Mathf.Clamp(threadCount, 1, coreCount) : threadCount;

            Init();
        }
Exemplo n.º 9
0
 public void ForLoop(LoopDelegate loop)
 {
     foreach (var item in data)
     {
         for (int i = 0; i < item.Value.Count; i++)
         {
             if (loop(item.Key, item.Value[i], i))
             {
                 return;
             }
         }
     }
 }
Exemplo n.º 10
0
 public static void ForLoop(int start, int end, LoopDelegate loopCode, bool multiThread)
 {
     if (multiThread)
     {
         Parallel.For(start, end, n => { loopCode(n); });
     }
     else
     {
         for (int n = start; n < end; n++)
         {
             loopCode(n);
         }
     }
 }
Exemplo n.º 11
0
        public static void InfRounds(LoopDelegate ld)
        {
            do
            {
                ld();
                Console.WriteLine("Continue?");
                var foo = Console.ReadLine();

                if (foo == "n" || foo == "N")
                {
                    break;
                }
                Console.Clear();
            }while(true);
        }
Exemplo n.º 12
0
        // ------------------------------------------------------------ Private auxillary methods -------------------------------------------------


        private static void ForTask(int start, int end, int grainSize, LoopDelegate loopbody)
        {
            int num = end - start;

            if (num > grainSize)
            {
                int   middle = (num / 2) + start;
                Async task   = new DelegateAsync(delegate { ForTask(start, middle, grainSize, loopbody); }).Start();
                ForTask(middle, end, grainSize, loopbody);

                task.WaitFor();
            }
            else
            {
                for (int i = start; i < end; i++)
                {
                    loopbody(i);
                }
            }
        }
Exemplo n.º 13
0
        public static FuncProfileResults ProfileFunction(int repeatCount, bool useMultithread, params Action[] actions)
        {
            FuncProfileResult[] results = new FuncProfileResult[actions.Length];

            LoopDelegate profileAction = (int startI, int endI) => {
                for (int actionI = startI; actionI < endI; ++actionI)
                {
                    Stopwatch watch = new Stopwatch();
                    watch.Restart();

                    Action action = actions[actionI];
                    if (action != null)
                    {
                        for (int repeatI = 0; repeatI < repeatCount; ++repeatI)
                        {
                            action();
                        }
                    }

                    watch.Stop();
                    results[actionI] = new FuncProfileResult(action, (float)watch.GetElapsedMilliseconds());
                }
            };

            if (useMultithread)
            {
                ParallelLoop pLoop = new ParallelLoop(0, actions.Length, ParallelPriolity.Full, profileAction);
                pLoop.RunWait();
            }
            else
            {
                profileAction(0, actions.Length);
            }

            return(new FuncProfileResults(results));
        }
Exemplo n.º 14
0
 // we need getter and setter because we can't directly access fields from RPython
 public void SetFunc(LoopDelegate func)
 {
     this.func = func;
 }
Exemplo n.º 15
0
 /// <summary>
 /// A parallel version of a standard for loop.
 /// </summary>
 /// <remarks>
 /// Parallel.For is similar to a standard for loop starting at index i = start and running as long as i is less than end.
 /// loopbody is called once in each iteration and index i is incremented by one after each iteration.<p/>
 /// Grain size is implicitly set to two, implying that the unit of work is one call to loopbody. Hence one task is created for
 /// each index i. It's often possible to increase performance by adjusting the grain size. To do so use
 /// Parallel.For(int start, int end, int grainSize, LoopDelegate loopbody).<p/>
 /// Parallel.For catches any uncaught exception raised inside the tasks. Once the exception is caught Parallel.For cancels all
 /// running tasks that it has started and throws a Jibu.CancelException, containing the original exception. Jibu cancels all
 /// tasks but it doesn't just brutally stop them - the user defined code must detect and handle the cancellation. For more
 /// information on cancellation see Jibu.Task.Cancel and Jibu.CancelException
 /// </remarks>
 /// <exception cref="Jibu.CancelException">If one of the Tasks is cancelled, Parallel.For cancels the remaining Tasks
 /// and throws a Jibu.CancelException.</exception>
 /// <param name="start">First index, which is incremented by one until it equals end.</param>
 /// <param name="end">Last index, which is not included in the loop. The For loop runs as long as start is less than end.</param>
 /// <param name="loopbody">A delegate containing the work. Loopbody is executed once for each iteration.</param>
 // <example>
 // - Parallel For Example -
 // <code><include ForExample/ForExample.cs></code>
 // </example>
 public static void For(int start, int end, LoopDelegate loopbody)
 {
     For(start, end, 1, loopbody);
 }
Exemplo n.º 16
0
 public static WDThreadPattern fromMethods(LoopDelegate onLoop, Action onStop = null, Action onStart = null)
 {
     return(fromMethods(100, onLoop, onStop, onStart));
 }
Exemplo n.º 17
0
 public void Loop(float timeBetweenLoops, LoopDelegate loopable)
 {
     StartCoroutine(LoopEnum(timeBetweenLoops, loopable));
 }
Exemplo n.º 18
0
 /// <summary>
 /// 스레드를 분할해 함수를 병렬로 실행합니다.
 /// </summary>
 /// <param name="parallelFunction">void (int startIndex, int endIndex) 형식의 병렬 함수</param>
 /// <param name="LoopRange">총 루프 횟수</param>
 /// <param name="threadCountFactor">코어 개수에 곱한 만큼의 스레드 생성</param>
 public ParallelLoop(int fromInclusive, int toExclusive, float threadCountFactor, LoopDelegate parallelFunction) : this(fromInclusive, toExclusive, (int)(Environment.ProcessorCount * threadCountFactor), parallelFunction)
 {
 }