CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > General Forums > Main CFD Forum

Where does the branch predictor store it's prediction data?

Register Blogs Community New Posts Updated Threads Search

Like Tree4Likes
  • 1 Post By aerosayan
  • 1 Post By flotus1
  • 2 Post By flotus1

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   November 14, 2020, 14:36
Default Where does the branch predictor store it's prediction data?
  #1
Senior Member
 
Sayan Bhattacharjee
Join Date: Mar 2020
Posts: 495
Rep Power: 8
aerosayan is on a distinguished road
Where does the branch predictor store it's prediction data?



I ask this question because I don't understand where the speculative execution pipeline of the CPU store it's prediction data.


As per my understanding, the Instruction Cache(ICACHE) will prefetch and load the code that is to be executed next. So it makes sense that the branch predictors will also load their prediction data from somewhere. I don't know from where though.


For example :


Code:
// Solver core loop
// code ...
// code ...
// code ...


// Calculate flux

if( flux == ROE_FLUX)
{
  // Calculate Roe flux

}
else
if( flux == VAN_LEER_FLUX)
{
 // Calculate Van Leer flux

}


// code ...
// code ...
// code ...

In the example given above, the flux calculation will happen in the core of the solver loop. However as we can see, the code has to decide which flux to calculate. Now, in a conventional solver, only a single type of flux will be used, so only one of those branches will be selected for all of the iterations and the other will be discarded.


I want to ensure that the branch predictor will always select the correct branch and not suffer from branch misprediction penalties.

So, will the branch predictors always know which branch to select (and if so, where do they load this data from), or will the branch predictor always has to test the two branches and suffer from ocassional branch mispredictions?

I'm not looking to do premature optimizations, but just to understand the process better.


Thanks
praveen likes this.
aerosayan is offline   Reply With Quote

Old   November 14, 2020, 15:24
Default
  #2
Senior Member
 
Kira
Join Date: Nov 2020
Location: Canada
Posts: 435
Rep Power: 8
aero_head is on a distinguished road
Hello Sayan,

I am pretty sure more context is needed but I'm not sure if it would be from you or from someone who knows more about the software.

I think branch prediction is pretty good if it’s the same every loop, that is to say that I am not sure if that would be a noticeable performance impact. I know you were looking into understanding the process. That process depends on how complicated the code is in the first if branch and if goes into some deep hardware execution stuff.

I am unsure if you are writing the code that is getting executed in the main loop or if some other code is being used. If you have access to some code that gets run before hand you could make a function pointer or use some other trick to prevent needing to do an if statement inside the loop, but I am unsure if you would have that ability because for something like this it will run iterations of the solver on several cores so it doesn’t follow typical execution flow.

That being said, do you have more code available for us to look at to try to help more?
aero_head is offline   Reply With Quote

Old   November 14, 2020, 16:28
Default
  #3
Super Moderator
 
flotus1's Avatar
 
Alex
Join Date: Jun 2012
Location: Germany
Posts: 3,426
Rep Power: 49
flotus1 has a spectacular aura aboutflotus1 has a spectacular aura about
The big question here is: where and how do you assign a value to "flux"?
aero_head likes this.
flotus1 is offline   Reply With Quote

Old   November 15, 2020, 03:42
Default
  #4
Senior Member
 
Sayan Bhattacharjee
Join Date: Mar 2020
Posts: 495
Rep Power: 8
aerosayan is on a distinguished road
Quote:
Originally Posted by flotus1 View Post
The big question here is: where and how do you assign a value to "flux"?

The value for flux is set during the initialization step of the solver. The value isn't known during compilation, so the compiler can't perform dead code/branch removal optimization. The value is guaranteed to be present in the L1 cache. All of the Plain Old Data variables in the performance critical section are kept together in memory so that they are loaded in a single cache line and are always available for use.
aerosayan is offline   Reply With Quote

Old   November 15, 2020, 04:34
Default
  #5
Senior Member
 
Arjun
Join Date: Mar 2009
Location: Nurenberg, Germany
Posts: 1,282
Rep Power: 34
arjun will become famous soon enougharjun will become famous soon enough
Do you also want us to guess the software you are talking about?? Just wondering.
arjun is offline   Reply With Quote

Old   November 15, 2020, 05:23
Default
  #6
Senior Member
 
Sayan Bhattacharjee
Join Date: Mar 2020
Posts: 495
Rep Power: 8
aerosayan is on a distinguished road
Quote:
Originally Posted by arjun View Post
Do you also want us to guess the software you are talking about?? Just wondering.

Writing my own code using C++ and Fortran 2003.

Question asked was regarding how modern pipelined microprocessors handle code execution in their Speculative Execution optimizer to reduce the cost of conditional branching, and not any commercial software.

It is a hardware related question.

Last edited by aerosayan; November 15, 2020 at 05:25. Reason: added last line.
aerosayan is offline   Reply With Quote

Old   November 15, 2020, 07:14
Default
  #7
Super Moderator
 
flotus1's Avatar
 
Alex
Join Date: Jun 2012
Location: Germany
Posts: 3,426
Rep Power: 49
flotus1 has a spectacular aura aboutflotus1 has a spectacular aura about
Since it's a run-time constant, there are certainly some tricks you could pull off to avoid branch prediction entirely.
But at the same time, branch prediction should have no issues handling this. After all, it's always the same code path, which is the best-case scenario short of knowing the code path at compile time. You could use a profiler to verify what's happening.
sbaffini and aerosayan like this.
flotus1 is offline   Reply With Quote

Old   November 15, 2020, 12:08
Default
  #8
Senior Member
 
Arjun
Join Date: Mar 2009
Location: Nurenberg, Germany
Posts: 1,282
Rep Power: 34
arjun will become famous soon enougharjun will become famous soon enough
Quote:
Originally Posted by aerosayan View Post
Writing my own code using C++ and Fortran 2003.

Question asked was regarding how modern pipelined microprocessors handle code execution in their Speculative Execution optimizer to reduce the cost of conditional branching, and not any commercial software.

It is a hardware related question.


None of this is clear from your original posting.
Good luck.
arjun is offline   Reply With Quote

Reply

Tags
program development, solver deveopment


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
[OpenFOAM] How to get the coordinates of velocity data at all cells and at all times vidyadhar ParaView 9 May 20, 2020 20:06
【Help】"Error: Update_Time_Level: invalid data" Chen FLUENT 2 August 24, 2014 07:51
Problem running in parralel Val OpenFOAM Running, Solving & CFD 1 June 12, 2014 02:47
[OpenFOAM] saving data in paraview aylalisa ParaView 3 May 31, 2014 11:38
using pressure varaition data for noise prediction Deosharan Roy CFX 2 July 13, 2005 01:30


All times are GMT -4. The time now is 21:01.