Re: parallelism with delegate

2023-09-22 Thread Imperatorn via Digitalmars-d-learn
On Friday, 22 September 2023 at 04:24:19 UTC, Vitaliy Fadeev 
wrote:

able ?
how to use correctly?

```d
  import std.parallelism;

  auto async_task = task!fn( args );  // error
  // Error: no property 
`opCall` for type `app.A`, did you mean `new A`?


  async_task.executeInNewThread();
```

where

```d
auto a = new A();
auto fn = 

class A
{
void fn( string url )
{
// DO
}
}
```

Playground:
https://run.dlang.io/is/HvhtoP

gist:
https://gist.github.com/run-dlang/218b69e1afd79e5944ea10aa7ca61e1b


Also check out std.concurrency


Re: parallelism with delegate

2023-09-22 Thread Jonathan M Davis via Digitalmars-d-learn
On Thursday, September 21, 2023 10:33:44 PM MDT Vitaliy Fadeev via 
Digitalmars-d-learn wrote:
> On Friday, 22 September 2023 at 04:24:19 UTC, Vitaliy Fadeev
>
> wrote:
> > ...
>
> Skip this thread. I see solution.
>
> How to delete missed posts on this forum ?

This forum is esentially just a web client for some D-specific newsgroups
(and a number of folks access it via either the newsgroup interface or its
associated mailing list rather than through the web interface). So, you
can't edit or remove posts. Admins can remove spam from the newsgroup (and
thus the forum), but that's pretty much it, and even then, that doesn't
remove it from the mailing list, because you can't get an e-mail back once
it's been sent. So, once you send something to the forum, it's out there
forever.

- Jonathan M Davis





Re: parallelism with delegate

2023-09-21 Thread user1234 via Digitalmars-d-learn
On Friday, 22 September 2023 at 04:33:44 UTC, Vitaliy Fadeev 
wrote:
On Friday, 22 September 2023 at 04:24:19 UTC, Vitaliy Fadeev 
wrote:

...


Skip this thread. I see solution.

How to delete missed posts on this forum ?


It's there forever, you have to live with that error ;)

See https://forum.dlang.org/help#about




Re: parallelism with delegate

2023-09-21 Thread Vitaliy Fadeev via Digitalmars-d-learn
On Friday, 22 September 2023 at 04:24:19 UTC, Vitaliy Fadeev 
wrote:

...


Skip this thread. I see solution.

How to delete missed posts on this forum ?


Re: parallelism

2018-01-28 Thread Arun Chandrasekaran via Digitalmars-d-learn

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.


Re: parallelism

2018-01-27 Thread thedeemon via Digitalmars-d-learn
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.


Re: parallelism

2018-01-27 Thread Arun Chandrasekaran via Digitalmars-d-learn

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




Re: parallelism

2018-01-27 Thread thedeemon via Digitalmars-d-learn
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!"); }


Re: parallelism

2018-01-27 Thread Arun Chandrasekaran via Digitalmars-d-learn
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() {}
```


Re: parallelism

2018-01-27 Thread Arun Chandrasekaran via Digitalmars-d-learn
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
```


Re: parallelism

2018-01-27 Thread Nicholas Wilson via Digitalmars-d-learn
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?




Re: parallelism

2018-01-27 Thread Arun Chandrasekaran via Digitalmars-d-learn
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..


Re: Parallelism Map and Reduce

2012-12-12 Thread Zardoz

On Tuesday, 11 December 2012 at 17:50:31 UTC, Ali Çehreli wrote:

On 12/11/2012 08:12 AM, Zardoz wrote:


Could you please move MapIntegrator() to module-level. Then it 
should work.


Ali


I try it and now even with normal Map function give me errors 
with dmd !


public Entity MapIntegrator ( Entity me) {
  me.Integrador3Orden ();
  return me;
}

void main() {
  Entity[] objects;
...
  objects = array( map!MapIntegrator(objects) );
...
}

