Skip to content

Commit

Permalink
ENH: Initialize Data - Increased Variance in Random Generation for Fl…
Browse files Browse the repository at this point in the history
…oats (#880)

Modified Initialize Data Algorithm to Increase Variance in Random Generation for Float32 and Float64
  • Loading branch information
nyoungbq authored Mar 7, 2024
1 parent 8e4e8f9 commit 023c989
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 18 deletions.
60 changes: 44 additions & 16 deletions src/Plugins/SimplnxCore/src/SimplnxCore/Filters/InitializeData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ void IncrementalFill(DataArray<T>& dataArray, const std::vector<std::string>& st
}
}

template <typename T, class DistributionT>
template <typename T, bool Ranged, class DistributionT>
void RandomFill(std::vector<DistributionT>& dist, DataArray<T>& dataArray, const uint64 seed, const bool standardizeSeed)
{
usize numComp = dataArray.getNumberOfComponents(); // We checked that the values string is greater than max comps size so proceed check free
Expand All @@ -180,11 +180,25 @@ void RandomFill(std::vector<DistributionT>& dist, DataArray<T>& dataArray, const
{
for(usize comp = 0; comp < numComp; comp++)
{
if constexpr(!std::is_same_v<T, bool>)
if constexpr(std::is_floating_point_v<T>)
{
dataArray[tup * numComp + comp] = static_cast<T>(dist[comp](generators[comp]));
if constexpr(Ranged)
{
dataArray[tup * numComp + comp] = static_cast<T>(dist[comp](generators[comp]));
}
if constexpr(!Ranged)
{
if constexpr(std::is_signed_v<T>)
{
dataArray[tup * numComp + comp] = static_cast<T>(dist[comp](generators[comp]) * (std::numeric_limits<T>::max() - 1) * (((rand() & 1) == 0) ? 1 : -1));
}
if constexpr(!std::is_signed_v<T>)
{
dataArray[tup * numComp + comp] = static_cast<T>(dist[comp](generators[comp]) * std::numeric_limits<T>::max());
}
}
}
if constexpr(std::is_same_v<T, bool>)
if constexpr(!std::is_floating_point_v<T>)
{
dataArray[tup * numComp + comp] = static_cast<T>(dist[comp](generators[comp]));
}
Expand All @@ -208,7 +222,7 @@ void FillIncForwarder(const StepType& stepType, ArgsT&&... args)
}
}

template <typename T, class... ArgsT>
template <typename T, bool Ranged, class... ArgsT>
void FillRandomForwarder(const std::vector<T>& range, usize numComp, ArgsT&&... args)
{
if constexpr(std::is_same_v<T, bool>)
Expand All @@ -218,25 +232,39 @@ void FillRandomForwarder(const std::vector<T>& range, usize numComp, ArgsT&&...
{
dists.emplace_back((range.at(comp) ? 1 : 0), (range.at(comp + 1) ? 1 : 0));
}
::RandomFill<T, std::uniform_int_distribution<int64>>(dists, std::forward<ArgsT>(args)...);
::RandomFill<T, Ranged, std::uniform_int_distribution<int64>>(dists, std::forward<ArgsT>(args)...);
return;
}
if constexpr(std::is_floating_point_v<T>)
if constexpr(!std::is_floating_point_v<T>)
{
std::vector<std::uniform_real_distribution<float64>> dists;
std::vector<std::uniform_int_distribution<int64>> dists;
for(usize comp = 0; comp < numComp * 2; comp += 2)
{
dists.emplace_back(range.at(comp), range.at(comp + 1));
}
::RandomFill<T, std::uniform_real_distribution<float64>>(dists, std::forward<ArgsT>(args)...);
::RandomFill<T, Ranged, std::uniform_int_distribution<int64>>(dists, std::forward<ArgsT>(args)...);
}
if constexpr(!std::is_floating_point_v<T> && !std::is_same_v<T, bool>)
if constexpr(std::is_floating_point_v<T>)
{
std::vector<std::uniform_int_distribution<int64>> dists;
for(usize comp = 0; comp < numComp * 2; comp += 2)
if constexpr(Ranged)
{
dists.emplace_back(range.at(comp), range.at(comp + 1));

std::vector<std::uniform_real_distribution<float64>> dists;
for(usize comp = 0; comp < numComp * 2; comp += 2)
{
dists.emplace_back(range.at(comp), range.at(comp + 1));
}
::RandomFill<T, Ranged, std::uniform_real_distribution<float64>>(dists, std::forward<ArgsT>(args)...);
}
if constexpr(!Ranged)
{
std::vector<std::uniform_real_distribution<float64>> dists;
for(usize comp = 0; comp < numComp * 2; comp += 2)
{
dists.emplace_back(0, 1);
}
::RandomFill<T, Ranged, std::uniform_real_distribution<float64>>(dists, std::forward<ArgsT>(args)...);
}
::RandomFill<T, std::uniform_int_distribution<int64>>(dists, std::forward<ArgsT>(args)...);
}
}

Expand Down Expand Up @@ -291,7 +319,7 @@ struct FillArrayFunctor
range.push_back(true);
}
}
return ::FillRandomForwarder<T>(range, numComp, dataArray, inputValues.seed, inputValues.standardizeSeed);
return ::FillRandomForwarder<T, false>(range, numComp, dataArray, inputValues.seed, inputValues.standardizeSeed);
}
case InitializeType::RangedRandom: {
auto randBegin = standardizeMultiComponent(numComp, inputValues.randBegin);
Expand All @@ -305,7 +333,7 @@ struct FillArrayFunctor
result = ConvertTo<T>::convert(randEnd[comp]);
range.push_back(result.value());
}
return ::FillRandomForwarder<T>(range, numComp, dataArray, inputValues.seed, inputValues.standardizeSeed);
return ::FillRandomForwarder<T, true>(range, numComp, dataArray, inputValues.seed, inputValues.standardizeSeed);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Plugins/SimplnxCore/test/InitializeDataTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -665,8 +665,8 @@ TEST_CASE("SimplnxCore::InitializeData 20: Multi Component Non-Standardized-Rand
}

::BoundsCheck<float32, false>(dataStructure.getDataRefAs<Float32Array>(::k_BaselinePath),
{std::numeric_limits<float32>::min(), std::numeric_limits<float32>::max(), std::numeric_limits<float32>::min(), std::numeric_limits<float32>::max(),
std::numeric_limits<float32>::min(), std::numeric_limits<float32>::max()});
{(-1 * (std::numeric_limits<float32>::max() - 1)), std::numeric_limits<float32>::max(), (-1 * (std::numeric_limits<float32>::max() - 1)),
std::numeric_limits<float32>::max(), (-1 * (std::numeric_limits<float32>::max() - 1)), std::numeric_limits<float32>::max()});
}

TEST_CASE("SimplnxCore::InitializeData 21: Boolean Single Component Fill Initialization", "[SimplnxCore][InitializeData]")
Expand Down

0 comments on commit 023c989

Please sign in to comment.