StorageVirtual.cxx 9.57 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
//============================================================================
//  Copyright (c) Kitware, Inc.
//  All rights reserved.
//  See LICENSE.txt for details.
//  This software is distributed WITHOUT ANY WARRANTY; without even
//  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
//  PURPOSE.  See the above copyright notice for more information.
//
//  Copyright 2014 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
//  Copyright 2014 UT-Battelle, LLC.
//  Copyright 2014 Los Alamos National Security.
//
//  Under the terms of Contract DE-NA0003525 with NTESS,
//  the U.S. Government retains certain rights in this software.
//
//  Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
//  Laboratory (LANL), the U.S. Government retains certain rights in
//  this software.
//============================================================================
20
#define vtk_m_cont_StorageVirtual_cxx
21 22 23 24 25 26 27 28 29 30
#include "StorageVirtual.h"

#include <vtkm/cont/internal/DeviceAdapterError.h>

namespace vtkm
{
namespace cont
{
namespace internal
{
31 32
namespace detail
{
33 34 35


//--------------------------------------------------------------------
36
StorageVirtual::StorageVirtual(const StorageVirtual& src)
37 38 39 40 41 42 43
  : HostUpToDate(src.HostUpToDate)
  , DeviceUpToDate(src.DeviceUpToDate)
  , DeviceTransferState(src.DeviceTransferState)
{
}

//--------------------------------------------------------------------
44
StorageVirtual::StorageVirtual(StorageVirtual&& src) noexcept
45 46 47 48 49 50 51
  : HostUpToDate(src.HostUpToDate),
    DeviceUpToDate(src.DeviceUpToDate),
    DeviceTransferState(std::move(src.DeviceTransferState))
{
}

//--------------------------------------------------------------------
52
StorageVirtual& StorageVirtual::operator=(const StorageVirtual& src)
53 54 55 56 57 58 59 60
{
  this->HostUpToDate = src.HostUpToDate;
  this->DeviceUpToDate = src.DeviceUpToDate;
  this->DeviceTransferState = src.DeviceTransferState;
  return *this;
}

//--------------------------------------------------------------------
61
StorageVirtual& StorageVirtual::operator=(StorageVirtual&& src) noexcept
62 63 64 65 66 67 68 69
{
  this->HostUpToDate = src.HostUpToDate;
  this->DeviceUpToDate = src.DeviceUpToDate;
  this->DeviceTransferState = std::move(src.DeviceTransferState);
  return *this;
}

//--------------------------------------------------------------------
70
StorageVirtual::~StorageVirtual()
71 72 73 74
{
}

//--------------------------------------------------------------------
75
void StorageVirtual::DropExecutionPortal()
76 77 78 79 80 81
{
  this->DeviceTransferState->releaseDevice();
  this->DeviceUpToDate = false;
}

//--------------------------------------------------------------------
82
void StorageVirtual::DropAllPortals()
83 84 85 86 87 88 89
{
  this->DeviceTransferState->releaseAll();
  this->HostUpToDate = false;
  this->DeviceUpToDate = false;
}

//--------------------------------------------------------------------
90
std::unique_ptr<StorageVirtual> StorageVirtual::NewInstance() const
91 92 93 94 95
{
  return this->MakeNewInstance();
}

//--------------------------------------------------------------------
96
const vtkm::internal::PortalVirtualBase* StorageVirtual::PrepareForInput(
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
  vtkm::cont::DeviceAdapterId devId) const
{
  if (devId == vtkm::cont::DeviceAdapterTagUndefined())
  {
    throw vtkm::cont::ErrorBadValue("device should not be VTKM_DEVICE_ADAPTER_UNDEFINED");
  }
  if (devId == vtkm::cont::DeviceAdapterTagError())
  {
    throw vtkm::cont::ErrorBadValue("device should not be VTKM_DEVICE_ADAPTER_ERROR");
  }

  const bool needsUpload = !(this->DeviceTransferState->valid(devId) && this->DeviceUpToDate);

  if (needsUpload)
  { //Either transfer state is pointing to another device, or has
    //had the execution resources released. Either way we
    //need to re-transfer the execution information
    auto* payload = this->DeviceTransferState.get();
    this->TransferPortalForInput(*payload, devId);
    this->DeviceUpToDate = true;
  }
  return this->DeviceTransferState->devicePtr();
}

//--------------------------------------------------------------------
122 123 124
const vtkm::internal::PortalVirtualBase* StorageVirtual::PrepareForOutput(
  vtkm::Id numberOfValues,
  vtkm::cont::DeviceAdapterId devId)
125 126 127 128 129 130 131 132 133 134 135 136 137
{
  if (devId == vtkm::cont::DeviceAdapterTagUndefined())
  {
    throw vtkm::cont::ErrorBadValue("device should not be VTKM_DEVICE_ADAPTER_UNDEFINED");
  }
  if (devId == vtkm::cont::DeviceAdapterTagError())
  {
    throw vtkm::cont::ErrorBadValue("device should not be VTKM_DEVICE_ADAPTER_ERROR");
  }

  const bool needsUpload = !(this->DeviceTransferState->valid(devId) && this->DeviceUpToDate);
  if (needsUpload)
  {
138 139 140 141 142 143 144 145 146
    this->TransferPortalForOutput(
      *(this->DeviceTransferState), OutputMode::WRITE, numberOfValues, devId);
    this->HostUpToDate = false;
    this->DeviceUpToDate = true;
  }
  return this->DeviceTransferState->devicePtr();
}

//--------------------------------------------------------------------
147 148
const vtkm::internal::PortalVirtualBase* StorageVirtual::PrepareForInPlace(
  vtkm::cont::DeviceAdapterId devId)
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
{
  if (devId == vtkm::cont::DeviceAdapterTagUndefined())
  {
    throw vtkm::cont::ErrorBadValue("device should not be VTKM_DEVICE_ADAPTER_UNDEFINED");
  }
  if (devId == vtkm::cont::DeviceAdapterTagError())
  {
    throw vtkm::cont::ErrorBadValue("device should not be VTKM_DEVICE_ADAPTER_ERROR");
  }

  const bool needsUpload = !(this->DeviceTransferState->valid(devId) && this->DeviceUpToDate);
  if (needsUpload)
  {
    vtkm::Id numberOfValues = this->GetNumberOfValues();
    this->TransferPortalForOutput(
      *(this->DeviceTransferState), OutputMode::READ_WRITE, numberOfValues, devId);
165 166 167 168 169 170 171
    this->HostUpToDate = false;
    this->DeviceUpToDate = true;
  }
  return this->DeviceTransferState->devicePtr();
}

//--------------------------------------------------------------------
172
const vtkm::internal::PortalVirtualBase* StorageVirtual::GetPortalControl()
173 174 175 176 177 178 179 180 181 182 183 184 185 186
{
  if (!this->HostUpToDate)
  {
    //we need to prepare for input and grab the host ptr
    auto* payload = this->DeviceTransferState.get();
    this->ControlPortalForOutput(*payload);
  }

  this->DeviceUpToDate = false;
  this->HostUpToDate = true;
  return this->DeviceTransferState->hostPtr();
}

//--------------------------------------------------------------------
187
const vtkm::internal::PortalVirtualBase* StorageVirtual::GetPortalConstControl() const
188 189 190 191 192 193 194 195 196 197 198 199
{
  if (!this->HostUpToDate)
  {
    //we need to prepare for input and grab the host ptr
    vtkm::cont::internal::TransferInfoArray* payload = this->DeviceTransferState.get();
    this->ControlPortalForInput(*payload);
  }
  this->HostUpToDate = true;
  return this->DeviceTransferState->hostPtr();
}

//--------------------------------------------------------------------
200
DeviceAdapterId StorageVirtual::GetDeviceAdapterId() const noexcept
201 202 203 204 205
{
  return this->DeviceTransferState->deviceId();
}

//--------------------------------------------------------------------
206
void StorageVirtual::ControlPortalForOutput(vtkm::cont::internal::TransferInfoArray&)
207
{
208 209
  throw vtkm::cont::ErrorBadValue(
    "StorageTagVirtual by default doesn't support control side writes.");
210 211 212
}

//--------------------------------------------------------------------
213 214 215 216
void StorageVirtual::TransferPortalForOutput(vtkm::cont::internal::TransferInfoArray&,
                                             OutputMode,
                                             vtkm::Id,
                                             vtkm::cont::DeviceAdapterId)
217
{
218
  throw vtkm::cont::ErrorBadValue("StorageTagVirtual by default doesn't support exec side writes.");
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

#define VTK_M_ARRAY_TRANSFER_VIRTUAL_INSTANTIATE(T)                                                \
  template class VTKM_CONT_EXPORT ArrayTransferVirtual<T>;                                         \
  template class VTKM_CONT_EXPORT ArrayTransferVirtual<vtkm::Vec<T, 2>>;                           \
  template class VTKM_CONT_EXPORT ArrayTransferVirtual<vtkm::Vec<T, 3>>;                           \
  template class VTKM_CONT_EXPORT ArrayTransferVirtual<vtkm::Vec<T, 4>>

VTK_M_ARRAY_TRANSFER_VIRTUAL_INSTANTIATE(char);
VTK_M_ARRAY_TRANSFER_VIRTUAL_INSTANTIATE(vtkm::Int8);
VTK_M_ARRAY_TRANSFER_VIRTUAL_INSTANTIATE(vtkm::UInt8);
VTK_M_ARRAY_TRANSFER_VIRTUAL_INSTANTIATE(vtkm::Int16);
VTK_M_ARRAY_TRANSFER_VIRTUAL_INSTANTIATE(vtkm::UInt16);
VTK_M_ARRAY_TRANSFER_VIRTUAL_INSTANTIATE(vtkm::Int32);
VTK_M_ARRAY_TRANSFER_VIRTUAL_INSTANTIATE(vtkm::UInt32);
VTK_M_ARRAY_TRANSFER_VIRTUAL_INSTANTIATE(vtkm::Int64);
VTK_M_ARRAY_TRANSFER_VIRTUAL_INSTANTIATE(vtkm::UInt64);
VTK_M_ARRAY_TRANSFER_VIRTUAL_INSTANTIATE(vtkm::Float32);
VTK_M_ARRAY_TRANSFER_VIRTUAL_INSTANTIATE(vtkm::Float64);

#undef VTK_M_ARRAY_TRANSFER_VIRTUAL_INSTANTIATE

#define VTK_M_STORAGE_VIRTUAL_INSTANTIATE(T)                                                       \
  template class VTKM_CONT_EXPORT StorageVirtualImpl<T, VTKM_DEFAULT_STORAGE_TAG>;                 \
  template class VTKM_CONT_EXPORT StorageVirtualImpl<vtkm::Vec<T, 2>, VTKM_DEFAULT_STORAGE_TAG>;   \
  template class VTKM_CONT_EXPORT StorageVirtualImpl<vtkm::Vec<T, 3>, VTKM_DEFAULT_STORAGE_TAG>;   \
  template class VTKM_CONT_EXPORT StorageVirtualImpl<vtkm::Vec<T, 4>, VTKM_DEFAULT_STORAGE_TAG>

VTK_M_STORAGE_VIRTUAL_INSTANTIATE(char);
VTK_M_STORAGE_VIRTUAL_INSTANTIATE(vtkm::Int8);
VTK_M_STORAGE_VIRTUAL_INSTANTIATE(vtkm::UInt8);
VTK_M_STORAGE_VIRTUAL_INSTANTIATE(vtkm::Int16);
VTK_M_STORAGE_VIRTUAL_INSTANTIATE(vtkm::UInt16);
VTK_M_STORAGE_VIRTUAL_INSTANTIATE(vtkm::Int32);
VTK_M_STORAGE_VIRTUAL_INSTANTIATE(vtkm::UInt32);
VTK_M_STORAGE_VIRTUAL_INSTANTIATE(vtkm::Int64);
VTK_M_STORAGE_VIRTUAL_INSTANTIATE(vtkm::UInt64);
VTK_M_STORAGE_VIRTUAL_INSTANTIATE(vtkm::Float32);
VTK_M_STORAGE_VIRTUAL_INSTANTIATE(vtkm::Float64);

#undef VTK_M_STORAGE_VIRTUAL_INSTANTIATE
260 261 262
}
}
}
263
} // namespace vtkm::cont::internal::detail