Ejemplo n.º 1
0
        //private static void TestLocalThreadQueueAsQueue(ILocalThreadQueue q, int elemCount, int fillFactor)
        //{
        //    int trackElemCount = elemCount;

        //    Stopwatch sw = Stopwatch.StartNew();


        //    while (trackElemCount > 0)
        //    {
        //        int initial = trackElemCount;

        //        for (int i = 0; i < fillFactor; i++)
        //        {
        //            if (!q.TryAddGlobal(initial--))
        //                Console.WriteLine("11");
        //        }

        //        initial = trackElemCount;
        //        for (int i = 0; i < fillFactor; i++)
        //        {
        //            object tmp = null;
        //            //q.TryRemove(out tmp);
        //            if (!q.TryRemoveGlobal(out tmp))
        //                Console.WriteLine("22");
        //            if ((int)tmp != initial--)
        //                Console.WriteLine("33");
        //        }
        //        trackElemCount -= fillFactor;
        //    }

        //    sw.Stop();

        //    Console.WriteLine(q.GetType().Name + ". Element count = " + elemCount.ToString() + ", FillFactor = " + fillFactor.ToString() + ", Time = " + sw.ElapsedMilliseconds.ToString() + "ms");
        //    Console.WriteLine();
        //}


        //private static void TestLocalThreadQueue2AsQueue(ILocalThreadQueue2 q, int elemCount, int fillFactor)
        //{
        //    int trackElemCount = elemCount;

        //    Stopwatch sw = Stopwatch.StartNew();


        //    while (trackElemCount > 0)
        //    {
        //        int initial = trackElemCount;

        //        for (int i = 0; i < fillFactor; i++)
        //        {
        //            if (!q.TryAddLocal(initial--))
        //                Console.WriteLine("11");
        //        }

        //        initial = trackElemCount;
        //        for (int i = 0; i < fillFactor; i++)
        //        {
        //            object tmp = null;
        //            //q.TryRemove(out tmp);
        //            if (!q.TryTakeLocal(out tmp))
        //                Console.WriteLine("22");
        //            if ((int)tmp != initial--)
        //                Console.WriteLine("33");
        //        }
        //        trackElemCount -= fillFactor;
        //    }

        //    sw.Stop();

        //    Console.WriteLine(q.GetType().Name + ". Element count = " + elemCount.ToString() + ", FillFactor = " + fillFactor.ToString() + ", Time = " + sw.ElapsedMilliseconds.ToString() + "ms");
        //    Console.WriteLine();
        //}


        //private static void TestLocalThreadQueueAsStack(LocalThreadQueue q, int elemCount, int fillFactor)
        //{
        //    int trackElemCount = elemCount;

        //    Stopwatch sw = Stopwatch.StartNew();


        //    while (trackElemCount > 0)
        //    {
        //        int initial = trackElemCount;
        //        for (int i = 0; i < fillFactor; i++)
        //        {
        //            if (!q.TryAddGlobal(initial--))
        //                Console.WriteLine("11");
        //        }

        //        for (int i = 0; i < fillFactor; i++)
        //        {
        //            object tmp = null;
        //            //q.TryRemove(out tmp);
        //            if (!q.TryRemoveGlobal(out tmp))
        //                Console.WriteLine("22");
        //            if ((int)tmp != ++initial)
        //                Console.WriteLine("33");
        //        }
        //        trackElemCount -= fillFactor;
        //    }

        //    sw.Stop();

        //    Console.WriteLine(q.GetType().Name + ". Element count = " + elemCount.ToString() + ", FillFactor = " + fillFactor.ToString() + ", Time = " + sw.ElapsedMilliseconds.ToString() + "ms");
        //    Console.WriteLine();
        //}


        private static void TestLocalThreadQueue2AsStack(ThreadPoolLocalQueue q, int elemCount, int fillFactor)
        {
            int trackElemCount = elemCount;

            Stopwatch sw = Stopwatch.StartNew();


            while (trackElemCount > 0)
            {
                int initial = trackElemCount;
                for (int i = 0; i < fillFactor; i++)
                {
                    if (!q.TryAddLocal(new TestThreadPoolItem(initial--)))
                    {
                        Console.WriteLine("11");
                    }
                }

                for (int i = 0; i < fillFactor; i++)
                {
                    ThreadPoolWorkItem tmp = null;
                    //q.TryRemove(out tmp);
                    if (!q.TryTakeLocal(out tmp))
                    {
                        Console.WriteLine("22");
                    }
                    if ((TestThreadPoolItem)tmp != ++initial)
                    {
                        Console.WriteLine("33");
                    }
                }
                trackElemCount -= fillFactor;
            }

            sw.Stop();

            Console.WriteLine(q.GetType().Name + ". Element count = " + elemCount.ToString() + ", FillFactor = " + fillFactor.ToString() + ", Time = " + sw.ElapsedMilliseconds.ToString() + "ms");
            Console.WriteLine();
        }
