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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
07-19-2004, 03:59 AM
|
#1
|
Member
Registered: Jul 2003
Location: new delhi,india
Distribution: slackware 9.0
Posts: 54
Rep:
|
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
|
|
|
07-19-2004, 10:11 AM
|
#2
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
Could you post the code if it's not too big?
|
|
|
07-19-2004, 10:26 AM
|
#3
|
Member
Registered: Jul 2003
Location: new delhi,india
Distribution: slackware 9.0
Posts: 54
Original Poster
Rep:
|
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);
}
|
|
|
07-20-2004, 01:06 AM
|
#4
|
Member
Registered: Sep 2003
Location: Pune, India
Distribution: Red Hat
Posts: 106
Rep:
|
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.
|
|
|
07-20-2004, 06:15 AM
|
#5
|
Member
Registered: Jul 2003
Location: new delhi,india
Distribution: slackware 9.0
Posts: 54
Original Poster
Rep:
|
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
|
|
|
07-20-2004, 02:02 PM
|
#6
|
Member
Registered: Jul 2003
Location: new delhi,india
Distribution: slackware 9.0
Posts: 54
Original Poster
Rep:
|
hey can anybody tell me how to use cpu affinity thing for pthreads on gcc linux
|
|
|
08-29-2004, 04:04 PM
|
#7
|
Member
Registered: Apr 2002
Location: Montreal, Quebec
Distribution: Debian, Gentoo, RedHat
Posts: 142
Rep:
|
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
|
|
|
All times are GMT -5. The time now is 02:22 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|