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
|
/*=========================================================================
Program: Visualization Toolkit
Module: TestDataArrayIterators.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm 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.
=========================================================================*/
#include "vtkFloatArray.h"
#include "vtkTimerLog.h"
#include "vtkTypedDataArray.h"
#include "vtkTypedDataArrayIterator.h"
#include "vtkNew.h"
#include <cassert>
#include <iostream>
// undefine this to print benchmark results:
#define SILENT
// Create a subclass of vtkTypedDataArray:
namespace
{
class MyArray : public vtkTypedDataArray<float>
{
vtkFloatArray *Data;
public:
vtkTypeMacro(MyArray, vtkTypedDataArray<float>)
static MyArray *New() { VTK_STANDARD_NEW_BODY(MyArray) }
void Init(vtkFloatArray *array)
{
this->Data = array;
this->NumberOfComponents = array->GetNumberOfComponents();
this->MaxId = array->GetMaxId();
}
ValueType& GetValueReference(vtkIdType idx) VTK_OVERRIDE
{
return *this->Data->GetPointer(idx);
}
// These pure virtuals are no-op -- all we care about is GetValueReference
// to test the iterator.
void SetTypedTuple(vtkIdType, const ValueType *) VTK_OVERRIDE {}
void InsertTypedTuple(vtkIdType, const ValueType *) VTK_OVERRIDE {}
vtkIdType InsertNextTypedTuple(const ValueType *) VTK_OVERRIDE { return 0; }
vtkIdType LookupTypedValue(ValueType) VTK_OVERRIDE { return 0; }
void LookupTypedValue(ValueType, vtkIdList*) VTK_OVERRIDE {}
ValueType GetValue(vtkIdType) const VTK_OVERRIDE { return 0; }
void SetValue(vtkIdType, ValueType) VTK_OVERRIDE {}
void GetTypedTuple(vtkIdType, ValueType*) const VTK_OVERRIDE {}
vtkIdType InsertNextValue(ValueType) VTK_OVERRIDE { return 0; }
void InsertValue(vtkIdType, ValueType) VTK_OVERRIDE {}
int Allocate(vtkIdType, vtkIdType) VTK_OVERRIDE { return 0; }
int Resize(vtkIdType) VTK_OVERRIDE { return 0; }
};
}
int TestDataArrayIterators(int, char *[])
{
vtkIdType numComps = 4;
vtkIdType numValues = 100000000; // 10 million
assert(numValues % numComps == 0);
vtkIdType numTuples = numValues / numComps;
vtkNew<vtkFloatArray> arrayContainer;
vtkFloatArray *array = arrayContainer.GetPointer();
array->SetNumberOfComponents(numComps);
array->SetNumberOfTuples(numTuples);
for (vtkIdType i = 0; i < numValues; ++i)
{
// Just fill with consistent data
array->SetValue(i, i % 97);
}
// Create the vtkTypedDataArray testing implementation:
vtkNew<MyArray> tdaContainer;
MyArray *tda = tdaContainer.GetPointer();
tda->Init(array);
// should be vtkAOSDataArrayTemplate<float>::Iterator (float*):
vtkFloatArray::Iterator datBegin = array->Begin();
vtkFloatArray::Iterator datIter = array->Begin();
if (typeid(datBegin) != typeid(float*))
{
std::cerr << "Error: vtkFloatArray::Iterator is not a float*.";
return EXIT_FAILURE;
}
// should be vtkTypedDataArrayIterator<float>:
vtkTypedDataArray<float>::Iterator tdaBegin =
vtkTypedDataArray<float>::FastDownCast(tda)->Begin();
vtkTypedDataArray<float>::Iterator tdaIter =
vtkTypedDataArray<float>::FastDownCast(tda)->Begin();
if (typeid(tdaBegin) != typeid(vtkTypedDataArrayIterator<float>))
{
std::cerr << "Error: vtkTypedDataArray<float>::Iterator is not a "
"vtkTypedDataArrayIterator<float>.";
return EXIT_FAILURE;
}
// Validate that the iterators return the same values from operator[] and
// operator* as GetValue;
for (vtkIdType i = 0; i < numValues; ++i)
{
float lookup = array->GetValue(i);
if (lookup != datBegin[i] || lookup != tdaBegin[i] ||
lookup != *datIter || lookup != *tdaIter)
{
std::cerr << "Mismatch at " << i << ":"
<< " GetValue(i)=" << lookup
<< " datBegin[i]=" << datBegin[i]
<< " tdaBegin[i]=" << tdaBegin[i]
<< " *datIter=" << *datIter
<< " *tdaIter=" << *tdaIter
<< std::endl;
return EXIT_FAILURE;
}
++datIter;
++tdaIter;
}
#ifndef SILENT
// Iterator timings.
vtkNew<vtkTimerLog> timer;
// Lookup:
float lookupSum = 0.f;
timer->StartTimer();
for (vtkIdType i = 0; i < numValues; ++i)
{
lookupSum += *array->GetPointer(i);
}
timer->StopTimer();
double lookupTime = timer->GetElapsedTime();
// Scalar iterator:
float datSum = 0.f;
timer->StartTimer();
vtkFloatArray::Iterator datEnd = array->End();
while (datBegin != datEnd)
{
datSum += *datBegin++;
}
timer->StopTimer();
double datTime = timer->GetElapsedTime();
// vtkTypedDataArrayIterator:
vtkTypedDataArray<float>::Iterator tdaEnd = tda->End();
float tdaSum = 0.f;
timer->StartTimer();
while (tdaBegin != tdaEnd)
{
tdaSum += *tdaBegin++;
}
timer->StopTimer();
double tdaTime = timer->GetElapsedTime();
std::cout << "GetValue time, sum: "
<< lookupTime << ", " << lookupSum << std::endl;
std::cout << "dat time, sum: "
<< datTime << ", " << datSum << std::endl;
std::cout << "tda time, sum: "
<< tdaTime << ", " << tdaSum << std::endl;
#endif
return EXIT_SUCCESS;
}
|