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

vectorization issue

h_krishnan
Beginner
501 Views

In my application, I have the following pattern occurring at many places:

loop_over_i(i)
{
do_something(i); // common for all loops
do_remaining_things(i);
}

The do_something part is common for most loops and so I am trying to extract it out with a class. However, the loop no longer vectorizes. A sample code is given below:

[cpp]#include 
using namespace std;

class Loop
{
public:
    Loop(int max) : max_(max), count_(0), sqr_(0)
    { }
    void more()
    { 
      ++count_ ;
      sqr_ = count_ * count_;
    }
    bool done() const
    {
      return count_ >= max_;
    }
    operator int() const
    {
      return count_;
    }
    int sqr() const
    {
      return sqr_;
    }
private:
    int count_, max_, sqr_;
};

int main()
{
    int square[50];
    // this loop vectorizes
    for (int i = 0; i < 50; ++i)
      {
        square = i*i;
      }
    // this loop doesn't vectorize
    for (Loop loop(50); !loop.done(); loop.more())
    {
      square[loop] = loop.sqr();
    }
}[/cpp]
[cpp]How can I implement what I need in a way that the loop vectorizes?[/cpp]
0 Kudos
1 Solution
jimdempseyatthecove
Honored Contributor III
501 Views
Quoting - h_krishnan

In my application, I have the following pattern occurring at many places:

loop_over_i(i)
{
do_something(i); // common for all loops
do_remaining_things(i);
}

The do_something part is common for most loops and so I am trying to extract it out with a class. However, the loop no longer vectorizes. A sample code is given below:

[cpp]#include 
using namespace std;

class Loop
{
public:
    Loop(int max) : max_(max), count_(0), sqr_(0)
    { }
    void more()
    { 
      ++count_ ;
      sqr_ = count_ * count_;
    }
    bool done() const
    {
      return count_ >= max_;
    }
    operator int() const
    {
      return count_;
    }
    int sqr() const
    {
      return sqr_;
    }
private:
    int count_, max_, sqr_;
};

int main()
{
    int square[50];
    // this loop vectorizes
    for (int i = 0; i < 50; ++i)
      {
        square = i*i;
      }
    // this loop doesn't vectorize
    for (Loop loop(50); !loop.done(); loop.more())
    {
      square[loop] = loop.sqr();
    }
}[/cpp]
[cpp]How can I implement what I need in a way that the loop vectorizes?[/cpp]

When indexing square, elements i and i+1, i+2, ... are in adjacent memory locations, thus permitting vectorization. The compiler vectorization code will recognize loop syntax such as i<50 and ++i and can analyse that for candidate of vectorization.

When you declare the class Loop, and declare the member functions, the compiler does not have the necessary information in order to vectorize the loop. The compiler _potentially_ could vectorize the code if you inline the member functions. But having the !loop.done() may present a problem.

Also note that your more() function is computing the result for the element after the last element. Consider converting the more() to return a bool and remove the done function.

for(Loop loop(50); loop.more();)

where

inline bool more()
{
if(count_>= max_) return false;
sqr_ = count_ * count_;
++count_;
return true;
}

This syntax will not necessarily vectorize. It would depend on how aggressive the the compiler was at vectorization.

Jim Dempsey

View solution in original post

0 Kudos
4 Replies
kalven
Beginner
501 Views
Quoting - h_krishnan

In my application, I have the following pattern occurring at many places:

loop_over_i(i)
{
do_something(i); // common for all loops
do_remaining_things(i);
}

The do_something part is common for most loops and so I am trying to extract it out with a class. However, the loop no longer vectorizes. A sample code is given below:

[cpp]#include 
using namespace std;

class Loop
{
public:
    Loop(int max) : max_(max), count_(0), sqr_(0)
    { }
    void more()
    { 
      ++count_ ;
      sqr_ = count_ * count_;
    }
    bool done() const
    {
      return count_ >= max_;
    }
    operator int() const
    {
      return count_;
    }
    int sqr() const
    {
      return sqr_;
    }
private:
    int count_, max_, sqr_;
};

int main()
{
    int square[50];
    // this loop vectorizes
    for (int i = 0; i < 50; ++i)
      {
        square = i*i;
      }
    // this loop doesn't vectorize
    for (Loop loop(50); !loop.done(); loop.more())
    {
      square[loop] = loop.sqr();
    }
}[/cpp]
[cpp]How can I implement what I need in a way that the loop vectorizes?[/cpp]

0 Kudos
jimdempseyatthecove
Honored Contributor III
502 Views
Quoting - h_krishnan

In my application, I have the following pattern occurring at many places:

loop_over_i(i)
{
do_something(i); // common for all loops
do_remaining_things(i);
}

The do_something part is common for most loops and so I am trying to extract it out with a class. However, the loop no longer vectorizes. A sample code is given below:

[cpp]#include 
using namespace std;

class Loop
{
public:
    Loop(int max) : max_(max), count_(0), sqr_(0)
    { }
    void more()
    { 
      ++count_ ;
      sqr_ = count_ * count_;
    }
    bool done() const
    {
      return count_ >= max_;
    }
    operator int() const
    {
      return count_;
    }
    int sqr() const
    {
      return sqr_;
    }
private:
    int count_, max_, sqr_;
};

int main()
{
    int square[50];
    // this loop vectorizes
    for (int i = 0; i < 50; ++i)
      {
        square = i*i;
      }
    // this loop doesn't vectorize
    for (Loop loop(50); !loop.done(); loop.more())
    {
      square[loop] = loop.sqr();
    }
}[/cpp]
[cpp]How can I implement what I need in a way that the loop vectorizes?[/cpp]

When indexing square, elements i and i+1, i+2, ... are in adjacent memory locations, thus permitting vectorization. The compiler vectorization code will recognize loop syntax such as i<50 and ++i and can analyse that for candidate of vectorization.

When you declare the class Loop, and declare the member functions, the compiler does not have the necessary information in order to vectorize the loop. The compiler _potentially_ could vectorize the code if you inline the member functions. But having the !loop.done() may present a problem.

Also note that your more() function is computing the result for the element after the last element. Consider converting the more() to return a bool and remove the done function.

for(Loop loop(50); loop.more();)

where

inline bool more()
{
if(count_>= max_) return false;
sqr_ = count_ * count_;
++count_;
return true;
}

This syntax will not necessarily vectorize. It would depend on how aggressive the the compiler was at vectorization.

Jim Dempsey

0 Kudos
h_krishnan
Beginner
501 Views
Good point about the issue with my more() function. I didn't realize I was doing something so stupid.
I am using inline functions throughout but still the vectorization failed with a "known dependency" message.
I guess I need to find another way to implement this.
Thanks for your feedback.

0 Kudos
jimdempseyatthecove
Honored Contributor III
501 Views

The fundamental problem you are experiencing is by making the code opaque by encapsulation with member functions and iterators you also tend to make it difficult, if not impossible for the compiler to vectorize or even to determinevectoizability ofthe code (as it chews through the data). This is the penalty you pay for progress. By using "old school" programming techniques such as array of like members of each object (as opposed to C++ arrays of objects of elements) the data layout favors vectorization. The requirements of the application would indicate the better of the two techniques. If you need the vectorization for performance then consider unpackaging the objects.

Jim

0 Kudos
Reply