Ejemplo n.º 2
0
        private static bool TestLocalThreadQueuePrimaryScenario(ThreadPoolLocalQueue q, int elemCount, int slealThCount, int fillFactor, bool useRandom)
        {
            int trackElemCount = elemCount;
            int addFinished    = 0;

            int atomicRandom = 0;

            Thread mainThread = null;

            Thread[] stealThreads = new Thread[slealThCount];

            List <int> global = new List <int>(elemCount);

            Action mainAction = () =>
            {
                List <int> data = new List <int>(elemCount);

                Random rnd = null;
                if (useRandom)
                {
                    rnd = new Random(Environment.TickCount + Interlocked.Increment(ref atomicRandom) * slealThCount);
                }

                while (Volatile.Read(ref trackElemCount) >= 0)
                {
                    int addCount = fillFactor;
                    if (rnd != null)
                    {
                        addCount = rnd.Next(fillFactor);
                    }

                    for (int i = 0; i < addCount; i++)
                    {
                        int item = --trackElemCount;
                        if (item < 0)
                        {
                            break;
                        }
                        if (!q.TryAddLocal(new TestThreadPoolItem(item)))
                        {
                            ++trackElemCount;
                            break;
                        }
                    }

                    int removeCount = fillFactor;
                    if (rnd != null)
                    {
                        removeCount = rnd.Next(fillFactor);
                    }

                    for (int i = 0; i < removeCount; i++)
                    {
                        ThreadPoolWorkItem item = null;
                        if (!q.TryTakeLocal(out item))
                        {
                            break;
                        }
                        data.Add((TestThreadPoolItem)item);
                    }
                }

                Interlocked.Increment(ref addFinished);

                ThreadPoolWorkItem finalItem = null;
                while (q.TryTakeLocal(out finalItem))
                {
                    data.Add((TestThreadPoolItem)finalItem);
                }

                lock (global)
                    global.AddRange(data);
            };

            Action stealAction = () =>
            {
                Random rnd = null;
                if (useRandom)
                {
                    rnd = new Random(Environment.TickCount + Interlocked.Increment(ref atomicRandom) * slealThCount);
                }

                List <int> data = new List <int>();

                while (Volatile.Read(ref addFinished) < 1 && Volatile.Read(ref trackElemCount) > elemCount / 1000)
                {
                    ThreadPoolWorkItem tmp;
                    if (q.TrySteal(out tmp))
                    {
                        data.Add((TestThreadPoolItem)tmp);
                    }

                    int sleepTime = Volatile.Read(ref trackElemCount) % 2;
                    if (rnd != null)
                    {
                        sleepTime = rnd.Next(2);
                    }
                    if (sleepTime > 0)
                    {
                        Thread.Sleep(sleepTime);
                    }
                }

                lock (global)
                    global.AddRange(data);
            };


            mainThread = new Thread(new ThreadStart(mainAction));
            for (int i = 0; i < stealThreads.Length; i++)
            {
                stealThreads[i] = new Thread(new ThreadStart(stealAction));
            }

            Stopwatch sw = Stopwatch.StartNew();

            mainThread.Start();
            for (int i = 0; i < stealThreads.Length; i++)
            {
                stealThreads[i].Start();
            }


            mainThread.Join();
            for (int i = 0; i < stealThreads.Length; i++)
            {
                stealThreads[i].Join();
            }


            sw.Stop();

            bool result = true;

            global.Sort();
            if (global.Count != elemCount)
            {
                result = false;
                Console.WriteLine("Incorrect element count");
            }

            HashSet <int> set = new HashSet <int>(global);

            if (set.Count != global.Count)
            {
                result = false;
                Console.WriteLine("Incorrect distinct element count");
            }

            for (int i = 0; i < Math.Min(elemCount, global.Count); i++)
            {
                if (global[i] != i)
                {
                    result = false;
                    Console.WriteLine("Incorrect data");
                    break;
                }
            }


            Console.WriteLine("PrimaryScenario " + q.GetType().Name + ". Element count = " + elemCount.ToString() + ", Time = " + sw.ElapsedMilliseconds.ToString() + "ms");
            Console.WriteLine();
            return(result);
        }