vtkmCellSetExplicit.cxx 12 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
//============================================================================
//  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.
//============================================================================

#include "vtkmlib/Storage.h"
#include "vtkmCellSetExplicit.h"
#include "vtkmConnectivityExec.h"

25 26
#include <vtkm/cont/ArrayHandleImplicit.h>
#include <vtkm/cont/internal/ReverseConnectivityBuilder.h>
27 28 29
#include <vtkm/worklet/WorkletMapField.h>
#include <vtkm/worklet/DispatcherMapField.h>

30 31 32 33 34 35 36 37
#include <utility>

namespace
{

// Converts [0, rconnSize) to [0, connSize), skipping cell length entries.
template <typename Device>
struct ExplicitRConnToConn
38
{
39 40 41 42 43 44 45 46 47 48
  using OffsetsArray = vtkm::cont::ArrayHandle<vtkm::Id, tovtkm::vtkAOSArrayContainerTag>;
  using OffsetsPortal = decltype(std::declval<OffsetsArray>().PrepareForInput(Device()));

  // Functor that modifies the offsets array so we can compute point id indices
  // Output is:
  // modOffset[i] = offsets[i] - i
  struct OffsetsModifier
  {
    OffsetsPortal Offsets;

49 50
    VTKM_SUPPRESS_EXEC_WARNINGS
    VTKM_EXEC_CONT
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
    OffsetsModifier(const OffsetsPortal& offsets = OffsetsPortal{})
      : Offsets(offsets)
    {
    }

    VTKM_EXEC
    vtkm::Id operator()(vtkm::Id inIdx) const
    {
      return this->Offsets.Get(inIdx) - inIdx;
    }
  };

  using ModOffsetsArray = vtkm::cont::ArrayHandleImplicit<OffsetsModifier>;
  using ModOffsetsPortal = decltype(std::declval<ModOffsetsArray>().PrepareForInput(Device()));

  VTKM_CONT
  ExplicitRConnToConn(const ModOffsetsPortal& offsets)
    : Offsets(offsets)
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
  }

  VTKM_EXEC
  vtkm::Id operator()(vtkm::Id rConnIdx) const
  {
    // Compute the connectivity array index (skipping cell length entries)
    // The number of cell length entries can be found by taking the index of
    // the upper bound of inIdx in Offsets and adding it to inIdx.
    //
    // Example:
    // Conn:  |  3  X  X  X  |  4  X  X  X  X  |  3  X  X  X  |  2  X  X  |
    // Idx:   |  0  1  2  3  |  4  5  6  7  8  |  9  10 11 12 |  13 14 15 |
    // InIdx:       0  1  2        3  4  5  6  |     7  8  9        10 11
    //
    // ModOffset[i] = Offsets[i] - i:
    // Offsets:     0  4  9  13 (16)
    // ModOffsets:  0  3  7  10 (12)
    //
    // Define UB(x) to return the index of the upper bound of x in ModOffsets,
    // the i'th point index's location in Conn is computed as:
    // OutId = UB(InIdx) + InIdx
    //
    // This gives us the expected out indices:
    // InIdx:     0  1  2  3  4  5  6  7  8  9  10 11
    // UB(InIdx): 1  1  1  2  2  2  2  3  3  3  4  4
    // OutIdx:    1  2  3  5  6  7  8  10 11 12 14 15
    const vtkm::Id uBIdx = this->UpperBoundIdx(rConnIdx);
    const vtkm::Id connIdx = rConnIdx + uBIdx;
    return connIdx;
  }

private:

  ModOffsetsPortal Offsets;

  VTKM_EXEC
  vtkm::Id UpperBoundIdx(vtkm::Id inIdx) const
  {
    vtkm::Id first = 0;
    vtkm::Id length = this->Offsets.GetNumberOfValues();

    while (length > 0)
112
    {
113 114 115 116 117 118 119 120 121 122 123 124
      vtkm::Id half = length / 2;
      vtkm::Id pos = first + half;
      vtkm::Id val = this->Offsets.Get(pos);
      if (val <= inIdx)
      {
        first = pos + 1;
        length -= half + 1;
      }
      else
      {
        length = half;
      }
125
    }
126 127

    return first;
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

// Converts a connectivity index to a cell id:
template <typename Device>
struct ExplicitCellIdCalc
{
  using OffsetsArray = vtkm::cont::ArrayHandle<vtkm::Id, tovtkm::vtkAOSArrayContainerTag>;
  using OffsetsPortal = decltype(std::declval<OffsetsArray>().PrepareForInput(Device()));

  vtkm::Id ConnSize;
  OffsetsPortal Offsets;

  VTKM_CONT
  ExplicitCellIdCalc(vtkm::Id connSize, const OffsetsPortal& offsets)
    : ConnSize(connSize)
    , Offsets(offsets)
  {
  }

  // Computes the cellid of the connectivity index i.
  //
  // For a mixed-cell connectivity, the offset table is used to compute
  // the cell id.
  //
  // Example:
  // Conn:   |  3  X  X  X  |  4  X  X  X  X  |  3  X  X  X  |  2  X  X  |
  // Idx:    |     1  2  3  |     5  6  7  8  |     10 11 12 |     14 15 |
  //
  // Offsets:    0  4  9  13
  // ModOffsets: 4  9  13 16
  //
  // Computing the index of the lower bound in ModOffsets for each Idx gives
  // the expected cell id values:
  // CellId: |     0  0  0  |     1  1  1  1  |     2  2  2  |     3  3  |
  VTKM_EXEC
  vtkm::Id operator()(vtkm::Id i) const
  {
    return this->LowerBound(i);
  }

private:
  /// Returns the i+1 offset, or the full size of the connectivity if at end.
  VTKM_EXEC
  vtkm::Id GetModifiedOffset(vtkm::Id i) const
  {
    ++i;
    return (i >= this->Offsets.GetNumberOfValues()) ? this->ConnSize
                                                    : this->Offsets.Get(i);
  }

  VTKM_EXEC
  vtkm::Id LowerBound(vtkm::Id inVal) const
  {
    vtkm::Id first = 0;
    vtkm::Id length = this->Offsets.GetNumberOfValues();

    while (length > 0)
    {
      vtkm::Id half = length / 2;
      vtkm::Id pos = first + half;
      vtkm::Id val = this->GetModifiedOffset(pos);
      if (val < inVal)
      {
        first = pos + 1;
        length -= half + 1;
      }
      else
      {
        length = half;
      }
    }

    return first;

  }
};

} // end anon namespace
207 208 209 210 211 212

namespace vtkm {
namespace cont {

//------------------------------------------------------------------------------
void vtkmCellSetExplicitAOS::Fill(
Sujin Philip's avatar
Sujin Philip committed
213
    vtkm::Id numberOfPoints,
214 215 216 217 218 219 220 221 222 223
    const vtkm::cont::ArrayHandle<vtkm::UInt8, tovtkm::vtkAOSArrayContainerTag>&
        cellTypes,
    const vtkm::cont::ArrayHandle<vtkm::Id, tovtkm::vtkCellArrayContainerTag>&
        connectivity,
    const vtkm::cont::ArrayHandle<vtkm::Id, tovtkm::vtkAOSArrayContainerTag>&
        offsets)
{
  this->Shapes = cellTypes;
  this->Connectivity = connectivity;
  this->IndexOffsets = offsets;
Sujin Philip's avatar
Sujin Philip committed
224
  this->NumberOfPoints = numberOfPoints;
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
}

//------------------------------------------------------------------------------
void vtkmCellSetExplicitAOS::PrintSummary(std::ostream& out) const
{
  out << "   vtkmCellSetExplicitAOS: " << this->Name << std::endl;
  out << "   Shapes: " << std::endl;
  vtkm::cont::printSummary_ArrayHandle(this->Shapes, out);
  out << "   Connectivity: " << std::endl;
  vtkm::cont::printSummary_ArrayHandle(this->Connectivity, out);
  out << "   IndexOffsets: " << std::endl;
  vtkm::cont::printSummary_ArrayHandle(this->IndexOffsets, out);
}

//------------------------------------------------------------------------------
template <typename Device>
typename vtkm::exec::ConnectivityVTKAOS<Device>
    vtkmCellSetExplicitAOS::PrepareForInput(Device,
                                            vtkm::TopologyElementTagPoint,
                                            vtkm::TopologyElementTagCell) const
{
  return vtkm::exec::ConnectivityVTKAOS<Device>(
      this->Shapes.PrepareForInput(Device()),
      this->Connectivity.PrepareForInput(Device()),
      this->IndexOffsets.PrepareForInput(Device()));
}

//------------------------------------------------------------------------------
template <typename Device>
typename vtkm::exec::ReverseConnectivityVTK<Device>
    vtkmCellSetExplicitAOS::PrepareForInput(Device,
                                            vtkm::TopologyElementTagCell,
                                            vtkm::TopologyElementTagPoint) const
{
  //One of the biggest questions when computing the reverse connectivity
  //is how are we going to layout the results.
  //We have two options:
  // 1. The layout mirrors that of the point->cell where the connectivity array
  // is VTK with the counts interlaced inside the array
  // 2. We go with a VTK-m approach where we keep a separate array.
  //
  //While #1 has the strength of being easily mapped to VTK, we are going with
  //#2 as it easier to construct
  if(!this->ReverseConnectivityBuilt)
  {
270
    const vtkm::Id numberOfPoints = this->GetNumberOfPoints();
271
    const vtkm::Id connectivityLength = this->Connectivity.GetNumberOfValues();
272
    const vtkm::Id rconnSize = connectivityLength - this->IndexOffsets.GetNumberOfValues();
273

274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
    auto offsetsPortal = this->IndexOffsets.PrepareForInput(Device());
    typename ExplicitRConnToConn<Device>::OffsetsModifier offsetModifier{offsetsPortal};
    auto modOffsets = vtkm::cont::make_ArrayHandleImplicit(offsetModifier,
                                                           this->IndexOffsets.GetNumberOfValues());

    const ExplicitRConnToConn<Device> rconnToConnCalc{modOffsets.PrepareForInput(Device())};
    const ExplicitCellIdCalc<Device> cellIdCalc{this->Connectivity.GetNumberOfValues(),
                                                this->IndexOffsets.PrepareForInput(Device())};

    vtkm::cont::internal::ReverseConnectivityBuilder builder;

    builder.Run(this->Connectivity,
                this->RConn,
                this->RNumIndices,
                this->RIndexOffsets,
                rconnToConnCalc,
                cellIdCalc,
                numberOfPoints,
                rconnSize,
                Device{});

    this->NumberOfPoints = this->RIndexOffsets.GetNumberOfValues();
296 297 298 299 300 301 302 303 304 305 306 307
    this->ReverseConnectivityBuilt = true;
  }

  //no need to have a reverse shapes array, as everything has the shape type
  //of vertex
  return vtkm::exec::ReverseConnectivityVTK<Device>(
      this->RConn.PrepareForInput(Device()),
      this->RNumIndices.PrepareForInput(Device()),
      this->RIndexOffsets.PrepareForInput(Device()));
}

// template methods we want to compile only once
308 309
template VTKACCELERATORSVTKM_EXPORT
  vtkm::exec::ConnectivityVTKAOS<vtkm::cont::DeviceAdapterTagSerial>
310
    vtkmCellSetExplicitAOS::PrepareForInput(vtkm::cont::DeviceAdapterTagSerial,
311
      vtkm::TopologyElementTagPoint, vtkm::TopologyElementTagCell) const;
312

313 314
template VTKACCELERATORSVTKM_EXPORT
  vtkm::exec::ReverseConnectivityVTK<vtkm::cont::DeviceAdapterTagSerial>
315
    vtkmCellSetExplicitAOS::PrepareForInput(vtkm::cont::DeviceAdapterTagSerial,
316
      vtkm::TopologyElementTagCell, vtkm::TopologyElementTagPoint) const;
317 318

#ifdef VTKM_ENABLE_TBB
319 320
template VTKACCELERATORSVTKM_EXPORT
  vtkm::exec::ConnectivityVTKAOS<vtkm::cont::DeviceAdapterTagTBB>
321
    vtkmCellSetExplicitAOS::PrepareForInput(vtkm::cont::DeviceAdapterTagTBB,
322
      vtkm::TopologyElementTagPoint, vtkm::TopologyElementTagCell) const;
323

324 325
template VTKACCELERATORSVTKM_EXPORT
  vtkm::exec::ReverseConnectivityVTK<vtkm::cont::DeviceAdapterTagTBB>
326
    vtkmCellSetExplicitAOS::PrepareForInput(vtkm::cont::DeviceAdapterTagTBB,
327
      vtkm::TopologyElementTagCell, vtkm::TopologyElementTagPoint) const;
328 329
#endif

330 331 332 333 334 335 336 337 338 339 340 341
#ifdef VTKM_ENABLE_OPENMP
template VTKACCELERATORSVTKM_EXPORT
  vtkm::exec::ConnectivityVTKAOS<vtkm::cont::DeviceAdapterTagOpenMP>
    vtkmCellSetExplicitAOS::PrepareForInput(vtkm::cont::DeviceAdapterTagOpenMP,
      vtkm::TopologyElementTagPoint, vtkm::TopologyElementTagCell) const;

template VTKACCELERATORSVTKM_EXPORT
  vtkm::exec::ReverseConnectivityVTK<vtkm::cont::DeviceAdapterTagOpenMP>
    vtkmCellSetExplicitAOS::PrepareForInput(vtkm::cont::DeviceAdapterTagOpenMP,
      vtkm::TopologyElementTagCell, vtkm::TopologyElementTagPoint) const;
#endif

342 343 344 345 346 347 348 349 350 351 352
#ifdef VTKM_ENABLE_CUDA
template VTKACCELERATORSVTKM_EXPORT
  vtkm::exec::ConnectivityVTKAOS<vtkm::cont::DeviceAdapterTagCuda>
    vtkmCellSetExplicitAOS::PrepareForInput(vtkm::cont::DeviceAdapterTagCuda,
      vtkm::TopologyElementTagPoint, vtkm::TopologyElementTagCell) const;

template VTKACCELERATORSVTKM_EXPORT
  vtkm::exec::ReverseConnectivityVTK<vtkm::cont::DeviceAdapterTagCuda>
    vtkmCellSetExplicitAOS::PrepareForInput(vtkm::cont::DeviceAdapterTagCuda,
      vtkm::TopologyElementTagCell, vtkm::TopologyElementTagPoint) const;
#endif
353 354
}
}