With this error :
dmd -w -wi -version=SReduction simulator.d entity.d vector.d 
-ofreduceSim
simulator.d(194): Error: template 
std.algorithm.map!(MapIntegrator).map does not match any function 
template declaration
/usr/include/dmd/phobos/std/algorithm.d(369): Error: template 
std.algorithm.map!(MapIntegrator).map(Range) if 
(isInputRange!(Unqual!(Range))) cannot deduce template function 
from argument types !()(Entity[])






Re: Parallelism Map and Reduce

2012-12-12 Thread Ali Çehreli

On 12/12/2012 05:47 AM, Zardoz wrote:
 On Tuesday, 11 December 2012 at 17:50:31 UTC, Ali Çehreli wrote:
 On 12/11/2012 08:12 AM, Zardoz wrote:


 Could you please move MapIntegrator() to module-level. Then it should
 work.

 Ali

 I try it and now even with normal Map function give me errors with dmd !

 public Entity MapIntegrator ( Entity me) {
 me.Integrador3Orden ();
 return me;
 }

 void main() {
 Entity[] objects;
 ...
 objects = array( map!MapIntegrator(objects) );
 ...
 }

 With this error :
 dmd -w -wi -version=SReduction simulator.d entity.d vector.d -ofreduceSim
 simulator.d(194): Error: template std.algorithm.map!(MapIntegrator).map
 does not match any function template declaration
 /usr/include/dmd/phobos/std/algorithm.d(369): Error: template
 std.algorithm.map!(MapIntegrator).map(Range) if
 (isInputRange!(Unqual!(Range))) cannot deduce template function from
 argument types !()(Entity[])

Strange. The following program works for me with dmd 2.060. It uses both 
the regular and parallel versions of map and reduce:


import std.array;
import std.algorithm;
import std.parallelism;

struct Entity
{
void Integrador3Orden()
{}
}

public Entity MapIntegrator ( Entity me) {
  me.Integrador3Orden ();
  return me;
}

void main() {
  Entity[] objects;

  objects = array( map!MapIntegrator(objects) );
  objects = array(taskPool.map!MapIntegrator(objects));

  int[] acelByObjs;
  int reduced = reduce!a + b(0, acelByObjs);
  reduced = taskPool.reduce!a + b(0, acelByObjs);
}

Ali



Re: Parallelism Map and Reduce

2012-12-11 Thread Ali Çehreli

On 12/11/2012 02:53 AM, Zardoz wrote:

 auto acelByObjs = map!( (Entity o) {
 Vector3 r = o.pos[0] - pos[0];
 return r * (o.mass / pow((r.sq_length + epsilon2), 1.5));
 } )(objects);
 newAcel = reduce!(a + b)(acelByObjs);

 It works very well with the std.algorithm Map and Reduce but when I try
 to use std.parallelism versions of it, parallel Map give me this
 compilataion errors :

 entity.d(63): Error: template instance map!(delegate @system
 Vector3(Entity o)
 {
 Vector3 r = o.pos[0LU].opBinary(this.pos[0LU]);
 return r.opBinary(o.mass / pow(r.sq_length() + epsilon2,1.5));
 }
 ) cannot use local '__lambda3' as parameter to non-global template
 map(functions...)

That used to work a couple of dmd versions ago. I think it was a bug 
that it worked, so it stopped working after bug fixes.


If I'm not mistaken this is actually related to a compiler 
implementation issue: Lambda's have a single pointer to store the 
context that they have been started in.


When a lambda is a free-standing function (aka module function or 
global function) then there is only the context to deal with. When the 
template is a member function (taskPool.map is) then there is also the 
object that the function is started on.


The single pointer of the lambda is not sufficient to store both without 
big changes in the compiler.


(I may be off with that description above. e.g. there may be two 
pointers when three are actually needed, etc.)


I had to change following chapter after dmd's behavior had changed:

  http://ddili.org/ders/d.en/parallelism.html

--- Quoting ---
import std.parallelism;
// ...
double averageGrade(Student student)
{
return student.averageGrade;
}
// ...
auto results = taskPool.map!averageGrade(students, 3);

