Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/exception handling method #87

Closed
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions DotMP/Exceptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,29 @@ public CannotPerformNestedWorksharingException(string msg) : base(msg) { }
/// <param name="ex">The inner exception.</param>
public CannotPerformNestedWorksharingException(string msg, Exception ex) : base(msg, ex) { }
}


/// <summary>
/// Exception thrown if a Parallel.Ordered is called outside of Parallel.For or Parallel.ForReduction.
/// </summary>
public class NotInForException: Exception
{
/// <summary>
/// Default constructor.
/// </summary>
public NotInForException() { }

/// <summary>
/// Constructor with a message.
/// </summary>
/// <param name="msg">The message to associate with the exception.</param>
public NotInForException(string msg): base(msg) { }

/// <summary>
/// Constructor with a message and inner exception.
/// </summary>
/// <param name="msg">The message to associate with the exception.</param>
/// <param name="ex">The inner exception.</param>
public NotInForException(string msg, Exception ex): base(msg, ex) { }
}
}
56 changes: 55 additions & 1 deletion DotMP/Parallel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ private static void FixArgs(int start, int end, ref Schedule sched, ref uint? ch
/// <exception cref="NotInParallelRegionException">Thrown when not in a parallel region.</exception>
public static void For(int start, int end, Action<int> action, Schedule schedule = Schedule.Static, uint? chunk_size = null)
{
ValidateParameters(start, end, chunk_size: chunk_size);

// jscpd:ignore-start
var freg = new ForkedRegion();

Expand Down Expand Up @@ -194,7 +196,8 @@ public static void For(int start, int end, Action<int> action, Schedule schedule
/// <exception cref="NotInParallelRegionException">Thrown when not in a parallel region.</exception>
public static void ForReduction<T>(int start, int end, Operations op, ref T reduce_to, ActionRef<T> action, Schedule schedule = Schedule.Static, uint? chunk_size = null)
{
// jscpd:ignore-start
ValidateParameters(start, end, chunk_size: chunk_size);

var freg = new ForkedRegion();

if (!freg.in_parallel)
Expand Down Expand Up @@ -280,6 +283,8 @@ public static void ForReduction<T>(int start, int end, Operations op, ref T redu
/// <exception cref="CannotPerformNestedParallelismException">Thrown if ParallelRegion is called from within another ParallelRegion.</exception>
public static void ParallelRegion(Action action, uint? num_threads = null)
{
ValidateParameters(0, 0, num_threads: num_threads);

if (InParallel())
throw new CannotPerformNestedParallelismException();

Expand Down Expand Up @@ -431,6 +436,8 @@ public static void Taskwait()
/// <returns>List of tasks generated by taskloop for use as future dependencies.</returns>
public static TaskUUID[] Taskloop(int start, int end, Action<int> action, uint? grainsize = null, uint? num_tasks = null, bool only_if = true, params TaskUUID[] depends)
{
ValidateParameters(start, end, num_tasks: num_tasks, grainsize: grainsize);

if (only_if)
{
ForkedRegion fr = new ForkedRegion();
Expand Down Expand Up @@ -693,6 +700,11 @@ public static void Ordered(int id, Action action)

WorkShare ws = new WorkShare();

if (!ws.in_for)
{
throw new NotInForException();
}

while (ordered[id] != ws.thread.working_iter)
{
freg.reg.spin[tid].SpinOnce();
Expand All @@ -704,6 +716,8 @@ public static void Ordered(int id, Action action)
{
ordered[id]++;
}

Barrier();
}

/// <summary>
Expand Down Expand Up @@ -834,5 +848,45 @@ public static uint GetChunkSize()
{
return new WorkShare().chunk_size;
}



/// <summary>
/// Validates the input parameters for a specific operation.
/// </summary>
/// <param name="start">The starting index.</param>
/// <param name="end">The ending index.</param>
/// <param name="numThreads">Number of threads (optional, set to null if not applicable).</param>
/// <param name="chunkSize">Chunk size (optional, set to null if not applicable).</param>
/// <param name="numTasks">Number of tasks (optional, set to null if not applicable).</param>
/// <param name="grainSize">Grain size (optional, set to null if not applicable).</param>
/// <exception cref="ArgumentException">Thrown when input parameters are invalid:
/// <para>- <paramref name="end"/> is less than <paramref name="start"/>.</para>
/// <para>- <paramref name="start"/> or <paramref name="end"/> is less than 0.</para>
/// <para>- <paramref name="numThreads"/> is 0.</para>
/// <para>- <paramref name="chunkSize"/> is 0.</para>
/// <para>- Either <paramref name="numTasks"/> or <paramref name="grainSize"/> is 0.</para>
/// </exception>
private static void ValidateParameters(
int start,
int end,
uint? num_threads = null,
uint? chunk_size = null,
uint? num_tasks = null,
uint? grainsize = null)
{
if (end < start) throw new ArgumentException("End index cannot be less than start index.");

if (start < 0 || end < 0) throw new ArgumentException("Start and end indices cannot be less than 0.");

if (num_threads is not null && num_threads == 0) throw new ArgumentException("Number of threads cannot be 0.");

if (chunk_size is not null && chunk_size == 0) throw new ArgumentException("Chunk size cannot be 0.");

if (num_tasks is not null
&& grainsize is not null
&& (num_tasks == 0 || grainsize == 0)) throw new ArgumentException("Number of tasks and grainsize cannot be 0.");

}
}
}