Home Database Mysql Tutorial .NET 4中的并行编程技术(也称之为多核编程技术) 2

.NET 4中的并行编程技术(也称之为多核编程技术) 2

Jun 07, 2016 pm 03:09 PM
.net parallel technology article series programming

本系列文章将会对.NET 4中的并行编程技术(也称之为多核编程技术)以及应用作全面的介绍。 本篇文章的议题如下: 1. 并行编程和多线程编程的区别。 2. 并行编程技术的利弊 3. 何时采用并行编程 1.并行编程和多线程编程的区别。 1.1并行编程。 现在随着多核计算

本系列文章将会对.NET 4中的并行编程技术(也称之为多核编程技术)以及应用作全面的介绍。

  本篇文章的议题如下:
   1. 并行编程和多线程编程的区别。 
   2. 并行编程技术的利弊 
   3. 何时采用并行编程

   1.并行编程和多线程编程的区别。

   1.1并行编程。 
   现在随着多核计算机的普及,并行编程技术,也就是多核编程技术也逐渐称为开发的主流。为此,在.NET 4 中就引入了“并行编程”。在.NET 4 中一些列的Library和类为并行编程提供了支持,如: Task Parallel Library,Parallel  LINQ等。


   其实在.NET 1.0中就有了并行编程技术的一些实现---多线线程技术。多线程最大的问题就是难于使用和管理。在使用多线程的使用,因为它的复杂性,往往使用我们把注意力分散了多线程上。而致使我们的最初目的被掩盖了。

   1.2 比较区别 
   在.NET 4中提出的并处编程的底层机制其实还是基于多线程的。但是他们之前最大的区别就是.NET 4中的并行编程更加方便。


   在传统的编程模型中,程序员负责创建线程,为线程分配任务,管理线程。一个形象的比喻:你拥有一批士兵,然后你对他们下达命令,之后,你就必须时刻监视你的那些士兵,确保他们按照你的意图办事。(很累吧!)


   在.NET 4中的并行编程是依赖Task Parallel Library(后面简称为TPL) 实现的。在TPL中,最基本的执行单元是task(中文可以理解为"任务"),一个task就代表了你要执行的一个操作。你可以为你所要执行的每一个操作定义一个task,TPL就负责创建线程来执行你所定义的task,并且管理线程。TPL是面向task的,自动的;而传统的多线程是以人工为导向的。


   Task机制使得我们把注意力关注在我们要解决的问题上面。如果之前的多线程技术使得我们放弃了一些并行编程的使用,那么.NET 4中的新的并行编程技术可以让我们重新建立信心。


   虽然有了新的并行技术,但是传统的多线程的技术还是很有用的。当我们使用TPL中的并行技术的时候来执行多个task的时候,我们不用在关心底层创建线程,管理线程等。

   2. 并行编程技术的利弊 
   使用并行技术最大的好处就是提高了系统的性能。并行处理过程一般是这样的:一个要执行的任务被拆分为很多很小的部分,然后这些很小的部分就分别在不同处理器(可以是多核的一台电脑,也可以使很多的电脑)上执行。因为这些很多很小的部分同时在执行,所以称之为"并行"。
   使用并处编程的时候需要考虑下面的问题:

   1.开销问题。 
   并行执行不是免费的,也是要开销的。在并行运行开始和管理都是需要开销的,就类比在线程的创建和管理一样。在程序中,你要执行的任务越多,那么使用并行的效果就越好。


   2. 数据的协调 
 如果在并行执行的那些小部分需要共享公共的数据,那么我们就要协调。一般来说,需要协调的数据越多,并行执行的性能损耗就越大。如果执行各个小部分之间都是独立的,那么我们就不用协调了。但是很多的时候,我们都是需要协调的。而且协调的技术也不是很难,在之后的文章中会一一讲述。


   3.性能提高多少 
 增加一台计算机的CPU可能会提高程序的运行速度,但是不是绝对的。我们知道,一个应用程序在单核的计算机上运行的时间不一定(往往也不是)双核计算机的
1/2.所以,采用并行编程不一定就一定会成倍的提高程序的性能。因为性能与很多的因数有关的,硬件就是很大的因数。

 

   3. 何时采用并行编程


   建议:如果一个问题能够用并行编程解决,那么就用,否则就不用。听起来好像是废话,但是确实是一个很不错的建议。因为并行编程也不是万能的,也只能
解决一类的问题,所以在用之前要分析问题了。如果一个问题确实能够用并行的方案来解决,但是有很多的因数影响,如之前我们提到的一些问题。权衡使用之后的开销和好处在决定是否使用。后续文章会给出很多的例子。

 

   今天暂时写到这里吧,理论讲了一大堆的,希望园里的朋友见谅啊!从下篇开始就用code+讲解的形式。
   注:很长时间没有写东西了, :),希望大家见谅!

 

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1252
24
Xiaomi 15 series full codenames revealed: Dada, Haotian, Xuanyuan Xiaomi 15 series full codenames revealed: Dada, Haotian, Xuanyuan Aug 22, 2024 pm 06:47 PM

