public void GetMultiplexedBatch_SlowAsyncValueFactory_InvokesMinimum()
        {
            var taskMultiplexer             = new TaskMultiplexer <int, string>();
            int valueFactoryInvocationCount = 0;
            Func <ICollection <int>, Task <IDictionary <int, string> > > valueFactory = async keys =>
            {
                await Task.Delay(5000).ConfigureAwait(false);

                Interlocked.Increment(ref valueFactoryInvocationCount);
                var values = keys
                             .Distinct()
                             .ToDictionary(k => k, k => k.ToString());
                return(values);
            };

            int threadIndex = -1;

            ThreadHelpers.InvokeThreads(10, () =>
            {
                // *[0,5], *[1,6], *[2,7], *[3,8], *[4,9], [0,5], [1,6], [2,7], [3,8], [4,9]
                var key1 = Interlocked.Increment(ref threadIndex) % 5;
                var key2 = key1 + 5;
                var keys = new[] { key1, key2 };
                taskMultiplexer.GetMultiplexed(keys, valueFactory).Wait();
            });

            Assert.AreEqual(5, valueFactoryInvocationCount);
        }
Esempio n. 2
0
        public override void Process()
        {
            PreInterpolate();

            // Gather values from state
            RawImage img = new RawImage(state);

            double[,] xyz_rgb = state.xyz_rgb;
            float[,] rgb_cam  = state.rgb_cam;
            float[] d65_white = state.d65_white;
            uint    filters   = state.filters;

            //-------------------------

            border_interpolate(img, filters, BORDER);

            float[,] xyz_cam = new float[3, 4];
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < img.colours; j++)
                {
                    for (int k = 0; k < 3; k++)
                    {
                        xyz_cam[i, j] += (float)xyz_rgb[i, k] * rgb_cam[k, j] / d65_white[i];
                    }
                }
            }

            Dictionary <Thread, TileData> threadTileData = new Dictionary <Thread, TileData>();

            using (TaskMultiplexer multiplexer = new TaskMultiplexer())
            {
                for (int top = 2; top < img.height - BORDER; top += TILE_SIZE - BORDER - 1)
                {
                    for (int left = 2; left < img.width - BORDER; left += TILE_SIZE - BORDER - 1)
                    {
                        int topCopy  = top;
                        int leftCopy = left;
                        multiplexer.QueueWorkItem(delegate
                        {
                            TileData td;

                            lock (threadTileData)
                            {
                                threadTileData.TryGetValue(Thread.CurrentThread, out td);
                                if (td == null)
                                {
                                    td = new TileData();
                                    threadTileData[Thread.CurrentThread] = td;
                                }
                            }

                            ProcessTile(img, leftCopy, topCopy, filters, xyz_cam, td);
                        });
                    }
                }
            }
        }
        public void GetMultiplexed_SlowSyncValueFactory_InvokesOnce()
        {
            var taskMultiplexer                     = new TaskMultiplexer <int, string>();
            int valueFactoryInvocationCount         = 0;
            Func <int, Task <string> > valueFactory = x =>
            {
                Thread.Sleep(5000);
                Interlocked.Increment(ref valueFactoryInvocationCount);
                return(Task.FromResult(x.ToString()));
            };

            ThreadHelpers.InvokeThreads(10, () => taskMultiplexer.GetMultiplexed(1, valueFactory).Wait());

            Assert.AreEqual(1, valueFactoryInvocationCount);
        }
        public void GetMultiplexed_SlowAsyncValueFactory_InvokesOnce()
        {
            var taskMultiplexer                     = new TaskMultiplexer <int, string>();
            int valueFactoryInvocationCount         = 0;
            Func <int, Task <string> > valueFactory = async x =>
            {
                await Task.Delay(5000).ConfigureAwait(false);

                Interlocked.Increment(ref valueFactoryInvocationCount);
                return(x.ToString());
            };

            ThreadHelpers.InvokeThreads(10, () => taskMultiplexer.GetMultiplexed(1, valueFactory).Wait());

            Assert.AreEqual(1, valueFactoryInvocationCount);
        }