Create many objects using threads

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon May 5 11:25:37 PDT 2014


On 05/05/2014 10:25 AM, Ali Çehreli wrote:

 > On 05/05/2014 10:14 AM, Caslav Sabani wrote:
 >
 >  > I want to have one array where I will store like 100000  objects.
 >  >
 >  > But I want to use 4 threads where each thread will create 25000 
objects
 >  > and store them in array above mentioned.
 >
 > 1) If it has to be a single array, meaning that all of the objects are
 > in consecutive memory, you can create the array and give four slices of
 > it to the four tasks.
 >
 > To do that, you can either create a proper D array filled with objects
 > with .init values; or you can allocate any type of memory and create
 > objects in place there.

Here is an example:

import std.stdio;
import std.parallelism;
import core.thread;
import std.conv;

enum elementCount = 8;
size_t elementPerThread;

static this ()
{
     assert((elementCount % totalCPUs) == 0,
            "Cannot distribute tasks to cores evenly");

     elementPerThread = elementCount / totalCPUs;
}

void main()
{
     auto arr = new int[](elementCount);

     foreach (i; 0 .. totalCPUs) {
         const beg = i * elementPerThread;
         const end = beg + elementPerThread;
         arr[beg .. end] = i.to!int;
     }

     thread_joinAll();    // (I don't think this is necessary with 
std.parallelism)

     writeln(arr);    // [ 0, 0, 1, 1, 2, 2, 3, 3 ]
}

 > 2) If it doesn't have to a single array, you can have the four tasks
 > create four separate arrays. You can then use them as a single range by
 > std.range.chain.

That is a lie. :) chain would work but it had to know the number of 
total cores at compile time. Instead, joiner or join can be used:

import std.stdio;
import std.parallelism;
import core.thread;

enum elementCount = 8;
size_t elementPerThread;

static this ()
{
     assert((elementCount % totalCPUs) == 0,
            "Cannot distribute tasks to cores evenly");

     elementPerThread = elementCount / totalCPUs;
}

void main()
{
     auto arr = new int[][](totalCPUs);

     foreach (i; 0 .. totalCPUs) {
         foreach (e; 0 .. elementPerThread) {
             arr[i] ~= i;
         }
     }

     thread_joinAll();    // (I don't think this is necessary with 
std.parallelism)

     writeln(arr);             // [[0, 0], [1, 1], [2, 2], [3, 3]]

     import std.range;
     writeln(arr.joiner);      // [ 0, 0, 1, 1, 2, 2, 3, 3 ]

     import std.algorithm;
     auto arr2 = arr.joiner.array;
     static assert(is (typeof(arr2) == int[]));
     writeln(arr2);           // [ 0, 0, 1, 1, 2, 2, 3, 3 ]

     auto arr3 = arr.join;
     static assert(is (typeof(arr3) == int[]));
     writeln(arr3);           // [ 0, 0, 1, 1, 2, 2, 3, 3 ]
}

 > This option allows you to have a single array as well.

arr2 and arr3 above are examples of that.

Ali



More information about the Digitalmars-d-learn mailing list