Parallel For

seany seany at uni-bonn.de
Tue Jun 15 06:39:24 UTC 2021


I know that c# has parallel for [like 
this](https://dotnettutorials.net/lesson/parallel-for-method-csharp/) .

I know that D has parallel foreach [like 
this](http://ddili.org/ders/d.en/parallelism.html).

I want to do the following :

Given 4 sets , A = {a_1, a_2, ... }; B = {b_1, b_2, ... } ; C = 
{c_1 , ... } ; D = {d_1, ... } - I need to make all Cartesian 
products, such as {a_1, b_1, c_1, d_1 }; {a_1, b_1, c_1, d_2}; 
... {a_n, b_n, c_n, d_n} ; then run an operation on the elements .

Then, I need to extract the maximum and minimum values of the 
results - either by storing them in an array, or by some other 
suitable means.

My attempt to solve can be seen in this minimal example :

         import std.stdio;
         import std.math;
         import std.stdio;
         import std.conv;
         import std.format;
         import std.math;
         import std.algorithm;
         import std.net.curl;
         import std.json;
         //import dlib.image;
         import std.path;
         import std.array;
         import std.net.curl;
         import core.stdc.stdlib;
         import std.datetime;
         import std.file;
         import opmix.dup;
         import std.parallelism;


         void main() {

                 int[] a = [1,2,3,4,5,6,7,8,9];
                 int[] b = [11,12,13,14,15,16,17,18];

                 int[] c ;
                 foreach(aa; parallel (a)) {
                         foreach (bb; parallel(b)) {

                                 c ~= aa+bb;

                         }

                 }
                 writeln(c);

         }


I expect the output : `[ 12, 13, 14 ....27 ]` ( not necessarily 
in this order - but that is okey).

I am getting the output : `[12, 13, 14, 15, 16, 17, 18, 19, 14, 
15, 16, 17, 18, 19, 20, 21, 15, 16, 17, 18, 19, 20, 21, 22, 0, 0, 
0, 0, 0, 0, 0, 17, 18, 19, 20, 21, 22, 23, 24, 19, 20, 21, 22, 
18, 0, 0, 0, 16, 21, 18, 22, 23, 24, 25, 20, 21, 22, 19]`.

In the next run : `[12, 13, 14, 15, 16, 17, 18, 19, 14, 15, 16, 
17, 18, 19, 20, 21, 15, 16, 17, 18, 19, 20, 21, 22, 16, 17, 18, 
19, 20, 21, 22, 23, 18, 19, 20, 21, 22, 23, 24, 25, 17, 18, 19, 
19, 21, 20, 23, 23, 24, 24, 20, 21, 22, 23, 24, 25, 26, 27]`

In the next run : `[12, 13, 14, 15, 16, 17, 18, 19, 14, 15, 16, 
17, 0, 0, 16, 18, 19, 20, 21, 17, 18, 19, 20, 15, 16, 17, 18, 19, 
20, 21, 22, 18, 19, 20, 21, 22, 23, 24, 25, 19, 20, 21, 22, 16, 
17, 20, 21, 22, 23, 24, 21, 22, 27, 23]`

In the 4th run : `[12, 13, 14, 15, 16, 17, 18, 19, 15, 16, 17, 
18, 19, 20, 21, 22, 14, 13, 14, 15, 16, 17, 18, 19, 20, 20, 21, 
22, 23, 24, 25, 26, 27, 18, 20, 22, 23, 24, 25, 20, 21, 22, 23, 
24]`

What am I doing wrong?

Thank you .


More information about the Digitalmars-d-learn mailing list