Skip to content
Snippets Groups Projects
Commit 2f254618 authored by Pietro Incardona's avatar Pietro Incardona
Browse files

Fixing CudaMemory for BMemory

parent e5d5d31a
No related branches found
No related tags found
No related merge requests found
Pipeline #460 failed
#!/bin/bash
mkdir ${HOME}/MPI
wget http://www.open-mpi.de/software/ompi/v1.8/downloads/openmpi-1.8.7.tar.bz2
tar -xvf openmpi-1.8.7.tar.bz2
cd openmpi-1.8.7
sh ./configure --prefix=${HOME}/MPI --enable-opal-multi-threads --enable-mpi-f90
make
make install
/* Coverty scan */
${DEFINE_COVERTY_SCAN}
/* GPU support */
${DEFINE_CUDA_GPU}
/* Debug */
${DEFINE_DEBUG} /**/
/* Debug */
${DEFINE_DEBUG_MODE} /**/
/* Define to dummy `main' function (if any) required to link to the Fortran
libraries. */
${DEFINE_F77_DUMMY_MAIN}
/* Define if F77 and FC dummy `main' functions are identical. */
${DEFINE_FC_DUMMY_MAIN_EQ_F77}
/* Define if you have a BLAS library. */
${DEFINE_HAVE_BLAS}
/* define if the Boost library is available */
${DEFINE_HAVE_BOOST}
/* define if the Boost::IOStreams library is available */
${DEFINE_HAVE_BOOST_IOSTREAMS} /**/
/* define if the Boost::PROGRAM_OPTIONS library is available */
${DEFINE_HAVE_BOOST_PROGRAM_OPTIONS} /**/
/* define if the Boost::Unit_Test_Framework library is available */
${DEFINE_HAVE_BOOST_UNIT_TEST_FRAMEWORK} /**/
/* Have clock time */
${DEFINE_HAVE_CLOCK_GETTIME} /**/
/* Define to 1 if you have the <dlfcn.h> header file. */
${DEFINE_HAVE_DLFCN_H}
/* Define if you have EIGEN library. */
${DEFINE_HAVE_EIGEN}
/* Define to 1 if you have the <Eigen/Dense> header file. */
${DEFINE_HAVE_EIGEN_DENSE}
/* Define to 1 if you have the <Eigen/LU> header file. */
${DEFINE_HAVE_EIGEN_LU}
/* Defined if you have HDF5 support */
${DEFINE_HAVE_HDF5}
/* Define to 1 if you have the <inttypes.h> header file. */
${DEFINE_HAVE_INTTYPES_H}
/* Define if you have LAPACK library */
${DEFINE_HAVE_LAPACK}
/* Define if you have LIBHILBERT library */
${DEFINE_HAVE_LIBHILBERT}
/* Have quad math lib */
${DEFINE_HAVE_LIBQUADMATH}
/* Define to 1 if you have the <memory.h> header file. */
${DEFINE_HAVE_MEMORY_H}
/* Define if you have METIS library */
${DEFINE_HAVE_METIS}
/* MPI Enabled */
${DEFINE_HAVE_MPI}
/* We have OSX */
${DEFINE_HAVE_OSX}
/* Define if you have PARMETIS library */
${DEFINE_HAVE_PARMETIS}
/* Define if you have PETSC library */
${DEFINE_HAVE_PETSC}
/* Define to 1 if you have the <stdint.h> header file. */
${DEFINE_HAVE_STDINT_H}
/* Define to 1 if you have the <stdlib.h> header file. */
${DEFINE_HAVE_STDLIB_H}
/* Define to 1 if you have the <strings.h> header file. */
${DEFINE_HAVE_STRINGS_H}
/* Define to 1 if you have the <string.h> header file. */
${DEFINE_HAVE_STRING_H}
/* Define if you have SUITESPARSE library. */
${DEFINE_HAVE_SUITESPARSE}
/* Define to 1 if you have the <sys/stat.h> header file. */
${DEFINE_HAVE_SYS_STAT_H}
/* Define to 1 if you have the <sys/types.h> header file. */
${DEFINE_HAVE_SYS_TYPES_H}
/* Define to 1 if you have the <unistd.h> header file. */
${DEFINE_HAVE_UNISTD_H}
/* Define to the sub-directory where libtool stores uninstalled libraries. */
#define LT_OBJDIR ".libs/"
/* NVCC compiling */
${DEFINE_NVCC} /**/
/* Name of package */
#define PACKAGE "openfpm_pdata"
/* Define to the address where bug reports for this package should be sent. */
#define PACKAGE_BUGREPORT "BUG-REPORT-ADDRESS"
/* Define to the full name of this package. */
#define PACKAGE_NAME "OpenFPM_pdata"
/* Define to the full name and version of this package. */
#define PACKAGE_STRING "OpenFPM_pdata 1.0.0"
/* Define to the one symbol short name of this package. */
#define PACKAGE_TARNAME "openfpm_pdata"
/* Define to the home page for this package. */
#define PACKAGE_URL ""
/* Define to the version of this package. */
#define PACKAGE_VERSION "1.0.0"
/* Test performance mode */
${DEFINE_PERFORMANCE_TEST}
/* Security enhancement class 1 */
${DEFINE_SE_CLASS1}
/* Security enhancement class 2 */
${DEFINE_SE_CLASS2}
/* Security enhancement class 3 */
${DEFINE_SE_CLASS3}
/* Define to 1 if you have the ANSI C header files. */
${DEFINE_STDC_HEADERS}
/* If an error occur stop the program */
${DEFINE_STOP_ON_ERROR}
/* Test coverage mode */
${DEFINE_TEST_COVERAGE_MODE}
/* when an error accur continue but avoid unsafe operation */
/* #undef THROW_ON_ERROR */
/* Version number of package */
#define VERSION "1.0.0"
......@@ -20,16 +20,14 @@ typedef unsigned char byte;
#define MEM_ALIGNMENT 32
/**
* \brief It is like HeapMemory but buffered
* \brief It override the behavior if size()
*
* The concept of buffer is different from allocated memory. The buffer size can be <= the allocated memory
* On normal memory like HeapMemory if you try to use resize to shrink the memory, nothing happen and size() return the old size.
* In case of BMemory<HeapMemory> if you try to shrink still the memory is not shrinked, but size() return the shrinked size.
* This gives a "feeling" of shrinkage. The real internal size can be retrieved with msize(). When we use resize to increase
* the memory size the behaviour remain the same as normal HeapMemory.
*
* It differs from HeapMemory in resize behavior.
*
* In the case of BHeapMemory if you try to shrink the memory nothing happen to the allocated memory.
* To destroy the internal memory you must use destroy.
*
* BHeapMemory does not shrink the memory, but it shrink the buffer size. size() always return the buffer size
* \note this wrapper can be used in combination also with CudaMemory
*
* ### Allocate memory
*
......@@ -58,7 +56,7 @@ public:
*
*/
BMemory(const BMemory<Memory> & mem)
:HeapMemory(mem),buf_sz(mem.size())
:Memory(mem),buf_sz(mem.size())
{
}
......@@ -182,7 +180,7 @@ public:
buf_sz = 0;
}
/*! \brief swap the two mwmory object
/*! \brief swap the two memory object
*
* \param mem Memory to swap with
*
......
......@@ -36,6 +36,14 @@ bool CudaMemory::allocate(size_t sz)
//! Allocate the device memory
if (dm == NULL)
{CUDA_SAFE_CALL(cudaMalloc(&dm,sz));}
else
{
if (sz != this->sz)
{
std::cout << __FILE__ << ":" << __LINE__ << " error FATAL: using allocate to resize the memory, please use resize." << std::endl;
return false;
}
}
this->sz = sz;
......@@ -192,19 +200,19 @@ size_t CudaMemory::size() const
bool CudaMemory::resize(size_t sz)
{
// if the allocated memory is enough, do not resize
if (sz <= size())
if (sz <= CudaMemory::size())
return true;
//! Allocate the device memory if not done yet
if (size() == 0)
return allocate(sz);
if (CudaMemory::size() == 0)
{return allocate(sz);}
//! Create a new buffer, if sz is bigger than the actual size
void * thm;
void * thm = NULL;
//! Create a new buffer, if sz is bigger than the actual size
void * tdm;
void * tdm = NULL;
if (dm != NULL)
{
......@@ -213,7 +221,7 @@ bool CudaMemory::resize(size_t sz)
//! copy from the old buffer to the new one
CUDA_SAFE_CALL(cudaMemcpy(tdm,dm,size(),cudaMemcpyDeviceToDevice));
CUDA_SAFE_CALL(cudaMemcpy(tdm,dm,CudaMemory::size(),cudaMemcpyDeviceToDevice));
}
if (hm != NULL)
......@@ -223,7 +231,7 @@ bool CudaMemory::resize(size_t sz)
//! copy from the old buffer to the new one
CUDA_SAFE_CALL(cudaMemcpy(thm,hm,size(),cudaMemcpyHostToHost));
CUDA_SAFE_CALL(cudaMemcpy(thm,hm,CudaMemory::size(),cudaMemcpyHostToHost));
}
//! free the old buffer
......@@ -252,10 +260,6 @@ void * CudaMemory::getPointer()
if (hm == NULL)
allocate_host(sz);
//! copy from device to host memory
CUDA_SAFE_CALL(cudaMemcpy(hm,dm,sz,cudaMemcpyDeviceToHost));
return hm;
}
......@@ -276,12 +280,40 @@ void CudaMemory::deviceToHost()
CUDA_SAFE_CALL(cudaMemcpy(hm,dm,sz,cudaMemcpyDeviceToHost));
}
/*! \brief It transfer to device memory from the host of another memory
*
* \param mem the other memory object
*
*/
void CudaMemory::hostToDevice(CudaMemory & mem)
{
// allocate an host memory if not allocated
if (mem.hm == NULL)
mem.allocate_host(sz);
if (mem.sz > sz)
{resize(mem.sz);}
//! copy from device to host memory
CUDA_SAFE_CALL(cudaMemcpy(dm,mem.hm,mem.sz,cudaMemcpyHostToDevice));
}
void CudaMemory::hostToDevice(size_t start, size_t stop)
{
// allocate an host memory if not allocated
if (hm == NULL)
allocate_host(sz);
//! copy from device to host memory
CUDA_SAFE_CALL(cudaMemcpy(((unsigned char *)dm)+start,((unsigned char *)hm)+start,(stop-start),cudaMemcpyHostToDevice));
}
/*! \brief Return a readable pointer with your data
*
* \return a readable pointer with your data
*
*/
void CudaMemory::deviceToHost(size_t start, size_t stop)
{
// allocate an host memory if not allocated
......@@ -307,10 +339,6 @@ const void * CudaMemory::getPointer() const
if (hm == NULL)
allocate_host(sz);
//! copy from device to host memory
CUDA_SAFE_CALL(cudaMemcpy(hm,dm,sz,cudaMemcpyDeviceToHost));
return hm;
}
......@@ -331,6 +359,17 @@ void CudaMemory::fill(unsigned char c)
*
*/
void * CudaMemory::getDevicePointer()
{
return dm;
}
/*! \brief Return a readable pointer with your data
*
* \return a readable pointer with your data
*
*/
void CudaMemory::hostToDevice()
{
// allocate an host memory if not allocated
if (hm == NULL)
......@@ -339,19 +378,8 @@ void * CudaMemory::getDevicePointer()
//! copy from device to host memory
CUDA_SAFE_CALL(cudaMemcpy(dm,hm,sz,cudaMemcpyHostToDevice));
return dm;
}
/*! \brief Return the CUDA device pointer (Do not copy to device)
*
* \return CUDA device pointer
*
*/
void * CudaMemory::getDevicePointerNoCopy()
{
return dm;
}
/*! \brief Swap the memory
*
......
......@@ -89,14 +89,19 @@ public:
//! get a readable pointer with the data
virtual void * getDevicePointer();
//! Move memory from host to device
virtual void hostToDevice();
//! Move memory from device to host
virtual void deviceToHost();
//! Move memory from device to host, just one chunk
//! Move memory from device to host, just the selected chunk
virtual void deviceToHost(size_t start, size_t stop);
//! get the device pointer, but do not copy the memory from host to device
virtual void * getDevicePointerNoCopy();
//! Move memory from host to device, just the selected chunk
virtual void hostToDevice(size_t start, size_t top);
void hostToDevice(CudaMemory & mem);
//! fill the buffer with a byte
virtual void fill(unsigned char c);
......
......@@ -187,16 +187,32 @@ public:
* \return the pointer
*
*/
virtual void * getDevicePointerNoCopy()
virtual void hostToDevice()
{
return mem->getDevicePointerNoCopy();
mem->hostToDevice();
}
/*! \brief Return the pointer of the last allocation
*
* \return the pointer
*
*/
virtual void hostToDevice(size_t start, size_t stop)
{
mem->hostToDevice(start,stop);
}
//! Do nothing
virtual void deviceToHost(){};
virtual void deviceToHost()
{
mem->deviceToHost();
};
//! Do nothing
virtual void deviceToHost(size_t start, size_t stop) {};
virtual void deviceToHost(size_t start, size_t stop)
{
mem->deviceToHost(start,stop);
};
/*! \brief Return the pointer of the last allocation
*
......
......@@ -39,9 +39,12 @@ bool HeapMemory::allocate(size_t sz)
{
//! Allocate the device memory
if (dm == NULL)
dmOrig = new byte[sz+alignement+extra_pad];
{dmOrig = new byte[sz+alignement+extra_pad];}
else
{
std::cerr << __FILE__ << ":" << __LINE__ << " error memory already allocated\n";
return false;
}
dm = dmOrig;
......@@ -241,17 +244,6 @@ void * HeapMemory::getDevicePointer()
return dm;
}
/*! \brief Return a readable pointer with your data
*
* Return a readable pointer with your data
*
*/
void * HeapMemory::getDevicePointerNoCopy()
{
return dm;
}
/*! \brief Return a readable pointer with your data
*
* Return a readable pointer with your data
......
......@@ -83,22 +83,24 @@ public:
//! get a device pointer for HeapMemory getPointer and getDevicePointer are equivalents
virtual void * getDevicePointer();
//! Get a device pointer for HeapMemory (avoid to copy from Host to device) for HeapMemory
//! getPointer() getDevicePointer and getDevicePointerNoCopy() are equivalents
virtual void * getDevicePointerNoCopy();
/*! \brief fill host and device memory with the selected byte
*
*
*/
virtual void fill(unsigned char c);
//! Do nothing
virtual void hostToDevice(){};
//! Do nothing
virtual void deviceToHost(){};
//! Do nothing
virtual void deviceToHost(size_t start, size_t stop) {};
//! Do nothing
virtual void hostToDevice(size_t start, size_t stop) {};
//! Increment the reference counter
virtual void incRef()
{ref_cnt++;}
......
......@@ -12,7 +12,7 @@
#include "memory/HeapMemory.hpp"
#include "memory/BHeapMemory.hpp"
#ifdef NVCC
#ifdef CUDA_GPU
#include "memory/CudaMemory.cuh"
#endif
......@@ -137,7 +137,12 @@ template<typename T> void Btest()
unsigned char * ptr = (unsigned char *)mem.getPointer();
for (size_t i = 0 ; i < mem.size() ; i++)
ptr[i] = i;
{ptr[i] = i;}
mem.hostToDevice();
mem.resize(0);
BOOST_REQUIRE_EQUAL(mem.size(),0);
BOOST_REQUIRE_EQUAL(mem.msize(),FIRST_ALLOCATION);
//! [BAllocate some memory and fill with data]
......@@ -158,6 +163,15 @@ template<typename T> void Btest()
BOOST_REQUIRE_EQUAL(ptr2[i],c);
}
// check that the data are retained on device
mem.deviceToHost();
for (size_t i = 0 ; i < FIRST_ALLOCATION ; i++)
{
unsigned char c = i;
BOOST_REQUIRE_EQUAL(ptr2[i],c);
}
//! [BShrink memory]
mem.resize(1);
......@@ -238,7 +252,7 @@ void copy_device_host_test()
{ptr1[i] = i;}
// force to move the memory from host to device
mem1.getDevicePointer();
mem1.hostToDevice();
mem2.copyDeviceToDevice(mem1);
mem2.deviceToHost();
......@@ -267,6 +281,11 @@ BOOST_AUTO_TEST_CASE( use_bheap_memory )
Btest<BMemory<HeapMemory>>();
}
BOOST_AUTO_TEST_CASE( use_bcuda_memory )
{
Btest<BMemory<CudaMemory>>();
}
BOOST_AUTO_TEST_CASE( swap_heap_memory )
{
Stest<HeapMemory>();
......
......@@ -174,16 +174,6 @@ void * PtrMemory::getDevicePointer()
return dm;
}
/*! \brief Return a pointer to the allocated memory
*
* \return the pointer
*
*/
void * PtrMemory::getDevicePointerNoCopy()
{
return dm;
}
/*! \brief Return a pointer to the allocated memory
*
......
......@@ -82,15 +82,19 @@ public:
//! get a readable pointer with the data
virtual void * getDevicePointer();
//! get a readable pointer with the data
virtual void * getDevicePointerNoCopy();
//! Do nothing
virtual void deviceToHost(){};
//! Do nothing
virtual void hostToDevice(){};
//! Do nothing
virtual void deviceToHost(size_t start, size_t stop) {};
//! Do nothing
virtual void hostToDevice(size_t start, size_t top) {};
/*! \brief fill memory with the selected byte
*
*
......
......@@ -131,6 +131,20 @@ class memory
*/
virtual void deviceToHost() = 0;
/*! \brief Copy the memory from host to device
*
*
*/
virtual void hostToDevice() = 0;
/*! \brief Copy the memory from host to device
*
* \param start
* \param stop
*
*/
virtual void hostToDevice(size_t start, size_t top) = 0;
/*! \brief Copy the memory from device to host
*
* \param start from start
......@@ -139,11 +153,6 @@ class memory
*/
virtual void deviceToHost(size_t start, size_t stop) = 0;
/*! \brief Get the device pointer (Do not copy from host to device)
*
*
*/
virtual void * getDevicePointerNoCopy() = 0;
/*! \brief Fill the buffer with a particular byte
*
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment