Quantcast
Channel: Intel® C++ Compiler
Viewing all articles
Browse latest Browse all 1616

Vectorization in C++

$
0
0

Hi,

The following code vectorizes nicely in Fortran, but I can't make it vectorize in C++.

program main
    implicit none

    integer, parameter :: dp = 8
    integer, parameter :: n = 2 * 10**9

    real(dp) :: somme
    integer :: i

    somme = 0.0_dp
    do i = 1, n
        somme = somme + 1.0_dp / real(i, dp)
    end do

    write (*,*) somme
end program main

And the C++ version is the following :

#include <iostream>

int main (int argc, char const *argv[])
{
	using namespace std;

	const int n {2000000000};
	double somme {0.0};
	for(size_t i = 1; i <= n; ++i)
	{
		somme += 1.0 / static_cast<double>(i);
	}
	cout << somme;

	return 0;
}

I compile the fortran version with ifort -Ofast and the c++ version with icpc -Ofast. The Fortran version is 2x faster as it uses vector instructions (checked in the assembly code), but the C++ version does not. Is there a reason for this ?

Best regards,

François


Viewing all articles
Browse latest Browse all 1616

Trending Articles