foreach (i; taskPool.parallel(0..2_000_000)

Salih Dincer salihdb at hotmail.com
Sun Apr 2 04:34:40 UTC 2023


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
> tasks/threads
> > ...?
>
> 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:

```d
struct RowlandSequence {
   import std.numeric : gcd;
   import std.format : format;
   import std.conv : text;

   long b, r, a = 3;
   enum empty = false;

   string[] front() {
     string result = format("%s, %s", b, r);
     return [text(a), result];
   }

   void popFront() {
     long result = 1;
     while(result == 1) {
       result = gcd(r++, b);
       b += result;
     }
     a = result;
   }
}

enum BP {
   f = 1, b = 7, r = 2, a = 1, /*
   f = 109, b = 186837516, r = 62279173, //*/
   s = 5
}

void main()
{
   RowlandSequence rs;
   long start, skip;

   with(BP) {
     rs = RowlandSequence(b, r);
     start = f;
     skip = s;
   }
   rs.popFront();

   import std.stdio, std.parallelism;
   import std.range : take;

   auto rsFirst128 = rs.take(128);
   foreach(r; rsFirst128.parallel)
   {
     if(r[0].length > skip)
     {
       start.writeln(": ", r);
     }
     start++;
   }
} /* PRINTS:

46: ["121403", "364209, 121404"]
48: ["242807", "728421, 242808"]
68: ["486041", "1458123, 486042"]
74: ["972533", "2917599, 972534"]
78: ["1945649", "5836947, 1945650"]
82: ["3891467", "11674401, 3891468"]
90: ["7783541", "23350623, 7783542"]
93: ["15567089", "46701267, 15567090"]
102: ["31139561", "93418683, 31139562"]
108: ["62279171", "186837513, 62279172"]

*/
```

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 `skip` to see the entire sequence. 
In cases where your processor power is not enough, you can create 
skip points.  Check out BP...

SDB at 79


More information about the Digitalmars-d-learn mailing list