Skip to content

Commit

Permalink
post cpp20 optimizations 1
Browse files Browse the repository at this point in the history
  • Loading branch information
nyoungbq committed Jul 12, 2024
1 parent 98e3e53 commit e01fbb2
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include "simplnx/Utilities/DataGroupUtilities.hpp"
#include "simplnx/Utilities/FilterUtilities.hpp"

#include <chrono>
#include <random>

using namespace nx::core;
Expand All @@ -17,7 +16,7 @@ struct InitializeTupleToZeroFunctor
template <typename T>
void operator()(DataStructure& dataStructure, const DataPath& arrayPath, usize index)
{
dataStructure.getDataRefAs<DataArray<T>>(arrayPath).initializeTuple(index, 0);
dataStructure.getDataAsUnsafe<DataArray<T>>(arrayPath)->initializeTuple(index, 0);
}
};
} // namespace
Expand All @@ -43,19 +42,17 @@ const std::atomic_bool& AddBadData::getCancel()
// -----------------------------------------------------------------------------
Result<> AddBadData::operator()()
{
std::random_device randomDevice; // Will be used to obtain a seed for the random number engine
std::mt19937 generator(randomDevice()); // Standard mersenne_twister_engine seeded with rd()
generator.seed(m_InputValues->SeedValue);
std::mt19937 generator(m_InputValues->SeedValue); // Standard mersenne_twister_engine seeded
std::uniform_real_distribution<float32> distribution(0.0F, 1.0F);

auto& imgGeom = m_DataStructure.getDataRefAs<ImageGeom>(m_InputValues->ImageGeometryPath);
auto& GBEuclideanDistances = m_DataStructure.getDataRefAs<Int32Array>(m_InputValues->GBEuclideanDistancesArrayPath);
const auto& imgGeom = m_DataStructure.getDataRefAs<ImageGeom>(m_InputValues->ImageGeometryPath);
const auto& GBEuclideanDistances = m_DataStructure.getDataRefAs<Int32Array>(m_InputValues->GBEuclideanDistancesArrayPath).getDataStoreRef();

auto childArrayPaths = GetAllChildArrayDataPaths(m_DataStructure, imgGeom.getCellDataPath());
auto voxelArrayPaths = childArrayPaths.has_value() ? childArrayPaths.value() : std::vector<DataPath>{};

float32 random = 0.0f;
size_t totalPoints = GBEuclideanDistances.getSize();
const size_t totalPoints = GBEuclideanDistances.getSize();
for(size_t i = 0; i < totalPoints; ++i)
{
if(m_InputValues->BoundaryNoise && GBEuclideanDistances[i] < 1)
Expand All @@ -65,7 +62,7 @@ Result<> AddBadData::operator()()
{
for(const auto& voxelArrayPath : voxelArrayPaths)
{
ExecuteDataFunction(InitializeTupleToZeroFunctor{}, m_DataStructure.getDataAs<IDataArray>(voxelArrayPath)->getDataType(), m_DataStructure, voxelArrayPath, i);
ExecuteDataFunction(InitializeTupleToZeroFunctor{}, m_DataStructure.getDataAsUnsafe<IDataArray>(voxelArrayPath)->getDataType(), m_DataStructure, voxelArrayPath, i);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ std::vector<DataPath> AlignSectionsList::getSelectedDataPaths() const
// -----------------------------------------------------------------------------
Result<> AlignSectionsList::findShifts(std::vector<int64>& xShifts, std::vector<int64>& yShifts)
{
auto& imageGeom = m_DataStructure.getDataRefAs<ImageGeom>(m_InputValues->ImageGeometryPath);
const auto& imageGeom = m_DataStructure.getDataRefAs<ImageGeom>(m_InputValues->ImageGeometryPath);

SizeVec3 udims = imageGeom.getDimensions();
int64 zDim = static_cast<int64>(udims[2]);
auto zDim = static_cast<int64>(udims[2]);

Result<> results = {};
if(m_InputValues->DREAM3DAlignmentFile)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ Result<> ApplyTransformationToGeometry::applyImageGeometryTransformation()
// -----------------------------------------------------------------------------
Result<> ApplyTransformationToGeometry::applyNodeGeometryTransformation()
{
INodeGeometry0D& nodeGeometry0D = m_DataStructure.getDataRefAs<INodeGeometry0D>(m_InputValues->SelectedGeometryPath);
auto& nodeGeometry0D = m_DataStructure.getDataRefAs<INodeGeometry0D>(m_InputValues->SelectedGeometryPath);

IGeometry::SharedVertexList& vertexList = nodeGeometry0D.getVerticesRef();

Expand Down Expand Up @@ -159,7 +159,7 @@ Result<> ApplyTransformationToGeometry::operator()()
}
case detail::k_PrecomputedTransformationMatrixIdx: // Transformation matrix from array
{
const Float32Array& precomputed = m_DataStructure.getDataRefAs<Float32Array>(m_InputValues->ComputedTransformationMatrix);
const auto& precomputed = m_DataStructure.getDataAsUnsafe<Float32Array>(m_InputValues->ComputedTransformationMatrix)->getDataStoreRef();
m_TransformationMatrix = ImageRotationUtilities::CopyPrecomputedToTransformationMatrix(precomputed);
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ struct CreateCalculatorArrayFunctor
template <typename T>
CalculatorItem::Pointer operator()(DataStructure& dataStructure, bool allocate, const IDataArray* iDataArrayPtr)
{
const DataArray<T>* inputDataArray = dynamic_cast<const DataArray<T>*>(iDataArrayPtr);
const auto* inputDataArray = dynamic_cast<const DataArray<T>*>(iDataArrayPtr);
CalculatorItem::Pointer itemPtr = CalculatorArray<T>::New(dataStructure, inputDataArray, ICalculatorArray::Array, allocate);
return itemPtr;
}
Expand All @@ -55,15 +55,22 @@ struct CopyArrayFunctor
template <typename T>
void operator()(DataStructure& dataStructure, const DataPath& calculatedArrayPath, const Float64Array* inputArray)
{
const auto& inputDataStore = inputArray->getDataStoreRef();
if(nullptr != inputArray)
{
DataArray<T>& convertedArray = dataStructure.getDataRefAs<DataArray<T>>(calculatedArrayPath);
auto& convertedDataStore = dataStructure.getDataAsUnsafe<DataArray<T>>(calculatedArrayPath)->getDataStoreRef();

int count = inputArray->getSize();
for(int i = 0; i < count; i++)
const usize count = inputDataStore.getSize();
for(usize i = 0; i < count; i++)
{
double val = (*inputArray)[i];
convertedArray[i] = val;
if constexpr(std::is_same_v<float64, T>)
{
convertedDataStore[i] = inputDataStore[i];
}
else
{
convertedDataStore[i] = static_cast<T>(inputDataStore[i]);
}
}
}
}
Expand All @@ -74,11 +81,17 @@ struct InitializeArrayFunctor
template <typename T>
void operator()(DataStructure& dataStructure, const DataPath& calculatedArrayPath, const Float64Array* inputArray)
{
DataArray<T>& convertedArray = dataStructure.getDataRefAs<DataArray<T>>(calculatedArrayPath);
auto& convertedDataStore = dataStructure.getDataAsUnsafe<DataArray<T>>(calculatedArrayPath)->getDataStoreRef();
if(nullptr != inputArray && inputArray->getSize() == 1)
{
const T& initializeValue = inputArray->at(0);
convertedArray.fill(initializeValue);
if constexpr(std::is_same_v<float64, T>)
{
convertedDataStore.fill(inputArray->at(0));
}
else
{
convertedDataStore.fill(static_cast<T>(inputArray->at(0)));
}
}
}
};
Expand Down Expand Up @@ -318,14 +331,13 @@ std::vector<std::string> ArrayCalculatorParser::getRegularExpressionMatches()
std::vector<std::string> itemList;
// Match all array names that start with two alphabetical characters and have spaces. Match all numbers, decimal or integer.
// Match one letter array names. Match all special character operators.
std::regex regExp("(\"((\\[)?\\d+(\\.\\d+)?(\\])?|(\\[)?\\.\\d+(\\])?|\\w{1,1}((\\w|\\s|\\d)*(\\w|\\d){1,1})?|\\S)\")|(((\\[)?\\d+(\\.\\d+)?(\\])?|(\\[)?\\.\\d+(\\])?|\\w{1,1}((\\w|\\s|\\d)"
"*(\\w|\\d){1,1})?|\\S))");
std::regex regExp(R"lit(("((\[)?\d+(\.\d+)?(\])?|(\[)?\.\d+(\])?|\w{1,1}((\w|\s|\d)*(\w|\d){1,1})?|\S)")|(((\[)?\d+(\.\d+)?(\])?|(\[)?\.\d+(\])?|\w{1,1}((\w|\s|\d)*(\w|\d){1,1})?|\S)))lit");

auto regExpMatchBegin = std::sregex_iterator(m_InfixEquation.begin(), m_InfixEquation.end(), regExp);
auto regExpMatchEnd = std::sregex_iterator();
for(std::sregex_iterator i = regExpMatchBegin; i != regExpMatchEnd; ++i)
{
std::smatch match = *i;
const std::smatch& match = *i;
itemList.push_back(match.str());
}

Expand Down Expand Up @@ -436,7 +448,7 @@ Result<> ArrayCalculatorParser::parseCommaOperator(std::string token, std::vecto
// For example, if we have root( 4*4, 2*3 ), then we need it to be root( (4*4), (2*3) )
parsedInfix.push_back(RightParenthesisItem::New());

std::vector<CalculatorItem::Pointer>::iterator iter = parsedInfix.end();
auto iter = parsedInfix.end();
iter--;
while(iter != parsedInfix.begin())
{
Expand Down Expand Up @@ -473,7 +485,7 @@ Result<> ArrayCalculatorParser::parseArray(std::string token, std::vector<Calcul
}

DataPath tokenArrayPath = m_SelectedGroupPath.empty() ? DataPath({token}) : m_SelectedGroupPath.createChildPath(token);
const IDataArray* dataArray = m_DataStructure.getDataAs<IDataArray>(tokenArrayPath);
const auto* dataArray = m_DataStructure.getDataAs<IDataArray>(tokenArrayPath);
if(firstArray_NumTuples < 0 && firstArray_Name.empty())
{
firstArray_NumTuples = dataArray->getNumberOfTuples();
Expand All @@ -491,7 +503,7 @@ Result<> ArrayCalculatorParser::parseArray(std::string token, std::vector<Calcul
}

// -----------------------------------------------------------------------------
Result<> ArrayCalculatorParser::checkForAmbiguousArrayName(std::string strItem, std::string warningMsg)
Result<> ArrayCalculatorParser::checkForAmbiguousArrayName(const std::string& strItem, std::string warningMsg)
{
if(m_IsPreflight && ContainsDataArrayName(m_DataStructure, m_SelectedGroupPath, strItem))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class SIMPLNXCORE_EXPORT ArrayCalculatorParser
Result<> parseIndexOperator(std::string token, std::vector<CalculatorItem::Pointer>& parsedInfix);
Result<> parseCommaOperator(std::string token, std::vector<CalculatorItem::Pointer>& parsedInfix);
Result<> parseArray(std::string token, std::vector<CalculatorItem::Pointer>& parsedInfix);
Result<> checkForAmbiguousArrayName(std::string strItem, std::string warningMsg);
Result<> checkForAmbiguousArrayName(const std::string& strItem, std::string warningMsg);

private:
const DataStructure& m_DataStructure;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,74 @@

namespace nx::core
{
namespace
{
/**
* @brief extractPatchData Extracts out the needed data values from the global arrays
* @param triId The seed triangle Id
* @param triPatch The group of triangles being used
* @param data The data to extract from
* @return Shared pointer to the extracted data
*/
std::shared_ptr<Float64DataStore> extractPatchData(int64_t triId, CalculateTriangleGroupCurvatures::UniqueFaceIds_t& triPatch, Float64AbstractDataStore& data)
{
IDataStore::ShapeType cDims = {3ULL};

for(auto iter = triPatch.begin(); iter != triPatch.end();)
{
int64_t t = *iter;

if(std::isnan(data[t * 3]) || std::isnan(data[t * 3 + 1]) || std::isnan(data[t * 3 + 2]))
{
iter = triPatch.erase(iter);
if(*iter == triId)
{
triId = *(triPatch.begin());
}
}
else
{
++iter;
}
}

if(triPatch.empty())
{
return nullptr;
}

size_t totalTuples = triPatch.size();
if(triPatch.count(triId) == 0)
{
totalTuples++;
}

IDataStore::ShapeType tupleShape = {totalTuples};
std::optional<float64> initValue = {};
std::shared_ptr<Float64DataStore> extractedData = std::make_shared<Float64DataStore>(tupleShape, cDims, initValue);
// This little chunk makes sure the current seed triangles centroid and normal data appear
// first in the returned arrays which makes the next steps a tad easier.
int32_t i = 0;
extractedData->setComponent(i, 0, data[triId * 3]);
extractedData->setComponent(i, 1, data[triId * 3 + 1]);
extractedData->setComponent(i, 2, data[triId * 3 + 2]);
++i;
triPatch.erase(triId);

for(int64_t t : triPatch)
{
extractedData->setComponent(i, 0, data[t * 3]);
extractedData->setComponent(i, 1, data[t * 3 + 1]);
extractedData->setComponent(i, 2, data[t * 3 + 2]);
++i;
}
triPatch.insert(triId);

extractedData->resizeTuples({triPatch.size()}); // Resize the TriPatch DataArray
return extractedData;
}
} // namespace

// -----------------------------------------------------------------------------
CalculateTriangleGroupCurvatures::CalculateTriangleGroupCurvatures(FeatureFaceCurvature* filter, int64_t nring, std::vector<int64_t> triangleIds, bool useNormalsForCurveFitting,
Float64Array* principleCurvature1, Float64Array* principleCurvature2, Float64Array* principleDirection1,
Expand All @@ -53,7 +121,7 @@ CalculateTriangleGroupCurvatures::CalculateTriangleGroupCurvatures(FeatureFaceCu
Float64Array* surfaceMeshTriangleCentroids, const IFilter::MessageHandler& messageHandler, const std::atomic_bool& shouldCancel)
: m_Filter(filter)
, m_NRing(nring)
, m_TriangleIds(triangleIds)
, m_TriangleIds(std::move(triangleIds))
, m_UseNormalsForCurveFitting(useNormalsForCurveFitting)
, m_PrincipleCurvature1(principleCurvature1)
, m_PrincipleCurvature2(principleCurvature2)
Expand Down Expand Up @@ -91,7 +159,7 @@ void subtractVector3d(Float64AbstractDataStore& data, double* v)
// -----------------------------------------------------------------------------
void CalculateTriangleGroupCurvatures::operator()() const
{
if(m_TriangleIds.size() == 0)
if(m_TriangleIds.empty())
{
return;
}
Expand Down Expand Up @@ -184,7 +252,7 @@ void CalculateTriangleGroupCurvatures::operator()() const

// Translate the patch to the 0,0,0 origin
double sub[3] = {patchCentroids->getComponentValue(0, 0), patchCentroids->getComponentValue(0, 1), patchCentroids->getComponentValue(0, 2)};
subtractVector3d(*patchCentroids.get(), sub);
subtractVector3d(*patchCentroids, sub);

double np[3] = {patchNormals->getComponentValue(0, 0), patchNormals->getComponentValue(0, 1), patchNormals->getComponentValue(0, 2)};

Expand Down Expand Up @@ -330,64 +398,4 @@ void CalculateTriangleGroupCurvatures::operator()() const
// Send some feedback
m_Filter->sendThreadSafeProgressMessage(1);
}

// -----------------------------------------------------------------------------
std::shared_ptr<Float64DataStore> CalculateTriangleGroupCurvatures::extractPatchData(int64_t triId, UniqueFaceIds_t& triPatch, Float64AbstractDataStore& data) const
{
IDataStore::ShapeType cDims = {3ULL};

for(auto iter = triPatch.begin(); iter != triPatch.end();)
{
int64_t t = *iter;

if(std::isnan(data[t * 3]) || std::isnan(data[t * 3 + 1]) || std::isnan(data[t * 3 + 2]))
{
iter = triPatch.erase(iter);
if(*iter == triId)
{
triId = *(triPatch.begin());
}
}
else
{
++iter;
}
}

if(triPatch.empty())
{
return nullptr;
}

size_t totalTuples = triPatch.size();
if(triPatch.count(triId) == 0)
{
totalTuples++;
}

IDataStore::ShapeType tupleShape = {totalTuples};
std::optional<float64> initValue = {};
std::shared_ptr<Float64DataStore> extractedData = std::make_shared<Float64DataStore>(tupleShape, cDims, initValue);
// This little chunk makes sure the current seed triangles centroid and normal data appear
// first in the returned arrays which makes the next steps a tad easier.
int32_t i = 0;
extractedData->setComponent(i, 0, data[triId * 3]);
extractedData->setComponent(i, 1, data[triId * 3 + 1]);
extractedData->setComponent(i, 2, data[triId * 3 + 2]);
++i;
triPatch.erase(triId);

for(UniqueFaceIds_t::iterator iter = triPatch.begin(); iter != triPatch.end(); ++iter)
{
int64_t t = *iter;
extractedData->setComponent(i, 0, data[t * 3]);
extractedData->setComponent(i, 1, data[t * 3 + 1]);
extractedData->setComponent(i, 2, data[t * 3 + 2]);
++i;
}
triPatch.insert(triId);

extractedData->resizeTuples({triPatch.size()}); // Resize the TriPatch DataArray
return extractedData;
}
} // namespace nx::core
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,6 @@ class SIMPLNXCORE_EXPORT CalculateTriangleGroupCurvatures

using UniqueFaceIds_t = std::set<int64_t>;

protected:
/**
* @brief extractPatchData Extracts out the needed data values from the global arrays
* @param triId The seed triangle Id
* @param triPatch The group of triangles being used
* @param data The data to extract from
* @return Shared pointer to the extracted data
*/
std::shared_ptr<Float64DataStore> extractPatchData(int64_t triId, UniqueFaceIds_t& triPatch, Float64AbstractDataStore& data) const;

private:
FeatureFaceCurvature* m_Filter = nullptr;
int64_t m_NRing;
Expand Down
1 change: 0 additions & 1 deletion src/simplnx/Utilities/AlignSections.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ Result<> AlignSections::execute(const SizeVec3& udims)

m_MessageHandler(fmt::format("Updating DataArray '{}'", cellArrayPath.toString()));
auto& cellArray = m_DataStructure.getDataRefAs<IDataArray>(cellArrayPath);

ExecuteParallelFunction<AlignSectionsTransferDataImpl>(cellArray.getDataType(), taskRunner, this, udims, xShifts, yShifts, cellArray);
}
// This will spill over if the number of DataArrays to process does not divide evenly by the number of threads.
Expand Down
2 changes: 1 addition & 1 deletion src/simplnx/Utilities/ImageRotationUtilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ std::string GenerateTransformationMatrixDescription(const ImageRotationUtilities
}

//------------------------------------------------------------------------------
ImageRotationUtilities::Matrix4fR CopyPrecomputedToTransformationMatrix(const Float32Array& precomputed)
ImageRotationUtilities::Matrix4fR CopyPrecomputedToTransformationMatrix(const AbstractDataStore<float32>& precomputed)
{
ImageRotationUtilities::Matrix4fR transformationMatrix;
transformationMatrix.fill(0.0F);
Expand Down
Loading

0 comments on commit e01fbb2

Please sign in to comment.