Will Dart execute isolates in parallel in a multi-core environment?
Asked Answered
R

3

21

Question

Will isolates in Dart run in parallel utilizing all available cores on a multiple core environment, or will it multiplex on a single core?

Background

Google has described isolates (a single-threaded unit of concurrency) in the Dart programming language as a "light weight thread" that operates on the main stack, without blocking.

Thus, it seems to me as it will only be able to multiplex on a single core and not be able to run in parallel over multiple cores in a SMP, dualcore, multicore or clustered environment.

Though, I can't find any information on this, hence my humble question.

Relive answered 6/7, 2012 at 13:59 Comment(3)
Did you run any experiments to figure this out ?Planula
It it doesn't have its own stack, it's not a thread. More like coroutine, maybe? If a 'thread' cannot block, it cannot wait for I/O or other threads. I don't see much of any upside to such a contraption <g>Loggins
Yes, indeed it is not a thread. I have just finished running some tests (silly me to not run them before I asked the question).Relive
R
16

Warning: the code below is out of date and does not work with Dart 1.0.

Short answer

Maybe.

Long Answer

The dart:isolate library guide states: "Isolates might run in a separate process or thread, depending on the implementation. For web applications, isolates can be compiled to Web workers, if they are available." (my emphasis)

Running this code and observing your CPU load will tell you if your implementation does this or not.

#import('dart:isolate');
main() {
  for (var tmp = 0; tmp < 5; ++tmp) {
    SendPort sendPort = spawnFunction(runInIsolate);
    sendPort.call(tmp).then((reply) {
      print(reply);
    });
  }
}

runInIsolate() {
  port.receive((msg, SendPort reply) {
    var k = 0;
    var max = (5 - msg) * 100000000; 
    for (var i = 0; i < max; ++i) {
        i = ++i - 1;
        k = i;
    }
    reply.send("I received: $msg and calculated $k");
  });
}

The standalone dartvm will run isolates in parallel, utilizing all available cores. Browser implementations of Dart will likely vary depending on whether Web Workers are implemented or not.

Relive answered 6/7, 2012 at 14:48 Comment(0)
C
9

Here is updated code for Dart 1.0.

import 'dart:isolate';

main() {
  int counter = 0;

  ReceivePort receivePort = new ReceivePort();

  receivePort.listen((msg) {
    if (msg is SendPort) {
      msg.send(counter++);
    } else {
      print(msg);
    }
  });

  for (var i = 0; i < 5; i++) {
    Isolate.spawn(runInIsolate, receivePort.sendPort);
  }
}

runInIsolate(SendPort sendPort) {
  ReceivePort receivePort = new ReceivePort();
  sendPort.send(receivePort.sendPort);

  receivePort.listen((msg) {
    var k = 0;
    var max = (5 - msg) * 100000000; 
    for (var i = 0; i < max; ++i) {
        i = ++i - 1;
        k = i;
    }
    sendPort.send("I received: $msg and calculated $k");
  });
}
Chondriosome answered 20/1, 2014 at 5:44 Comment(1)
Does Dartvm run isolates in parallel ? Will it be able to use all available cores ?Warmup
L
4

I looked it up. The isolates seem to be actual threads.

The only mechanism available to communicate between isolates is to pass messages.

Very good, except

Each isolate has its own heap, which means that all values in memory, including globals, are available only to that isolate.

Not at all good, because of the messages:

it is also possible to send object instances (which would be copied in the process

Very bad.

This scheme would seem to make it impossible to communicate large amounts of data from one isolate to another without copying it, so eliminating efficient interisolate communication.

I would not use it because of this restriction that disallows the passing of large objects/buffers by address, as one would normally do with conventional threads.

It looked interesting at first, because I use almost exclusively message-passing designs, but they knackered the inter-thread comms by insisting on only private heaps.

Loggins answered 6/7, 2012 at 14:32 Comment(7)
The library docs state this: "Isolates might run in a separate process or thread, depending on the implementation. For web applications, isolates can be compiled to Web workers, if they are available." (My emphasis)Relive
I wouldn't worry about this that much. Dart people are surely aware of this issue, as it is Google Chrome who pioneers the concept of objects transferable between Web Workers (see updates.html5rocks.com/2011/12/…). I expect something like this to appear in Dart too.Affirm
This is not a bug, but a feature to prevent one thread changing anything by ref that is used in a different thread. Rust does it the same way on purpose. D does not have separate heaps for every thread, but you cannot send anything by reference to a thread in D.Phonetist
I think it is because the GC is single threaded and it is just for one Isolate.So it is impossible for sharing objects between Isolates. V8 has a similar design and limits.Carillon
Are isolates basically Web Workers, but for Dart? Was thinking about switching from nodejs to a dart server, but... if V8 is single threaded.. I'm still going to have be passing the objects around to the other isolate or web worker, regardless right?Riannon
The isolates seems to be very low level API, however, it lacks the power of control that a low level API should have. For developing Flutter App, I found Isolate is very hard to use compare to pthread. Data sharing and synchronizing are extremely painful. Time spent on working around/solving isolates' limitation other than focusing on the problem itself.Alansen
For what its worth I'm using it to create images in an isolate and pass it back (as bytes), and it works pretty well. Sure, there is some loss in efficiency, but for heavy processing on a different isolate, it can prevent the main isolate getting locked and ui remaining decent.Dunite

© 2022 - 2024 — McMap. All rights reserved.