-
December 14th, 2011, 11:35 PM
#1
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
-
December 15th, 2011, 01:24 AM
#2
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.
-
December 15th, 2011, 04:28 AM
#3
Re: Order of operation on multi core hardware
Originally Posted by sockman
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
-
December 15th, 2011, 04:40 AM
#4
Re: Order of operation on multi core hardware
Originally Posted by sockman
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
-
December 15th, 2011, 04:56 AM
#5
Re: Order of operation on multi core hardware
Originally Posted by D_Drmmr
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.
-
December 15th, 2011, 04:57 AM
#6
Re: Order of operation on multi core hardware
Originally Posted by JohnW@Wessex
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.
-
December 15th, 2011, 05:05 AM
#7
Re: Order of operation on multi core hardware
Originally Posted by nuzzle
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
-
December 15th, 2011, 05:25 AM
#8
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
-
December 15th, 2011, 05:41 AM
#9
Re: Order of operation on multi core hardware
Originally Posted by nuzzle
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
-
December 15th, 2011, 05:50 AM
#10
Re: Order of operation on multi core hardware
Originally Posted by D_Drmmr
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
-
December 15th, 2011, 06:04 AM
#11
Re: Order of operation on multi core hardware
Originally Posted by JohnW@Wessex
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.
-
December 15th, 2011, 06:49 AM
#12
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
-
December 15th, 2011, 07:52 AM
#13
Re: Order of operation on multi core hardware
Originally Posted by nuzzle
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.
Originally Posted by nuzzle
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
-
December 15th, 2011, 10:50 AM
#14
Re: Order of operation on multi core hardware
Originally Posted by nuzzle
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...
Originally Posted by nuzzle
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...
-
December 15th, 2011, 11:44 AM
#15
Re: Order of operation on multi core hardware
Originally Posted by D_Drmmr
Here, section 1.9.1
Also, refer to:
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|