Usage of Parallel also for dependant references to the underlying span/array?

64 views Asked by At

Consider this hypothetical code-snippet:

for (slotIdx = 1; slotIdx < slotCount/2; slotIdx++)
{              
    //we always have to go 2x slotIdx, to find the value we wanna swap behind!
    int safeNextValueIdx = slotIdx * 2;
    ref Slot nextSegmentValue = ref layout[safeNextValueIdx];
    ref Slot currPadding = ref layout[slotIdx];

    //DO NOT CHANGE THE <EXECUTION-ORDER> OF THIS CODE BLOCK! IT WORKS INTENDED IN THIS ORDER!
    short shift = tmpSrc.Swap(ref currPadding, ref nextSegmentValue);
    SwapPositions(ref nextSegmentValue, ref currPadding);
    AdjustPosition(layout, shift, safeNextValueIdx , slotIdx - 1);
}

From your guys perspective, can I somehow make use of, and hence get the benefits of, using Parallel class here?

I tried already with Parallel.For but it does super weird things because sadly the elements depend sort of each other and hence are being messed up by multiple threads apparently, but could the Partitioner.Create(source) work here sort of, so that I can somehow slice the entire operation into independent chunks, so that

 ref Slot nextSlot = ref layout[safeNextValueIdx];
 ref Slot current = ref layout[slotIdx];

always get their values from a unique range and do not overlap with the already reserved indices?

I literally hope I did formulate myself properly!

More info: I am doing internal swaps to that layout span (slots[]) and Slot is a struct which has some data like {id, type, oldPos, newPos}. Then I am using that layout to extract certain information of a very large string I get from outside, like some complex generated hash string where I have a pattern how to find certain things in it. This is a rough explanation because it's internal stuff which I cannot share.

1

There are 1 answers

3
OwnageIsMagic On

You have dependencies all the way from start to end (n depends on n-1), so if you can't break this chain -- you can't parallelize anything here.