none
SIMD question RRS feed

  • Question

  • I've tried to search these forums and search the web and haven't gotten any luck, so here I am.

    Will .Net use SIMD instructions for your code
    If it will, what format is best used to help the compiler figure out when to use them?
    Friday, November 13, 2009 9:59 PM

Answers

  • Yes the JIT compiler uses SIMD instructions where the developers deemed appropriate.  Trying to micromanage managed code is usually highly counterproductive.  If you must retain microscopic control of your program and its code, native C++ is a better vehicle.
    Friday, November 13, 2009 10:53 PM

All replies

  • Yes the JIT compiler uses SIMD instructions where the developers deemed appropriate.  Trying to micromanage managed code is usually highly counterproductive.  If you must retain microscopic control of your program and its code, native C++ is a better vehicle.
    Friday, November 13, 2009 10:53 PM
  • Yes the JIT compiler uses SIMD instructions where the developers deemed appropriate.

    Really?  Do you have a source for that?  I thought none of the JIT variants used SSE etc.
    Saturday, November 14, 2009 3:50 PM
  • Yes the JIT compiler uses SIMD instructions where the developers deemed appropriate.

    Really?  Do you have a source for that?  I thought none of the JIT variants used SSE etc.

    nobugz.  Bing for it on the forums.
    Saturday, November 14, 2009 3:59 PM
  • Searching for "nobugz JIT SIMD" brings up two threads, in neither of which nobugz says that the JITter generates SIMD instructions.  In fact, in one of them he explicitly says that you need manual assembly coding in order to use SIMD instructions:

    http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/09be655e-4e07-470c-9abd-11d125e657b6

    "The ultimate fallback for optimized number crunching is writing this code in assembly with Masm.  That also lets you take advantage of the SIMD instruction set, it is likely that it can be helpful here when you do so many divisions."

    So... could you please search for what you've seen and post a link?
    Saturday, November 14, 2009 4:48 PM
  • Looked at the other thread again, and there nobugz says that the x86 JITter "really likes the MOVQ instruction" which is indeed an MMX instruction.

    http://social.msdn.microsoft.com/Forums/en-US/clr/thread/7c3db914-152a-4c3c-a9b1-00e94e2a4ac6/

    And in yet another thread he states that the XP (x86 workstation) JITter "uses SSE instructions" though what I'm seeing is just once again the MOVQ instruction:

    http://social.msdn.microsoft.com/Forums/en-US/clr/thread/c5655f3d-ec0c-4188-b054-164cbe1ddcb1/

    So the x86 JITter appears to use one SIMD instruction, namely MOVQ, to quickly move data around -- but that's it.  You need to write your own SIMD code to parallelize calculations.
    Saturday, November 14, 2009 4:56 PM
  • Search for "nobugz JIT SSE".  The JIT compiler uses SSE on older machines where it's beneficial.  I don't know if it's used at all on current computers with capable graphic cards.  There is some info on these forums and also on Microsoft blogs.
    Saturday, November 14, 2009 5:05 PM
  • Not seeing it, sorry.  That just brings up the same threads, plus another statement that the JITter (apparently x64 in this case) uses SSE instructions to load & store floating point variables.  That's very different from using SIMD instructions for actual calculations.  Now the OP didn't specify what exactly he had in mind but so far it certainly seems that the JITter's (any version) usage of SIMD instructions is extremely limited.

    None of this has anything to do with graphics cards, by the way -- no JITter generates codes for those!  MMX and SSE are CPU extensions.
    Saturday, November 14, 2009 5:18 PM
  • As I said in my first post.  Micromanaging managed code is usually counter productive.  Use C++ native code if you want to control your code at the machine code level.  The reason SSEs are used less and less is because of the increased use of the GPU, particularly for video.
    Saturday, November 14, 2009 5:46 PM
  • I know they mentioned that some MMX instructions are used, but MMX has been standard in almost every CPU since the Pentium 233 first came out. I would assume that MS also assumes all platforms support basic MMX.

    The two posts that talked about SSE are VERY old and  there's been a lot of changes to .Net since and I would assume that MS may have looked at tweaking the compiler to take advantage of SSE where it could.

    Someone did mention .Net *using* SSE, but only in the actual framework and they never said the compiler actually compiled code with SSE in mind.

    In response to "Trying to micromanage managed code is usually highly counterproductive."    I'm just curious because if I ever run into a time where I want to multiply a few numbers by a same number, having SIMD in mind can change the way I write the code. If I thought the compile would not use SSE, I may write the code one way, which may be hard or impossible for the compiler to use SIMD. But if I knew the compiler could use SSE and I knew what the compiler looked for, then I may write my code to best help the compiler take advantage.
    Monday, November 16, 2009 4:28 PM
  • Here's a possibly related question. Lets assume JIT won't use SIMD in general for computation. Most SIMD style computation is in matrix form anyway. I read somewhere that DirectX has math libraries for matrix math and these DirectX calls would use SIMD when they could. Maybe just making DirectX calls for matrix math would be better than worrying about the compiler or writing your own unmanaged ASM code.
    Monday, November 16, 2009 5:35 PM
  • In response to "Trying to micromanage managed code is usually highly counterproductive."    I'm just curious because if I ever run into a time where I want to multiply a few numbers by a same number, having SIMD in mind can change the way I write the code. If I thought the compile would not use SSE, I may write the code one way, which may be hard or impossible for the compiler to use SIMD. But if I knew the compiler could use SSE and I knew what the compiler looked for, then I may write my code to best help the compiler take advantage.

    This is a very good example of the counter productiveness.  If you need to get this deep into knowing the actual machine code used in your application, you probably shouldn't be using managed code.  Considering the coding to the IL level is reasonable.  To the machine code level is not.  The JIT compiler is free to change the implementation of code at any time.  64 bits, multicore cpus and gpus have changed what the most efficient implementation of an algorithm might be.
    Monday, November 16, 2009 6:11 PM
  • In response to "Trying to micromanage managed code is usually highly counterproductive."    I'm just curious because if I ever run into a time where I want to multiply a few numbers by a same number, having SIMD in mind can change the way I write the code. If I thought the compile would not use SSE, I may write the code one way, which may be hard or impossible for the compiler to use SIMD. But if I knew the compiler could use SSE and I knew what the compiler looked for, then I may write my code to best help the compiler take advantage.

    This is a very good example of the counter productiveness.  If you need to get this deep into knowing the actual machine code used in your application, you probably shouldn't be using managed code.  Considering the coding to the IL level is reasonable.  To the machine code level is not.  The JIT compiler is free to change the implementation of code at any time.  64 bits, multicore cpus and gpus have changed what the most efficient implementation of an algorithm might be.
    I do understand your concern, but...

    You could say the same thing about multi-threaded programming. The logic behind producing safe and efficient parallel code is much much harder than grouping numbers together so the compiler can use more efficient instructions. This knowledge of of multi-threaded programming dives well beyond individual instructions and into NUMA, cache hierarchy, cache lines, locking, etc. Not knowing how stuff works the background is like telling a car mechanic that they don't need to know how an engine works, just that turning the key starts it.

     A good example of my above statement would be telling a programmer that atomic operations are thread safe. Little did this hypothetical programmer know is that a 64bit INT in on a 32bit machine uses more than one instruction and is no longer atomic and now his code has weird issues. Yes, knowing the machine language produced can be useful.

    I primarily work with SQL. There are many ways to make a working query, but some ways can be hundreds to thousands of times faster. Without my knowledge of how SQL thinks, I'm just making sub-par code.

    C# isn't nearly as finicky as as SQL for obvious reasons, but I don't like to *just* make working code, I like to make the *best* possible code. I am working within the confines of C#, so I'm not doing to break out C++/ASM and call DLLs; but if C# is capable of making use of SIMD and running several times faster in that section of code, I would like to know.

    "64 bits, multicore cpus and gpus have changed what the most efficient implementation of an algorithm might be."

    That's why you use delegates once you figure what you're working with.



    I do appreciate the input, but obviously SIMD in strictly managed code is a mystery as of right now, and if you need it, just make some unmanaged code and call it
    Wednesday, November 18, 2009 7:22 PM
  • "SIMD in strictly managed code is a mystery as of right now, and if you need it, just make some unmanaged code and call it

    My sentiments exactly.  Even if the workings of the JIT compiler weren't mysterious, you can't count upon today's implementation being the same as tomorrow's.  If managed code doesn't meet your requirments, your time will be better spent learning alternative coding techniques, than delving into the workings of the JIT compiler.
    Wednesday, November 18, 2009 9:49 PM
  • What's SIMD?
    Dave
    Thursday, December 10, 2009 11:25 AM
  • Hello

    SIMD (Single Instruction, Multiple Data; colloquially, "vector instructions") is a technique employed to achieve data level parallelism. You can find more readings about SIMD here:
    http://en.wikipedia.org/wiki/SIMD


    Regards,
    Jialiang Ge
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Thursday, December 10, 2009 11:45 AM
    Moderator