Go Back   openSUSE Forums > Archives > SF Archives > ARCHIVES - Programming & Scripting
Forums FAQ Members List Search Today's Posts Mark Forums Read


ARCHIVES - Programming & Scripting A place to discuss website design, programming, shell scripts, etc

 
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 29-Feb-2008, 15:48
FuryWS
Guest
 
Posts: n/a
Unhappy

I have a problem with pthreads, and I am not sure whether I am doing something wrong. I am win32 programmer who's just learning Linux.

I did a small test program just to see how threads on Linux behave. More importantly I wanted to see how threads are scheduled across multiple CPUs depending on their number etc... However, running single thread in addition to main process thread slows down program execution by a factor of roughly 10. I have checked through KDE System Guard, runing a load graph on both CPU cores, and apparently both are active (for more than 1 thread in the process), yet 10 times slower.

The same app on Windows, but with CreateThread instead of pthread_create (the only difference, except the headers) runs fine and expectedly fast.

I am assuming I am compiling the thing wrong or something. I am also aware there are, like, three threading libs on Linux? Linux Threads, NGLT and NPLT. How can I compile for NPLT? Or is this it? So many questions.

Anyways, here is the test code. Please ignore any lack of elegance, this is just a test app. It requires two parameters, first being the number of threads to use and the second being the size of memory block to go through in the hogging function (this is to test the CPU caches). If you run it with 1 thread, that one will be the main process thread and no additional thread will be launched.

The Code:
Code:
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>

char *globmem;

int MEMSIZE;

int hogfunc() {
**int i, j;
**float z;

**for (i=0; i<100000; i++) {
****j= (int) (((float) (rand()) / RAND_MAX) * MEMSIZE);
****z= exp(globmem[j]) * sqrt(i+1+globmem[j]);
**}
}

void* print_xs (void *threadno) {
**int i, c;

**c= (int) threadno;
**c+= 49;

**for (i=0; i<200; i++) {
****hogfunc();
****fputc (c, stderr);
**}
**return NULL;
}

int main(int argc, char **argv) {
**pthread_t thread_id;
**int i, j, nthreads;
**float z, exectime;
**clock_t start, end;

**start= clock();

**if (argc!=3)
****return 1;
**
**nthreads= atoi (argv[1]);
**MEMSIZE= atoi (argv[2]);

**globmem= (char*) malloc(MEMSIZE);
**if (globmem==NULL) {
****fprintf (stderr, "Unable to malloc!");
****return 1;
**}

**for (i=1; i<nthreads; i++) {
****pthread_create (&thread_id, NULL, &print_xs, (void*) i);
**}

**print_xs((void*) 0);

**free(globmem);

**end= clock();

**exectime= (float) (end - start) / CLOCKS_PER_SEC;

**fprintf (stderr, "\nExec time: %.4f sec\n", exectime);

**return 0;

}
Compiled with:
Code:
g++ -o threads -lm -lpthread -m64 -O3 -D_REENTRANT threads.c
Run for example like this:
Code:
./threads 2 512000
When run with above params, I get exec time of 34 seconds! (On win, it is less than 1 sec). Compiling for 32-bit makes it even slower.

OpenSuSE 10.3 (with all updates)
4.2.1 gcc and g++ with all devel libs, both 32 and 64 bit
AMD Turion 64 X2
2GB RAM (1GB+ free when running the app)

I did a test forking a process instead of spawning a thread, and it works as expected, but I need threads.

Thanks for any help I can get!


EDIT:

The KDE Sys Guard shows that when this is run with 1 thread (only the main process thread), it utilizes the CPU 100% for about 3 seconds. When run with more than 1 thread, it utilizes the CPU 100% for 30+ seconds, but 60% is user load and 40% is system load on both cores. What does that mean?
  #2 (permalink)  
Old 29-Feb-2008, 19:54
andrewd18
Guest
 
Posts: n/a
Default

I can't say I know the answer, as I'm not much of a coder, but the O'Reilly Posix IV book might help you out. They also have other books on that website about pthreads that you could look into.

http://www.oreilly.com/catalog/posix4/
  #3 (permalink)  
Old 01-Mar-2008, 09:26
FuryWS
Guest
 
Posts: n/a
Default

Thanks for the reply, but that's not an issue. I am familiar with POSIX threads, I am just not familiar with compiling multithreaded apps on Linux. In fact the program up above is from one of the tutorials on POSIX threads, but they don't give you compiler options!

The books you suggest and majority of documentation on the net is TOO OLD. Various libraries exist for various kernel versions apparently all with same interface - pthreads.

Aparently NPLT is the latest, and present in the libc that comes with OpenSuse 10.3. I guess libc version 6 works with NPLT library which is latest and best performing.

I am sure that I am doing something wrong, possibly missing compiler options, because I refuse to believe that a threated operation on Windows that takes less than one second with 4 threads, takes almost a minute on Linux on the same architecture!
  #4 (permalink)  
Old 01-Mar-2008, 11:14
FuryWS
Guest
 
Posts: n/a
Default

K, I found the problem. It is the rand() function, it serializes the application, possibly because of some non-threadsafe system time code.

With that out of the window, the threads work fine. Thanks for your attention. :blink: :lol: <_<
 

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




 

Search Engine Friendly URLs by vBSEO 3.3.0 RC2