📜 ⬆️ ⬇️

The book "Competitiveness and concurrency on the platform .NET. Patterns of effective design "

image Hi, Habrozhiteli! The Ricardo Terrelli book (Riccardo Terrell) provides insight into the recommended methods for creating competitive and scalable programs in .NET, highlighting the advantages of the functional paradigm and providing the appropriate tools and principles to easily and properly maintain competitiveness. As a result, armed with new skills, you will gain the knowledge necessary to become an expert in providing successful high-performance solutions.

If you are writing multithreaded code in .NET, then this book can help you. If you are interested in using a functional paradigm to simplify competitive programming and maximize application performance, then this book will be an important guide for you. It will benefit any .NET developers who want to write competitive, reactive and asynchronous applications that scale and automatically adapt to existing hardware resources wherever such programs work.

Publication structure: roadmap


The fourteen chapters of this book are divided into three parts. Part I presents the functional concepts of competitive programming and describes the skills needed to understand the functional aspects of writing multi-threaded programs.


Part II examines in depth the various models of competitive programming in the functional paradigm. We will explore topics such as the Task Parallel Library (TPL) and implement parallel templates such as Fork / Join, Divide and Conquer, and MapReduce. This part also discusses declarative layout, high-level abstractions in asynchronous operations, agent-based programming, and message passing semantics.
')

Part III shows how to put into practice all the functional methods of competitive programming studied in previous chapters.


The book also contains three applications.


Excerpt 11.6. F # MailboxProcessor: 10,000 Agents for Game of Life


Compared with the flows MailboxProcessor in combination with asynchronous workflows is a simple computing unit (primitive). Agents can appear and be destroyed with minimal cost. You can distribute the work among several MailboxProcessor objects in the same way that you can use threads without the additional overhead associated with creating a new thread. Due to this, it is quite possible to create applications consisting of hundreds of thousands of agents working in parallel, with minimal load on computer resources.

In this section, we will use multiple instances of MailboxProcessor to implement the Game of Life game (Life game) ( wiki-eng and wiki-rus ). According to Wikipedia, Game of Life, in simple terms, is a cellular automaton. It is a game without players — in other words, when a game starts with a random initial configuration, it runs without any other input. The game consists of a set of cells that form a grid; in each cell several mathematical rules are fulfilled. Cells can live, die and multiply. Each cell interacts with eight neighbors (neighboring cells). To move cells in accordance with these rules, it is necessary to constantly calculate the new state of the grid.

Game of Life has the following rules:


Depending on the initial conditions, the cells form characteristic structures throughout the game. Through repeated application of the rules, the next generations of cells are created until the cells reach a stable state (Fig. 11.12).

Listing 11.9 shows the implementation of the Game of Life AgentCell cell, based on the MailboxProcessor F # -types based on MailboxProcessor. Each cell-agent interacts with neighboring cells through asynchronous message passing, thus creating a fully parallelized Game of Life. For brevity, I have omitted some parts of the code, since they are not related to the main topic of the example. Full implementation you will find in the source code for this book, laid out on the publisher's website.

image

image

AgentCell describes a cell in the Game of Life grid. The basic concept is that each agent communicates with its neighboring cells about its current state through asynchronous message passing. This template creates a chain of interconnected parallel communications that involves all cells that send their updated state to the MailboxProcessor updateAgent. After receiving this data, updateAgent updates the graphics in the user interface (Listing 11.10).

image

image

updateAgent, as the name suggests, updates the state of each pixel according to the cell value received in the Update message. The agent maintains the state of pixels and uses it to create a new image when all cells transfer their new state. Then updateAgent updates the graphical WPF user interface using this new image that matches the current Game of Life grid:

do! Async.SwitchToContext ctx image.Source <- createImage pixels do! Async.SwitchToThreadPool() 

It is important to note that the updateAgent agent uses the current synchronization context to correctly update the WPF controller. The current thread is switched to the user interface stream using the Async.SwitchToContext function (described in chapter 9).

The final code snippet for executing Game of Life generates a grid that serves as a playing field for the cells, and then the timer notifies the cells to perform the update (Listing 11.11). In this example, the grid is a square of 100 Ă— 100 cells, a total of 10,000 cells (MailboxProcessor objects), which are calculated in parallel by a timer every 50 ms, as shown in Fig. 11.13. Ten thousand MailboxProcessor objects interact and update the user interface 20 times per second (the code to which you should pay attention is in bold).

image

image

Notifications to all cells (agents) are sent in parallel, using PLINQ. Cells are F # -sequences that are considered .NET IEnumerable, which makes LINQ / PLINQ easy to integrate.

image

When executing the code, the program generates 10,000 F # objects of the type MailboxProcessor in less than 1 ms, while the agents occupy less than 25 MB in memory. Impressive!

Summary



»More information about the book can be found on the publisher's website.
» Table of Contents
» Excerpt

Hubrozhiteley 20% discount coupon - Concurrency in .NET

Upon payment of the paper version of the book, an electronic version of the book is sent to the e-mail.

Source: https://habr.com/ru/post/453804/


All Articles