Intel® C++ Compiler
Community support and assistance for creating C++ code that runs on platforms based on Intel® processors.
7956 Discussions

Automatic vectorization and std::vector

nupshur1
Beginner
381 Views

Hi,

Can the compiler vectorize simple loops with std::vector? Isawa much earlier post involving std::vector in which it seemed that the compiler can do this. I tried it for myself and got a message saying "dereference too complex":

#include

#include

#include

using

namespace std;

static

void foo(vector<int> ints1, vector<int> ints2);

int

main() {

vector<

int> ints1;

vector<

int> ints2;

foo(ints1, ints2);

}

void

foo(vector<int> ints1, vector<int> ints2)

{

#pragma

ivdep

#pragma

vector always

for (int i = 0; i < 1000; ++i)

ints1 = ints2;

}

Thanks,

~Nafis

0 Kudos
4 Replies
TimP
Honored Contributor III
381 Views
As the restrict extension isn't permitted with std::vector, inability to vectorize this is not surprising. Optimization of a simple copy is generally done in C or C++ by persuading the compiler to substitute memcpy() or memmove(), as std::copy() does, but I take your point.
0 Kudos
nupshur1
Beginner
381 Views

In another post (answered by Aart), someone posted the code below and claimed that test1 reported as vectorized. When I compiled it, test1 did not vectorize. Are you able to get this to work?

typedef

std::vector<double> DoubleVector;

struct

Test

{

DoubleVector v1;

DoubleVector v2;

void test1(const double x);

void test2(const double x);

void test3(const double x);

void test4(const double x);

};

void

f1(const double x, const DoubleVector& v1, DoubleVector& v2)

{

const DoubleVector::size_type size = v1.size();

// this IS vectorized!

for(DoubleVector::size_type i = 0; i < size; ++i)

{

v2 = exp(v1*x);

}

}

void

Test::test1(const double x)

{

// this IS vectorized!

f1(x, v1, v2);

}

void

Test::test2(const double x)

{

const DoubleVector::size_type size = v1.size();

// this IS NOT vectorized

for(DoubleVector::size_type i = 0; i < size; ++i)

{

v2 = exp(v1 * x);

}

}

void

Test::test3(const double x)

{

const DoubleVector::size_type size = v1.size();

// this IS NOT vectorized

// not even with the 'ignore dependencies' pragma

#pragma ivdep

for(DoubleVector::size_type i = 0; i < size; ++i)

{

v2 = exp(v1 * x);

}

}

void

Test::test4(const double x)

{

const DoubleVector::size_type size = v1.size();

const double* p1 = &v1[0];

double* p2 = &v2[0];

// this IS vectorized!

// but goodbye to the vector abstraction

for(DoubleVector::size_type i = 0; i < size; ++i)

{

p2 = exp(p1 * x);

}

}

0 Kudos
Intel_C_Intel
Employee
381 Views

Dear Nafis,

The code in test1() is obviously only vectorized when inlined, so you may want to try Qip, -Qipo, or fast to observe vectorization, depending on your compiler version. You seem to have a great interest in vectorization. Perhaps I may recommend the vectorization handbook as next reading?

http://www.intel.com/intelpress/sum_vmmx.htm

Sincerely,

Aart Bik

http://www.aartbik.com/

0 Kudos
nupshur1
Beginner
381 Views

Probably not a bad idea. Thanks,

~Nafis

0 Kudos
Reply