- Home /
IJobParallelForTransform is not Multi threaded?
when i Schedule " IJobParallelFor ", Profiler shows that all item are properly divided into batches and executed by all Workers threads equally, but not in the case of " IJobParalleteForTransform ", it just randomly pick just one single thread and let the main thread waiting longer in worst cases, if anyone know the fix, pls reply me and help me with this issue, Example code and Profiler Screenshot below,
So first here is the test script
Job script
//this job to calculate just positions loop
public struct XLoop : IJobParallelFor
{
public NativeArray<Vector3> posJ;
//some several other variables
public void Execute(int i)
{
//some calculations on local var x, y and z
posJ[i] = new Vector3(x, y, z);
}
}
//this job helps to just transform GameObjects
public struct GOPos : IJobParallelForTransform
{
[ReadOnly] public NativeArray<Vector3> pos;
public void Execute(int index, TransformAccess transform)
{
transform.position = pos[index];
}
}
Main Script:
public class Graph : MonoBehaviour
{
private JobHandle mathHandle;
private XLoop mathLoop;
private JobHandle gOHandle;
private GOPos gOpos;
private TransformAccessArray transforms;
private NativeArray<Vector3> pos;
//and some other var, not important to include here
private void Awake()
{
//this TransformArray is created struct, and "points" object of Transform in array
TransformArray.points = new Transform[10000];
//instantiating all the gameOject into array
for (int i = 0; i < TransformArray.points.Length; i++)
{
TransformArray.points[i] = Instantiate(pointPrefab, new Vector3(0f, 0f, -10f), Quaternion.identity, transform);
}
transforms = new TransformAccessArray(TransformArray.points, -1);
}
private void Update()
{
pos = new NativeArray<Vector3>(TransformArray.points.Length, Allocator.TempJob);
mathLoop = new XLoop()
{
posJ = pos,
//several other var for calculations in this job
};
gOpos = new GOPos()
{
pos = pos
};
mathHandle = mathLoop.Schedule(ResVar.CurrentFrameRes, 64);
mathHandle.Complete();
gOHandle = gOpos.Schedule(transforms , mathHandle);
JobHandle.ScheduleBatchedJobs();
}
private void LateUpdate()
{
gOHandle.Complete();
pos.Dispose();
}
//when this gameObjec Disables/Inactive
private void OnDisable()
{
mathHandle.Complete();
transforms.Dispose();
}
}
info related to attached Screenshot Image:
-"GOpos" (transform job) is depended on "Xloop" (pos calculation Job).
-"Xloop" is Implementing IJobParallelFor (mean all items are divided into batches) this worked good because it take Parameter as (int index, int batch count), so I can set how many Batches I want to Execute.
-However, GOPos is Implementing IJobParallelForTransform which take just one parameter as (transform array), meaning, it is taking Index from Transform.length, but not batch count, so i cannot set batch and so by default all item in the array are executed in one single Batch.
Answer by SauronDark · Dec 26, 2018 at 02:01 PM
so i finally found myself after a bit more digging, such a simple solution
Your answer
Follow this Question
Related Questions
Unity Threading Issue: Not able to delegate task 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
IJobParralelFor giving me random result. 0 Answers