Is The Game Coded For Multiple Cores?

rmbrown09rmbrown09 Join Date: 2012-10-17 Member: 162592Members
edited February 2013 in NS2 General Discussion
The answer here is yes, but I am wondering how many cores is the game coded to take advantage of? Many of us are rocking hyperthreaded i7's that run a total of 8 logical cores. A potential improvement in performance could be had chugging all that Lua by using every single core. Some people are probably even using 2011 series 6 core, 12 hyperthreaded CPU's. Haswell is around the corner and we will only see the core count go up.

Just want to make sure that at minimum this game is taking advantage of all 4 cores, hopefully 8 if they are available. I don't know if the answer is something that can be derived from the game code or if someone from UWE will respond. Thanks either way.
«1

Comments

  • XariusXarius Join Date: 2003-12-21 Member: 24630Members, Reinforced - Supporter
    It barely uses multiple cores really. We've had this discussion in the past and apparently it's really not that easy to just spread the performance load over multiple cores.
  • SixtyWattManSixtyWattMan Join Date: 2004-09-05 Member: 31404Members
    lol Man. Why didn't they just do this? You've single handedly solved all their problems.
  • RamblemoeRamblemoe Join Date: 2010-12-21 Member: 75812Members, Forum Moderators, NS2 Playtester, Subnautica PT Lead, Retired Community Developer
    I get the same framerates running the game on 2 cores as I do on 4. CPU being the bottle neck in both cases. They probably have more than two threads running, but the problem with multithreaded stuff is dividing the work equally (read: at all). Afaik multithreading is the biggest challenge in todays programming.

    That said most games these days only provide a fps boost within the margin of error when going from 2 to 4 cores, so I think it's kinda harsh to expect a indie dev team to overcome this challenge.
  • |strofix||strofix| Join Date: 2012-11-01 Member: 165453Members
    You can't simply use more cores. It doesn't work that way. The thing you are distributing between the cores needs to be parallelizable.

    Games are in a select few cases, like perhaps AI and physics, but other than that it is all very sequential, and very interdependant, which means you can't get respectable speed-ups from multiple cores.

    No games use 8 cores. If it was that easy, games would be running exclusively on GPUs.
  • lwflwf Join Date: 2006-11-03 Member: 58311Members, Constellation
    Yes. It isn't heavily multithreaded but few games are. It's very difficult to make a game like NS2 effectively take advantage of many CPUs.
  • TalesinTalesin Our own little well of hate Join Date: 2002-11-08 Member: 7710NS1 Playtester, Forum Moderators
    Yep. Using multiple cores isn't as easy as setting the UseMultipleCores=Y flag on the compiler. It's frankly a pain in the backside; some of the easiest stuff to parcel out is unit AI. Of which NS has... almost none. The rest you have to make sure the different processing threads are synchronized, with not too much work being handed to one, otherwise it just becomes a big waiting game with every other thread waiting for one to finish, and rotating through. Leading to jerky start-stop performance, which would get a lot more people angry.
    Almost certain that (at the moment) NS2 is using one core for the vast majority of work (the primary processing thread), and offloading what it can to a secondary. Which isn't much. Task scheduling takes an immense amount of work, testing, and planning to get right.

    Saying 'use multiple cores to improve performance' is about as helpful as saying 'press the jump button!' when watching someone play a video game. They know. It's just not that simple.
  • FrothybeverageFrothybeverage Join Date: 2003-02-15 Member: 13593Members
    edited February 2013
    Hitting the jump button is easy.

    What you're describing is more akin to doing a brutality in MK3.
  • rmbrown09rmbrown09 Join Date: 2012-10-17 Member: 162592Members
    Ok new idea:

    Take all the slow Lua code, and compile it to something faster like C. There are Lua to C converters as far as I can tell all over. I understood one major reason they use Lua is because it is more easily moddable. But you could construct or implement a Lua to C converter in the game so that you can edit in Lua and then it is transformed to C with the converter when you're done.

    Something like this, just build it into the game: http://lua-users.org/lists/lua-l/2008-08/msg00004.html

    My flame shield is ready I stand not afraid.
  • |strofix||strofix| Join Date: 2012-11-01 Member: 165453Members
    rmbrown09 wrote: »
    Ok new idea:

    Take all the slow Lua code, and compile it to something faster like C. There are Lua to C converters as far as I can tell all over. I understood one major reason they use Lua is because it is more easily moddable. But you could construct or implement a Lua to C converter in the game so that you can edit in Lua and then it is transformed to C with the converter when you're done.

    Something like this, just build it into the game: http://lua-users.org/lists/lua-l/2008-08/msg00004.html

    My flame shield is ready I stand not afraid.

    Does that say 25-75% performance of LUA?
  • TalesinTalesin Our own little well of hate Join Date: 2002-11-08 Member: 7710NS1 Playtester, Forum Moderators
    edited February 2013
    |strofix| wrote: »
    rmbrown09 wrote: »
    Ok new idea:

    Take all the slow Lua code, and compile it to something faster like C. There are Lua to C converters as far as I can tell all over. I understood one major reason they use Lua is because it is more easily moddable. But you could construct or implement a Lua to C converter in the game so that you can edit in Lua and then it is transformed to C with the converter when you're done.

    Something like this, just build it into the game: http://lua-users.org/lists/lua-l/2008-08/msg00004.html

    My flame shield is ready I stand not afraid.

    Does that say 25-75% performance of LUA?

    Yep. This would just make performance worse.
    The engine is already coded as a native compiled binary, and is essentially (sort of) a realtime version of the software rmbrown09 had linked, but with better performance.
    It also doesn't address the multicore question which was the original intent of this thread, and would not farm it out to multiple cores.
  • rmbrown09rmbrown09 Join Date: 2012-10-17 Member: 162592Members
    |strofix| wrote: »
    rmbrown09 wrote: »
    Ok new idea:

    Take all the slow Lua code, and compile it to something faster like C. There are Lua to C converters as far as I can tell all over. I understood one major reason they use Lua is because it is more easily moddable. But you could construct or implement a Lua to C converter in the game so that you can edit in Lua and then it is transformed to C with the converter when you're done.

    Something like this, just build it into the game: http://lua-users.org/lists/lua-l/2008-08/msg00004.html

    My flame shield is ready I stand not afraid.

    Does that say 25-75% performance of LUA?

    I was just linking the first one I found to show the potential for the idea. I feel like if UWE or someone made a converter that would take Lua code and allow it to perform at C speeds things would run much better seeing as how the game logic is the biggest burden.

  • SquishpokePOOPFACESquishpokePOOPFACE -21,248 posts (ignore below) Join Date: 2012-10-31 Member: 165262Members, Reinforced - Shadow
    I know that it's already been said, but it really needs to be emphasized.

    Multithreading is awful. It's akin to programming with Zerg swarm logic.

    Here's a simple example:

    Master: "Go! Bring me three apples!"

    Threads: "Does he have less than three apples?"

    Master: "Yea"

    (All available threads go get apples at the same time and dump them back on Master's lap)

    Master: "I have 5 apples. What the hell, guys?"

    Threads: "Well there are 5 of us and you had less than 3 apples when we started. We don't care or know what the other guys are doing!"


    Master: "God dammit, now I have to micromanage you morons. Only go get apples one at a time for now on!"

    Threads: "But then how can we be fast like threads are supposed to do??!"

  • |strofix||strofix| Join Date: 2012-11-01 Member: 165453Members
    That's if every threads task is interrelated in some way. However, if the LUA code is chugging along, and it has 5 whips to do a couple of checks for, does it do them sequentially?

    For the most part, none of the whips need to know anything about the others, so their checks can be done in parallel. Is this the way the game logic currently works? From a quick google, it does seem LUA supports some form of rudimentary threading.
  • matsomatso Master of Patches Join Date: 2002-11-05 Member: 7000Members, Forum Moderators, NS2 Developer, Constellation, NS2 Playtester, Squad Five Blue, Squad Five Silver, Squad Five Gold, Reinforced - Shadow, NS2 Community Developer
    Lua supports coroutines, but not multithreading, so there is no performance benefits in a single Lua VM from multiple cores.

    The NS2 server runs on about 1.05 cores, while the client runs the logic in one thread and the renderer in another; if you get bad fps, its usually the logic thread that slows things down.
  • WhiteWeaselWhiteWeasel Join Date: 2012-11-25 Member: 173197Members
    I know not of coding.

    I genuinely want to know what is the difference and (or dis) advantages between codes like C, lua, and other computer languages?
  • YMICrazyYMICrazy Join Date: 2012-11-02 Member: 165986Members
    I know not of coding.

    I genuinely want to know what is the difference and (or dis) advantages between codes like C, lua, and other computer languages?

    Luckily Hugh wrote an article a long time ago for the rest of us plebs who do not understand jack about coding

    http://www.ns2hd.com/2012/04/performance-where-is-it-now-and-where.html

    The parts that answer your question:

    "Lua is 'high level' programming language. Imagine a spectrum. On one end of the spectrum is ones and zeros, or 'Binary' code. This is the language that your PC speaks. Every calculation it makes is done in Binary. At the other end of the scale is the English/French/German/other spoken language. This is what you use to communicate with other humans, and can most readily understand. Programming languages act as intermediaries between Binary and our spoken human languages, so that we can tell computers what we want them to do.

    A 'high level' programming language sits further towards the English/French/German end of the spectrum, while a 'low level' programming language sits further towards the Binary end of the spectrum.

    Lua sits very far towards the spoken language end of the spectrum. It is very easy to understand, and that is why I was so easily able to create my little spectator mode mod. Natural Selection 2 is entirely written in Lua, so that people can modify it very, very easily. It can be modified with a simple text editor such as Notepad, and does not need to be 'compiled' before it can be executed.(roughly, to compile a program means to prepare it to run, and takes time).

    In fact, it is perfectly accurate to call Natural Selection 2 a mod of the Spark engine. Spark is the in-house engine developed as the brain-child of Max 'm4x0r' McGuire. Spark is written in C++, a fast low level language. NS2 sits on top of Spark, written entirely in easily accessible Lua. It is possible to mess with the game without ever having to deal with the C++ engine.

    So far, this sounds really cool. Why don't all games run in languages like Lua, on top of low level language engines? Let's go back to the spectrum. The further a language is towards the Binary end, the faster it runs on your computer. The further it is towards the spoken language end, the slower it runs on your computer. This is not to say that Lua is absolutely slow, only that it is relatively slow compared to a low level language such as C++. It is relatively slow because it takes more effort to move all the way from the spoken language end of the spectrum to the Binary end of the spectrum, where the computer can understand and execute the game code.

    In summary, Lua trades some execution speed for ease of access, and therefore ease of modding. Not just ease of modding, but ease of writing.

    Right now, Lua takes up around 50% of the CPU cycles that NS2 goes through to produce ever frame. This is, on its own, almost entirely responsible for the limited framerates that people experience when playing in the NS2 Beta. Rendering in Spark is actually very fast. This is why turning down graphics options, or playing on a more powerful GPU, yields negligible performance benefits."

    Of course it's not in beta anymore so things have improved since then.
  • AmbAmb Join Date: 2012-11-09 Member: 168647Members, Reinforced - Supporter
    my CPU metering software shows that the 4 cores in my i5 are only being utilized at about 50-60% at peak performance
  • lwflwf Join Date: 2006-11-03 Member: 58311Members, Constellation
    edited February 2013
    I know not of coding.

    I genuinely want to know what is the difference and (or dis) advantages between codes like C, lua, and other computer languages?

    Trading programmer time for CPU time and memory usage.

    A lower level language like C can but won't necessarily run more efficiently, but is slower and may be more cumbersome at all times but runtime. A higher level language like Lua is slower at runtime and may use more memory but is faster to write, maintain, debug, tweak, test and doesn't need be compiled.

    Trying to "convert" code using automated tools is likely useless. I have not even bothered to check, but there's no way the Lua VM and libs aren't already written in C or maybe C++, so why some converter to the very same language it already runs under, that hasn't received as much testing or programmer time would do better I don't know. The code you'd end up with would also be a unreadable mess and using a debugger would be hell.
  • WhiteWeaselWhiteWeasel Join Date: 2012-11-25 Member: 173197Members
    edited February 2013
    So C is better performance but harder modability vs lua that is more easily used/changed, but with lower performance?

    Rephrase: C runs better but much harder to work with, while lua slower but more user-friendly. This is why 7 dudes in Australia can churn out builds as fast as some big name companies.
  • rmbrown09rmbrown09 Join Date: 2012-10-17 Member: 162592Members
    edited February 2013
    I know not of coding.

    I genuinely want to know what is the difference and (or dis) advantages between codes like C, lua, and other computer languages?

    I'm currently a computer science major (junior) so I have experience using a decent amount of languages on the scale so far.
    But basically the farther from 1's and 0's you get the slower it goes. Actually had a 3 hour lab yesterday writing a program in assembly. (the lowest level language that literally translates what you write into machine code)
    Jesus that language is awful. Makes you really appreciate things in Python like: x = "hi"

    Learning to program in assembly actually has been the most helpful though in understanding how a computer really truly works at the basic level. For example:

    in normal programming languages you can do something like
    x = 5
    y = 10
    z = x+y

    So that z ends up being 15 without ever actually explicitly saying yo z, you're 15.

    But even simple things in assembly like multiplying by 8 are stupidly hard. At least with what we've learned.
    You take the number, and shift its binary value in the register.

    In binary numbers are 0 and 1.

    0001 is the number 1
    0101 is the number 5
    0010 is the number 2

    In assembly you store the value in something called an accumulator and then perform operations. So to like do the operation 2*8

    you would take the binary value of 2, 0010 and shift the most significant bit over left twice to 8: 1000
    In assembly the command is LSRA.
    So you do that twice and then restore the value in A
    looks like:

    LSRA
    LSRA
    STAA

    Anyways the whole point here is that low level languages can be freaking hard to do anything in, but are much more efficient if you know what you're doing.
    Lua is really far away from binary shifting in the CPU register to do simple steps, but as a result is slower as it needs to get translated to assembly language and then machine code before it is run.

    /rant




  • SquishpokePOOPFACESquishpokePOOPFACE -21,248 posts (ignore below) Join Date: 2012-10-31 Member: 165262Members, Reinforced - Shadow
  • bp2008bp2008 Join Date: 2012-11-28 Member: 173581Members, Reinforced - Shadow, WC 2013 - Gold
    So C is better performance but harder modability vs lua that is more easily used/changed, but with lower performance?

    Yes. Possibly the most important point though is that C code is a whole lot harder and more time consuming to write than Lua.
  • WhiteWeaselWhiteWeasel Join Date: 2012-11-25 Member: 173197Members
    bp2008 wrote: »
    So C is better performance but harder modability vs lua that is more easily used/changed, but with lower performance?

    Yes. Possibly the most important point though is that C code is a whole lot harder and more time consuming to write than Lua.

    Ay, I phrased that wrong, I meant as in modifying or changing the code, not a game mod #-o
  • |strofix||strofix| Join Date: 2012-11-01 Member: 165453Members
    In addition, high level languages also tend to automate a lot of functionality.

    (I assume) Something like LUA would have no concept of memory management. Its interpreter would simply allocate space when an object came into existence, and deallocate it once that object fell out of scope, or was no longer usable. The process of monitoring when and how to do this, while incredibly useful, isn't free, and doesn't do it in an optimal fashion.
    C++ also allows direct access to memory addresses, while essentially all languages of a higher level only expose abstracted references.

    Because interpreters and compilers can't handle ambiguity, the way higher level languages handle things like this, while usually adequate, is not optimal.
  • _Necro__Necro_ Join Date: 2011-02-15 Member: 81895Members, Reinforced - Shadow
    edited February 2013
    Some good posts here. A little bit off topic, but maybe it is interesting for some of you.

    To get you some examples of HOW powerful Assembler (=most low level programming language) can be: There are festivals where some really talented programmers make Demos (=real time rendered video with nice graphics and sound). http://en.wikipedia.org/wiki/Demoscene

    Now, the most astounding thing isn't how nice looking the things are, that they can develop. But that there are categories for down to 1Kbyte. We are talking here about videos with music that are much much smaller than a MP3! This is some real algorithmic magic!

    If you want to see for yourself: This is the 3rd place of the Assembly 2010 in the category 64Kbyte
    http://www.scene.org/file.php?file=/demos/groups/acceptcorp/ars_nova.zip&fileinfo
    (You need DirectX installed. It will render it live on your PC.)
    Warning: Some Virus-Scanners have problems with exe-files that use direct memory access in assembler. Some may even delete the files instantly when unpacking.

    If you don't want or can't try out the real files, here is a youtube video of this thing: http://archive.assembly.org/2010/64k-intro/ars-nova-by-phantom-lord-accept-corp
    But you won't really get the magic when you are not rendering it on your own pc. Seeing this tiny 64kbyte file and what it brings on your screen is really astounding.

    Also, this is the 2008 winner at the BreakPoint in the free category (I love that song):

    And here is the archive (also with youtube videos) if you are now interested: http://archive.assembly.org/2010/64k-intro
  • FrothybeverageFrothybeverage Join Date: 2003-02-15 Member: 13593Members
    Thank you for sharing that.
  • MilaniumMilanium Join Date: 2012-12-27 Member: 176388Members
    matso wrote: »
    Lua supports coroutines, but not multithreading, so there is no performance benefits in a single Lua VM from multiple cores.

    The NS2 server runs on about 1.05 cores, while the client runs the logic in one thread and the renderer in another; if you get bad fps, its usually the logic thread that slows things down.

    Is there a spark mod which just serves as a tech demo where you can see how much FPS are possible on your system with minimal game logic?
  • _Necro__Necro_ Join Date: 2011-02-15 Member: 81895Members, Reinforced - Shadow
    edited February 2013
    Milanium wrote: »
    matso wrote: »
    Lua supports coroutines, but not multithreading, so there is no performance benefits in a single Lua VM from multiple cores.

    The NS2 server runs on about 1.05 cores, while the client runs the logic in one thread and the renderer in another; if you get bad fps, its usually the logic thread that slows things down.

    Is there a spark mod which just serves as a tech demo where you can see how much FPS are possible on your system with minimal game logic?

    Yes, its called the ready room. ;)
    Most game logic and movement code isn't calculated while you are in it. Thats why you get higher fps before you join a team.
  • ObraxisObraxis Subnautica Animator & Generalist, NS2 Person Join Date: 2004-07-24 Member: 30071Super Administrators, Forum Admins, NS1 Playtester, Forum Moderators, NS2 Developer, Constellation, NS2 Playtester, Squad Five Silver, WC 2013 - Supporter, Subnautica Developer, Pistachionauts
    The good thing with NS2, is that performance will pretty much only increase as time goes on through innovative techniques and optimizing the actual Lua game code. This is pretty new territory for games, writing an entire game in Lua. Some games have done partial things but very few have gone Lua for their whole game code. This is proving very beneficial for modders and in the long run.
  • Ghosthree3Ghosthree3 Join Date: 2010-02-13 Member: 70557Members, Reinforced - Supporter
    Wait, the whole game is Lua? I thought most of the core stuff was C++ and just variable shit like resource collection was Lua.
Sign In or Register to comment.