www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - std.threads : smp programming

reply Wiz <clem.codeur gmail.com> writes:
std.threads is a great lib, helped me more than one time.
But there was a missing thing, something to really help programmers
making smp code.
We need to create a thread by core/cpu, but, there is nothing like a
function that return the number of processors.
So I googled that, and found in c the get_nprocs function, returning the
  number of available cpus. It's used by including sys/sysinfo.h

Here is a D code that exploits it :


import std.stdio;
import std.thread;
import std.c.time;
import std.math;


extern (C)
{
	//sys/sysinfo.h       info libc --index-search=_SC_NPROCESSORS_CONF
	extern int get_nprocs();
}

class Nb1Calc {
	int increment;
	int number;
	this(int n, int i) {
		increment=i;
		number=n;

		Thread t=new Thread(&calc);
		t.start();
	}
	bool isnb1(int a) {
		int m=cast(int)sqrt(cast(float)a);
		for (int i=2; i<=m; i++)
			if (a%i == 0)
				return false;
		return true;
	}
	int calc() {
		writefln("[Thread 0x", cast(void*)Thread.getThis(), "] Starting thread
(", number, " + x * ", increment, ")");
		for (int i=number; ; i+=increment)
			if (isnb1(i)==true)
				writefln("[Thread 0x", cast(void*)Thread.getThis(), "] Number ", i,
" is a prime number");
		return 0;
	}

}

int main(char[][] args) {
	int ncpu=get_nprocs();
	writefln("Separating in ", ncpu, " threads.");
	for (int i=0; i < ncpu; i++)
		new Nb1Calc((i+1)*2+1, ncpu*2);

        for (;;)
		sleep(2);

	return 0;
}



Determinating prime numbers using each thread by core.
Sorry for using no lock while writefln, it's just an simple example.


Is there a way that get_nprocs could be added in the d's std lib ?
Something like int Thread.get_nprocs(); would be fine.
Jul 31 2007
next sibling parent "Vladimir Panteleev" <thecybershadow gmail.com> writes:
On Wed, 01 Aug 2007 01:43:15 +0300, Wiz <clem.codeur gmail.com> wrote:

 std.threads is a great lib, helped me more than one time.
 But there was a missing thing, something to really help programmers
 making smp code.
 We need to create a thread by core/cpu, but, there is nothing like a
 function that return the number of processors.
I'd just like to add that on Windows, this is done using the GetSystemInfo function (the dwNumberOfProcessors field of the SYSTEM_INFO structure). There's some more info on the subject in this Tango ticket: http://www.dsource.org/projects/tango/ticket/371 -- Best regards, Vladimir mailto:thecybershadow gmail.com
Jul 31 2007
prev sibling parent renoX <renosky free.fr> writes:
Wiz Wrote:

 std.threads is a great lib, helped me more than one time.
 But there was a missing thing, something to really help programmers
 making smp code.
 We need to create a thread by core/cpu, but, there is nothing like a
 function that return the number of processors.
One thing to be cautious: when Intel "hyperthreading" is activated, each core can be seen as two cores, except that the performance is very different (around 20% max with hyperthreading compared to a max of 100% for another real core). So, there should be more information returned: number of real core, number of virtual core for example. renoX
Aug 01 2007