K
kumarchi
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-
t2 = new ();
t2->func = testfunc;
t2->arg = &t2d;
status = pthread_create (&(t2->t), &(t2->t_atr), t2->func, t2-
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);
}
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);
}