The Xiaomi Mi 15 series is expected to be officially released in October, and its full series codenames have been exposed in the foreign media MiCode code base. Among them, the flagship Xiaomi Mi 15 Ultra is codenamed "Xuanyuan" (meaning "Xuanyuan"). This name comes from the Yellow Emperor in Chinese mythology, which symbolizes nobility. Xiaomi 15 is codenamed "Dada", while Xiaomi 15Pro is named "Haotian" (meaning "Haotian"). The internal code name of Xiaomi Mi 15S Pro is "dijun", which alludes to Emperor Jun, the creator god of "The Classic of Mountains and Seas". Xiaomi 15Ultra series covers

The best time to buy Huawei Mate 60 series, new AI elimination + image upgrade, and enjoy autumn promotions The best time to buy Huawei Mate 60 series, new AI elimination + image upgrade, and enjoy autumn promotions Aug 29, 2024 pm 03:33 PM

Since the Huawei Mate60 series went on sale last year, I personally have been using the Mate60Pro as my main phone. In nearly a year, Huawei Mate60Pro has undergone multiple OTA upgrades, and the overall experience has been significantly improved, giving people a feeling of being constantly new. For example, recently, the Huawei Mate60 series has once again received a major upgrade in imaging capabilities. The first is the new AI elimination function, which can intelligently eliminate passers-by and debris and automatically fill in the blank areas; secondly, the color accuracy and telephoto clarity of the main camera have been significantly upgraded. Considering that it is the back-to-school season, Huawei Mate60 series has also launched an autumn promotion: you can enjoy a discount of up to 800 yuan when purchasing the phone, and the starting price is as low as 4,999 yuan. Commonly used and often new products with great value

Revolutionary GPT-4o: Reshaping the human-computer interaction experience Revolutionary GPT-4o: Reshaping the human-computer interaction experience Jun 07, 2024 pm 09:02 PM

The GPT-4o model released by OpenAI is undoubtedly a huge breakthrough, especially in its ability to process multiple input media (text, audio, images) and generate corresponding output. This ability makes human-computer interaction more natural and intuitive, greatly improving the practicality and usability of AI. Several key highlights of GPT-4o include: high scalability, multimedia input and output, further improvements in natural language understanding capabilities, etc. 1. Cross-media input/output: GPT-4o+ can accept any combination of text, audio, and images as input and directly generate output from these media. This breaks the limitation of traditional AI models that only process a single input type, making human-computer interaction more flexible and diverse. This innovation helps power smart assistants

Unleash Your Inner Programmer: C for Absolute Beginners Unleash Your Inner Programmer: C for Absolute Beginners Oct 11, 2024 pm 03:50 PM

C is an ideal language for beginners to learn programming, and its advantages include efficiency, versatility, and portability. Learning C language requires: Installing a C compiler (such as MinGW or Cygwin) Understanding variables, data types, conditional statements and loop statements Writing the first program containing the main function and printf() function Practicing through practical cases (such as calculating averages) C language knowledge

Problem-Solving with Python: Unlock Powerful Solutions as a Beginner Coder Problem-Solving with Python: Unlock Powerful Solutions as a Beginner Coder Oct 11, 2024 pm 08:58 PM

Pythonempowersbeginnersinproblem-solving.Itsuser-friendlysyntax,extensivelibrary,andfeaturessuchasvariables,conditionalstatements,andloopsenableefficientcodedevelopment.Frommanagingdatatocontrollingprogramflowandperformingrepetitivetasks,Pythonprovid

The Key to Coding: Unlocking the Power of Python for Beginners The Key to Coding: Unlocking the Power of Python for Beginners Oct 11, 2024 pm 12:17 PM

Python is an ideal programming introduction language for beginners through its ease of learning and powerful features. Its basics include: Variables: used to store data (numbers, strings, lists, etc.). Data type: Defines the type of data in the variable (integer, floating point, etc.). Operators: used for mathematical operations and comparisons. Control flow: Control the flow of code execution (conditional statements, loops).

Python Power, Simplified: A Beginner-Friendly Approach to Programming Python Power, Simplified: A Beginner-Friendly Approach to Programming Oct 11, 2024 pm 04:53 PM

Getting Started with Python Programming Install Python: Download and install from the official website. HelloWorld!: Use print("HelloWorld!") to print the first line of code. Practical case: Calculate the area of ​​a circle: Use π (3.14159) and the radius to calculate the area of ​​the circle. Variables and data types: Use variables to store data. Data types in Python include integers, floating point numbers, strings, and Boolean values. Expressions and assignments: Use operators to connect variables, constants, and functions, and use the assignment operator (=) to assign values ​​to variables. Control flow: if-else statement: execute different code blocks based on conditions, determine odd

Demystifying C: A Clear and Simple Path for New Programmers Demystifying C: A Clear and Simple Path for New Programmers Oct 11, 2024 pm 10:47 PM

C is an ideal choice for beginners to learn system programming. It contains the following components: header files, functions and main functions. A simple C program that can print "HelloWorld" needs a header file containing the standard input/output function declaration and uses the printf function in the main function to print. C programs can be compiled and run by using the GCC compiler. After you master the basics, you can move on to topics such as data types, functions, arrays, and file handling to become a proficient C programmer.

See all articles