Click to See Complete Forum and Search --> : Vey General Question about Threading and its performance
shahin
February 13th, 2006, 05:15 PM
Hello All
I have a very general question about threading and the performance gains by threading which I think will be pretty easy for you to answer.
Question 1
When a program runs in window, how much CPU power does it consume??
My understanding is that it tries to get as much CPU power which is available in CPU to do its calculations and therefore it depends on how much the calculation load of the program is. So it can consume either 50% or 70% or even 100%.
Am I right??
Question 2
If I have a function that when it runs uses 100% of CPU power, then what will happen in windows if I call another thread with the same function ( of course, different arguments) ?? Will window treat them as sequential tasks??!! And therefore there will be no speed gain by using a thread instead of waiting and calling the function again under the same thread.
Question 3
When using MMX instruction in window, I only have 7 registers available to me. If I use a thread and call another function which uses MMX instruction, what will happen?? How does window allocated more registers and ….. Will there be any performance speed gain??
Thank you for your help.
Siddhartha
February 13th, 2006, 05:26 PM
Question 1
When a program runs in window, how much CPU power does it consume??
My understanding is that it tries to get as much CPU power which is available in CPU to do its calculations and therefore it depends on how much the calculation load of the program is. So it can consume either 50% or 70% or even 100%.
Am I right??Correct. Additionally, factors such as File I/O, network I/O, etc - basically activities that make the processor dependent on external devices result in reduction of processor usage (the OS in turn may optionally choose to switch context to another application that runs whilst the I/O activity completes)
Question 2
If I have a function that when it runs uses 100% of CPU power, then what will happen in windows if I call another thread with the same function ( of course, different arguments) ?? Will window treat them as sequential tasks??!! And therefore there will be no speed gain by using a thread instead of waiting and calling the function again under the same thread.Windows will treat them as two threads. No different.
If your function is consuming 100% of the CPU, there may not be much of a gain in creating a new thread observed on a regular CPU - but, perhaps a dual-core CPU will get better utilized in this scenario.
Also, there exists a likelyhood that in event of many applications running and competing for CPU time, an application that has two threads may end up with a greater slice of CPU's time than it would with a single thread.
Question 3
When using MMX instruction in window, I only have 7 registers available to me. If I use a thread and call another function which uses MMX instruction, what will happen?? How does window allocated more registers and ….. Will there be any performance speed gain??
Registers are a processor-feature (presuming we are talking of the same term - registers). So, the number of registers in question will remain the same. Of course, a dual core processor is another ball game. Every processor should have it's set of registers.
Anyways, as a programmer of multithreaded applications, registers are not what you should be worrying about.
shahin
February 13th, 2006, 05:32 PM
Thank you for your quick respons.
My question about registers was bc of MMX instructions. I was not sure how a processor will use the MMX registers when there are threads involved. Since you said the number of registers stay the same , then my guess would be the processor will perform those thread sequentialy again.
follow up question: Is there a way in window to increase a priority of a application so the CPU gives all of its power to the specific process?
Siddhartha
February 13th, 2006, 05:43 PM
FYI: I added some more details to my previous post.
My question about registers was bc of MMX instructions. I was not sure how a processor will use the MMX registers when there are threads involved. Processor really doesn't know or care about threads. The OS does.
The only thing the processor does is run a sequence of instructions, and effect a context switch where it can store the state of the system (it's registers, etc) and continue from another location. It is Windows that uses the context switch to bring another thread into focus. So, what looks like a simultaneous multithreaded application to you is actually a processor that is running one-thread-at-a-time and an OS that is switching threads at a pace you cannot percieve.
So, the registers remain the same in number (these are physical entities that cannot be multiplied at the whim and fancy of the programmer).
Since you said the number of registers stay the same , then my guess would be the processor will perform those thread sequentialy again.I hope the above explanation answers your question.
follow up question: Is there a way in window to increase a priority of a application so the CPU gives all of its power to the specific process?Yes, you can use Win APIs -
SetPriorityClass (http://msdn.microsoft.com/library/en-us/dllproc/base/setpriorityclass.asp) - To change the category in which the process is given the Processor's Time slice.
SetThreadPriority (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/setthreadpriority.asp) - To change the thread's relative priority inside that class.
Note that tempting as it might be - a process priority class of REALTIME_PRIORITY_CLASS and a thread priority of THREAD_PRIORITY_HIGHEST will actually leave windows (the OS itself) with little time to do it's own system-maintenance activities and probably lead to a system crash. So, increase priority class to not beyond HIGH_PRIORITY_CLASS.
And... You don't need to do any of this unless there is a reason to complain about.
shahin
February 13th, 2006, 06:06 PM
Thank you once again :)
The reason I was asking this is that I am writing ( well, wrote) and GPS Software reciver which performs in real time. The challenge with this pice of software was the high computational burden of the program.
a GPS signal needed to be processed for multiple Satellites and it was so much that normal C operator were not able to process all the the data in a high rate. Therefore , program would have fall behind and would loss the signal.
So I used MMX and SSE2 insturctions for each Satellites to process the data. So this helped me to increase my speed to the level I needed. However, I still process the Satellite on sequential bases, ( sat 1, sat 2 and ....).
So I thoguht, maybe I can gain a better speed by processing all the satellite as different threads ( in paralelle) :)
However, I notieced that when my program is runing ( in sequential mode) , I am almost using 100% of CPU power in windows, so I was not sure if paralelle processing of different Satellite will help.
Anyway, I think you answered many of my question ( I need to think about some more :) ).
Thank you
Siddhartha
February 14th, 2006, 03:53 AM
You are welcome... :)
The best way to verify scenarions is to test them... Besides, multithreading may have it's distinct advantages for you when run on a dual-core processor.
So, I hope you haven't ruled this option out.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.