If or function pointers in fortran

前端 未结 4 760
南旧
南旧 2021-01-21 01:39

as it is so common with Fortran, I\'m writing a massively parallel scientific code. In the beginning of my code I read my configuration file which tells me which type of solver

4条回答
  •  广开言路
    2021-01-21 01:53

    Knowing nothing about Fortran, this is my answer: The main problem with branching is that a CPU potentially cannot speculatively execute code across them. To mitigate this problem, branch prediction was introduced (which is very sophisticated in modern CPUs).

    Indirect calls through a function pointer can be a problem for the prediction unit of the CPU. If it can't predict where the call will actually go, this will stall the pipeline.

    I am quite sure that the CPU will correctly predict that your branch will always be taken or not taken because it is a trivial case of prediction.

    Maybe the CPU can speculate across the indirect call, maybe it can't. This is why you need to test which is better.

    If it cannot, you will certainly notice in your benchmark.

    In addition, maybe you can hoist the if test out of your inner loop so it won't be called often. This will make the actual performance of the branch irrelevant.

提交回复
热议问题