单个列表中的任务并行编程 [英] Task Parallel Programming from a Single List

查看:42
本文介绍了单个列表中的任务并行编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有一个1000个数字的数组,现在我想使用TPL(任务并行编程),从中写出每个数字的平方单个记事本文件中的数组。

(注意,在我的项目中它有相同的场景,我无法解释,因为它需要很多功能来解释)。



任何人都可以帮助我使用TPL概念做同样的事情。

Hi,

I have an array of 1000 Numbers, Now i want to use TPL (Task Parallel Programming), to write the square of each number from that array in a single note pad file.
(Note that in my project it has the same kind of scenario, where I can't explain as it needs lot of functionality to explain).

can any one help me in doing the same kind of stuff using only TPL concept.

推荐答案

你的问题是连续的在性质上,并不适合平行。



唯一的方法是将问题转化为2个问题:

a第一个问题可以是分成许多并行问题

和第二个收集第一个结果并按顺序编写它们。



更新:我不做线程,但我知道如何。

原则1:

- 创建一个大小为1000的数组来保存正方形的值。

- 拆分com在线程之间放置正方形。

- 等到所有线程都完成。

- 按顺序扫描数组以将正方形写入文本文件。



您还可以在线程中将方块转换为字符串,以最大限度地减少顺序写入的工作量。
Your problem is sequential in nature, and not suited to parallel.

The only way is to transform the problem in 2 problems:
a first problem that can be split in many parallel problems
and a second that gather the results of the first one and write them in a sequential manner.

Update: I don't do threading, but I know how to.
Principle 1:
- create an array of size 1000 to hold the values of squares.
- split the computing of squares between threads.
- Wait until all threads are finish.
- Scan sequentialy the array to write the squares to text file.

You can also convert the squares to strings while in threads to minimize the workload of sequential writing.


不要。

您提供的示例不适合并行处理:输出需要在单个文件中按顺序排列:当您启动多线程1000操作时,您无法控制订单,并且每个都必须等待轮到访问文件 - 或者同时运行的所有文件中的一个会发现正在使用的文件并且无法写入。

此外,请记住每个线程都需要一个可用的实际运行的核心 - 所以除非你有1000个内核可用,多线程这样的任务将使整个操作比单个线程更长,因为任务设置和切换开销将超过实际l按数量级处理!

Don't.
The example you give is not a good candidate for parallel processing: the output needs to be in order in a single file: when you start multi threading 1000 operations, you can't control the order, and each one has to "wait it's turn" to access the file - or all bar one of those running at the same time will find the file in use and fail to write.
In addition, remember that each thread needs an available core to actually run - so unless you have 1000 cores available, multithreading a task like this will make the whole operation take longer than it would in a single thread, as the task setup and switching overhead will outweigh the actual processing by orders of magnitude!

我很欣赏这实际上并不是你在项目中想要做的事情,但很多相同的问题都会发生如果你的真实世界任务有任何相似之处(因为它应该是一个有用的描述)。

I appreciate that this isn't actually what you are trying to do in your project, but many of the same problems are going to occur if your "real world" task is in any way similar (as it should be to be a useful description here).

我会停下来想想你是什么做以及为什么你需要在进一步之前进行并行化!

I'd stop and think about what you are doing and why you need to parallelise it before you go any further!


请参阅我对解决方案1的评论。计算数字的平方值的整个想法在数组中没有多大意义,但真正完全违背并行性的目的是将所有数据写入单个文件中。文件流是共享资源。这是由TPL创建的所有线程访问的共享资源。它在功能上等同于顺序操作,即使方块的某些计算可以并行进行,但与其余任务相比,这种计算速度非常快。

Please see my comment to Solution 1. The whole idea of calculating square values of the numbers in an array makes little sense, but what really totally defeats the purpose of parallelism is writing all data in a single file. The file stream is the shared resource. This is a shared resource to be accessed by all of the threads created by TPL. It will be functionally equivalent to sequential operation, even though some of the calculations of the squares could go in parallel, but this calculation will be extremely fast compared to the rest of the task.

此外,与简单的顺序循环相比,TPL和同步的内存和CPU使用开销可能导致并行版计算的平均吞吐量大大降低。你想试试看吗? : - )

Moreover, memory and CPU usage overhead of TPL and synchronization may lead to average throughput of your parallel version of the calculation considerably lower, compared with simple sequential loop. Do you want to try it and see? :-)


这篇关于单个列表中的任务并行编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