CellSetPermutation.h 9.83 KB
Newer Older
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
//============================================================================
//  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 2015 Sandia Corporation.
//  Copyright 2015 UT-Battelle, LLC.
//  Copyright 2015 Los Alamos National Security.
//
//  Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
//  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.
//============================================================================
#ifndef vtk_m_cont_CellSetPermutation_h
#define vtk_m_cont_CellSetPermutation_h

#include <vtkm/CellShape.h>
#include <vtkm/CellTraits.h>
#include <vtkm/cont/ArrayHandlePermutation.h>
#include <vtkm/cont/CellSet.h>
#include <vtkm/cont/CellSetExplicit.h>
#include <vtkm/cont/CellSetStructured.h>
#include <vtkm/exec/ConnectivityStructuredPermuted.h>

namespace vtkm {
namespace cont {

namespace internal{
template<typename ValidCellArrayHandleType, typename OriginalCellSet>
struct CellSetPermutationTraits
{
  typedef typename OriginalCellSet::ShapeArrayType ShapeArrayType;
  typedef typename OriginalCellSet::NumIndicesArrayType NumIndicesArrayType;
  typedef typename OriginalCellSet::ConnectivityArrayType ConnectivityArrayType;
  typedef typename OriginalCellSet::IndexOffsetArrayType IndexOffsetArrayType;


  typedef vtkm::cont::ArrayHandlePermutation<ValidCellArrayHandleType,
    ShapeArrayType> PermShapeArrayType;

  typedef vtkm::cont::ArrayHandlePermutation<ValidCellArrayHandleType,
    NumIndicesArrayType> PermNumIndicesArrayType;

  typedef vtkm::cont::ArrayHandlePermutation<ValidCellArrayHandleType,
    IndexOffsetArrayType> PermIndexOffsetArrayType;


  typedef vtkm::cont::CellSetExplicit<
    typename PermShapeArrayType::StorageTag,
    typename PermNumIndicesArrayType::StorageTag,
    typename ConnectivityArrayType::StorageTag,
    typename PermIndexOffsetArrayType::StorageTag> PermutedCellSetType;
};

template<typename ValidCellArrayHandleType, vtkm::IdComponent DIMENSION>
struct CellSetPermutationTraits<ValidCellArrayHandleType, CellSetStructured<DIMENSION> >
{
};


}

template< typename ValidCellArrayHandleType,
          typename OriginalCellSet >
class CellSetPermutation : public CellSet
{
73 74 75
  typedef vtkm::cont::CellSetPermutation<
      ValidCellArrayHandleType,OriginalCellSet> Thisclass;

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
public:
  typedef typename vtkm::cont::internal::CellSetPermutationTraits<
  ValidCellArrayHandleType,OriginalCellSet>::PermutedCellSetType PermutedCellSetType;


  typedef typename PermutedCellSetType::ShapeArrayType ShapeArrayType;
  typedef typename PermutedCellSetType::NumIndicesArrayType NumIndicesArrayType;
  typedef typename PermutedCellSetType::ConnectivityArrayType ConnectivityArrayType;
  typedef typename PermutedCellSetType::IndexOffsetArrayType IndexOffsetArrayType;

  VTKM_CONT_EXPORT
  CellSetPermutation(const ValidCellArrayHandleType& validCellIds,
                const OriginalCellSet& cellset,
                const std::string &name = std::string(),
                vtkm::IdComponent dimensionality = 3)
    : CellSet(name,dimensionality),
      PermutedCellSet(0, name, dimensionality)
  {
    this->Fill(validCellIds, cellset);
  }

  VTKM_CONT_EXPORT
  CellSetPermutation(const std::string &name = std::string(),
                vtkm::IdComponent dimensionality = 3)
    : CellSet(name,dimensionality),
      ValidCellIds(),
      PermutedCellSet(0, name, dimensionality)
  {
  }

106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
  VTKM_CONT_EXPORT
  CellSetPermutation(const Thisclass &src)
    : CellSet(src),
      ValidCellIds(src.ValidCellIds),
      PermutedCellSet(src.PermutedCellSet)
  {  }

  VTKM_CONT_EXPORT
  Thisclass &operator=(const Thisclass &src)
  {
    this->CellSet::operator=(src);
    this->ValidCellIds = src.ValidCellIds;
    this->PermutedCellSet = src.PermutedCellSet;
    return *this;
  }

  virtual ~CellSetPermutation() {  }

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 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
  //This is the way you can fill the memory from another system without copying
  VTKM_CONT_EXPORT
  void Fill(const ValidCellArrayHandleType &validCellIds,
            const OriginalCellSet& cellset)
  {
    typedef vtkm::cont::internal::CellSetPermutationTraits<
      ValidCellArrayHandleType, OriginalCellSet> Traits;

    typedef vtkm::TopologyElementTagPoint ElemPointTag;
    typedef vtkm::TopologyElementTagCell ElemCellTag;

    PermutedCellSetType permutedCellSet(0,this->PermutedCellSet.GetName(),
                                        cellset.GetDimensionality());

    typename Traits::PermShapeArrayType shapeArray(validCellIds,
                                                   cellset.GetShapesArray(ElemPointTag(),ElemCellTag()));
    typename Traits::PermNumIndicesArrayType numArray(validCellIds,
                                                      cellset.GetNumIndicesArray(ElemPointTag(),ElemCellTag()));
    typename Traits::PermIndexOffsetArrayType offsArray(validCellIds,
                                                      cellset.GetIndexOffsetArray(ElemPointTag(),ElemCellTag()));

    permutedCellSet.Fill( shapeArray,
                          numArray,
                          cellset.GetConnectivityArray(ElemPointTag(),ElemCellTag()),
                          offsArray);

    this->ValidCellIds = validCellIds;
    this->PermutedCellSet = permutedCellSet;
  }

