Esempio n. 1
0
        public async Task ParallelTaskUpdateState()
        {
            var tasks = new List <Task>();

            for (int i = 0; i < gridHeight; i++)
            {
                int capturedI = i;
                var task      = Task.Run(() =>
                {
                    for (int j = 0; j < gridWidth; j++)
                    {
                        int capturedJ     = j;
                        var liveNeighbors = GetLiveNeighbors(capturedI, capturedJ);
                        nextState[capturedI, capturedJ] =
                            LifeRules.GetNewState(CurrentState[capturedI, capturedJ], liveNeighbors);
                    }
                });
                tasks.Add(task);
            }

            await Task.WhenAll(tasks);

            CurrentState = nextState;
            nextState    = new CellState[gridHeight, gridWidth];
        }
Esempio n. 2
0
        public Task OverlyParallelForUpdateState()
        {
            Parallel.For(0, gridHeight, i =>
            {
                Parallel.For(0, gridWidth, j =>
                {
                    var liveNeighbors = GetLiveNeighbors(i, j);
                    nextState[i, j]   =
                        LifeRules.GetNewState(CurrentState[i, j], liveNeighbors);
                });
            });

            CurrentState = nextState;
            nextState    = new CellState[gridHeight, gridWidth];
            return(Task.CompletedTask);
        }
Esempio n. 3
0
        public Task UpdateState()
        {
            for (int i = 0; i < gridHeight; i++)
            {
                for (int j = 0; j < gridWidth; j++)
                {
                    var liveNeighbors = GetLiveNeighbors(i, j);
                    nextState[i, j] =
                        LifeRules.GetNewState(CurrentState[i, j], liveNeighbors);
                }
            }

            CurrentState = nextState;
            nextState    = new CellState[gridHeight, gridWidth];
            return(Task.CompletedTask);
        }