Add methods to copy data between host and device

This commit is contained in:
Tom Deakin 2016-04-26 15:02:41 +01:00
parent ae679a5775
commit 8e534daf8b
4 changed files with 29 additions and 1 deletions

View File

@ -23,6 +23,23 @@ CUDAStream<T>::CUDAStream(const unsigned int ARRAY_SIZE)
check_error(); check_error();
} }
template <class T>
void CUDAStream<T>::write_arrays(const std::vector<T>& a, const std::vector<T>& b, const std::vector<T>& c)
{
// Copy host memory to device
cudaMemcpy(d_a, a.data(), a.size()*sizeof(T), cudaMemcpyHostToDevice);
check_error();
cudaMemcpy(d_b, b.data(), b.size()*sizeof(T), cudaMemcpyHostToDevice);
check_error();
cudaMemcpy(d_c, c.data(), c.size()*sizeof(T), cudaMemcpyHostToDevice);
check_error();
}
template <class T>
void CUDAStream<T>::read_arrays(std::vector<T>& a, std::vector<T>& b, std::vector<T>& c)
{
}
template <typename T> template <typename T>
__global__ void copy_kernel(const T * a, T * c) __global__ void copy_kernel(const T * a, T * c)
{ {
@ -34,6 +51,9 @@ template <class T>
void CUDAStream<T>::copy() void CUDAStream<T>::copy()
{ {
copy_kernel<<<1024, 1024>>>(d_a, d_c); copy_kernel<<<1024, 1024>>>(d_a, d_c);
check_error();
cudaDeviceSynchronize();
check_error();
} }
template <class T> template <class T>

View File

@ -22,4 +22,8 @@ class CUDAStream : public Stream<T>
void mul(); void mul();
void triad(); void triad();
void write_arrays(const std::vector<T>& a, const std::vector<T>& b, const std::vector<T>& c);
void read_arrays(std::vector<T>& a, std::vector<T>& b, std::vector<T>& c);
}; };

View File

@ -14,7 +14,9 @@ class Stream
virtual void add() = 0; virtual void add() = 0;
virtual void triad() = 0; virtual void triad() = 0;
// Copy memory between host and device
virtual void write_arrays(const std::vector<T>& a, const std::vector<T>& b, const std::vector<T>& c) = 0;
virtual void read_arrays(std::vector<T>& a, std::vector<T>& b, std::vector<T>& c) = 0;
// Implementation specific device functions // Implementation specific device functions
static std::vector<int> getDeviceList(); static std::vector<int> getDeviceList();

View File

@ -29,6 +29,8 @@ int main(int argc, char *argv[])
// Use the CUDA implementation // Use the CUDA implementation
stream = new CUDAStream<double>(ARRAY_SIZE); stream = new CUDAStream<double>(ARRAY_SIZE);
stream->write_arrays(a, b, c);
stream->copy(); stream->copy();
delete[] stream; delete[] stream;