Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pointer to aligned memory

I often use compiler-based vectorization, e.g., for AVX. I am trying to come up with a cleaner way without relying on compiler-based extensions (such as Intel's #pragma vector aligned) by relying on C++11 alignment features. If you consider the code below, e.g., aligned::array<double,48> my_array; allows me to declare an array in stack with proper alignment, and if it is used in the same translation unit compilers seem to recognize this.

My question now concerns how to declare a function with aligned parameters. My most successful attempt is, e.g., aligned::ptr<double>, as used in the function f() below.

gcc compiles this without warnings (use -std=c++0x -O3), and the loop is vectorized. Intel's icc however, gives a warning and does not vectorize properly (warning #3463: alignas does not apply here; using type alignas(64) = T;).

Who is correct? Is there something wrong with my usage of alignas? Is there a better way to accomplish this?

namespace aligned {
  template <class T, int N>
    using array alignas(64) = T[N];

  template <class T>
    using type alignas(64) = T;

  template <class T>
    using ptr = type<T> *;
}

#ifdef __ICC
#define IVDEP "ivdep"
#else
#define IVDEP "GCC ivdep"
#endif

void f(aligned::ptr<double> x, const aligned::ptr<double> y) {
  _Pragma(IVDEP)
  for(int i=0; i<4; i++)
    x[i] = x[i]*y[i];
}
like image 962
Simon Avatar asked May 04 '15 07:05

Simon


People also ask

What is an aligned pointer?

Aligned Pointer means that pointer with adjacent memory location that can be accessed by a adding a constant and its multiples. for char a[5] = "12345"; here a is constant pointer if you and the size of char to it every time you can access the next chracter that is, a +sizeofchar will access 2.

What does it mean for memory to be aligned?

Alignment refers to the arrangement of data in memory, and specifically deals with the issue of accessing data as proper units of information from main memory. First we must conceptualize main memory as a contiguous block of consecutive memory locations. Each location contains a fixed number of bits.

Does malloc aligned memory?

Since many CPUs as well as many operation system do have alignment requirements, most malloc implementation will always return aligned memory but which alignment rules it follows is system specific.

What is memory alignment C?

When the database server passes the data type to a UDR, it aligns opaque-type data on a specified byte boundary. Alignment requirements depend on the C definition of the opaque data type and on the system (hardware and compiler) on which the opaque data type is compiled.


1 Answers

It seems like a bug to me. Your syntax is perfectly correct and accepted by newest versions of GCC and Clang.

First of all, it is important what version of Intel C++ Compiler you currently use.

According to this document:

3.2 New and Changed Features

C++ Composer XE 2015 now contains Intel® C++ Compiler XE 15.0. The following features are new or significantly enhanced in this version:

  • [...]
  • Full C++11 language support (includes these feature new to 15.0) (/Qstd=c++11):

    • value categories (N3055)
    • alignas and alignof (N2341)
    • decltype extensions (N3049, N3276)
    • inheriting constructors (N2540)
    • user defined literals (N2765)
    • thread_local (N2659)

First of all, notice the presence of alignas on the list - you can assume full (or at least "improved comparing with previous version") support of these features starting from ICC 15.0. Secondly, "new or significantly enhanced" is not equal to "fully supported", if you ask me.

This summary also confirms support of alignment features in this version.

It notes, however, that:

Full C++11 support requires gcc 4.8 environment or newer on Linux.

I also encountered this, which may suggest, that not everything works properly yet.


As @Simon has found out, this is a confirmed issue (or, to be more precise, lack of support) and has been reported. Tracker number is DPD200361116. More info can be found in this thread. If anyone else will encounter this problem, I suggest to track updates on this page, they will be posted for sure.

like image 75
Mateusz Grzejek Avatar answered Sep 23 '22 05:09

Mateusz Grzejek