Skip to content

Commit

Permalink
Merge pull request #114 from computablee/features
Browse files Browse the repository at this point in the history
Implement optimized index calculations for collapse(4) and higher
  • Loading branch information
computablee authored Nov 16, 2023
2 parents 20c89b1 + 8444e6b commit 87f1fb8
Showing 1 changed file with 52 additions and 10 deletions.
62 changes: 52 additions & 10 deletions DotMP/Wrappers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,15 @@ private enum ActionSelector
private ValueTuple<int, int>[] ranges;

/// <summary>
/// Array of indices to pass to actions.
/// Array representing the starting indices.
/// </summary>
private int[] indices;

/// <summary>
/// Array representing the ending indices.
/// </summary>
private int[] end_indices;

/// <summary>
/// Array which represents the length of collapsed loops in each dimension.
/// </summary>
Expand Down Expand Up @@ -266,6 +271,7 @@ internal ForAction(Action<int, int, int, int> action, (int, int)[] ranges)
IsCollapse = true;
IsReduction = false;
indices = new int[4];
end_indices = new int[4];
diffs = ranges.Select(r => r.Item2 - r.Item1).ToArray();
}

Expand All @@ -282,6 +288,7 @@ internal ForAction(Action<int[]> action, (int, int)[] ranges)
IsCollapse = true;
IsReduction = false;
indices = new int[ranges.Length];
end_indices = new int[ranges.Length];
diffs = ranges.Select(r => r.Item2 - r.Item1).ToArray();
}

Expand Down Expand Up @@ -326,6 +333,7 @@ internal ForAction(ActionRef4<T> action, (int, int)[] ranges)
IsCollapse = true;
IsReduction = true;
indices = new int[4];
end_indices = new int[4];
diffs = ranges.Select(r => r.Item2 - r.Item1).ToArray();
}

Expand All @@ -342,6 +350,7 @@ internal ForAction(ActionRefN<T> action, (int, int)[] ranges)
IsCollapse = true;
IsReduction = true;
indices = new int[ranges.Length];
end_indices = new int[ranges.Length];
diffs = ranges.Select(r => r.Item2 - r.Item1).ToArray();
}

Expand Down Expand Up @@ -386,7 +395,8 @@ private void ComputeIndices3(int curr_iter, int start1, int start2, int start3,
/// Computes the indices for collapsed loops with 4 or more indices.
/// </summary>
/// <param name="curr_iter">The current iteration to unflatten.</param>
private void ComputeIndicesN(int curr_iter)
/// <param name="indices">The array to store indices in.</param>
private void ComputeIndicesN(int curr_iter, int[] indices)
{
int mod = curr_iter;
for (int r = 0; r < ranges.Length - 1; r++)
Expand Down Expand Up @@ -483,17 +493,33 @@ internal void PerformLoop(ref int curr_iter, int start, int end, ref T local)
}
break;
case ActionSelector.Collapse4:
for (curr_iter = start; curr_iter < end; curr_iter++)
ComputeIndicesN(start, indices);
ComputeIndicesN(end, end_indices);

while (!indices.SequenceEqual(end_indices))
{
ComputeIndicesN(curr_iter);
omp_col_4(indices[0], indices[1], indices[2], indices[3]);

for (int i = 3; i >= 0; i--)
if (++indices[i] == ranges[i].Item2 && i > 0)
indices[i] = ranges[i].Item1;
else
break;
}
break;
case ActionSelector.CollapseN:
for (curr_iter = start; curr_iter < end; curr_iter++)
ComputeIndicesN(start, indices);
ComputeIndicesN(end, end_indices);

while (!indices.SequenceEqual(end_indices))
{
ComputeIndicesN(curr_iter);
omp_col_n(indices);

for (int i = indices.Length - 1; i >= 0; i--)
if (++indices[i] == ranges[i].Item2 && i > 0)
indices[i] = ranges[i].Item1;
else
break;
}
break;
case ActionSelector.ReductionCollapse2:
Expand Down Expand Up @@ -547,17 +573,33 @@ internal void PerformLoop(ref int curr_iter, int start, int end, ref T local)
}
break;
case ActionSelector.ReductionCollapse4:
for (curr_iter = start; curr_iter < end; curr_iter++)
ComputeIndicesN(start, indices);
ComputeIndicesN(end, end_indices);

while (!indices.SequenceEqual(end_indices))
{
ComputeIndicesN(curr_iter);
omp_red_col_4(ref local, indices[0], indices[1], indices[2], indices[3]);

for (int i = 3; i >= 0; i--)
if (++indices[i] == ranges[i].Item2 && i > 0)
indices[i] = ranges[i].Item1;
else
break;
}
break;
case ActionSelector.ReductionCollapseN:
for (curr_iter = start; curr_iter < end; curr_iter++)
ComputeIndicesN(start, indices);
ComputeIndicesN(end, end_indices);

while (!indices.SequenceEqual(end_indices))
{
ComputeIndicesN(curr_iter);
omp_red_col_n(ref local, indices);

for (int i = indices.Length - 1; i >= 0; i--)
if (++indices[i] == ranges[i].Item2 && i > 0)
indices[i] = ranges[i].Item1;
else
break;
}
break;
}
Expand Down

0 comments on commit 87f1fb8

Please sign in to comment.