CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 37
  1. #1
    Join Date
    Aug 2006
    Posts
    157

    Order of operation on multi core hardware

    Hello,
    On a multi-core machine, am I guaranteed to have my code executed in the precise order it was written?
    Cheers,
    BJW

  2. #2
    Join Date
    May 2009
    Posts
    2,413

    Re: Order of operation on multi core hardware

    Yes, unless you make active use of multithreading language constructs there will only be one thread of execution also on a multicore computer.

  3. #3
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Order of operation on multi core hardware

    Quote Originally Posted by sockman View Post
    Hello,
    On a multi-core machine, am I guaranteed to have my code executed in the precise order it was written?
    Cheers,
    BJW
    No, you never are. The computer doesn't execute your code, it executes binary instructions. Your code is translated into these instructions by the compiler and linker. The C++ standard only mandates that the observable behavior of the program corresponds with your code. The compiler is free to make optimizations as long as the observable behavior remains the same. This is particularly the case for return value optimization and copy elision, which implies that you are never guaranteed that a certain exact number of copies of an object is made.

    The C++ standard implicitly assumes single-threaded execution, so if you use multiple threads in your program, you will need to provide synchronization between variables that are shared by multiple threads, where at least one thread writes to the variable.
    Whether the program is executed on a single or multi-core machine makes no difference.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  4. #4
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Order of operation on multi core hardware

    Quote Originally Posted by sockman View Post
    Hello,
    On a multi-core machine, am I guaranteed to have my code executed in the precise order it was written?
    You can't rely on that even on a single core.
    For example, say you were to set a flag in a function to indicate to another thread that you had completed some action. Without a memory barrier, the compiler would be free to move the flag setting code to anywhere within the function, if moving it has no effect on the outcome of that function. Even marking the variable 'volatile' cannot guarantee this.

    e.g.
    Code:
    bool done = false;
    
    void Function()
    {
        // Do some processing.
        ...
    
       // Flag as 'done'
       done = true; // You cannot guarantee that this will happen last, after optimisation.
    }
    The compiler may look at the code above and decide it is more optimal to set the flag at the beginning of the function.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  5. #5
    Join Date
    May 2009
    Posts
    2,413

    Re: Order of operation on multi core hardware

    Quote Originally Posted by D_Drmmr View Post
    No, you never are. The computer doesn't execute your code, it executes binary instructions.
    That's bullshit. The execution order is defined by the programming language alone. A program will run as written. Multicore hardware or not has no influence on that.
    Last edited by nuzzle; December 15th, 2011 at 05:03 AM.

  6. #6
    Join Date
    May 2009
    Posts
    2,413

    Re: Order of operation on multi core hardware

    Quote Originally Posted by JohnW@Wessex View Post
    You can't rely on that even on a single core.
    That's bullshit. The execution order is defined by the programming language alone. A program will run as written. Multicore hardware or not has no influence on that.
    Last edited by nuzzle; December 15th, 2011 at 05:03 AM.

  7. #7
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Order of operation on multi core hardware

    Quote Originally Posted by nuzzle View Post
    That's bullshit. The execution order is defined by the programming language alone. A program will run as written. Multicore or not has no influence on that.
    If it's defined by the programming language, then please quote the section of the standard that states this.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  8. #8
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Order of operation on multi core hardware

    Here's a quote from Herb Sutter about reordering of reads and writes.

    Second, what about nearby ordinary reads and writes -- can those still be reordered around unoptimizable reads and writes? Today, there is no practical portable answer because C/C++ compiler implementations vary widely and aren't likely to converge anytime soon. For example, one interpretation of the C++ Standard holds that ordinary reads can move freely in either direction across a C/C++ volatile read or write, but that an ordinary write cannot move at all across a C/C++ volatile read or write -- which would make C/C++ volatile both less restrictive and more restrictive, respectively, than an ordered atomic. Some compiler vendors support that interpretation; others don't optimize across volatile reads or writes at all; and still others have their own preferred semantics.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  9. #9
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Order of operation on multi core hardware

    Quote Originally Posted by nuzzle View Post
    That's bullshit. The execution order is defined by the programming language alone. A program will run as written.
    Code:
    int a;
    int b;
    int c;
    
    void Function()
    {
        a = 0;
        b = 1;
        c = 2;
    
        // Some other stuff.
    }
    Are you really saying that you believe a, b & c are guaranteed to be initialised in that order? Everything I've read about compiler optimisations and CPU instruction reordering imply otherwise.
    Last edited by JohnW@Wessex; December 15th, 2011 at 06:00 AM. Reason: made variables global
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  10. #10
    Join Date
    May 2009
    Posts
    2,413

    Re: Order of operation on multi core hardware

    Quote Originally Posted by D_Drmmr View Post
    If it's defined by the programming language, then please quote the section of the standard that states this.
    What a silly request.

    A programming language defines an abstract computer and how it behaves is defined by the standard as a whole. But if you need a place to start you can have a look at 1.9 Program execution.

    http://www.open-std.org/jtc1/sc22/wg...2010/n3092.pdf

  11. #11
    Join Date
    May 2009
    Posts
    2,413

    Re: Order of operation on multi core hardware

    Quote Originally Posted by JohnW@Wessex View Post
    Are you really saying that you believe a, b & c are guaranteed to be initialised in that order? Everything I've read about compiler optimisations and CPU instruction reordering imply otherwise.
    You're confusing the language with implementations of the language.

    The language defines an abstract computer and it will execute a program written in that language exactly as the standard specifies.

    Physical implementations of the abstract computer, such as compilers, interpreters and various hardware cannot change that. If they do they're not conformant with the language standard.

  12. #12
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Order of operation on multi core hardware

    You're confusing the language with implementations of the language.
    The OP asked
    On a multi-core machine, am I guaranteed to have my code executed in the precise order it was written?
    He is talking about a real implementation of C++ code on a real machine. Real compilers can re-order reads and writes. Real CPUs can execute 'out of order'.
    If you don't take this into account when writing multi-threaded / multi-core applications then you are heading for big trouble.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  13. #13
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Order of operation on multi core hardware

    Quote Originally Posted by nuzzle View Post
    A programming language defines an abstract computer and how it behaves is defined by the standard as a whole.
    Here, section 1.9.1
    This International Standard places no requirement on the structure of conforming implementations. In particular, they need not copy or emulate the structure of the abstract machine. Rather, conforming implementations are required to emulate (only) the observable behavior of the abstract machine as explained below.
    Next time, please take one minute to investigate before calling something bullshit or otherwise, provide some well-funded arguments.
    Quote Originally Posted by nuzzle View Post
    The language defines an abstract computer and it will execute a program written in that language exactly as the standard specifies.

    Physical implementations of the abstract computer, such as compilers, interpreters and various hardware cannot change that. If they do they're not conformant with the language standard.
    Let's consider a practical example that just popped up in another thread. We know the double check locking pattern for the implementation of a singleton is broken. However, this cannot be explained in terms of the code itself, only in terms of the hardware. So following your logic, this would mean that modern PC's are not conformant with C++ (whatever that should mean, I never heard of such a notion and don't think it makes any sense). That makes your point moot, because you cannot execute a program on an abstract machine.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  14. #14
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Order of operation on multi core hardware

    Quote Originally Posted by nuzzle View Post
    That's bullshit. The execution order is defined by the programming language alone. A program will run as written.
    That is a pretty strong language for such a weak statement...
    Quote Originally Posted by nuzzle View Post
    Multicore hardware or not has no influence on that.
    Well, this IS correct, as regardless of the number of cores the order is not guaranteed
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  15. #15
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Order of operation on multi core hardware

    Quote Originally Posted by D_Drmmr
    Here, section 1.9.1
    Also, refer to:
    Quote Originally Posted by C++11 Clause 1.9 Note 5
    This provision is sometimes called the "as-if" rule, because an implementation is free to disregard any requirement of this International Standard as long as the result is as if the requirement had been obeyed, as far as can be determined from the observable behavior of the program. For instance, an actual implementation need not evaluate part of an expression if it can deduce that its value is not used and that no side effects affecting the observable behavior of the program are produced.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured