p.Chaos.Spring.ParallelConstraintCount

p.Chaos.Spring.ParallelConstraintCount

#Overview

name: p.Chaos.Spring.ParallelConstraintCount

This variable is created as a Console Variable (cvar).

It is referenced in 6 C++ source files.

#Summary

#Usage in the C++ source code

The purpose of p.Chaos.Spring.ParallelConstraintCount is to control the threshold for parallel processing of spring constraints in Unreal Engine’s Chaos physics system. It determines when the system should switch from sequential to parallel execution for applying spring constraints.

This setting variable is primarily used in the Chaos physics subsystem, specifically in the spring constraint solver module. It’s part of the Experimental Chaos runtime in Unreal Engine.

The value of this variable is set through a console variable (CVar) system, allowing it to be adjusted at runtime in non-shipping builds. It’s initialized with a default value of 100.

The associated variable Chaos_Spring_ParallelConstraintCount directly interacts with p.Chaos.Spring.ParallelConstraintCount. They share the same value, with the CVar system providing a way to modify this value during runtime.

Developers should be aware that:

  1. This variable affects performance optimization, not physics accuracy.
  2. It’s only effective in non-shipping builds due to the #if !UE_BUILD_SHIPPING condition.
  3. Changing this value can impact performance, especially for simulations with many spring constraints.

Best practices when using this variable include:

  1. Profiling the application to determine the optimal threshold for your specific use case.
  2. Testing different values to find the best balance between parallel execution overhead and performance gains.
  3. Considering the target hardware when setting this value, as optimal thresholds may vary across different systems.

Regarding the associated variable Chaos_Spring_ParallelConstraintCount:

Developers should note that modifying this variable can have wide-reaching effects on the physics simulation performance across different types of constraints in the Chaos system.

#References in C++ code

#Callsites

This variable is referenced in the following C++ source code:

#Loc: <Workspace>/Engine/Source/Runtime/Experimental/Chaos/Private/Chaos/PBDSpringConstraints.cpp:27

Scope (from outer to inner):

file
namespace    Chaos::Softs

Source code excerpt:

static int32 Chaos_Spring_ParallelConstraintCount = 100;
#if !UE_BUILD_SHIPPING
FAutoConsoleVariableRef CVarChaosSpringParallelConstraintCount(TEXT("p.Chaos.Spring.ParallelConstraintCount"), Chaos_Spring_ParallelConstraintCount, TEXT("If we have more constraints than this, use parallel-for in Apply."));
#endif

template<typename SolverParticlesOrRange>
void FPBDSpringConstraints::InitColor(const SolverParticlesOrRange& Particles)
{
	// In dev builds we always color so we can tune the system without restarting. See Apply()

#Associated Variable and Callsites

This variable is associated with another variable named Chaos_Spring_ParallelConstraintCount. They share the same value. See the following C++ source code.

#Loc: <Workspace>/Engine/Source/Runtime/Experimental/Chaos/Private/Chaos/PBDSpringConstraints.cpp:25

Scope (from outer to inner):

file
namespace    Chaos::Softs

Source code excerpt:


// @todo(chaos): the parallel threshold (or decision to run parallel) should probably be owned by the solver and passed to the constraint container
static int32 Chaos_Spring_ParallelConstraintCount = 100;
#if !UE_BUILD_SHIPPING
FAutoConsoleVariableRef CVarChaosSpringParallelConstraintCount(TEXT("p.Chaos.Spring.ParallelConstraintCount"), Chaos_Spring_ParallelConstraintCount, TEXT("If we have more constraints than this, use parallel-for in Apply."));
#endif

template<typename SolverParticlesOrRange>
void FPBDSpringConstraints::InitColor(const SolverParticlesOrRange& Particles)
{
	// In dev builds we always color so we can tune the system without restarting. See Apply()
#if UE_BUILD_SHIPPING || UE_BUILD_TEST
	if (Constraints.Num() > Chaos_Spring_ParallelConstraintCount)
#endif
	{
		const TArray<TArray<int32>> ConstraintsPerColor = FGraphColoring::ComputeGraphColoringParticlesOrRange(Constraints, Particles, ParticleOffset, ParticleOffset + ParticleCount);
		
		// Reorder constraints based on color so each array in ConstraintsPerColor contains contiguous elements.
		TArray<TVec2<int32>> ReorderedConstraints;

#Loc: <Workspace>/Engine/Source/Runtime/Experimental/Chaos/Private/Chaos/PBDSpringConstraints.cpp:96

Scope (from outer to inner):

file
namespace    Chaos::Softs
function     void FPBDSpringConstraints::Apply

Source code excerpt:

	TRACE_CPUPROFILER_EVENT_SCOPE(FPBDSpringConstraints_Apply);
	SCOPE_CYCLE_COUNTER(STAT_PBD_Spring);
	if ((ConstraintsPerColorStartIndex.Num() > 1) && (Constraints.Num() > Chaos_Spring_ParallelConstraintCount))
	{
		const int32 ConstraintColorNum = ConstraintsPerColorStartIndex.Num() - 1;
		if (!Stiffness.HasWeightMap())
		{
			const FSolverReal ExpStiffnessValue = (FSolverReal)Stiffness;

#Loc: <Workspace>/Engine/Source/Runtime/Experimental/Chaos/Public/Chaos/PBDAltitudeSpringConstraints.h:307

Scope (from outer to inner):

file
namespace    Chaos::Softs
class        class FPBDAltitudeSpringConstraints
function     void ApplyInParallel

Source code excerpt:

			{
				TRACE_CPUPROFILER_EVENT_SCOPE(STAT_ChaosPBDAltitudeSpringApply);
				if ((ConstraintsPerColorStartIndex.Num() > 1))//&& (MeshConstraints.Num() > Chaos_Spring_ParallelConstraintCount))
				{
					const int32 ConstraintColorNum = ConstraintsPerColorStartIndex.Num() - 1;

					for (int32 ConstraintColorIndex = 0; ConstraintColorIndex < ConstraintColorNum; ++ConstraintColorIndex)
					{
						const int32 ColorStart = ConstraintsPerColorStartIndex[ConstraintColorIndex];

#Loc: <Workspace>/Engine/Source/Runtime/Experimental/Chaos/Public/Chaos/XPBDCorotatedConstraints.h:295

Scope (from outer to inner):

file
namespace    Chaos::Softs
class        class FXPBDCorotatedConstraints
function     void ApplyInParallel

Source code excerpt:

				SCOPE_CYCLE_COUNTER(STAT_ChaosXPBDCorotated);
				TRACE_CPUPROFILER_EVENT_SCOPE(STAT_ChaosXPBDCorotatedApply);
				if ((ConstraintsPerColorStartIndex.Num() > 1))//&& (MeshConstraints.Num() > Chaos_Spring_ParallelConstraintCount))
				{
					const int32 ConstraintColorNum = ConstraintsPerColorStartIndex.Num() - 1;

					for (int32 ConstraintColorIndex = 0; ConstraintColorIndex < ConstraintColorNum; ++ConstraintColorIndex)
					{
						const int32 ColorStart = ConstraintsPerColorStartIndex[ConstraintColorIndex];

#Loc: <Workspace>/Engine/Source/Runtime/Experimental/Chaos/Public/Chaos/XPBDVolumeConstraints.h:80

Scope (from outer to inner):

file
namespace    Chaos::Softs
class        class FXPBDVolumeConstraints
function     void ApplyInParallel

Source code excerpt:

			{
				TRACE_CPUPROFILER_EVENT_SCOPE(STAT_ChaosXPBDVolumeApply);
				if ((ConstraintsPerColorStartIndex.Num() > 1))//&& (MeshConstraints.Num() > Chaos_Spring_ParallelConstraintCount))
				{
					const int32 ConstraintColorNum = ConstraintsPerColorStartIndex.Num() - 1;

					for (int32 ConstraintColorIndex = 0; ConstraintColorIndex < ConstraintColorNum; ++ConstraintColorIndex)
					{
						const int32 ColorStart = ConstraintsPerColorStartIndex[ConstraintColorIndex];