Edited device class copy functions, and related documentation

main
Kenneth Jao 2 years ago
parent 1f2794cfa3
commit 310a60020b
  1. 4
      BLAS.h
  2. 16
      Macros.h
  3. 4
      docs/source/core.rst
  4. 14
      docs/source/usage.rst
  5. 2
      samples/2_CoreClass/main.cu.cpp
  6. 2
      tests.cu.cpp

@ -304,8 +304,8 @@ StreamID GEMV(const T alpha, const Array<T>& A, const Array<T>& x, const T beta,
* Computes the matrix-matrix product: \f$ C = \alpha AB + \beta C \f$. It will automatically
* broadcast the operation if applicable.
*/
template <typename T, typename U, typename V>
StreamID GEMM(const T alpha, const Array<U>& A, const Array<U>& B, const T beta, const Array<V>& C,
template <typename T>
StreamID GEMM(const T alpha, const Array<T>& A, const Array<T>& B, const T beta, const Array<T>& C,
const StreamID& stream = DEF_CUBLAS_STREAM) {
BatchInfo bi = Check::isBroadcastable(A, B, C, "A", "B", "C");

@ -67,7 +67,7 @@
#define BASIC_LOOP(N)
/**
* \def DEVICE_CLASS(name)
* \def DEVICE_COPY(name)
* Can be used inside a class declaration (header) which generates boilerplate code to allow this
* class to be used on the device.
*
@ -76,6 +76,8 @@
*
* void allocateDevice(): allocates the memory on the device for this class instance.
*
* void freeDevice(): frees the memory on the device for this class instance.
*
* CudaTools::StreamID updateHost(const CudaTools::StreamID& stream): updates the host instance
* of the class.
*
@ -83,7 +85,7 @@
* the device instance of the class.
* \param name the name of the class
*/
#define DEVICE_CLASS(name)
#define DEVICE_COPY(name)
/**
* \def CT_ERROR_IF(a, op, b, msg)
@ -175,22 +177,24 @@
return CudaTools::copy(this, that(), sizeof(name)); \
}
#ifdef CUDA
#ifdef CUDACC
#define DEVICE_CLASS(name) \
#define DEVICE_COPY(name) \
private: \
name* __deviceInstance__ = nullptr; \
\
public: \
inline name* that() { return __deviceInstance__; } \
inline name* that() { return __deviceInstance__; }; \
inline void allocateDevice() { __deviceInstance__ = (name*)CudaTools::malloc(sizeof(name)); }; \
inline void freeDevice() { CudaTools::free(__deviceInstance__); }; \
UPDATE_FUNC(name)
#else
#define DEVICE_CLASS(name) \
#define DEVICE_COPY(name) \
public: \
inline name* that() { return this; }; \
inline void allocateDevice(){}; \
inline void freeDevice(){}; \
UPDATE_FUNC(name)
#endif

@ -53,10 +53,10 @@ Device Helpers
.. doxygendefine:: BASIC_LOOP
Device Class
Device Copy
------------
.. doxygendefine:: DEVICE_CLASS
.. doxygendefine:: DEVICE_COPY
Memory Functions

@ -61,7 +61,7 @@ being device-compatible. We follow the previous example in a similar fashion.
.. code-block:: cpp
class intPair {
DEVICE_CLASS(intPair)
DEVICE_COPY(intPair)
public:
int x, y;
@ -92,11 +92,13 @@ being device-compatible. We follow the previous example in a similar fashion.
return 0;
}
In this example, we create a class called ``intPair``, which is then made available on the device through
the ``DEVICE_CLASS(name)`` macro. Specifically, that macro introduces a few functions, like
``allocateDevice()``, ``updateDevice()``, ``updateHost()``, and ``that()``. The ``that()`` function
returns a pointer to the copy on the device. As a result, the programmer **must** define a destructor
that frees the pointer using ``CudaTools::free(that)``. For more details, see :ref:`here <Device Class>`.
In this example, we create a class called ``intPair``, and enable device-copying functions through
the ``DEVICE_COPY(name)`` macro. This is not necessary for a class or struct to be available on the device, as we can always pass objects through the kernel function arguments. This is useful to prevent constant copying, and potentially separating class copies between host and device.
The aforementioned macro introduces a few functions, like
``allocateDevice()``, ``freeDevice()``, ``updateDevice()``, ``updateHost()``, and ``that()``.
The ``that()`` function returns a pointer to the copy on the device. As a result when using this, the programmer
**must** define a destructor that frees the pointer using ``freeDevice()``. For more details, see :ref:`here <Device Copy>`.
.. warning::
The ``updateDevice()`` and ``updateHost()`` in most cases will need to be explicitly called

@ -2,7 +2,7 @@
#include <Core.h>
class intPair {
DEVICE_CLASS(intPair)
DEVICE_COPY(intPair)
public:
int x, y;

@ -88,7 +88,7 @@ CT::Shape makeRandom2DShape() {
///////////
class TestClass {
DEVICE_CLASS(TestClass);
DEVICE_COPY(TestClass);
public:
int x;

Loading…
Cancel
Save