Introduction to multi-thread and parallel computing under. Net (14th) Parallel Computing

Source: Internet
Author: User

PreviousArticleWe introduced how to use the relevant class library for multi-threaded programming in. net. We know that. Net 4.0 has been officially launched, and the important feature brought about is the parallel library. This article will talk about some understanding and opinions on parallel computing. Parallel Computing is not a new concept. In fact, it is the process of splitting a single task into multiple subtasks for parallel execution through multiple threads .. The. NET 4.0 Parallel library not only provides support in this aspect, but also encapsulates various multi-threaded development scenarios, so that we can develop multithreading without relying on "underlying" objects such as thread/synchronization elements. Without the. NET 4.0 parallel computing library, we can perform parallel computing, but we need to manually split the data structure andAlgorithmAnd put them in different threads, and then use the thread synchronization method to summarize and process the execution results of these threads in a unified manner. The reason why parallel computing is required is that as the CPU of our computer is not upgraded, the core is scaled horizontally.ProgramIn this way, the capacity of scale-up cannot be achieved with the CPU scale-out. Therefore, you need to adjust the program logic so that an originally unsplit task can be split into multiple segments for simultaneous execution, in this way, multiple cores can be used to provide program performance.

Every time Microsoft launches a new framework, there will be a lot of publicity, but we have not seen some suggestions from these publicity. When should we not use this framework. After the release of LINQ to SQL, many people began to use and gave up the stored procedure, but they did not care about what the underlying framework did when using it, because Microsoft's things are too easy to use, those who do not know Orm can easily learn to use LINQ to SQL:

1) randomly store the database access package in a loop, or use the itemdatabound method similar to the cycle trigger event of the gridview control to access the database, resulting in hundreds of thousands of database connections on a page, no one used the stored procedure in the past.

2) even if a field is read, all fields and thousands of rows in the Word Table associated with the row are retrieved.

This leads to the emergence of a dangerous product in the form of LINQ to SQL, because before they are used, the old method prevents mistakes. Once they become automated, they are easy to make mistakes. In fact, what I want to talk about is that the parallel framework of. Net 4.0 may also be like this. Why? In fact, for a programmer of a Windows application. net 4.0 Parallel library, they are also very clear about how to use threads, multi-thread programming ,. the arrival of the net 4.0 parallel Library only simplifies the multi-thread programming method and makes it easier for us to perform parallel computing. What I want to say is that if the program needs to be optimized using multiple cores, it will do so before the Parallel library arrives. However, for ASP. net programmers are different. Many ASP. net programmers in. the term "net 4.0 Parallel library" or "Parallel Computing" has never been used in multi-thread development before, and is rarely used directly on websites. net 4.0 Parallel library, it is easy to think that this is a good way to improve performance. It is also very easy to use. We only need to add a few lines.CodeWe can turn loop traversal into a parallel behavior, so that different code segments in the same method can be executed in parallel in multiple threads.

In fact, for ASP. NET programs, this approach may not necessarily improve performance, may reduce performance, or even lead to website paralysis, and may cause many inexplicable bugs. There are two main reasons:

1) There was no multi-thread background before. Because it was too easy to use, the program was blindly transformed into a parallel program, but no potential problems were found.

2) The web program itself is in a multi-threaded environment. Unlike Windows programs, our users are not one. Our programs have been executed by several threads in the thread pool of the Web server at the same time. In other words, even if a web application is only a thread from processing the UI from the beginning to the end to reading data to formatting the UI, we can also make full use of CPU resources and use multiple cores, because the Operating System Schedules different threads to different cores.

Now let's take a look at these two issues. First, what are the potential problems of Parallel Computing (multithreading?

1) multiple threads share the same memory allocation. A typical pair of values is accumulated. Is the accumulation accurate if multiple threads are simultaneously accessed?

2) Excessive threads, even if there is no shared memory, if one hundred new threads are used to execute one hundred short-term operations, the final running time may be more than one hundred times in a thread in a circular order.

3) even if our program is thread-safe, is it thread-safe to call. Net class libraries or other class libraries in a multi-threaded environment? Even if the thread is secure, we need to be aware that if it uses the lock method to ensure thread security, it will not improve the performance even if we call this method through multiple threads.

4) multi-threaded UI operations.

5) Mutual waiting or deadlock.

6) debugging/platform adaptation and other issues.

The second question is, should ASP. NET applications or Web applications use parallel libraries?

1) if our operations involve I/O (DISK/database/Network) and the operation takes a long time and can be separated, we can use multiple threads to perform a large operation, or multiple operations can be executed at the same time to speed up the time when the main thread completes the operation. The acceleration time not only improves the user experience, but also increases the system throughput. You can think about the same load, with 100 worker threads (I/O) in the thread pool ), is 100 I/O threads better than 25 working threads and 100 I/O threads better?

2) if many of our operations are CPU-bound, it depends on the situation. If the number of connections is high and our operations are very short, the parallel operation will not work very well, because our CPU is not idle and we are constantly processing tasks. The thread pool also opens any thread and divides what a thread can do into several threads, it increases the burden of thread switching, increases the threads required by the thread pool, and increases the system load and reduces the throughput. It is only when our programs are complex and have few connections (such as internal ERP), or even Windows programs, to use parallel computing can benefit, increase CPU utilization, and improve execution speed.

3) note that when developing the class library, if we know that the future use group of the class library may be ASP. NET application, so we should carefully consider whether to introduce a large number of highly parallel operations in the class library.

4) Sometimes we still need to use test data as the basis and cannot take it for granted. For example, we can think that downloading data from 10 websites with multiple threads at the same time will increase the efficiency, then, we naturally think that using 10 threads to access the database to update the data entries I * 100 (I = 0-9) is a little faster than the serial update of 1000 data entries. But do we know what lock operations will be performed on the database?

What I want to say is that when there are few or even one or two requests, it may indeed increase the speed. But who makes it clear that there are more requests, for example, if we transfer our luggage in parallel on a non-wide conveyor belt and divide it into four heaps (four tracks), it may be faster if only one person's luggage needs to be transferred. But if it is one thousand people, does each person's luggage spread out or does each person's luggage occupy only one fast track? Hard to say! For ASP. NET applications with a large number of accesses, please be careful. We only need one working thread and one Io thread for one request.

 

In any case, the parallel library of. Net 4.0 provides us with tools. We also need to judge how to make good use of the tools as needed. Besides data parallelism and task parallelism, it also provides a lot of collection data structures suitable for multi-threaded environments (it is not that simple to implement a data structure suitable for multithreading with excellent lock-free performance before that ).

 

Finally, can parallel computing not be limited to CPU usage or GPU usage? Http://www.infoq.com/cn/news/2010/05/Brahma

Can I pass the entire Method Instruction stack and data stack as parameters to multiple servers so that the server can return results after processing?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.