What's new

How do I code for dual core?

xtra krazzy

Dolphin Developer
How do you actually code for dual core? My coding usually tends to be not-synchronized and sometimes hop to being run on the same processor...

The dual core/x64 dolphin development(screenshot) topic has been there(and locked) for a while so I guessed that this thing is indeed possible without screwing up.

Thanks in advance
 

Doomulation

?????????????????????????
Interprocess communication, alright. How do I set the processor affinity for each of the processes inside the code?

You realize that Windows automatically splits thread onto different processors, yes? If you have two threads, they'll run on both processors.
 
OP
X

xtra krazzy

Dolphin Developer
You realize that Windows automatically splits thread onto different processors, yes? If you have two threads, they'll run on both processors.

Alas, it seldom works... I found that most of the stress is on the second processor, and the profiler didn't have any stress on the things which were meant to run on one processor.

If this is correct, then my processor is a gimp(one works faster and the other slower) (I ran the same pressure tests on both processors, together).
EDIT: It might be because of the stress which was on the processor beforehand.

So let me get this straight: In Windows, the first two threads which fork from the main thread go to different cores by default, and the sub-threads of the two threads each go to the same core?

If so, how, in Windows terms of functions, can I synchronize two threads in other ways than waiting for a thread to finish? (semaphores in Windows?)
 
Last edited:

Doomulation

?????????????????????????
Alas, it seldom works... I found that most of the stress is on the second processor, and the profiler didn't have any stress on the things which were meant to run on one processor.

If this is correct, then my processor is a gimp(one works faster and the other slower) (I ran the same pressure tests on both processors, together).
EDIT: It might be because of the stress which was on the processor beforehand.
Windows will try to balance threads on each processor. But it will also try to split threads from the same application on different processors (so that if an app has two threads, both threads won't run on the same processor). But remember: though Windows may split the threads, it won't split the processor load. In other words, you can run demanding code in one thread and easy code in one. Of course the processor which runs the demanding thread will get more load - this is normal. Windows will only split the threads - it's up to you to split the load in the threads.

So let me get this straight: In Windows, the first two threads which fork from the main thread go to different cores by default, and the sub-threads of the two threads each go to the same core?
I'm not 100% sure, but I don't think Windows cares what thread spawned what - they'll still be split onto different processors as Windows sees fit.

If so, how, in Windows terms of functions, can I synchronize two threads in other ways than waiting for a thread to finish? (semaphores in Windows?)

There are many synchronization functions, but one that you might benefit on is the ability to create and set events. Use CreateEvent to create an event, then use SetEvent and ResetEvent to set or reset the event respectively. You can then use WaitForSingleObject (or WaitForMultipleObjects) to wait to when the event is set. One thread can then wait until another thread sets this event signaling that it is done with whatever the first thread should wait for. Do you get me? There's plenty information about this on the web if you need to know more.
 
OP
X

xtra krazzy

Dolphin Developer
There are many synchronization functions, but one that you might benefit on is the ability to create and set events. Use CreateEvent to create an event, then use SetEvent and ResetEvent to set or reset the event respectively. You can then use WaitForSingleObject (or WaitForMultipleObjects) to wait to when the event is set. One thread can then wait until another thread sets this event signaling that it is done with whatever the first thread should wait for. Do you get me? There's plenty information about this on the web if you need to know more.

Oh, that's great, I didn't know that WaitForSingleObject also waits for event handles as well as thread handles to finish, for use to "wake" the waiting thread. Thanks.


So there's no actual guarantee that the code will run with a certain affinity, but the probability of having the program's threads run simultaneously is high enough. I guess that sums it up. Thank you very much.
 

Slougi

New member
So there's no actual guarantee that the code will run with a certain affinity, but the probability of having the program's threads run simultaneously is high enough. I guess that sums it up. Thank you very much.

The scheduler will try to balance processes/threads between the available cpus, yes. I am not certain whether playing with the processor affinity is really needed except in special cases.
 

ector

Emulator Developer
When you only have 2 heavy threads in your program, at least it doesn't hurt to set affinity on them, and it is useful because then you can see exactly how much cpu power each one uses just by looking at the cpu monitor in task manager :)

Of course, if you have a quad core and want to run two programs simultaneously that both hog cpu #1 and cpu #2, then you are in trouble :p

So I'd recommend to only set affinities during development.
 
Last edited:

Top