On 10/10/2019 05:41 PM, Murilo wrote:
> I have started working with neural networks and for that I need a lot of
> computing power but the programs I make only use around 30% of the cpu,
> or at least that is what Task Manager tells me. How can I make it use
> all 4 cores of my AMD FX-4300 and how can I make it use 100% of it?

Your threads must allocate as little memory as possible because memory allocation can trigger garbage collection and garbage collection stops all threads (except the one that's performing collection).

We studied the effects of different allocation schemes during our last local D meetup[1]. The following program has two similar worker threads. One allocates in an inner scope, the other one uses a static Appender and clears its state as needed.

The program sets 'w' to 'worker' inside main(). Change it to 'worker2' to see a huge difference: On my 4-core laptop its 100% versus 400% CPU usage.

import std.random;
import std.range;
import std.algorithm;
import std.concurrency;
import std.parallelism;

enum inner_N = 100;

void worker() {
  ulong result;
  while (true) {
    int[] arr;
    foreach (j; 0 .. inner_N) {
      arr ~= uniform(0, 2);
    }
    result += arr.sum;
  }
}

void worker2() {
  ulong result;
  static Appender!(int[]) arr;
  while (true) {
    arr.clear();
    foreach (j; 0 .. inner_N) {
      arr ~= uniform(0, 2);
    }
    result += arr.data.sum;
  }
}

void main() {
  // Replace with 'worker2' to see the speedup
  alias w = worker;

  auto workers = totalCPUs.iota.map!(_ => spawn(&w)).array;

  w();
}

The static Appender is thread-safe because each thread gets their own copy due to data being thread-local by default in D. However, it doesn't mean that the functions are reentrant: If they get called recursively perhaps indirectly, then the subsequent executions would corrupt previous executions' Appender states.

Ali

[1] https://www.meetup.com/D-Lang-Silicon-Valley/events/kmqcvqyzmbzb/ Are you someone in the Bay Area but do not come to our meetups? We've been eating your falafel wraps! ;)

Reply via email to