Re: Fiber and Thread Communication

2016-04-09 Thread Ali Çehreli via Digitalmars-d-learn

On 04/09/2016 07:45 AM, Nordlöw wrote:
> On Friday, 8 April 2016 at 10:51:49 UTC, Nordlöw wrote:

> AFAICT, it is not clear what are the limitations of the current
> std.concurrency and, from what. An illustrating example on task-based
> parallellism (such as the ones in jin.go) should partly alleviate this
> problem.

I don't know how much this helps but I was able to write an example that 
seems to work (elsewhere in this thread):


  http://forum.dlang.org/post/nec62k$26v7$1...@digitalmars.com

Ali



Re: Fiber and Thread Communication

2016-04-09 Thread Ali Çehreli via Digitalmars-d-learn

On 04/08/2016 02:42 PM, Dicebot wrote:

>> Thanks Dicebot. I don't think the included
>> std.concurrency.FiberScheduler has support for message passing because
>> FiberScheduler.spawn does not return a Tid. If so, I don't see how
>> it's possible to send messages between fibers.
>>
>> Ali
>
> Looks like a (funny) oversight.

Sorry, I misled you. :)

> Note that you get it for get fiber via
> 
https://github.com/D-Programming-Language/phobos/blob/master/std/concurrency.d#L1337 


> (and FiberScheduler specifically extends Fiber to add ThreadInfo to it)
> but there is no clear way to pass that info to spawn host. I have a
> feeling that if that code is patched to simply provide Tid, message
> passing will just magically work. Needs to be checked though.

It turns out, instead of calling scheduler.spawn() directly, the program 
sets the __gshared 'scheduler' variable first and then calls spawn() as 
usual, which does return that fiber's Tid:


import std.stdio;
import std.concurrency;
import std.range;
import std.algorithm;

struct Done {
}

void workerTask(int id) {
writefln("workerTask %s started", id);

bool done = false;
while (!done) {
receive(
(int message) {
writefln("workerTask %s received %s", id, message);
ownerTid.send(message * id);
},
(Done message) {
writefln("workerTask %s received Done", id);
done = true;
});

// Seems not to be needed:
// scheduler.yield();
}

writefln("workerTask %s exiting", id);
}

void mainTask() {
enum workerCount = 5;
enum loopCount = 3;

writeln("mainTask started");

auto workers = iota(workerCount)
   .map!(id => spawn(, id))
   .array;

foreach (i; 0 .. loopCount) {
foreach (id, worker; workers) {
worker.send(i);
auto response = receiveOnly!int();
assert(response == i * id);
writefln("mainTask received %s", response);
}
}

writeln("mainTask sending Done messages");

foreach (worker; workers) {
worker.send(Done());
}

writeln("mainTask exiting");
}

void main() {
scheduler = new FiberScheduler;
scheduler.start({
mainTask();
});
}

Ali



Re: Fiber and Thread Communication

2016-04-09 Thread Nordlöw via Digitalmars-d-learn

On Friday, 8 April 2016 at 10:51:49 UTC, Nordlöw wrote:

Are there any plans to unite


AFAICT, it is not clear what are the limitations of the current 
std.concurrency and, from what. An illustrating example on 
task-based parallellism (such as the ones in jin.go) should 
partly alleviate this problem. Any ideas on what such an example 
should contain and illustrate. Current limitations and plans on 
fixing should be described aswell.


References:

http://forum.dlang.org/post/mailman.776.1459177268.26339.digitalmar...@puremagic.com
http://code.dlang.org/packages/jin-go
https://github.com/nin-jin/go.d

Further, who's up for the job of add the missing parts in 
std.concurrency using ideas and code from 
https://github.com/nin-jin/go.d ? :)


Re: Fiber and Thread Communication

2016-04-08 Thread Dicebot via Digitalmars-d-learn

On Friday, 8 April 2016 at 20:25:11 UTC, Ali Çehreli wrote:

On 04/08/2016 01:16 PM, Dicebot wrote:

On Friday, 8 April 2016 at 19:46:17 UTC, tcak wrote:

On Friday, 8 April 2016 at 15:33:46 UTC, Dicebot wrote:

On Friday, 8 April 2016 at 14:08:39 UTC, Nordlöw wrote:

So a TId can represent either a thread or a fiber?


AFAIR, yes (I haven't used std.concurrency in a long while, 
telling

all from memory only).


yes what? Thread or Fiber.


Yes both :) Tid represent abstract execution context, with no
implications about underlying executor.


Thanks Dicebot. I don't think the included 
std.concurrency.FiberScheduler has support for message passing 
because FiberScheduler.spawn does not return a Tid. If so, I 
don't see how it's possible to send messages between fibers.


Ali


Looks like a (funny) oversight. Note that you get it for get 
fiber via 
https://github.com/D-Programming-Language/phobos/blob/master/std/concurrency.d#L1337 (and FiberScheduler specifically extends Fiber to add ThreadInfo to it) but there is no clear way to pass that info to spawn host. I have a feeling that if that code is patched to simply provide Tid, message passing will just magically work. Needs to be checked though.


Re: Fiber and Thread Communication

2016-04-08 Thread Ali Çehreli via Digitalmars-d-learn

On 04/08/2016 01:16 PM, Dicebot wrote:

On Friday, 8 April 2016 at 19:46:17 UTC, tcak wrote:

On Friday, 8 April 2016 at 15:33:46 UTC, Dicebot wrote:

On Friday, 8 April 2016 at 14:08:39 UTC, Nordlöw wrote:

So a TId can represent either a thread or a fiber?