  VTKM_CONT_EXPORT
  vtkm::Id GetNumberOfCells() const
  {
    return this->PermutedCellSet.GetNumberOfCells();
  }

  VTKM_CONT_EXPORT
  vtkm::Id GetSchedulingRange(vtkm::TopologyElementTagCell) const
  {
    return this->PermutedCellSet.GetNumberOfCells();
  }

  VTKM_CONT_EXPORT
  vtkm::Id GetSchedulingRange(vtkm::TopologyElementTagPoint) const
  {
    return this->PermutedCellSet.GetNumberOfPoints();
  }

  template <typename DeviceAdapter, typename FromTopology, typename ToTopology>
  struct ExecutionTypes
  {
    VTKM_IS_DEVICE_ADAPTER_TAG(DeviceAdapter);
    VTKM_IS_TOPOLOGY_ELEMENT_TAG(FromTopology);
    VTKM_IS_TOPOLOGY_ELEMENT_TAG(ToTopology);

    typedef typename PermutedCellSetType::template ExecutionTypes<
                                                         DeviceAdapter,
                                                         FromTopology,
                                                         ToTopology>::ExecObjectType ExecObjectType;
  };


  template<typename Device, typename FromTopology, typename ToTopology>
  typename ExecutionTypes<Device,FromTopology,ToTopology>::ExecObjectType
  PrepareForInput(Device d, FromTopology f, ToTopology t) const
  {
    return this->PermutedCellSet.PrepareForInput(d,f,t);
  }

  virtual void PrintSummary(std::ostream &out) const
  {
    out << "   CellSetPermutation<ExplicitCellType>: " <<std::endl;
    PermutedCellSet.PrintSummary(out);
  }

private:
  ValidCellArrayHandleType ValidCellIds;
  PermutedCellSetType PermutedCellSet;
};

template< typename ValidCellArrayHandleType,vtkm::IdComponent Dimension>
class CellSetPermutation<ValidCellArrayHandleType, CellSetStructured<Dimension> > : public CellSet
{
  typedef vtkm::internal::ConnectivityStructuredInternals<Dimension>
      InternalsType;
public:
  VTKM_CONT_EXPORT
  CellSetPermutation(const ValidCellArrayHandleType& validCellIds,
                const CellSetStructured<Dimension>& cellset,
                const std::string &name = std::string(),
                vtkm::IdComponent dimensionality = Dimension)
    : CellSet(name,dimensionality),
      ValidCellIds(),
      FullCellSet()
  {
    this->Fill(validCellIds, cellset);
  }

  VTKM_CONT_EXPORT
  CellSetPermutation(const std::string &name = std::string(),
                vtkm::IdComponent dimensionality = Dimension)
    : CellSet(name,dimensionality),
      ValidCellIds(),
      FullCellSet()
  {
  }

  VTKM_CONT_EXPORT
  vtkm::Id GetNumberOfCells() const
  {
    return this->ValidCellIds.GetNumberOfValues();
  }

  //This is the way you can fill the memory from another system without copying
  VTKM_CONT_EXPORT
  void Fill(const ValidCellArrayHandleType &validCellIds,
            const CellSetStructured<Dimension>& cellset)
  {
    ValidCellIds = validCellIds;
    FullCellSet = cellset;
  }

  template<typename TopologyElement>
  VTKM_CONT_EXPORT
  vtkm::Id GetSchedulingRange(TopologyElement) const {
    VTKM_IS_TOPOLOGY_ELEMENT_TAG(TopologyElement);
    return this->ValidCellIds.GetNumberOfValues();
  }

  template<typename Device, typename FromTopology, typename ToTopology>
  struct ExecutionTypes {
    VTKM_IS_DEVICE_ADAPTER_TAG(Device);
    VTKM_IS_TOPOLOGY_ELEMENT_TAG(FromTopology);
    VTKM_IS_TOPOLOGY_ELEMENT_TAG(ToTopology);

    typedef typename ValidCellArrayHandleType::template ExecutionTypes<Device>::PortalConst ExecPortalType;

    typedef vtkm::exec::ConnectivityStructuredPermuted< ExecPortalType,
                                                        FromTopology,ToTopology,Dimension> ExecObjectType;
  };

  template<typename Device, typename FromTopology, typename ToTopology>
  typename ExecutionTypes<Device,FromTopology,ToTopology>::ExecObjectType
  PrepareForInput(Device d, FromTopology f, ToTopology t) const
  {
    typedef typename
        ExecutionTypes<Device,FromTopology,ToTopology>::ExecObjectType
            ConnectivityType;
    return ConnectivityType(this->ValidCellIds.PrepareForInput(d),
                            this->FullCellSet.PrepareForInput(d,f,t) );
  }

  virtual void PrintSummary(std::ostream &out) const
  {
    out << "   CellSetPermutation<StructuredCellType<"<<Dimension<<"> >: " <<std::endl;
  }

private:
  std::string Name;
  vtkm::Id Dimensionality;

  ValidCellArrayHandleType ValidCellIds;
  CellSetStructured<Dimension> FullCellSet;
};

}
} // namespace vtkm::cont

#endif //vtk_m_cont_CellSetPermutation_h