www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Multithreading in D

reply "Konstantin" <konstantin_kraus t-online.de> writes:
Hello D-World,

I've written a small terraingenerator in D based on the 
Hill-Algorithm.

To generate a terrain I only need to call the method 
generateTerrain(...) which returns a float-Array containing the 
height of each pixel (2D Array mapped with a 1D array with length 
resolution^2).

What I'd like to do:
Generate #treads seperate maps in a own thread and combine and 
normalize them afterwards.

I have little to no experience working with multiple threads. I 
tried it once in java for University but did not really get 
working.

What I imagine as solution (I know it won't work this way, but to 
give you a better idea):

for(int i = 0; i < #threads; i++){
     runInThread(generateTerrain(...));
}

Greetings and thanks in advance Konstantin
Oct 08 2014
parent reply "AsmMan" <jckj33 gmail.com> writes:
 What I imagine as solution (I know it won't work this way, but 
 to give you a better idea):

 for(int i = 0; i < #threads; i++){
     runInThread(generateTerrain(...));
 }
Are you looking for parallel? http://dlang.org/library/std/parallelism/parallel.html
Oct 08 2014
parent reply "Konstantin" <konstantin_kraus t-online.de> writes:
 Are you looking for parallel? 
 http://dlang.org/library/std/parallelism/parallel.html
I have seen this, but I'm not sure how to use it. Maybe: float[][] maps = new float[#threads][resolution * resolution]; foreach(i, ref elem; parallel(maps)){ elem = generateTerrain(...); } Does this look right?
Oct 09 2014
next sibling parent reply "Sag Academy" <sagacademyjaipur gmail.com> writes:
On Thursday, 9 October 2014 at 10:10:20 UTC, Konstantin wrote:
 Are you looking for parallel? 
 http://dlang.org/library/std/parallelism/parallel.html
I have seen this, but I'm not sure how to use it. Maybe: float[][] maps = new float[#threads][resolution * resolution]; foreach(i, ref elem; parallel(maps)){ elem = generateTerrain(...); } Does this look right?
Yeah, it is.
Oct 09 2014
parent Russel Winder via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Thu, 2014-10-09 at 11:29 +0000, Sag Academy via Digitalmars-d-learn
wrote:
 On Thursday, 9 October 2014 at 10:10:20 UTC, Konstantin wrote:
 Are you looking for parallel?=20
 http://dlang.org/library/std/parallelism/parallel.html
I have seen this, but I'm not sure how to use it. Maybe: float[][] maps =3D new float[#threads][resolution * resolution]; foreach(i, ref elem; parallel(maps)){ elem =3D generateTerrain(...); } Does this look right?
=20 Yeah, it is.
Or maybe not. The code above is fine per se, but only tackles the generation phase, there will be later phases. It may be that other parts of http://dlang.org/library/std/parallelism.html used slightly differently would be better for describing more of the problem. taskPool has a lot of interesting capabilities, including parallel reduce which gives a full map=E2=80=93reduce type scatter=E2=80=93gather approach: OP di= d mention that there was a generation then integration activity overall. Of course, if absolute performance is required things get a bit more tricky avoiding overheads. --=20 Russel. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.winder ekiga.n= et 41 Buckmaster Road m: +44 7770 465 077 xmpp: russel winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder
Nov 02 2014
prev sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 10/09/2014 03:10 AM, Konstantin wrote:
 Are you looking for parallel?
 http://dlang.org/library/std/parallelism/parallel.html
I have seen this, but I'm not sure how to use it.
I have the following chapter with some examples: http://ddili.org/ders/d.en/parallelism.html If concurrency is more suited to your application, then there is the following as well: http://ddili.org/ders/d.en/concurrency.html Ali
Oct 09 2014