digitalmars.D.learn - parallelism
- Arun Chandrasekaran (36/36) Jan 27 2018 Hi All,
- Arun Chandrasekaran (4/7) Jan 27 2018 Damn! The subject should've been something else.. naming is
- Nicholas Wilson (3/15) Jan 27 2018 Does that do the trick?
- Arun Chandrasekaran (16/31) Jan 27 2018 No it doesn't.
- Arun Chandrasekaran (25/29) Jan 27 2018 Simplified test case that still errors:
- thedeemon (22/23) Jan 27 2018 You got really close here. Here's a working version:
- Arun Chandrasekaran (5/28) Jan 27 2018 Thanks, that did the trick.
- thedeemon (20/21) Jan 27 2018 Just follow the compiler suggestion:
- Arun Chandrasekaran (5/8) Jan 28 2018 I was just trying to see if static foreach can be used here, but
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
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
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
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: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 `````` 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
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: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() {} ```... [snip]
Jan 27 2018
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
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: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 foreachSimplified 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
On Saturday, 27 January 2018 at 20:49:43 UTC, Arun Chandrasekaran wrote:Error: must use labeled break within static foreachJust 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
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 hereI 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