From 0982605f7b480183e967532db48ea79a41036539 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jakub=20Klinkovsk=C3=BD?= <klinkjak@fjfi.cvut.cz>
Date: Wed, 3 Jul 2019 17:15:28 +0200
Subject: [PATCH] Used MIC allocator in smart pointers

---
 src/TNL/Devices/MIC.h               | 23 -----------------------
 src/TNL/Pointers/DevicePointer.h    |  5 +++--
 src/TNL/Pointers/SharedPointerMic.h |  7 ++++---
 src/TNL/Pointers/UniquePointer.h    |  5 +++--
 4 files changed, 10 insertions(+), 30 deletions(-)

diff --git a/src/TNL/Devices/MIC.h b/src/TNL/Devices/MIC.h
index db1a238097..f347a24d1f 100644
--- a/src/TNL/Devices/MIC.h
+++ b/src/TNL/Devices/MIC.h
@@ -134,29 +134,6 @@ class MIC
             }
         };
 
-        static inline
-        void* AllocMIC(size_t size)
-        {
-            Devices::MICHider<void> hide_ptr;
-            #pragma offload target(mic) out(hide_ptr) in(size)
-            {
-                hide_ptr.pointer=malloc(size);
-            }
-            return hide_ptr.pointer;
-        };
-
-        static inline
-        void FreeMIC(void* ptr)
-        {
-                Devices::MICHider<void> hide_ptr;
-                hide_ptr.pointer=ptr;
-                #pragma offload target(mic) in(hide_ptr)
-                {
-                        free(hide_ptr.pointer);
-                }
-        };
-
-
 #endif
 
    static void insertSmartPointer( Pointers::SmartPointer* pointer )
diff --git a/src/TNL/Pointers/DevicePointer.h b/src/TNL/Pointers/DevicePointer.h
index 867f18fcbf..b0c0a934fa 100644
--- a/src/TNL/Pointers/DevicePointer.h
+++ b/src/TNL/Pointers/DevicePointer.h
@@ -12,6 +12,7 @@
 
 #pragma once
 
+#include <TNL/Allocators/Default.h>
 #include <TNL/Devices/Host.h>
 #include <TNL/Devices/Cuda.h>
 #include <TNL/Devices/MIC.h>
@@ -695,7 +696,7 @@ class DevicePointer< Object, Devices::MIC > : public SmartPointer
          if( ! this->pd )
             return false;
          // pass to device
-         this->mic_pointer = (ObjectType*)Devices::MIC::AllocMIC(sizeof(ObjectType));
+         this->mic_pointer = Allocators:::MIC< ObjectType >().allocate(1);
          if( ! this->mic_pointer )
             return false;
          Devices::MIC::CopyToMIC((void*)this->mic_pointer,(void*)this->pointer,sizeof(ObjectType));
@@ -733,7 +734,7 @@ class DevicePointer< Object, Devices::MIC > : public SmartPointer
                delete this->pd;
                this->pd = nullptr;
                if( this->mic_pointer )
-                  Devices::MIC::FreeMIC( (void*) this->mic_pointer );
+                  Allocators:::MIC< ObjectType >().deallocate(this->mic_pointer, 1);
             }
          }
       }
diff --git a/src/TNL/Pointers/SharedPointerMic.h b/src/TNL/Pointers/SharedPointerMic.h
index 0e19e78b21..0c2958b4ad 100644
--- a/src/TNL/Pointers/SharedPointerMic.h
+++ b/src/TNL/Pointers/SharedPointerMic.h
@@ -14,6 +14,7 @@
 
 #include "SharedPointer.h"
 
+#include <TNL/Allocators/Default.h>
 #include <TNL/Devices/MIC.h>
 #include <TNL/Pointers/SmartPointer.h>
 
@@ -307,7 +308,7 @@ class SharedPointer< Object, Devices::MIC > : public SmartPointer
          if( ! this->pd )
             return false;
 
-         mic_pointer=(Object*)Devices::MIC::AllocMIC(sizeof(Object));
+         mic_pointer = Allocators::MIC< Object >().allocate(1);
          Devices::MIC::CopyToMIC((void*)this->mic_pointer,(void*) &this->pd->data,sizeof(Object));
 
          if( ! this->mic_pointer )
@@ -350,8 +351,8 @@ class SharedPointer< Object, Devices::MIC > : public SmartPointer
                this->pd = nullptr;
                if( this->mic_pointer )
                {
-                   Devices::MIC::FreeMIC((void*)mic_pointer);
-                   mic_pointer=nullptr;
+                  Allocators:::MIC< ObjectType >().deallocate(mic_pointer, 1);
+                  mic_pointer=nullptr;
                }
 #ifdef TNL_DEBUG_SHARED_POINTERS
                std::cerr << "...deleted data." << std::endl;
diff --git a/src/TNL/Pointers/UniquePointer.h b/src/TNL/Pointers/UniquePointer.h
index c8efd5afe3..cfb7b543fc 100644
--- a/src/TNL/Pointers/UniquePointer.h
+++ b/src/TNL/Pointers/UniquePointer.h
@@ -12,6 +12,7 @@
 
 #pragma once
 
+#include <TNL/Allocators/Default.h>
 #include <TNL/Devices/Host.h>
 #include <TNL/Devices/Cuda.h>
 #include <TNL/Devices/MIC.h>
@@ -446,7 +447,7 @@ class UniquePointer< Object, Devices::MIC > : public SmartPointer
          if( ! this->pd )
             return false;
          // pass to device
-         this->mic_pointer=(Object*)Devices::MIC::AllocMIC(sizeof(Object));
+         this->mic_pointer = Allocators::MIC< Object >().allocate(1);
          if( ! this->mic_pointer )
             return false;
          Devices::MIC::CopyToMIC((void*)mic_pointer,(void*)&this->pd->data,sizeof(Object));
@@ -477,7 +478,7 @@ class UniquePointer< Object, Devices::MIC > : public SmartPointer
          if( this->pd )
             delete this->pd;
          if( this->mic_pointer )
-             Devices::MIC::FreeMIC(mic_pointer);
+            Allocators:::MIC< ObjectType >().deallocate(mic_pointer, 1);
       }
 
       PointerData* pd;
-- 
GitLab