[eigen] Re: platform detection for aligned malloc

[ Thread Index | Date Index | More lists.tuxfamily.org/eigen Archives ]


Forgot to attach the patch...

2009/1/9 Benoit Jacob <jacob.benoit.1@xxxxxxxxx>:
> Hi,
>
> I'm reworking the platform detection for aligned malloc.
> One recent change is that we now need aligned malloc even if
> vectorization is not used. So if we want to use _mm_malloc we need to
> use it from another header than emmintrin.h. I heard there's a
> mm_malloc.h on some platforms but i'm not too sure.
>
> Another finding i made is that on freebsd too, malloc() is 16 byte aligned.
>
> Please find attached a patch, it's the best i can do but it needs improvements.
> *Linux:
>  --> question: in current SVN we always add manually the prototype of
> posix_memalign. with this patch, we do it only if not declared by
> <cstdlib> that is if _XOPEN_SOURCE<600. Anyway, is this really good
> practice? If XOPEN_SOURCE<600 then maybe we really can't use
> posix_memalign, i mean, there's got to be a reason why it's not
> declared....
> ---> need to check especially with GCC 3...
> *Mac OSX and FreeBSD:
> ---> Problem solved, we use malloc()
> *Other BSDs:
> ---> no clue
> *Solaris:
> ----> no clue
> MSVC:
> ----> problem solved, we use _aligned_malloc
> ICC:
> ---> we use _mm_malloc. Question: do we need to #include some header,
> like <mm_malloc.h>? When vectorization is not enabled we can't count
> on emmintrin.h.
> Cygwin:
> ---> Gael formerly made it use _mm_malloc, so same question as ICC,
> which header to include?
> Old GCC:
>
> Cheers,
> Benoit
>
Index: Eigen/src/Core/util/Memory.h
===================================================================
--- Eigen/src/Core/util/Memory.h	(révision 907840)
+++ Eigen/src/Core/util/Memory.h	(copie de travail)
@@ -26,11 +26,29 @@
 #ifndef EIGEN_MEMORY_H
 #define EIGEN_MEMORY_H
 
-#ifdef __linux
-// it seems we cannot assume posix_memalign is defined in the stdlib header
-extern "C" int posix_memalign (void **, size_t, size_t) throw ();
+#if defined(__APPLE__) || defined(__FreeBSD__)
+  #define EIGEN_MALLOC_ALREADY_ALIGNED 1
+#else
+  #define EIGEN_MALLOC_ALREADY_ALIGNED 0
 #endif
 
+#if (!EIGEN_MALLOC_ALREADY_ALIGNED) && defined(__linux)
+  #define EIGEN_USE_POSIX_MEMALIGN 1
+  #if (defined _GNU_SOURCE) || ((defined _XOPEN_SOURCE) && (_XOPEN_SOURCE >= 600))
+    // nothing to do, posix_memalign should be already declared
+  #else
+    extern "C" int posix_memalign (void **, size_t, size_t) throw ();
+  #endif
+#else
+  #define EIGEN_USE_POSIX_MEMALIGN 0
+#endif
+
+#if defined(__ICC) || defined(__INTEL_COMPILER)
+  #define EIGEN_USE_MM_MALLOC 1
+#else
+  #define EIGEN_USE_MM_MALLOC 0
+#endif
+
 /** \internal allocates \a size bytes. The returned pointer is guaranteed to have 16 bytes alignment.
   * On allocation error, the returned pointer is undefined, but if exceptions are enabled then a std::bad_alloc is thrown.
   */
@@ -41,7 +59,7 @@
   #endif
 
   void *result;
-  #ifdef __linux
+  #if EIGEN_USE_POSIX_MEMALIGN
     #ifdef EIGEN_EXCEPTIONS
       const int failed =
     #endif
@@ -49,10 +67,12 @@
   #else
     #ifdef _MSC_VER
       result = _aligned_malloc(size, 16);
-    #elif defined(__APPLE__)
-      result = malloc(size); // Apple's malloc() already returns 16-byte-aligned ptrs
+    #elif EIGEN_MALLOC_ALREADY_ALIGNED
+      result = malloc(size);
+    #elif EIGEN_USE_MM_MALLOC
+      result = _mm_malloc(size, 16);
     #else
-      result = _mm_malloc(size, 16);
+      #error "Don't know how to allocate aligned memory on this platform"
     #endif
     #ifdef EIGEN_EXCEPTIONS
       const int failed = (result == 0);
@@ -102,14 +122,16 @@
   */
 inline void ei_aligned_free(void *ptr)
 {
-  #if defined(__linux)
+  #if EIGEN_USE_POSIX_MEMALIGN
     free(ptr);
-  #elif defined(__APPLE__)
+  #elif EIGEN_MALLOC_ALREADY_ALIGNED
     free(ptr);
   #elif defined(_MSC_VER)
     _aligned_free(ptr);
+  #elif EIGEN_USE_MM_MALLOC
+    _mm_free(ptr);
   #else
-    _mm_free(ptr);
+    #error "Don't know how to free aligned memory on this platform"
   #endif
 }
 


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/