You are not logged in.

serg

Beginner

  • "serg" is male
  • "serg" started this thread

Posts: 32

Location: Russia, Moscow

Occupation: Programmer

  • Send private message

1

Monday, November 1st 2004, 1:56pm

Problem with QThread under Solaris 8

I have problem with QThread under Solaris 8.
Threads don't use all processors, all threads of one process use only one processor!!!
I wrote next test


Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <qthread.h>
#include <qdatetime.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <math.h>
 
#ifdef PROCESSES_RELEASE
  #include <unistd.h>
#endif

int **arr;
const int sz = 1000000;

void init( int na )
// Allocate na arrays and filling it random numbers
{
	srand( (unsigned)time( NULL ) );
	qWarning( "Initialize arrays" );
	arr = new int*[na];
	for ( int i=0; i < na; i++) {
		arr[i] = new int[sz];	
		for ( int j=0; j < sz; j++ ) arr[i][j] = rand();
	}
}

class Worker : public QThread
{
public:
	Worker( int begarr, int endarr ) : QThread(), ba(begarr), ea(endarr) {}
	void run() {
#ifdef PROCESSES_RELEASE
	  int pid = fork();
	  if ( ! pid ) {
#endif
			for ( int i=ba; i <= ea; i++ ) {
				double sum = 0;
				for (int j=0; j < sz; j++) sum += sin( double(arr[i][j]) );
			}
#ifdef PROCESSES_RELEASE
	  }
	  else {
	    waitpid( pid, 0, 0);
	  }	
#endif
	} 
private:
	int ba, ea;
};

int main( int argc, char** argv )
{
	if ( argc < 3 ) qFatal( "Usage : %s nArrays nThreads", argv[0] );
	const int numarr = atoi(argv[1]);
	const int numthr = atoi(argv[2]);

	init( numarr );
	QThread** threads = new QThread*[numthr];
	int cnt = 0, i, j;

	int apt = numarr / numthr;
	for ( i = 0; i < numarr; i+=apt) {
		j = i + apt - 1;
		j = j >= sz ? sz - 1 : j;
	    qDebug("Thread %d : %d - %d", cnt, i, j); 
		threads[cnt] = new Worker( i, j );
		cnt++;				
	}

	QTime tm;
	tm.start();
	qWarning( "Running threads..." );
	for ( i = 0; i < numthr; i++ ) threads[i]->start();
	for ( i = 0; i < numthr; i++ ) threads[i]->wait();
	qWarning( "Time elapsed : %d ms", tm.elapsed() );
	return 0;	
}


and run it on SUNFIRE V880( 8 SPARC III processors, 16 GB RAM )
Then I don't define PROCESSES_RELEASE I have :

./threadtest 8 8
Initialize arrays
Thread 0 : 0 - 0
Thread 1 : 1 - 1
Thread 2 : 2 - 2
Thread 3 : 3 - 3
Thread 4 : 4 - 4
Thread 5 : 5 - 5
Thread 6 : 6 - 6
Thread 7 : 7 - 7
Running threads...
Time elapsed : 3818 ms

and with PROCESSES_RELEASE :

./threadtest 8 8
Initialize arrays
Thread 0 : 0 - 0
Thread 1 : 1 - 1
Thread 2 : 2 - 2
Thread 3 : 3 - 3
Thread 4 : 4 - 4
Thread 5 : 5 - 5
Thread 6 : 6 - 6
Thread 7 : 7 - 7
Running threads...
Time elapsed : 716 ms

I using Qt 3.2.2 ( multi-threaded ), SunOS 5.8 and gcc 3.3.2
Do you have any idea?
Thanx
P.S. sorry for terrible English

  • "wysota" is male

Posts: 4,276

Location: Warsaw, POLAND

  • Send private message

serg

Beginner

  • "serg" is male
  • "serg" started this thread

Posts: 32

Location: Russia, Moscow

Occupation: Programmer

  • Send private message

3

Monday, November 1st 2004, 3:40pm

Quoted

But what exactly is your question?

Why threads don't use all processors in system, though must do it!

  • "wysota" is male

Posts: 4,276

Location: Warsaw, POLAND

  • Send private message

4

Monday, November 1st 2004, 3:56pm

Maybe the scheduler was ordered not to put threads from one process on different processors. Ask your sysadmin...

serg

Beginner

  • "serg" is male
  • "serg" started this thread

Posts: 32

Location: Russia, Moscow

Occupation: Programmer

  • Send private message

5

Tuesday, November 2nd 2004, 4:59pm

Quoted


scheduler was ordered not to put threads from one process on different processors

Why do you think what it's possible?

  • "wysota" is male

Posts: 4,276

Location: Warsaw, POLAND

  • Send private message

6

Tuesday, November 2nd 2004, 5:42pm

Quoted

Originally posted by serg
Why do you think what it's possible?


Why shouldn't it be? :) Different scheduling strategies can be implemented into the kernel. Putting all threads of one process to one CPU is certainly an option. "What for?" - you may ask... For example to minimize context-switching. There are strategies that force putting a process to one CPU (even single threaded ones). It's all according to system's needs :) You name it - we've got it :P

Of course I don't say it has to be the case here... but it's possible. I don't know what is the difference between Qt for Linux and Qt for Solaris, but there is probability that they don't differ much. Qt for Linux uses posix threads (if I'm not mistaken) for threading. If I'm not mistaken (again) pthreads don't implement its own, user-configurable scheduling, so the threads shouldn't be limited to one CPU by this library. Furthermore, there is no such thing as 'thread' on Linux platform, as Linux implements threads as processes (threads are created using fork or it's lowlevel variant to be precise). Solaris is a real Unix and implements threads as real threads. When you do fork() on Linux, there is no difference (besides memory mappings) to threads, using threads and forking() on Solaris are two different mechanisms.

However, there is a possibility that Qt handles threads internally and schedules them by itself (which I personally doubt).

You can make an experiment. Make a program (while(1) sleep(1); is ok), run it on Solaris and check if the scheduler assigns it to the same CPU everytime.