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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
|
// MIT License
//
// Copyright (c) 2019 Jonathan R. Madsen
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
//
#pragma once
#include "common.hh"
#include "constants.hh"
#include "macros.hh"
#include "rotate_utils.hh"
#include "typedefs.hh"
//======================================================================================//
class CpuData
{
public:
typedef std::shared_ptr<CpuData> data_ptr_t;
typedef std::vector<data_ptr_t> data_array_t;
typedef std::tuple<data_array_t, float*, const float*> init_data_t;
public:
CpuData(unsigned id, int dy, int dt, int dx, int nx, int ny, const float* data,
float* recon, float* update, Mutex* upd_mutex, Mutex* sum_mutex)
: m_id(id)
, m_dy(dy)
, m_dt(dt)
, m_dx(dx)
, m_nx(nx)
, m_ny(ny)
, m_rot(farray_t(scast<uintmax_t>(m_nx * m_ny), 0.0f))
, m_tmp(farray_t(scast<uintmax_t>(m_nx * m_ny), 0.0f))
, m_update(update)
, m_recon(recon)
, m_data(data)
, m_upd_mutex(upd_mutex)
, m_sum_mutex(sum_mutex)
{
// we don't want null pointers here
assert(m_upd_mutex && m_sum_mutex);
}
~CpuData() {}
public:
farray_t& rot() { return m_rot; }
farray_t& tmp() { return m_tmp; }
const farray_t& rot() const { return m_rot; }
const farray_t& tmp() const { return m_tmp; }
float* update() const { return m_update; }
float* recon() { return m_recon; }
const float* recon() const { return m_recon; }
const float* data() const { return m_data; }
Mutex* upd_mutex() const { return m_upd_mutex; }
Mutex* sum_mutex() const { return m_sum_mutex; }
void reset()
{
// reset temporaries to zero (NECESSARY!)
// -- note: the OpenCV effectively ensures that we overwrite all values
// because we use cv::Mat::zeros and copy that to destination
memset(m_rot.data(), 0, scast<uintmax_t>(m_nx * m_ny) * sizeof(float));
memset(m_tmp.data(), 0, scast<uintmax_t>(m_nx * m_ny) * sizeof(float));
}
public:
// static functions
static init_data_t initialize(unsigned nthreads, int dy, int dt, int dx, int ngridx,
int ngridy, float* recon, const float* data,
float* update, Mutex* upd_mtx, Mutex* sum_mtx)
{
data_array_t cpu_data(nthreads);
for(unsigned ii = 0; ii < nthreads; ++ii)
{
cpu_data[ii] = data_ptr_t(new CpuData(ii, dy, dt, dx, ngridx, ngridy, data,
recon, update, upd_mtx, sum_mtx));
}
return init_data_t(cpu_data, recon, data);
}
static void reset(data_array_t& data)
{
// reset "update" to zero
for(auto& itr : data)
itr->reset();
}
protected:
unsigned m_id;
int m_dy;
int m_dt;
int m_dx;
int m_nx;
int m_ny;
farray_t m_rot;
farray_t m_tmp;
float* m_update;
float* m_recon;
const float* m_data;
Mutex* m_upd_mutex;
Mutex* m_sum_mutex;
};
//======================================================================================//
#if defined(__NVCC__) && defined(PTL_USE_CUDA)
//======================================================================================//
class GpuData
{
public:
// typedefs
typedef GpuData this_type;
typedef std::shared_ptr<GpuData> data_ptr_t;
typedef std::vector<data_ptr_t> data_array_t;
typedef std::tuple<data_array_t, float*, float*> init_data_t;
public:
// ctors, dtors, assignment
GpuData(int device, int id, int dy, int dt, int dx, int nx, int ny, const float* data,
float* recon, float* update)
: m_device(device)
, m_id(id)
, m_grid(GetGridSize())
, m_block(GetBlockSize())
, m_dy(dy)
, m_dt(dt)
, m_dx(dx)
, m_nx(nx)
, m_ny(ny)
, m_rot(nullptr)
, m_tmp(nullptr)
, m_update(update)
, m_recon(recon)
, m_data(data)
{
cuda_set_device(m_device);
m_streams = create_streams(m_num_streams, cudaStreamNonBlocking);
m_rot = gpu_malloc<float>(m_dy * m_nx * m_ny);
m_tmp = gpu_malloc<float>(m_dy * m_nx * m_ny);
}
~GpuData()
{
cudaFree(m_rot);
cudaFree(m_tmp);
destroy_streams(m_streams, m_num_streams);
}
GpuData(const this_type&) = delete;
GpuData(this_type&&) = default;
this_type& operator=(const this_type&) = delete;
this_type& operator=(this_type&&) = default;
public:
// access functions
int device() const { return m_device; }
int grid() const { return compute_grid(m_dx); }
int block() const { return m_block; }
float* rot() const { return m_rot; }
float* tmp() const { return m_tmp; }
float* update() const { return m_update; }
float* recon() { return m_recon; }
const float* recon() const { return m_recon; }
const float* data() const { return m_data; }
cudaStream_t stream(int n = 0) { return m_streams[n % m_num_streams]; }
public:
// assistant functions
int compute_grid(int size) const
{
return (m_grid < 1) ? ((size + m_block - 1) / m_block) : m_grid;
}
void sync(int stream_id = -1)
{
auto _sync = [&](cudaStream_t _stream) { stream_sync(_stream); };
if(stream_id < 0)
for(int i = 0; i < m_num_streams; ++i)
_sync(m_streams[i]);
else
_sync(m_streams[stream_id % m_num_streams]);
}
void reset()
{
// reset destination arrays (NECESSARY!)
gpu_memset<float>(m_rot, 0, m_dy * m_nx * m_ny, *m_streams);
gpu_memset<float>(m_tmp, 0, m_dy * m_nx * m_ny, *m_streams);
}
public:
// static functions
static init_data_t initialize(int device, int nthreads, int dy, int dt, int dx,
int ngridx, int ngridy, float* cpu_recon,
const float* cpu_data, float* update)
{
uintmax_t nstreams = 2;
auto streams = create_streams(nstreams, cudaStreamNonBlocking);
float* recon =
gpu_malloc_and_memcpy<float>(cpu_recon, dy * ngridx * ngridy, streams[0]);
float* data = gpu_malloc_and_memcpy<float>(cpu_data, dy * dt * dx, streams[1]);
data_array_t gpu_data(nthreads);
for(int ii = 0; ii < nthreads; ++ii)
{
gpu_data[ii] = data_ptr_t(
new GpuData(device, ii, dy, dt, dx, ngridx, ngridy, data, recon, update));
}
// synchronize and destroy
destroy_streams(streams, nstreams);
return init_data_t(gpu_data, recon, data);
}
static void reset(data_array_t& data)
{
// reset "update" to zero
for(auto& itr : data)
itr->reset();
}
static void sync(data_array_t& data)
{
// sync all the streams
for(auto& itr : data)
itr->sync();
}
protected:
// data
int m_device;
int m_id;
int m_grid;
int m_block;
int m_dy;
int m_dt;
int m_dx;
int m_nx;
int m_ny;
float* m_rot;
float* m_tmp;
float* m_update;
float* m_recon;
const float* m_data;
int m_num_streams = 1;
cudaStream_t* m_streams = nullptr;
};
#endif // NVCC and PTL_USE_CUDA
//======================================================================================//
|