he Host class is responsible for allocating/deallocating a block of memory on
host. The motivation for existence is discussed in the previous section. The
following is a partial code.
template
<typename dataType>
class
Host
{
public:
typedef
typename Index::type index;
private:
class
Impl : boost::noncopyable
{
private:
index
theSize;
dataType*
theData;
void
init( index size );
void
destroy();
public:
Impl()
: theSize(0),theData(NULL) {}
explicit
Impl( index size ) : theSize(0),theData(NULL) { init(size); }
explicit
Impl( const boost::python::object& x ) : theSize(0),theData(NULL) {
init(x); }
~Impl()
{ destroy(); }
void
put( index at, dataType x );
dataType
get( index at ) const;
void
copyFrom( const Device<dataType>& x )
};
boost::shared_ptr<Impl>
theImpl;
public:
explicit
Host( index size ) : theImpl(new Impl(size)) {}
Host()
: theImpl(new Impl()) {}
Host(
const Host& x ) : theImpl(x.theImpl) {}
Host&
operator=( const Host& x ) { theImpl=x.theImpl; return *this; }
void
copyFrom( const Host<dataType>& x ) { theImpl=x.theImpl; }
void
copyFrom( const Device<dataType>& x ) { theImpl->copyFrom(x);
}
void
put( index at, dataType x ) { theImpl->put(at,x); }
dataType
get( index at ) const { return theImpl->get(at); }
void
dispose() { theImpl=boost::shared_ptr<Impl>(new Impl()); }
};
This is a standard bridge pattern implementation of a handler to be passed by
value. The pointer theData holds a host-based address of a memory block.
We use this class to pass values into the function Device::copyFrom. The
alternative use is to read data from Device by using the function
Host::copyFrom.
|