473,326 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

multithreading ; linux adn windows behave differently

hello:

I wrote a simple program which does simple math loop and I am testing
under dual core processor

systm1:
intel dual core laptop ; windows xp os
when I spawn of two threads in windows (both under visual c and cygwin
cc) the program behaves as expected.
in single thread mode the time is 2x and clearly one of hte one do of
the processor is being utilized

system2:
amd 4200x2 desktop ubuntu hardy 8.04
here actually the single thread gets slightly better performance. in
multi thereaded both the cpu's are 100% utilized. but even in single
threaded both the cpu's are alternatively being used 50/100 %!!

gurus:
any idea why the linux system (i am assuming the difference is due to
OS) is behaving differently?

here is the simple code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <pthread.h>

typedef struct {
pthread_t t;
pthread_attr_t t_atr;
void *(*func) (void *);
void *arg;
}genpth_type;

long times=200;

static void dest (void *pthv)
{
genpth_type *pth = (genpth_type *)pthv;

if(pth)
free (pth);
}

static genpth_type *new (void)
{
genpth_type *item=0;

item = calloc (1, sizeof (*item));

return item;
}

static void *testfunc (double *val)
{
long cnt=2e5;
long k = 0;
long i=0;

for (k=0; k<times; k++)
{
for (i=0; i<cnt; i++)
{
double d = (double)rand();
double x = 0;

x = pow(d, 0.55);
x = exp(x);

x *= 0.8;

x += 3.5;

x = log10(x);

*val = x;
}
}

return val;
}

int main (int argc, char **argv)
{
genpth_type *t1=0;
genpth_type *t2=0;

double t1d=1;
double t2d=10;
long i =0;
double *val=0;
long status = 0;
pthread_t self;
time_t tt1, tt2;
double dt=0;
time (&tt1);
if(argc 1)
{

times *= 2;

testfunc (&t1d);

time (&tt2);

dt = difftime (tt2, tt1);
printf ("\n dt = %lg \n", dt);
}

else
{

t1 = new ();

t1->func = testfunc;
t1->arg = &t1d;

status = pthread_create (&(t1->t), &(t1->t_atr), t1->func, t1-
>arg);


t2 = new ();

t2->func = testfunc;
t2->arg = &t2d;

status = pthread_create (&(t2->t), &(t2->t_atr), t2->func, t2-
>arg);


pthread_join (t1->t, 0);
pthread_join (t2->t, 0);

printf ("\n t1d=%lg\n", t1d);
printf ("\n t2d=%lg\n", t2d);
time (&tt2);
dt = difftime (tt2, tt1);
printf ("\n dt = %lg \n", dt);
}
exit (0);

}

Jun 27 '08 #1
9 2041
On May 16, 3:02*pm, kumar...@gmail.com wrote:
hello:

I wrote a simple program which does simple math loop *and I am testing
under dual core processor
This question is definitely off topic here, but I managed to find one
topical aspect:
library functions (especially rand()) are not reentrant.
Do not use them without a synchronising mechanism. For better
performance, implement your own or use libc's reentrant
random number generators, where the internal information about
the next random number is stored in automatic variables, which
are on a thread local stack on your platforms.
Avoid calling the same library functions from the threads and
<off>
avoid the frequent access of the same memory (page) from
several threads.
</off>

Szabolcs
Jun 27 '08 #2
ku******@gmail.com writes:
I wrote a simple program which does simple math loop and I am testing
under dual core processor
[...]
>
here is the simple code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <pthread.h>
[...]

Try asking in comp.programming.threads.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #3
On 16 May 2008 at 14:02, ku******@gmail.com wrote:
amd 4200x2 desktop ubuntu hardy 8.04
here actually the single thread gets slightly better performance. in
multi thereaded both the cpu's are 100% utilized. but even in single
threaded both the cpu's are alternatively being used 50/100 %!!
I find dt = 4 running the two threads, versus dt = 7 for one thread
(Debian, 2.6.24 kernel), so I guess there's some problem with your
setup.

Are you running an SMP kernel? Does cat /proc/cpuinfo report both cores?
What was the system load when you ran the test?

Jun 27 '08 #4
On 16 May 2008 at 14:44, Szabolcs Borsanyi wrote:
library functions (especially rand()) are not reentrant.
Do not use them without a synchronising mechanism. For better
performance, implement your own or use libc's reentrant
random number generators, where the internal information about
the next random number is stored in automatic variables, which
are on a thread local stack on your platforms.
I'm not sure what function you have in mind. The reentrant version of
rand provided by POSIX is rand_r(), which takes a pointer to a seed as
its argument - it's very unlikely that TLS will be needed or desirable.

Jun 27 '08 #5
On May 16, 6:31*pm, Antoninus Twink <nos...@nospam.invalidwrote:
I'm not sure what function you have in mind. The reentrant version of
rand provided by POSIX is rand_r(), which takes a pointer to a seed as
its argument - it's very unlikely that TLS will be needed or desirable.
You are right, rand_r() is posix, indeed. I did not mean TLS
as thread local storage for global or static variables, but the stack,
owned by the thread, that holds the object pointed to by the argument
of
rand_r(). And since rand_r takes just an unsigned*, its quality of
RNG is limited by design, this often rules out this function.

Szabolcs
Jun 27 '08 #6
On May 16, 1:09*pm, Antoninus Twink <nos...@nospam.invalidwrote:
On 16 May 2008 at 14:02, kumar...@gmail.com wrote:
amd 4200x2 desktop ubuntu hardy 8.04
here actually the single thread gets slightly better performance. in
multi thereaded both the cpu's are 100% utilized. but even in single
threaded both the cpu's are alternatively being used 50/100 %!!

I find *dt = 4 running the two threads, versus dt = 7 for one thread
(Debian, 2.6.24 kernel), so I guess there's some problem with your
setup.

Are you running an SMP kernel? Does cat /proc/cpuinfo report both cores?
What was the system load when you ran the test?
yes my kernel is 2.6.24-17 and the procinfo reports the 2 cores
Jun 27 '08 #7
ku******@gmail.com writes:
On May 16, 1:09*pm, Antoninus Twink <nos...@nospam.invalidwrote:
[...]
>
yes my kernel is 2.6.24-17 and the procinfo reports the 2 cores
Antoninus Twink is trying to disrupt this newsgroup by encouraging
off-topic posts. Please post any further questions to
comp.programming.threads or to a newsgroup that deals with your
operating system, where you will find experts who can actually answer
your questions. Thank you.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #8
On May 16, 1:09*pm, Antoninus Twink <nos...@nospam.invalidwrote:
On 16 May 2008 at 14:02, kumar...@gmail.com wrote:
amd 4200x2 desktop ubuntu hardy 8.04
here actually the single thread gets slightly better performance. in
multi thereaded both the cpu's are 100% utilized. but even in single
threaded both the cpu's are alternatively being used 50/100 %!!

I find *dt = 4 running the two threads, versus dt = 7 for one thread
(Debian, 2.6.24 kernel), so I guess there's some problem with your
setup.

Are you running an SMP kernel? Does cat /proc/cpuinfo report both cores?
What was the system load when you ran the test?
I compiled with optimization . mo i am getting dt=21(for 2 treads) vs
dt=27 ; some improvement. by the what ishte hardware you are using .
it is way way faster than mine 5x ; that is incredible
Jun 27 '08 #9
On 16 May 2008 at 23:36, ku******@gmail.com wrote:
I compiled with optimization . mo i am getting dt=21(for 2 treads) vs
dt=27 ; some improvement. by the what ishte hardware you are using .
it is way way faster than mine 5x ; that is incredible
Probably not - I reduced the number of outer loops :)

Jun 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Codemonkey | last post by:
Hi, Sorry for the cross group post, but I couldn't find a group that deals with threading in .net. Anyway, I've noticed a difference in the way my program acts on Windows 98 than it does on...
1
by: Codemonkey | last post by:
Hi, Sorry for the cross group post, but I couldn't find a group that deals with threading in .net. Anyway, I've noticed a difference in the way my program acts on Windows 98 than it does on...
6
by: JaxDawg | last post by:
OK... Crazy (possibly dumb) question here... I have a site running apache, mysql, php in a Win2K environment. Primary development is that env also. I also am setting up a linux box (old laptop),...
42
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
I'm currently writing a program and I've got in mind to keep it as portable as possible. In particular I want it to run on Linux and Windows, but I'm also keeping an open mind to any machine that...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.