Thanks in advance for any assistance.
As the subject line suggests can I do something like? :
foreach (i; taskPool.parallel(0..2_000_000))
Obviously this exact syntax doesn't work but I think it expresses the gist of my challenge.
Thread overview | |||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
April 01 foreach (i; taskPool.parallel(0..2_000_000) | ||||
---|---|---|---|---|
| ||||
Thanks in advance for any assistance. As the subject line suggests can I do something like? :
Obviously this exact syntax doesn't work but I think it expresses the gist of my challenge. |
April 01 Re: foreach (i; taskPool.parallel(0..2_000_000) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paul | On 4/1/23 2:25 PM, Paul wrote: >Thanks in advance for any assistance. As the subject line suggests can I do something like? :
Obviously this exact syntax doesn't work but I think it expresses the gist of my challenge.
-Steve |
April 01 Re: foreach (i; taskPool.parallel(0..2_000_000) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | Thanks Steve. |
April 01 Re: foreach (i; taskPool.parallel(0..2_000_000) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | >
-Steve Is there a way to verify that it split up the work in to tasks/threads ...? The example you gave me works...compiles w/o errors but the execution time is the same as the non-parallel version. They both take about 6 secs to execute. totalCPUs tells me I have 8 CPUs available. |
April 01 Re: foreach (i; taskPool.parallel(0..2_000_000) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On Saturday, 1 April 2023 at 18:30:32 UTC, Steven Schveighoffer wrote: >On 4/1/23 2:25 PM, Paul wrote:
-Steve Is there a way to tell if the parallelism actually divided up the work? Both versions of my program run in the same time ~6 secs. |
April 01 Re: foreach (i; taskPool.parallel(0..2_000_000) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paul | On 4/1/23 15:30, Paul wrote: > Is there a way to verify that it split up the work in to tasks/threads > ...? It is hard to see the difference unless there is actual work in the loop that takes time. You can add a Thread.sleep call. (Commented-out in the following program.) Another option is to monitor a task manager like 'top' on unix based systems. It should multiple threads for the same program. However, I will do something unspeakably wrong and take advantage of undefined behavior below. :) Since iteration count is an even number, the 'sum' variable should come out as 0 in the end. With .parallel it doesn't because multiple threads are stepping on each other's toes (values): import std; void main() { long sum; foreach(i; iota(0, 2_000_000).parallel) { // import core.thread; // Thread.sleep(1.msecs); if (i % 2) { ++sum; } else { --sum; } } if (sum == 0) { writeln("We highly likely worked serially."); } else { writefln!"We highly likely worked in parallel because %s != 0."(sum); } } If you remove .parallel, 'sum' will always be 0. Ali |
April 02 Re: foreach (i; taskPool.parallel(0..2_000_000) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | On Saturday, 1 April 2023 at 22:48:46 UTC, Ali Çehreli wrote: >On 4/1/23 15:30, Paul wrote: >Is there a way to verify that it split up the work in to It is hard to see the difference unless there is actual work in the loop that takes time. I always use the Rowland Sequence for such experiments. At least it's better than the Fibonacci Range:
The operation is simple, again multiplication, addition, subtraction and module, i.e. So four operations but enough to overrun the CPU! I haven't seen rsFirst256 until now because I don't have a fast enough processor. Maybe you'll see it, but the first 108 is fast anyway. PS: Decrease value of the SDB@79 |
April 02 Re: foreach (i; taskPool.parallel(0..2_000_000) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Salih Dincer | On Sunday, 2 April 2023 at 04:34:40 UTC, Salih Dincer wrote: >I haven't seen rsFirst256 until now... Edit: I saw, I saw :) I am struck with consternation! I've never seen these results before. Interesting, there is such a thing as parallel threading :) Here are my skipPoints:
It looks like there are 5 total skipPoints until 256 where it loops for a long time. (while passing 1's). SDB@79 |
April 02 Re: foreach (i; taskPool.parallel(0..2_000_000) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paul | On 4/1/23 6:32 PM, Paul wrote:
> On Saturday, 1 April 2023 at 18:30:32 UTC, Steven Schveighoffer wrote:
>> On 4/1/23 2:25 PM, Paul wrote:
>>
>> ```d
>> import std.range;
>>
>> foreach(i; iota(0, 2_000_000).parallel)
>> ```
>>
>
> Is there a way to tell if the parallelism actually divided up the work? Both versions of my program run in the same time ~6 secs.
It's important to note that parallel doesn't iterate the range in parallel, it just runs the body in parallel limited by your CPU count.
If your `foreach` body takes a global lock (like `writeln(i);`), then it's not going to run any faster (probably slower actually).
If you can disclose more about what you are trying to do, it would be helpful.
Also make sure you have more than one logical CPU.
-Steve
|
April 03 Re: foreach (i; taskPool.parallel(0..2_000_000) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On Sunday, 2 April 2023 at 15:32:05 UTC, Steven Schveighoffer wrote: >It's important to note that parallel doesn't iterate the range in parallel, it just runs the body in parallel limited by your CPU count. If your If you can disclose more about what you are trying to do, it would be helpful. Also make sure you have more than one logical CPU. |