From d46372d3db114dd2dc95b3b03d7d9b906287d253 Mon Sep 17 00:00:00 2001
From: Pietro Incardona <i-bird@localhost.localdomain>
Date: Wed, 20 Apr 2016 17:55:50 +0200
Subject: [PATCH] Fixing Cuda compilation

---
 src/memory/CudaMemory.cu  | 40 ++++++++++++++++++++++++++++++++-------
 src/memory/CudaMemory.cuh | 19 +++++++++++--------
 2 files changed, 44 insertions(+), 15 deletions(-)

diff --git a/src/memory/CudaMemory.cu b/src/memory/CudaMemory.cu
index 2df5de7..8610430 100644
--- a/src/memory/CudaMemory.cu
+++ b/src/memory/CudaMemory.cu
@@ -55,7 +55,7 @@ void CudaMemory::destroy()
  *
  */
 
-void CudaMemory::allocate_host(size_t sz)
+void CudaMemory::allocate_host(size_t sz) const
 {
 	if (hm == NULL)
 	{
@@ -74,7 +74,7 @@ void CudaMemory::allocate_host(size_t sz)
  *	\param ptr
  *	\return true if success
  */
-bool CudaMemory::copyFromPointer(void * ptr)
+bool CudaMemory::copyFromPointer(const void * ptr)
 {
 	// check if we have a host buffer, if not allocate it
 
@@ -87,7 +87,7 @@ bool CudaMemory::copyFromPointer(void * ptr)
 
 	// memory copy
 
-	memcpy(ptr,dvp,sz);
+	memcpy(dvp,ptr,sz);
 
 	return true;
 }
@@ -100,7 +100,7 @@ bool CudaMemory::copyFromPointer(void * ptr)
  *
  * \return true is success
  */
-bool CudaMemory::copyDeviceToDevice(CudaMemory & m)
+bool CudaMemory::copyDeviceToDevice(const CudaMemory & m)
 {
 	//! The source buffer is too big to copy it
 
@@ -123,10 +123,10 @@ bool CudaMemory::copyDeviceToDevice(CudaMemory & m)
  * \param m a memory interface
  *
  */
-bool CudaMemory::copy(memory & m)
+bool CudaMemory::copy(const memory & m)
 {
 	//! Here we try to cast memory into OpenFPMwdeviceCudaMemory
-	CudaMemory * ofpm = dynamic_cast<CudaMemory *>(&m);
+	const CudaMemory * ofpm = dynamic_cast<const CudaMemory *>(&m);
 
 	//! if we fail we get the pointer and simply copy from the pointer
 
@@ -152,11 +152,12 @@ bool CudaMemory::copy(memory & m)
  *
  */
 
-size_t CudaMemory::size()
+size_t CudaMemory::size() const
 {
 	return sz;
 }
 
+
 /*! \brief Resize the allocated memory
  *
  * Resize the allocated memory, if request is smaller than the allocated memory
@@ -241,3 +242,28 @@ void * CudaMemory::getPointer()
 
 	return hm;
 }
+
+
+/*! \brief Return a readable pointer with your data
+ *
+ * Return a readable pointer with your data
+ *
+ */
+
+const void * CudaMemory::getPointer() const
+{
+	//| allocate an host memory if not allocated
+	if (hm == NULL)
+		allocate_host(sz);
+
+	//! if the host buffer is synchronized with the device buffer return the host buffer
+
+	if (is_hm_sync)
+		return hm;
+
+	//! copy from device to host memory
+
+	CUDA_SAFE_CALL(cudaMemcpy(hm,dm,sz,cudaMemcpyDeviceToHost));
+
+	return hm;
+}
diff --git a/src/memory/CudaMemory.cuh b/src/memory/CudaMemory.cuh
index f2af777..2bebb5d 100644
--- a/src/memory/CudaMemory.cuh
+++ b/src/memory/CudaMemory.cuh
@@ -46,13 +46,19 @@ class CudaMemory : public memory
 	void * dm;
 	
 	//! host memory
-	void * hm;
+	mutable void * hm;
 
 	//! Reference counter
 	size_t ref_cnt;
 	
 	//! Allocate an host buffer
-	void allocate_host(size_t sz);
+	void allocate_host(size_t sz) const;
+	
+	//! copy from GPU to GPU buffer directly
+	bool copyDeviceToDevice(const CudaMemory & m);
+	
+	//! copy from Pointer to GPU
+	bool copyFromPointer(const void * ptr);
 	
 public:
 	
@@ -63,17 +69,14 @@ public:
 	//! copy from a General device
 	virtual bool copy(const memory & m);
 	//! the the size of the allocated memory
-	virtual size_t size();
+	virtual size_t size() const;
 	//! resize the momory allocated
 	virtual bool resize(size_t sz);
 	//! get a readable pointer with the data
 	void * getPointer();
 	
-	//! copy from GPU to GPU buffer directly
-	bool copyDeviceToDevice(CudaMemory & m);
-	
-	//! copy from Pointer to GPU
-	bool copyFromPointer(void * ptr);
+	//! get a readable pointer with the data
+	virtual const void * getPointer() const;
 	
 	//! This function notify that the device memory is not sync with
 	//! the host memory, is called when a task is performed that write
-- 
GitLab