问题标签 [parallel.for]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
123 浏览

c# - 在 C# 中将两层嵌套的 for 循环转换为嵌套的 Parallel.for 循环

我想将 C# 中的以下代码转换为并行代码。我搜索了互联网,但找不到合适的方法。感谢您的帮助。

0 投票
2 回答
249 浏览

c# - ThreadSafe Parallel.For

cOrders 不会以 API 返回的所有响应结束。如果我将它更改为一个简单的 For 循环,一切都很好,但它需要很长时间,因为对于某些调用,有许多循环返回 API 来获取我需要的所有数据。

0 投票
0 回答
101 浏览

c# - C# Parallel.For 执行 SQLCommands

我想提高一个控制台应用程序的性能,该应用程序接收一个包含数百万行的文本文件,并且当前一次将每一行插入到 SQL 表中。

使用 Parallel.For,我想改进这一点,因为每个操作都独立于下一个操作。由于每次迭代都执行一个 SQLCommand,我想知道这是否是一种安全的方法。我不确定这在内部是如何工作的,并且不想在数据库上引起性能问题。例如,如果 Parallel.For 创建了太多任务让 sql server 处理,数据库会冻结或崩溃吗?

0 投票
2 回答
238 浏览

c# - 并行 For 循环使用局部变量运行函数并将结果写入局部变量

我想在 C# 中使用并行 For 循环来运行函数并将结果写入变量。

这是我正在使用的当前 for 循环:

此外,如果有办法做到这一点,那么treads在写入变量之前暂停直到前一个线程完成也会很好地保持事情有序但没有必要。

是的,我现在多次运行同一件事,因为我之前没有做过并行循环,我想在添加变化之前确保一致性。如何将此 for 循环重写为并行 for 循环,以便获得一致的结果?

这是我似乎得到大部分错误的陈述:

0 投票
1 回答
777 浏览

c# - 使用带有并行 for 循环 C# 的线程安全变量

这是我第一次使用并行 for 循环,我了解基础知识,正如您在下面的代码中看到的那样,但我不了解如何使循环线程内的变量安全。

我正在关注https://docs.microsoft.com/en-us/dotnet/standard/parallel-programming/how-to-write-a-parallel-for-loop-with-thread-local-variables上的文章

我目前经常遇到以下错误:当对数据执行计算时,我的 Calculations 类中不包含任何元素。我是否遗漏了一些简单的东西来使所有线程安全?

更新:我为 Calculations 类添加了所有相关代码,显示了一个方法作为示例,该方法返回常量序列不包含任何元素异常,以及到目前为止我为解决问题所做的工作(异常仍在继续)

更新 2:我在我的代码中添加了自定义类,这应该允许它现在编译。

在此处输入图像描述

0 投票
2 回答
225 浏览

c# - 在递归方法中如何知道我的所有线程何时完成执行?

我一直在做一个网页抓取项目。

我有两个问题,一个是以百分比形式处理的 url 数量,但更大的问题是我无法弄清楚我如何知道我创建的所有线程何时完全完成。

注意:我知道一旦完成并行 foreach 就会移动,但这是在递归方法中。

我的代码如下:

注意Scrape多次调用(递归)

像这样调用方法:

反过来又被这样调用:

我的结果:

在此处输入图像描述

我该如何解决这个问题?

0 投票
0 回答
78 浏览

c# - 并行循环抛出异常

目前我正在开发一个 Windows 窗体应用程序。首先,我使用了一个 for 循环,它工作得非常好,但是数据变大了,它开始非常缓慢地生成文件。我切换到并行循环,它在“document.Save(fileName + “.pdf”)上抛出异常“Parameter is not valid”;”

这是我的代码片段

对不起,如果我的问题很愚蠢,但我一直在努力解决这个问题并寻找解决方案几个小时。

提前致谢!

0 投票
2 回答
1300 浏览

c# - Using Random in Parallel.For

My program performs many simulations, each of which calls for many random numbers to be generated. The serial method is straightforward and works. However, in my pursuit of parallelizing the work I believe I created a more straightforward method than what I could find. The other methods are somewhat dated and something might be possible now that wasn't possible then.

Am I missing something that will make my method susceptible to any of the myriad of multithreading problems? My method uses the ability of a Parallel.For to instantiate a variable for individual thread use and thus it doesn't require another class like the other methods I found. In this case each thread gets its own Random.

Timing:

My method: 4s

Stephen: 14s

Jon: 16s

Clearly I don't know as much as Stephen or Jon so I'm concerned I missed something.

My method:

This next method is by Stephen Toub on the MSDN Blog:

This next method is by Jon Skeet on his blog:

Update/answer: Brian has pointed out that I was using Jon's method incorrectly. A more correct way would be to call an ThreadLocalRandom.Instance for each Parallel.For loop and use that instance for the internal for loop. This prevents the thread check on each call and instead there is only one thread check per Parallel.For loop. Using Jon's method correctly makes his method faster than the overload of Parallel.For that I was using.

0 投票
2 回答
1563 浏览

c# - Resource sharing in nested Parallel.For loop C#

Background

I have a piece of code which is highly parallelizable and I found that most of the time I am using only one core at 100% while the rest does nothing. To tackle this I've tinkered with multithreading, implementing semaphores and what not to realize that Parallel.For() is fine grained and more efficient than any of my solutions.

The Code

To simplify I will only write the pieces of code structurally important.

All of the ambiguously named functions are more or less just mathematical equations and are of time complexity O(1).

Important Details

Pay attention to the inner loop that has the variable i as upper boundary as well as the summation variable named sharedResource. The execution order in this case is not important as addition is commutative and I don't see any obvious reason for Amdahl's Law to apply as all instance combinations (i, j) of both loops can be calculated independently.

Question

Is it smart to use a nested Parallel.For() loop in this scenario or should I only use it instead of the outer loop (or only on the inner respectively)?

The only thing that concerns me is the sharedResource as I don't get a lot of in-depth insight of how Parallel.For() works from the documentation. Another important thing is that if I do use two Parallel.For() loops some instances will finish almost instantly due to the break while others will take much more time. Will it be able to balance this?

0 投票
1 回答
47 浏览

c# - Parallel.For 在范围内声明 null c#

我是使用 Parallel 的新手,所以我可能遗漏了一些东西。使用运行此代码时,Parallel.For我得到一个 NullReferenceExcempion。当用 normal 做完全相同的事情时,它可以完美地工作。我发现其他人也问过同样的问题,但在他们的情况下,问题始终是他们在 for 范围之外声明变量,这当然会产生各种混乱的事情。我发现没有人遇到这个问题,因为我正在经历它。在我的情况下,变量是在内部声明的......我怎样才能获得空引用?

错误显示在评论中。我正在向图表中添加一个系列,该系列的引用会立即存储在一个对象中,以便以后能够使用它来做我的事情。我在分配阶段得到空引用异常。通常它会在 5 到 15 次之间的可变迭代次数后阻塞。

这是发生的事情:

异常截图

带有红色 X 的行正在引发异常。工具提示在分配阶段显示空值。