Skip to content

Commit

Permalink
Merge pull request #41 from computablee/bug-fixes
Browse files Browse the repository at this point in the history
Tidy up sections / performance improvements for sections
  • Loading branch information
computablee authored Sep 20, 2023
2 parents 88c0b58 + 4bce611 commit 655cd1b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 18 deletions.
15 changes: 5 additions & 10 deletions DotMP/Parallel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ public static void ParallelForReduction<T>(int start, int end, Operations op, re
public static void Sections(params Action[] actions)
{
var freg = new ForkedRegion();
bool successful;

if (!freg.in_parallel)
{
Expand All @@ -375,19 +376,13 @@ public static void Sections(params Action[] actions)

Barrier();

while (sc.actions.Count > 0)
do
{
Action do_action;

lock (sc.actions)
{
if (sc.actions.Count > 0)
do_action = sc.actions.Dequeue();
else break;
}
Action do_action = sc.GetNextItem(out successful);

do_action();
if (successful) do_action();
}
while (successful);

Barrier();
}
Expand Down
19 changes: 11 additions & 8 deletions DotMP/Sections.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;

namespace DotMP
Expand All @@ -14,23 +15,25 @@ internal class SectionsContainer
/// <summary>
/// The actions submitted by the individual `section` directives.
/// </summary>
private static Queue<Action> actions_pv;
private static ConcurrentBag<Action> actions_pv;

/// <summary>
/// Getter for singleton queue SectionsContainer.actions_pv.
/// Gets the next item from the actions bag.
/// </summary>
internal Queue<Action> actions
/// <param name="successful">Whether or not fetching from the bag was successful.</param>
/// <returns>The action pulled from the bag, if successful. If unsuccessful, undefined.</returns>
internal Action GetNextItem(out bool successful)
{
get
{
return actions_pv;
}
Action action;
successful = actions_pv.TryTake(out action);
return action;
}

/// <summary>
/// Constructor which takes a list of actions and ensures the master thread assigns to SectionsContainer.actions_pv.
/// </summary>
/// <param name="actions">The actions that the Parallel.Sections region will perform.</param>
internal SectionsContainer(IEnumerable<Action> actions) =>
Parallel.Master(() => actions_pv = new Queue<Action>(actions));
Parallel.Master(() => actions_pv = new ConcurrentBag<Action>(actions));
}
}

0 comments on commit 655cd1b

Please sign in to comment.