1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
/*
* -----------------------------------------------------------------
* $Revision$
* $Date$
* -----------------------------------------------------------------
* Programmer(s): Slaven Peles @ LLNL
* -----------------------------------------------------------------
* LLNS Copyright Start
* Copyright (c) 2014, Lawrence Livermore National Security
* This work was performed under the auspices of the U.S. Department
* of Energy by Lawrence Livermore National Laboratory in part under
* Contract W-7405-Eng-48 and in part under Contract DE-AC52-07NA27344.
* Produced at the Lawrence Livermore National Laboratory.
* All rights reserved.
* For details, see the LICENSE file.
* LLNS Copyright End
* -----------------------------------------------------------------
*/
/**
* Vector class
*
* Manages vector data layout for CUDA implementation of N_Vector.
*
*/
#ifndef _NVECTOR_HPP_
#define _NVECTOR_HPP_
#include <cstdlib>
#include <iostream>
#include <cuda_runtime.h>
#include "ThreadPartitioning.hpp"
#include <nvector/nvector_cuda.h>
namespace suncudavec
{
template <typename T, typename I>
class Vector : public _N_VectorContent_Cuda
{
public:
Vector(I N)
: size_(N),
mem_size_(N*sizeof(T)),
ownPartitioning_(true)
{
// Set partitioning
partStream_ = new StreamPartitioning<T, I>(N, 256);
partReduce_ = new ReducePartitioning<T, I>(N, 256);
allocate();
}
/// Copy constructor does not copy values
explicit Vector(const Vector& v)
: size_(v.size()),
mem_size_(size_*sizeof(T)),
partStream_(v.partStream_),
partReduce_(v.partReduce_),
ownPartitioning_(false)
{
allocate();
}
~Vector()
{
if (ownPartitioning_)
{
delete partReduce_;
delete partStream_;
}
clear();
}
void allocate()
{
cudaError_t err;
h_vec_ = static_cast<T*>(malloc(mem_size_));
if(h_vec_ == NULL)
std::cerr << "Failed to allocate host vector!\n";
err = cudaMalloc((void**) &d_vec_, mem_size_);
if(err != cudaSuccess)
std::cerr << "Failed to allocate device vector (error code " << err << ")!\n";
}
void clear()
{
free(h_vec_);
cudaError_t err = cudaFree(d_vec_);
if(err != cudaSuccess)
std::cerr << "Failed to free device vector (error code " << err << ")!\n";
}
int size() const
{
return size_;
}
T* host()
{
return h_vec_;
}
const T* host() const
{
return h_vec_;
}
T* device()
{
return d_vec_;
}
const T* device() const
{
return d_vec_;
}
void copyToDev()
{
cudaError_t err = cudaMemcpy(d_vec_, h_vec_, mem_size_, cudaMemcpyHostToDevice);
if(err != cudaSuccess)
std::cerr << "Failed to copy vector from host to device (error code " << err << ")!\n";
}
void copyFromDev()
{
cudaError_t err = cudaMemcpy(h_vec_, d_vec_, mem_size_, cudaMemcpyDeviceToHost);
if(err != cudaSuccess)
std::cerr << "Failed to copy vector from device to host (error code " << err << ")!\n";
}
StreamPartitioning<T, I>& partStream() const
{
return *partStream_;
}
ReducePartitioning<T, I>& partReduce() const
{
return *partReduce_;
}
private:
I size_;
I mem_size_;
T* h_vec_;
T* d_vec_;
StreamPartitioning<T, I>* partStream_;
ReducePartitioning<T, I>* partReduce_;
bool ownPartitioning_;
};
// Vector extractor
template <typename T, typename I>
inline Vector<T, I> *extract(N_Vector v)
{
return static_cast<Vector<T, I>*>(v->content);
}
// Get Vector device data
template <typename T, typename I>
inline T *getDevData(N_Vector v)
{
Vector<T,I> *vp = static_cast<Vector<T, I>*>(v->content);
return vp->device();
}
// Get Vector length
template <typename T, typename I>
inline I getSize(N_Vector v)
{
Vector<T,I> *vp = static_cast<Vector<T, I>*>(v->content);
return vp->size();
}
} // namespace suncudavec
#endif // _NVECTOR_HPP_
|