merge with kernels from MH's master thesis
[LbmBenchmarkKernelsPublic.git] / src / Memory.c
index d9d1f942c7e11c3c8ddf05e4d2d5ab58fac1f24e..ef4ccec2879171b0a7b63abbbd9e3d46065e3545 100644 (file)
@@ -8,6 +8,10 @@
 //   Viktor Haag, 2016
 //   LSS, University of Erlangen-Nuremberg, Germany
 //
+//   Michael Hussnaetter, 2017-2018
+//   University of Erlangen-Nuremberg, Germany
+//   michael.hussnaetter -at- fau.de
+//
 //  This file is part of the Lattice Boltzmann Benchmark Kernels (LbmBenchKernels).
 //
 //  LbmBenchKernels is free software: you can redistribute it and/or modify
 //  along with LbmBenchKernels.  If not, see <http://www.gnu.org/licenses/>.
 //
 // --------------------------------------------------------------------------
+// TODO: make configurable
+#define HAVE_HUGE_PAGES
+
+
+#ifdef HAVE_HUGE_PAGES
+#define _BSD_SOURCE
+#endif
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>  // strerror
 #include <errno.h>
 
+
+#ifdef HAVE_MEMKIND
+#include <hbwmalloc.h>
+#endif
+
+#ifdef HAVE_HUGE_PAGES
+#include <sys/mman.h> // madvise
+#endif
+
 #include "Base.h"
 #include "Memory.h"
 
@@ -60,6 +81,19 @@ int MemAllocAligned(void ** ptr, size_t bytesToAlloc, size_t alignmentBytes)
                exit(1);
        }
 
+#ifdef HAVE_HUGE_PAGES
+
+       if (alignmentBytes % 4096 == 0) {
+               ret = madvise(*ptr, bytesToAlloc, MADV_HUGEPAGE);
+
+               if (ret != 0) {
+                       DEBUG_BREAK_POINT();
+                       Error("madvise(%p, %lu, MADV_HUGEPAGE) failed: %d - %s.\n", *ptr, bytesToAlloc, errno, strerror(errno));
+                       exit(1);
+               }
+       }
+#endif
+
        return 0;
 }
 
@@ -84,3 +118,48 @@ int MemZero(void * ptr, size_t bytesToZero)
 
        return 0;
 }
+
+#ifdef HAVE_MEMKIND
+int HbwAlloc(void ** ptr, size_t bytesToAlloc)
+{
+       void * tmpPtr;
+
+       tmpPtr = hbw_malloc(bytesToAlloc);
+
+       if (tmpPtr == NULL) { //  && bytesToAlloc != 0) {
+               Error("allocation of %lu bytes in HBM failed: %d - %s\n", bytesToAlloc, errno, strerror(errno));
+               exit(1);
+       }
+
+       *ptr = tmpPtr;
+
+       return 0;
+}
+
+int HbwAllocAligned(void ** ptr, size_t bytesToAlloc, size_t alignmentBytes)
+{
+       int ret;
+
+       ret = hbw_posix_memalign(ptr, alignmentBytes, bytesToAlloc);
+
+       if (ret) {
+               Error("allocation of %lu bytes in HBM aligned to %lu bytes failed: %d - %s\n", bytesToAlloc, alignmentBytes, errno, strerror(errno));
+               exit(1);
+       }
+
+       return 0;
+}
+
+
+int HbwFree(void ** ptr)
+{
+       Assert(*ptr != NULL);
+
+       hbw_free(*ptr);
+
+       *ptr = NULL;
+
+       return 0;
+}
+
+#endif // HAVE_MEMKIND
This page took 0.078966 seconds and 5 git commands to generate.