www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - parallelism

reply Arun Chandrasekaran <aruncxy gmail.com> writes:
Hi All,

Is there a way to rewrite this

```
     import std.parallelism;
     auto pool = new TaskPool(options.threadCount);
     foreach (_; 0 .. options.iterationCount) {
         switch (options.operation) {
         case Operation.a:
             pool.put(task!a(options));
             break;

         case Operation.b:
             pool.put(task!b(options));
             break;

         case Operation.c:
             pool.put(task!c(options));
             break;

         case Operation.d:
             pool.put(task!d(options));
             break;

         /// and so on.
     }
     pool.finish();
```

into this?

```
     import std.parallelism;
     import std.conv: to;
     auto pool = new TaskPool(options.threadCount);
     foreach (_; 0 .. options.iterationCount) {
         pool.put(task!(to!string(options.operation))(options)); 
// this line errs.
     }
     pool.finish();
```

--
Arun
Jan 27 2018
next sibling parent Arun Chandrasekaran <aruncxy gmail.com> writes:
On Saturday, 27 January 2018 at 10:28:10 UTC, Arun Chandrasekaran 
wrote:
 Hi All,

 Is there a way to rewrite this

 [...]
Damn! The subject should've been something else.. naming is surely hard..
Jan 27 2018
prev sibling parent reply Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Saturday, 27 January 2018 at 10:28:10 UTC, Arun Chandrasekaran 
wrote:
 ```
     import std.parallelism;
     auto pool = new TaskPool(options.threadCount);
     foreach (_; 0 .. options.iterationCount) {
         switch (options.operation) {
             static foreach(e; EnumMembers!Operation) {
                 case e:
                      pool.put(task!e(options));
                      break;
     }
     pool.finish();
 ```
Does that do the trick?
Jan 27 2018
parent reply Arun Chandrasekaran <aruncxy gmail.com> writes:
On Saturday, 27 January 2018 at 10:38:25 UTC, Nicholas Wilson 
wrote:
 On Saturday, 27 January 2018 at 10:28:10 UTC, Arun 
 Chandrasekaran wrote:
 ```
     import std.parallelism;
     auto pool = new TaskPool(options.threadCount);
     foreach (_; 0 .. options.iterationCount) {
         switch (options.operation) {
             static foreach(e; EnumMembers!Operation) {
                 case e:
                      pool.put(task!e(options));
                      break;
     }
     pool.finish();
 ```
Does that do the trick?
No it doesn't. ``` /usr/include/dmd/phobos/std/parallelism.d(507,34): Error: function expected before (), not cast(Operation)0 of type Operation /usr/include/dmd/phobos/std/parallelism.d(835,16): Error: template instance std.parallelism.Task!(cast(Operation)0, Options) error instantiating src/app.d(159,32): instantiated from here: task!(cast(Operation)0, Options) src/app.d(160,17): Error: must use labeled break within static foreach ```
Jan 27 2018
parent reply Arun Chandrasekaran <aruncxy gmail.com> writes:
On Saturday, 27 January 2018 at 10:49:45 UTC, Arun Chandrasekaran 
wrote:
 On Saturday, 27 January 2018 at 10:38:25 UTC, Nicholas Wilson 
 wrote:
 ...
 [snip]
Simplified test case that still errors: ``` enum Operation { a, b } import std.traits; import std.conv; void main(string[] args) { foreach (_; 0 .. args.length) { Operation operation; switch (operation) { static foreach(e; EnumMembers!Operation) { case e: mixin(to!string(e))(); break; } } } } void a() {} void b() {} ```
Jan 27 2018
parent reply thedeemon <dlang thedeemon.com> writes:
On Saturday, 27 January 2018 at 11:19:37 UTC, Arun Chandrasekaran 
wrote:
 Simplified test case that still errors:
You got really close here. Here's a working version: enum Operation { a, b } import std.traits, std.conv, std.stdio; void main(string[] args) { auto op = Operation.a; foreach (_; 0 .. args.length) { final switch (op) { foreach(e; EnumMembers!Operation) { case e: mixin(e.to!string ~ "();"); break; } } } } void a() { writeln("A!"); } void b() { writeln("B!"); }
Jan 27 2018
parent reply Arun Chandrasekaran <aruncxy gmail.com> writes:
On Saturday, 27 January 2018 at 17:54:53 UTC, thedeemon wrote:
 On Saturday, 27 January 2018 at 11:19:37 UTC, Arun 
 Chandrasekaran wrote:
 Simplified test case that still errors:
You got really close here. Here's a working version: enum Operation { a, b } import std.traits, std.conv, std.stdio; void main(string[] args) { auto op = Operation.a; foreach (_; 0 .. args.length) { final switch (op) { foreach(e; EnumMembers!Operation) { case e: mixin(e.to!string ~ "();"); break; } } } } void a() { writeln("A!"); } void b() { writeln("B!"); }
Thanks, that did the trick. How to use break inside a static foreach? Changing the above foreach each to static gives the following error: Error: must use labeled break within static foreach
Jan 27 2018
parent reply thedeemon <dlang thedeemon.com> writes:
On Saturday, 27 January 2018 at 20:49:43 UTC, Arun Chandrasekaran 
wrote:

 Error: must use labeled break within static foreach
Just follow the compiler suggestion: void main(string[] args) { auto op = Operation.a; foreach (_; 0 .. args.length) { ops: final switch (op) { static foreach(e; EnumMembers!Operation) { case e: mixin(e.to!string ~ "();"); break ops; } } } } Here 'ops' is a label that we use to tell break what exactly it breaks. But really I'm not sure why you want static foreach here, a simple foreach is fine, it gets unrolled statically here just like static one.
Jan 27 2018
parent Arun Chandrasekaran <aruncxy gmail.com> writes:
On Sunday, 28 January 2018 at 04:44:23 UTC, thedeemon wrote:
 On Saturday, 27 January 2018 at 20:49:43 UTC, Arun 
 Chandrasekaran wrote:

 But really I'm not sure why you want static foreach here
I was just trying to see if static foreach can be used here, but well, you showed that it's not required. I just got intrigued to see the error on labeled break. Thanks anyway.
Jan 28 2018