LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-19-2004, 03:59 AM   #1
zaman
Member
 
Registered: Jul 2003
Location: new delhi,india
Distribution: slackware 9.0
Posts: 54

Rep: Reputation: 15
pthread


i have got an HT processor and executed two codes ..

1. on posix threads i ran two functions(nearly same) to compute greatest prime no
between given range (computation intensive)...simultaneously

2. ran the two functions sequentially ..

but unexpectedly my thread version is running a bit slower ....

i ran it on suse 9.0 ..

can any1 explain this or tell if some things have to be done for seeing time gain
 
Old 07-19-2004, 10:11 AM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Could you post the code if it's not too big?
 
Old 07-19-2004, 10:26 AM   #3
zaman
Member
 
Registered: Jul 2003
Location: new delhi,india
Distribution: slackware 9.0
Posts: 54

Original Poster
Rep: Reputation: 15
the code

#include <stdio.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <pthread.h>
#include <sys/types.h>
#include <math.h>
#include <time.h>


void function1(void);
void function2(void);

main() {


struct timeval before,after,diff;
pthread_t thread1 , thread2;

gettimeofday(&before, 0);
pthread_create(&thread1,NULL,(void *)function1,NULL);
pthread_create(&thread2,NULL,(void *)function2,NULL);
pthread_join(thread1,NULL);
pthread_join(thread2,NULL);

gettimeofday(&after, 0);
timersub(&after, &before, &diff);
printf("program took %d:%d seconds\n", diff.tv_sec, diff.tv_usec);
exit(0);
}

void function1(void)
{
int i=2,n,k,s,f,z1;


do{
int z=1,n=2;
k=sqrt(i);
do{
s=fmod(i,n);
/* printf("s is %d\t%d\t",s,i); */
if(s==0){
f=0;
}
else f=1;
z= f&&z;
n=n++;
}while(n<=k);

if(z==1){
z1=i;
}
i=i++;

}while(i<50000);
printf("this is greatest prime1 %d\n",z1);

}
void function2(void)
{
int i,n,k,s,f,z2;
for(i=2;i<50000;i++)
{
int z=1;
k=sqrt(i);
for(n=2;n<=k;n++)
{
s=fmod(i,n);
if(s==0){
f=0;
}
else f=1;
z= f&&z;

}
if(z==1){
z2=i;

}

}
printf("this is greatest prime2 %d\n",z2);

}
 
Old 07-20-2004, 01:06 AM   #4
Kumar
Member
 
Registered: Sep 2003
Location: Pune, India
Distribution: Red Hat
Posts: 106

Rep: Reputation: 15
I too executed the program on RH 9 and got the same results. But the results are not unexpected I guess as the overheads of a threading model are more than that of a linear execution model. Things like maintaining separate stacks, switching between the threads etc all add to this overhead. Whereas in a linear model, it's just a single thread of execution. I guess this should explain the time difference.
 
Old 07-20-2004, 06:15 AM   #5
zaman
Member
 
Registered: Jul 2003
Location: new delhi,india
Distribution: slackware 9.0
Posts: 54

Original Poster
Rep: Reputation: 15
no but i executed it on the new hyperthreaded P4 3.4 gigs processor ...and as it is
hyperthreaded the threaded version should run nearly parallel on it ....and so the
t1/t2 should be nearly 0.65 as intel claims or atleast something nearby ....it should not
exceed ...the sequential one
 
Old 07-20-2004, 02:02 PM   #6
zaman
Member
 
Registered: Jul 2003
Location: new delhi,india
Distribution: slackware 9.0
Posts: 54

Original Poster
Rep: Reputation: 15
hey can anybody tell me how to use cpu affinity thing for pthreads on gcc linux
 
Old 08-29-2004, 04:04 PM   #7
BoldKiller
Member
 
Registered: Apr 2002
Location: Montreal, Quebec
Distribution: Debian, Gentoo, RedHat
Posts: 142

Rep: Reputation: 15
Hi,

HT does not actually let you execute two thread at the same time. The OS sees two processor, but there is still only one.

What HT does is allow two thread to use the two main logic units (ALU and the other one)
It also allows for context switching (switching between process) to be done very rapidly. Only a few clock ticks.

If you run two thread that need the exact same ressources, HT wont really help. Althrought other process might use the other logic unit. That is actually one of the beauty of HT. The programmer does not need to be aware of it. In some limit, same applies for the OS (it only needs to support multiprocessor).


Hope this help you to understand HyperThreading
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Pthread JanusPaul Programming 8 02-09-2005 05:46 PM
pthread and C++ gt73 Programming 17 11-04-2003 03:01 AM
about pthread c12ayon Programming 6 10-25-2003 04:37 AM
need help about pthread c12ayon Programming 1 10-24-2003 07:47 AM
Pthread rch Programming 1 05-28-2003 02:20 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration