Sign in to follow this  
BloodLust666

multi core multi threading

Recommended Posts

http://msdn.microsoft.com

If you come into the forums with a very stuck up attitude, you will find that you will not get much help.

Both Google and MSDN provide very simplified, broken down, and easy to understand and re-use demonstrations of multithreading; if you are unwilling to do the research required to learn from these very usefull resources, you probably should not be in the industry.

Share this post


Link to post
Share on other sites
Actually that's not stuck up at all; i'm merely saying I would like someone to walk me through it as opposed to having someone refer me to a website that I could have gone to on my own. It's a waste of a post and it only means you don't know how to do it either. If you do, SHOW me, don't refer me somewhere else and send me on a continued wild goose chase.

I repeat.

Share this post


Link to post
Share on other sites
So, what you are saying is you want someone to take time out of their day to show you how to do something when you are apprently willing to put zero effort in yourself to find out how to do it?

News flash; programming is about problem solving. If you aren't willing to do some leg work yourself then you might as well give up now.

Share this post


Link to post
Share on other sites
ok, now we're on post number 5 and still no solution, amazing

If we're going to get into the logic on people "taking time out of there day" then why is there a forum?!?! Apparently people have NO time at all and have none to spare to help out so why spend it on a forum??

If people have the answer and are willing to share, the great, wonderful, splendid! However, if people are just going to take their time to tell me to search a site which i ALREADY did and found no answer to, then that's just a waste of MY time to read and a waste of their time posting something I already tried.

I thought this was a forum for people to help out? Not one to talk about off topic things like who's wasting who's time asking for help or referring to google, interesting there's enough time in the day for that!

Share this post


Link to post
Share on other sites
Yes, this forum is for helpping people however there is such a thing as 'give and take'; all that is asked is that you show what you've done to solve the problem on your own.

Coming in and saying 'show me how to do this' without any evidence of having used your own brain isn't what this is all about.

Anyway, despite your attitude I'll answer the 2nd half of your question as I'm here; threads are automatically scheulded to run on a free core or CPU when it is their turn. How this is done varies from OS to OS.

MSDN holds everything else you need to know about threads on windows.

Share this post


Link to post
Share on other sites
Quote:
Original post by BloodLust666
Actually that's not stuck up at all

True. Stuck up probably isn't the right word (for your post as it is now. I assume you've edited it). It is however, extremely lazy.

Is your time more valuable than ours? Should we spend 5 minutes of our time just to save you the same amount? Why, exactly?

Quote:
i'm merely saying I would like someone to walk me through it as opposed to having someone refer me to a website that I could have gone to on my own.

We would all like a lot of things. Personally, I'd quite like someone to give me fifty thousand dollars, for example.

But if you could have looked it up on your own, why didn't you?

Quote:

It's a waste of a post and it only means you don't know how to do it either. If you do, SHOW me, don't refer me somewhere else and send me on a continued wild goose chase.

No, a waste of a post would be where you don't learn anything. The responses you've got above should have taught you much more than you'd have gotten if someone had just done your research for you. And how can it be a "continued wild goose chase"?
- "Continued" implies that you've already been searching for it, doesn't it? That doesn't seem to be the case. Nothing you've said in your posts so far indicate that you've tried to find the information yourself.
- "Wild goose chase" implies that it'd be a waste of time, that you'd never find what you needed. I don't see why that would be the case.


If you have searched for this information, and have been unable to find what you needed, then say so. And if that is the case, I'm sure a lots of people here would be happy to provide you with links to the necessary pages on MSDN (assuming you're working on Windows)

And finally, I suppose it can't hurt to help you out a bit.
You didn't say so, but I assume you're programming in C or C++, and I assume you're working on a Windows platform.
If so, you need to type the following into [Google]:
"windows create thread"

Click on the very first result.
Read that page.
You'll note the following:
1: It describes a function called CreateThread. Hmm, I wonder what that does.
2: Under "Example code", it contains a link named "Creating Threads". That might just have some of what you need.
3: It explains that under some circumstances, you should call a different function instead.

Now, once you've done the above, feel free to ask here if there's something you need clarified. There are plenty of people here who are happy to help beginners out. But it requires the beginner in question to do *some* of the work themselves.

Share this post


Link to post
Share on other sites
Well first of all.. There are hundreds of different ways to accomplish what you are asking for. Depending on platforms, programming languages, libraries and methods within those libraries.

And secondly... The forums are not here to spoonfeed people. If you need help understanding something that's fine, but outright REFUSING to attempt to solve the problem yourself is just beyond ignorant.

Share this post


Link to post
Share on other sites
You know, this forum has really gone down hill. People used to be able to come here, ask questions without being harassed with how they ask it, what information they put in it dealing with what they've found before. It was: ask a question, attempt to get a solution, try it out, repeat (something like that). now it's: refer to google (which yes i HAVE already done!), talk about the theory of asking questions and break down my post into how you interpret it. it's bullshit.

I'm not going to even respond to any more crap about the way I asked my question, I'm here for a solution, not a debate about posting in forums!

Thank you phantom for being the only one here to actually give some positive information to get to a solution!

I understand how multi-threading works and concurrency and whatnot. My question is pertaining specifically to _beginthread(). I'm not sure if that, if available, puts the newly created thread on a different core OR if there's another function to do that. (EVERYONE WATCH MY NEXT SENTANCE!!) I've googled around for it and tried MSDN, but no where does it talk about specific code to put in it to that, it just goes into theory of multi-core processing.

Share this post


Link to post
Share on other sites
Since you didn't say anything, I'm assuming this is for Windows and in C (or C++).

CreateThread and SetThreadAffinityMask.

CreateThread has an example on how to use it. SetThreadAffinityMask is fairly obvious.

Parallel processing is not such a simple topic that it can be well covered in a single post. Expecting people to recreate what has been covered in necessary detail in books is fairly unreasonable. If you cannot figure out how to use it on your own from the references and examples provided in the API, it's highly recommended you read a book on it. It's not something you're likely to get a good understanding of from an online tutorial (or forum post). Actually, even if you do figure out how to use it from the references, I'd still recommend reading a book. There are concerns to parallel code that are not immediately obvious just from looking at an API reference.

Wikipedia has a general introduction to the variety of issues that will come about in parallel coding. You should be able to get a reasonable idea of what to research from there based on your project's goals.

Share this post


Link to post
Share on other sites
The thing about CreateThread() is it's windows specific. I'm actually trying to keep most of my functions as non-proprietary as possible which is why I opt to use _beginthread(). Is there a similar function to that that also checked for multiple cores? or even some sort of platform specific function that returns the number of cores? or even some assembly? something to get the number of cores, then once I have that, some function that not only creates a thread, but also puts it on whatever core I want. Is that possible?

Share this post


Link to post
Share on other sites
Quote:
Original post by BloodLust666
You know, this forum has really gone down hill. People used to be able to come here, ask questions without being harassed with how they ask it, what information they put in it dealing with what they've found before. It was: ask a question, attempt to get a solution, try it out, repeat (something like that). now it's: refer to google (which yes i HAVE already done!), talk about the theory of asking questions and break down my post into how you interpret it. it's bullshit.


Most people have no problem putting a good amount of effort into a question that was asked with a good amount of effort. You don't even specify what OS or language you are using. If someone asked posted, "how i make mmorpgz liek wow?", would you want to sit there and write for a detailed, helpful response?

Quote:
Original post by BloodLust666
It's a waste of a post and it only means you don't know how to do it either.


No, it means we don't want to take the time and effort to show you when you don't seem to put any time or effort in yourself.

Quote:
Original post by BloodLust666
i'm merely saying I would like someone to walk me through it as opposed to having someone refer me to a website that I could have gone to on my own.


This isn't algebra where you plug variable X into an equation to solve for Y. You do have to come up with your own stuff. The theory is all you should need once you pass the level of a very beginner programmer. If you can't write code from theory, it shows that you don't understand how to code very well. If you were stuck on something like, "I know to use API X but I don't understand what to pass with the parameters", thats a fine.

Quote:
Original post by BloodLust666
The thing about CreateThread() is it's windows specific.


And this is one of those details you originally lacked to mention. Everyone just assumed you wanted Windows since you never said you wanted a more cross-platform approach.

So you want it to support Windows and...? Also, what language? If you don't answer those, we won't be able to be sure to provide you with the best answer. For all I know you're using C# under Mono, in which case the answer would be nowhere near the same as if you were using C++.

Quote:
Original post by BloodLust666
but also puts it on whatever core I want.


Usually you just create the thread then set the affinity to whatever core. What are you trying to accomplish with setting the affinity? Must the thread be used by only a specific core at all times? Do you want to tell the OS to just prefer using a specific core for the thread when possible? Or...? When you create a new thread, all cores will access it, so you don't need to manually place it on a core, which it sounds like you are thinking you need to do.

Share this post


Link to post
Share on other sites
Quote:
Original post by BloodLust666
How would I go about creating another thread, and, if available, running that thread on another core?


Just an FYI: If you had asked this exact same question with the exact same wording, minus your signature, I can almost guarantee you would have had a better response. Your signature only serves to create hostility by stating that you merely want your hand held for you rather than having you attempt to solve it on your own.

Now, in response to your actual question. It sounds to me like you don't even understand how to perform basic threading in C/C++ let alone need to worry about which core your thread is running on. A multi-core processor will automatically use multiple cores for your program, assuming you have spawned threads. You only need to set the core affinity in specific situations.

Now, to answer your first question - how to go about creating a thread. C++ does not have native threading support. Period. So asking for some highly portable cross platform solution is impossible. At best, you can use a library which is ported to multiple platforms (like POSIX pthreads or Boost::thread - which both have a Linux and Windows library).

First you need to pick a library which can only be accomplished by looking at the information for each, then deciding which one will better fit your needs. After that, you should do a google search for tutorials on said library. IF you still have questions, then you can ask again - but without your signature.

[Edited by - CaspianB on May 10, 2008 10:05:53 PM]

Share this post


Link to post
Share on other sites
honestly, about 2/3 of what you wrote was a waste and i didn't even read.

It's all C++ and I'm looking to platform to Linux and Mac OS. I do have some functions that have a wrapped define around it for specific platform calls. which is just fine too.

Because I'm not too familiar with the CreateThread and SetThreadAffinityMask fuctions, I'm not sure exactly what the Affinity Mask is and which core has which mask. I just looked it up and I understand how it's a mask which designates which processor the thread is allowed to run on, but how do I get what the mask is for each core?

I'm trying to do this so I can get the parallel processing I'm trying to accomplish. In my engine, I want to split up the processing of rendering graphics on one core (the main one the program starts on) then having all the updating, collision, AI, etc running on the other core.

Share this post


Link to post
Share on other sites
Quote:
Original post by BloodLust666
honestly, about 2/3 of what you wrote was a waste and i didn't even read.


And thats pretty unfortunate. But your choice. Ironically, with that attitude, you will probably want to make even better friends with Google since you are just cutting down the amount of people who will want to help you with the kind of remarks you have been making.

Quote:
Original post by BloodLust666
I'm trying to do this so I can get the parallel processing I'm trying to accomplish. In my engine, I want to split up the processing of rendering graphics on one core (the main one the program starts on) then having all the updating, collision, AI, etc running on the other core.


Let the OS handle which core works on which thread when its time for that thread to be handled. Don't do it manually. Lets say you have 2 cores, the drawing on one and everything else on the other. Well its likely that your drawing will finish first since its not too CPU intensive and will just be sitting around waiting for the GPU to finish. The other core will be juggling tons of tasks, struggling to even finish before the GPU, wondering why it has to do all the work while the other kicks back in a lawn chair, drinking lemonaid and soaking up the rays from your fan lights.

Share this post


Link to post
Share on other sites
Yea, but I'm going to investigate and try something out. The only time I'm going to attempt to "care" what both threads are doing is when a delete of an entity is happening (so the program doesn't crash when it's both attempting to delete it while rendering). I'm going to try to see how things looks when the rendering core renders things as fast as it can and the updating core updates things as fast as it can. and whenever the rendering is about to render an entity, it just grabs the current values from it no matter if it's been updated or that in that frame.

I'm hoping to get some results back, whether it's good or bad; i want to test this out in my scenarios.

Share this post


Link to post
Share on other sites
That is most likely going to result in a lot of partially-updated scenes. If your characters are based off of a skeletal model and your update thread is moving the skeleton forward, then the model is drawn half way through the position update, you're going to have some of your model at the old location and some at the new. It'll look like you just turned the poor guy into a Stretch Armstrong for that frame.

Also, whats the point of rendering more than you can update? Or updating more than you can render? As long as your update is hitting the required rate for the simulation to properly update, rendering more than once per update is just going to copy the same information to the screen while updating more than once per render is just going to result in visually skipping a frame completely. Anything in-between will just turn into what I mentioned above.

Along with this, it still doesn't explain why you would want to set the processor affinity. If you want to have an update and draw thread run as much and as fast as they can, go right ahead - which CPU they being used by doesn't change anything.

Share this post


Link to post
Share on other sites
Quote:
I'm going to try to see how things looks when the rendering core renders things as fast as it can and the updating core updates things as fast as it can. and whenever the rendering is about to render an entity, it just grabs the current values from it no matter if it's been updated or that in that frame.


Read up on locking, or else you're going to have some fun bugs to fix later.

Also, there is no real portable way of doing threading across different operating systems because each system is different (I'm pretty sure you can use libraries for *nix based systems like Linux and OSX, but not for Windows) - and the system will automatically choose which CPU to put your thread on (typically the first thread on the first CPU, the second thread on the second CPU, and so on - when no more CPUs are available then they start sharing) which usually works nicely.

Share this post


Link to post
Share on other sites
bah, good point... I thought I thought of all possiblities; never really though of mid-updated rendering. Hmm... well, one reasoning why I decided on trying this is games like Crysis that utilize all 4 cores. What could they possibly be doing that utilizes all 4 cores? and not have the problems that could happen in my situation?

Share this post


Link to post
Share on other sites
Rendering, physics, AI, sound, asset streaming.. There's at least five things you could put in separate threads.

Also, they will have your issues, but they will have come up with solutions. For example, when they unload assets, they may flag the asset as deinitialising, and then wait a frame or two to ensure that it is not being rendered before unloading it.

sam

Share this post


Link to post
Share on other sites
but according to what we just deciphered, the rendering and physics need to be in the same thread due to occurrences where the physics and updating of an entity could happen mid-rendering...

I suppose AI, sound, and loading assets could be on separate threads. What's everyone's thoughts about that? would that work? AI i'm a little iffy about, but I guess AI would just be changing the absolute velocities and direction of each entities, nothing really too drastic that could mess things up if it happened during mid-render.

Share this post


Link to post
Share on other sites
A better question is how can they utilize all 4 cores yet have absolutely horrendous performance for what it is doing? ;)

Keep in mind that although it uses 4 cores, it doesn't use them all fully. Also, keep in mind that synchronization between threads, along with having multiple threads, is not free.

How they did it I can not say - I haven't looked into it (I hated the game and found the engine way too over-hyped). It looks like they broke it up mostly into rendering, particle system, physics, audio, AI, and input/output. AI, I/O and audio could probably run at full speed without a concern (maybe not the AI... depending on what it does), but most of it still has to complete before the rendering can go. Also, a few of those things aren't very intensive at all, which seems to be a bit confirmed when I see some benchmarks showing the game mostly using just 2 cores of a quad-core CPU.

When thinking of what you can break into individual threads, just think of what you can isolate from the rest of the code. The more parts of the thread that are dependent on other threads, the more synchronization will be required which will result in more overhead. Combined with thread management on the OS, this can result in multiple threads being slower than just one, especially if theres more threads than CPU cores. You'll want to research a lot of multithreading theory and concepts before attempting to make a game that works across many threads - plenty of people have enough of a challenge just efficiently using 2 main threads for their games.

Share this post


Link to post
Share on other sites
hmm... so do you think it's just not worth doing multi threading? I was thinking just not caring about the syncing, that way there's no possibility of overhead. which is why is said about the rendering and updating. there's got to be somethings that don't necessarily need to care about what the other one is doing. I've done multi threading before and I get about the concurrency and setting locks and all that. I'm trying to find a way to eliminate all that safely.

Share this post


Link to post
Share on other sites
Sorry just noticed your previous post. Rendering and updating doesn't have to be in the same thread, you just have to ensure that you don't draw partially updated entities. You could, for example, make a snapshot of the previous scene, render that, then start updating the new scene. This would create a lot of overhead, but they can then run completely independent of one another.

My threading knowledge pretty much ends here. The only threading I really do is with networking, which is nowhere near as complex as threading a whole game.

If its for education, feel free to try out as much threading as you want. If its to actually gain something from, such as better performance, I wouldn't recommend even bothering.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this