Simple parallel foreach and summation/reduction
Ali Çehreli
acehreli at yahoo.com
Fri Sep 21 12:15:59 UTC 2018
On 09/21/2018 12:25 AM, Chris Katko wrote:
> On Thursday, 20 September 2018 at 05:51:17 UTC, Neia Neutuladh wrote:
>> On Thursday, 20 September 2018 at 05:34:42 UTC, Chris Katko wrote:
>>> All I want to do is loop from 0 to [constant] with a for or foreach,
>>> and have it split up across however many cores I have.
>>
>> You're looking at std.parallelism.TaskPool, especially the amap and
>> reduce functions. Should do pretty much exactly what you're asking.
>>
>> auto taskpool = new TaskPool();
>> taskpool.reduce!((a, b) => a + b)(iota(1_000_000_000_000L));
>
> I get "Error: template instance `reduce!((a, b) => a + b)` cannot use
> local __lambda1 as parameter to non-global template
> reduce(functions...)" when trying to compile that using the online D
> editor with DMD and LDC.
>
> Any ideas?
You can use a free-standing function as a workaround, which is included
in the following chapter that explains most of std.parallelism:
http://ddili.org/ders/d.en/parallelism.html
That chapter is missing e.g. the newly-added fold():
https://dlang.org/phobos/std_parallelism.html#.TaskPool.fold
Ali
More information about the Digitalmars-d-learn
mailing list