AFAIR, yes (I haven't used std.concurrency in a long while, telling
all from memory only).


yes what? Thread or Fiber.


Yes both :) Tid represent abstract execution context, with no
implications about underlying executor.


Thanks Dicebot. I don't think the included 
std.concurrency.FiberScheduler has support for message passing because 
FiberScheduler.spawn does not return a Tid. If so, I don't see how it's 
possible to send messages between fibers.


Ali



Re: Fiber and Thread Communication

2016-04-08 Thread Dicebot via Digitalmars-d-learn

On Friday, 8 April 2016 at 19:46:17 UTC, tcak wrote:

On Friday, 8 April 2016 at 15:33:46 UTC, Dicebot wrote:

On Friday, 8 April 2016 at 14:08:39 UTC, Nordlöw wrote:

So a TId can represent either a thread or a fiber?


AFAIR, yes (I haven't used std.concurrency in a long while, 
telling all from memory only).


yes what? Thread or Fiber.


Yes both :) Tid represent abstract execution context, with no 
implications about underlying executor.


Re: Fiber and Thread Communication

2016-04-08 Thread tcak via Digitalmars-d-learn

On Friday, 8 April 2016 at 15:33:46 UTC, Dicebot wrote:

On Friday, 8 April 2016 at 14:08:39 UTC, Nordlöw wrote:

So a TId can represent either a thread or a fiber?


AFAIR, yes (I haven't used std.concurrency in a long while, 
telling all from memory only).


yes what? Thread or Fiber.

---

Anyway. Since, Fiber is not like a thread, and when a thread 
starts a Fiber, it is like calling a normal function, I guess TId 
represents the thread still.


Re: Fiber and Thread Communication

2016-04-08 Thread Alex Parrill via Digitalmars-d-learn

On Friday, 8 April 2016 at 14:08:39 UTC, Nordlöw wrote:


So a TId can represent either a thread or a fiber?


It represents a "logical thread", which currently consists of 
coroutines or OS threads but could theoretically be extended to, 
say, other processes or even other machines.


Re: Fiber and Thread Communication

2016-04-08 Thread Dicebot via Digitalmars-d-learn

On Friday, 8 April 2016 at 14:08:39 UTC, Nordlöw wrote:

So a TId can represent either a thread or a fiber?


AFAIR, yes (I haven't used std.concurrency in a long while, 
telling all from memory only).


Re: Fiber and Thread Communication

2016-04-08 Thread Nordlöw via Digitalmars-d-learn

On Friday, 8 April 2016 at 13:15:07 UTC, Dicebot wrote:

On Friday, 8 April 2016 at 11:18:11 UTC, Nordlöw wrote:

On Friday, 8 April 2016 at 11:01:21 UTC, Dicebot wrote:
Doesn't std.concurrency support both right now? I remember 
seeing PR that adds message box support to fibers ages ago.


See https://issues.dlang.org/show_bug.cgi?id=12090 and 
https://github.com/D-Programming-Language/phobos/pull/1910 for 
relevant code (as you can see it was merged several releases 
ago)



1. What functions provide message box communication?


The same ones as thread ones. API is completely transparent.


2. But Fibers cannot currently be moved between threads right?


Yes, and this is by design. It harms performance of concurrent 
apps.


So a TId can represent either a thread or a fiber?


Re: Fiber and Thread Communication

2016-04-08 Thread Dicebot via Digitalmars-d-learn

On Friday, 8 April 2016 at 11:18:11 UTC, Nordlöw wrote:

On Friday, 8 April 2016 at 11:01:21 UTC, Dicebot wrote:
Doesn't std.concurrency support both right now? I remember 
seeing PR that adds message box support to fibers ages ago.


See https://issues.dlang.org/show_bug.cgi?id=12090 and 
https://github.com/D-Programming-Language/phobos/pull/1910 for 
relevant code (as you can see it was merged several releases ago)



1. What functions provide message box communication?


The same ones as thread ones. API is completely transparent.


2. But Fibers cannot currently be moved between threads right?


Yes, and this is by design. It harms performance of concurrent 
apps.


Re: Fiber and Thread Communication

2016-04-08 Thread Nordlöw via Digitalmars-d-learn

On Friday, 8 April 2016 at 11:01:21 UTC, Dicebot wrote:
Doesn't std.concurrency support both right now? I remember 
seeing PR that adds message box support to fibers ages ago.


What progress has been since post:

http://forum.dlang.org/post/k4jsef$26h6$1...@digitalmars.com


Re: Fiber and Thread Communication

2016-04-08 Thread Nordlöw via Digitalmars-d-learn

On Friday, 8 April 2016 at 11:01:21 UTC, Dicebot wrote:
Doesn't std.concurrency support both right now? I remember 
seeing PR that adds message box support to fibers ages ago.


1. What functions provide message box communication?

2. But Fibers cannot currently be moved between threads right?


Re: Fiber and Thread Communication

2016-04-08 Thread Dicebot via Digitalmars-d-learn

On Friday, 8 April 2016 at 10:51:49 UTC, Nordlöw wrote:

Are there any plans to unite

fiber-to-fiber communication

with

thread-to-thread communication

in Phobos?

Does vibe.d give any solutions here?


Doesn't std.concurrency support both right now? I remember seeing 
PR that adds message box support to fibers ages ago.


Fiber and Thread Communication

2016-04-08 Thread Nordlöw via Digitalmars-d-learn

Are there any plans to unite

fiber-to-fiber communication

with

thread-to-thread communication

in Phobos?

Does vibe.d give any solutions here?