Note: The free-standing averageGrade() function above is needed due to a 
limitation that involves using local delegates with member function 
templates like TaskPool.map:


  auto results = taskPool.map!(a = a.averageGrade)(students, 3); 
// ← compilation ERROR

--

As you see above, the solution is to use a function with taskPool.map, 
not a lambda.


Ali



Re: Parallelism Map and Reduce

2012-12-11 Thread bearophile

Ali Çehreli:

The single pointer of the lambda is not sufficient to store 
both without big changes in the compiler.


I think adding a heavier 3-word delegate is not too much hard to 
do. But it makes the language more complex, so so far Walter is 
not willing to introduce them.


But in the end introducing them may become inevitable :-) I think 
such hypothetical 3-word delegates need to be discussed in the 
main D newsgroup.


Bye,
bearophile


Re: Parallelism Map and Reduce

2012-12-11 Thread Zardoz

On Tuesday, 11 December 2012 at 15:22:49 UTC, Ali Çehreli wrote:
That used to work a couple of dmd versions ago. I think it was 
a bug that it worked, so it stopped working after bug fixes.


If I'm not mistaken this is actually related to a compiler 
implementation issue: Lambda's have a single pointer to store 
the context that they have been started in.


When a lambda is a free-standing function (aka module 
function or global function) then there is only the context 
to deal with. When the template is a member function 
(taskPool.map is) then there is also the object that the 
function is started on.


The single pointer of the lambda is not sufficient to store 
both without big changes in the compiler.


(I may be off with that description above. e.g. there may be 
two pointers when three are actually needed, etc.)


I had to change following chapter after dmd's behavior had 
changed:


  http://ddili.org/ders/d.en/parallelism.html

--- Quoting ---
import std.parallelism;
// ...
double averageGrade(Student student)
{
return student.averageGrade;
}
// ...
auto results = taskPool.map!averageGrade(students, 3);

Note: The free-standing averageGrade() function above is needed 
due to a limitation that involves using local delegates with 
member function templates like TaskPool.map:


  auto results = taskPool.map!(a = a.averageGrade)(students, 
3); // ← compilation ERROR

--

As you see above, the solution is to use a function with 
taskPool.map, not a lambda.


Ali


I try to use a function instead of a lambda function I'm keep 
getting compiler errors. Code :

  Entity MapIntegrator (ref Entity me) {
me.Integrador3Orden (iDeltaT);
return me;
  }
  objects = array( taskPool.map!MapIntegrator(objects) );

With taskPool.Map I get this errors :
simulator.d(196): Error: template instance map!(MapIntegrator) 
cannot use local 'MapIntegrator' as parameter to non-global 
template map(functions...)
/usr/include/dmd/phobos/std/parallelism.d(1969): Error: function 
std.parallelism.TaskPool.map!(MapIntegrator).map!(Entity[]).map.Map.fillBuf 
cannot access frame of function D main
/usr/include/dmd/phobos/std/parallelism.d(1974): Error: template 
instance amap!(MapIntegrator) cannot use local 'MapIntegrator' as 
parameter to non-global template amap(functions...)
/usr/include/dmd/phobos/std/parallelism.d(1675): Error: function 
std.parallelism.TaskPool.amap!(MapIntegrator).amap!(Entity[],ulong,Entity[]).amap 
cannot access frame of function D main
/usr/include/dmd/phobos/std/parallelism.d(1706): Error: function 
std.parallelism.TaskPool.amap!(MapIntegrator).amap!(Entity[],ulong,Entity[]).amap.doIt 
cannot access frame of function D main
/usr/include/dmd/phobos/std/parallelism.d(1974): Error: template 
instance 
std.parallelism.TaskPool.amap!(MapIntegrator).amap!(Entity[],ulong,Entity[]) 
error instantiating

simulator.d(196):instantiated from here: map!(Entity[])
simulator.d(196): Error: template instance 
std.parallelism.TaskPool.map!(MapIntegrator).map!(Entity[]) error 
instantiating

make: *** [predSim] Error 1

But again, with std.algorthim Map it don give any error and works 
fine.