I'm trying to use Unity Job system to speed up some calculation related to Reaction-Diffusion simulation. The calculations requires 2 matrixes, current
and next
and the current
will be used to calculated next
, then next
will become current
. I wanted each job to handle a single row of the matrix.
The problem lies in the Laplace()
function, which when inputed a coordination, return a value based on that cell and 8 other cells around it:
private float laplaceA(int x, int y)
{
float sum = 0f;
for (int idx = -1; idx <= 1; idx++)
{
for (int j = -1; j <= 1; j++)
{
int newX = x + idx, newY = y + j;
if (withinBorder(newX, newY))
{
float val = cur[newX*height + newY].x;
if (idx == 0 && j == 0) { sum += val * -1f;} //center
else if (Absy(idx) == 1 && Absy(j) == 1) {sum += val * 0.05f;} //diagonal
else { sum += val * 0.2f;} //adjacent
}
}
}
return sum;
//NOTE: Absy() is just Mathf.Abs() rewrite for using jobs
}
I tried some ways to make it works:
IJobParallel
seems promising at first, but then I realizeLaplace()
can't access what it need, as I need the previous and next row, not just the current row (hence this post).- Tried
NativeArray<NativeArray<float2>>
to pass in each array one of a time, but failed as Unity doesn't support this yet.List<NativeArray<float2>>
to try and sneak in failed as well. - Tried using just
IJob
and creating aNativeArray<>
each row and a job with it, but I haven't figured out a way to store each array to dispose of it later.
Any idea to fix this problem, or other methods for speeding up calculations?
NativeArray<float2>
? \$\endgroup\$List<List<Vector2>> current, next
that everything take information from. So the general process looks like: Copy info fromcurrent
toNativeArray<>
-> Get allJobHandle
into a list of some sort and callCompleteAll
-> Copy back info fromNativeArray<>
tonext
. So since nestedNativeArray
aren't allow, I don't know how to dispose of theNativeArray<>
created in step 1. \$\endgroup\$NativeArray<>
(s) and do everything with justNativeArray<>
that is declared withAllocator.Persistent
, but I'd have to rewrite a lot of the code 'cause I have to flatten the matrixes etc... and IMO I just don't like the general idea. \$\endgroup\$