Thread overview | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
January 27, 2018 parallelism | ||||
---|---|---|---|---|
| ||||
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 |
January 27, 2018 Re: parallelism | ||||
---|---|---|---|---|
| ||||
Posted in reply to Arun Chandrasekaran | 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..
|
January 27, 2018 Re: parallelism | ||||
---|---|---|---|---|
| ||||
Posted in reply to Arun Chandrasekaran | 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?
|
January 27, 2018 Re: parallelism | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nicholas Wilson | 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 ### and so on till the end of enum ``` |
January 27, 2018 Re: parallelism | ||||
---|---|---|---|---|
| ||||
Posted in reply to Arun Chandrasekaran | 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() {}
```
|
January 27, 2018 Re: parallelism | ||||
---|---|---|---|---|
| ||||
Posted in reply to Arun Chandrasekaran | 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!"); }
|
January 27, 2018 Re: parallelism | ||||
---|---|---|---|---|
| ||||
Posted in reply to thedeemon | 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
|
January 28, 2018 Re: parallelism | ||||
---|---|---|---|---|
| ||||
Posted in reply to Arun Chandrasekaran | 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.
|
January 29, 2018 Re: parallelism | ||||
---|---|---|---|---|
| ||||
Posted in reply to thedeemon | 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.
|
Copyright © 1999-2021 by the D Language Foundation