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

Add support for .NET Framework 4.7.1 and .NET Standard 2.1 #127

Merged
merged 5 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 8 additions & 1 deletion .github/workflows/integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,22 @@ jobs:

steps:
- uses: actions/checkout@v4

- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 8.0.x

- name: Install Mono
run: sudo apt-get install mono-complete -y
shell: bash

- name: Run integration tests
run: dotnet test --verbosity normal -p:CollectCoverage=true -p:CoverletOutputFormat=opencover DotMP-Tests

- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
with:
files: coverage.net6.0.opencover.xml
files: coverage.net8.0.opencover.xml
directory: DotMP-Tests
token: ${{ secrets.CODECOV_TOKEN }}
26 changes: 14 additions & 12 deletions DotMP-Tests/CPUTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -292,10 +292,10 @@ public void Reduction_collapse_works()
DotMP.Parallel.ParallelForReductionCollapse((256, 512), (512, 600),
op: Operations.Add, reduce_to: ref total_iters_executed,
num_threads: 8, chunk_size: 7, schedule: Schedule.Static,
action: (ref int total_iters_executed, int i, int j) =>
action: (ref int total_iters_executed_loc, int i, int j) =>
{
DotMP.Atomic.Inc(ref iters_hit[i, j]);
total_iters_executed += 1;
total_iters_executed_loc += 1;
});

for (int i = 0; i < 1024; i++)
Expand All @@ -315,10 +315,10 @@ public void Reduction_collapse_works()
DotMP.Parallel.ParallelForReductionCollapse((35, 64), (16, 100), (10, 62),
op: Operations.Add, reduce_to: ref total_iters_executed,
num_threads: 8, chunk_size: 3, schedule: Schedule.Dynamic,
action: (ref int total_iters_executed, int i, int j, int k) =>
action: (ref int total_iters_executed_loc, int i, int j, int k) =>
{
DotMP.Atomic.Inc(ref iters_hit_3[i, j, k]);
total_iters_executed += 1;
total_iters_executed_loc += 1;
});

for (int i = 0; i < 128; i++)
Expand All @@ -339,10 +339,10 @@ public void Reduction_collapse_works()
DotMP.Parallel.ParallelForReductionCollapse((1, 31), (10, 16), (5, 20), (21, 30),
op: Operations.Add, reduce_to: ref total_iters_executed,
num_threads: 8, chunk_size: 11, schedule: Schedule.Static,
action: (ref int total_iters_executed, int i, int j, int k, int l) =>
action: (ref int total_iters_executed_loc, int i, int j, int k, int l) =>
{
DotMP.Atomic.Inc(ref iters_hit_4[i, j, k, l]);
total_iters_executed += 1;
total_iters_executed_loc += 1;
});

total_iters_executed.Should().Be((31 - 1) * (16 - 10) * (20 - 5) * (30 - 21));
Expand All @@ -351,10 +351,10 @@ public void Reduction_collapse_works()
DotMP.Parallel.ParallelForReductionCollapse(new (int, int)[] { (1, 31), (10, 16), (5, 20), (21, 30) },
op: Operations.Add, reduce_to: ref total_iters_executed,
num_threads: 8, chunk_size: 11, schedule: Schedule.Static,
action: (ref int total_iters_executed, int[] indices) =>
action: (ref int total_iters_executed_loc, int[] indices) =>
{
DotMP.Atomic.Inc(ref iters_hit_4[indices[0], indices[1], indices[2], indices[3]]);
total_iters_executed += 1;
total_iters_executed_loc += 1;
});

total_iters_executed.Should().Be((31 - 1) * (16 - 10) * (20 - 5) * (30 - 21));
Expand Down Expand Up @@ -663,6 +663,7 @@ public void Atomic_works()
long_totals[2].Should().Be((long)threads * 2);
ulong_totals[2].Should().Be((ulong)threads * 2);

#if NET6_0_OR_GREATER
//and
DotMP.Parallel.ParallelRegion(num_threads: threads, action: () =>
{
Expand Down Expand Up @@ -712,6 +713,7 @@ public void Atomic_works()
uint_totals[4].Should().Be((uint)res);
long_totals[4].Should().Be((long)res);
ulong_totals[4].Should().Be((ulong)res);
#endif

uint_totals[5] = threads * 2 + 1;
ulong_totals[5] = threads * 2 + 3;
Expand Down Expand Up @@ -766,14 +768,14 @@ public void Ordered_works()
[Fact]
public void Reduction_works()
{
int total = 0;
int total_int = 0;

DotMP.Parallel.ParallelForReduction(0, 1024, DotMP.Operations.Add, ref total, num_threads: 8, chunk_size: 1, schedule: DotMP.Schedule.Static, action: (ref int total, int i) =>
DotMP.Parallel.ParallelForReduction(0, 1024, DotMP.Operations.Add, ref total_int, num_threads: 8, chunk_size: 1, schedule: DotMP.Schedule.Static, action: (ref int total, int i) =>
{
total += i;
});

total.Should().Be(1024 * 1023 / 2);
total_int.Should().Be(1024 * 1023 / 2);

long total_long = 0;

Expand Down Expand Up @@ -1691,7 +1693,7 @@ private static uint CreateRegion()

DotMP.Parallel.ParallelRegion(() =>
{
Interlocked.Add(ref threads_spawned, 1);
DotMP.Atomic.Inc(ref threads_spawned);
});

return threads_spawned;
Expand Down
2 changes: 1 addition & 1 deletion DotMP-Tests/DotMP-Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<TargetFrameworks>net471;net8.0</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
Expand Down
3 changes: 2 additions & 1 deletion DotMP-Tests/GPUTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text.Json.Serialization;
using System.Threading;
using DotMP;
using DotMP.GPU;
Expand All @@ -13,6 +12,7 @@

namespace DotMPTests
{
#if NET6_0_OR_GREATER
/// <summary>
/// GPU tests for the DotMP library.
/// </summary>
Expand Down Expand Up @@ -133,4 +133,5 @@ private void random_init<T>(T[] arr)
}
}
}
#endif
}
40 changes: 40 additions & 0 deletions DotMP/Atomic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public static int Dec(ref int target)
return Interlocked.Decrement(ref target);
}

#if NET6_0_OR_GREATER
/// <summary>
/// Performs a bitwise And operation on two specified 32-bit integers and replaces the first integer with the result, as an atomic operation.
/// </summary>
Expand All @@ -91,6 +92,7 @@ public static int Or(ref int target, int value)
{
return Interlocked.Or(ref target, value);
}
#endif
#endregion

#region UInt32
Expand All @@ -102,7 +104,11 @@ public static int Or(ref int target, int value)
/// <returns>The new value stored as a result of the operation.</returns>
public static uint Add(ref uint target, uint value)
{
#if NET6_0_OR_GREATER
return Interlocked.Add(ref target, value);
#else
return unchecked((uint)Interlocked.Add(ref Unsafe.As<uint, int>(ref target), Unsafe.As<uint, int>(ref value)));
#endif
}

/// <summary>
Expand All @@ -114,7 +120,11 @@ public static uint Add(ref uint target, uint value)
public static uint Sub(ref uint target, uint value)
{
uint neg = ~value + 1;
#if NET6_0_OR_GREATER
return Interlocked.Add(ref target, neg);
#else
return unchecked((uint)Interlocked.Add(ref Unsafe.As<uint, int>(ref target), Unsafe.As<uint, int>(ref neg)));
#endif
}

/// <summary>
Expand All @@ -124,7 +134,11 @@ public static uint Sub(ref uint target, uint value)
/// <returns>The new value stored as a result of the operation.</returns>
public static uint Inc(ref uint target)
{
#if NET6_0_OR_GREATER
return Interlocked.Increment(ref target);
#else
return unchecked((uint)Interlocked.Increment(ref Unsafe.As<uint, int>(ref target)));
#endif
}

/// <summary>
Expand All @@ -134,9 +148,14 @@ public static uint Inc(ref uint target)
/// <returns>The new value stored as a result of the operation.</returns>
public static uint Dec(ref uint target)
{
#if NET6_0_OR_GREATER
return Interlocked.Decrement(ref target);
#else
return unchecked((uint)Interlocked.Decrement(ref Unsafe.As<uint, int>(ref target)));
#endif
}

#if NET6_0_OR_GREATER
/// <summary>
/// Performs a bitwise And operation on two specified 32-bit unsigned integers and replaces the first integer with the result, as an atomic operation.
/// </summary>
Expand All @@ -158,6 +177,7 @@ public static uint Or(ref uint target, uint value)
{
return Interlocked.Or(ref target, value);
}
#endif
#endregion

#region Int64
Expand Down Expand Up @@ -203,6 +223,7 @@ public static long Dec(ref long target)
return Interlocked.Decrement(ref target);
}

#if NET6_0_OR_GREATER
/// <summary>
/// Performs a bitwise And operation on two specified 64-bit integers and replaces the first integer with the result, as an atomic operation.
/// </summary>
Expand All @@ -224,6 +245,7 @@ public static long Or(ref long target, long value)
{
return Interlocked.Or(ref target, value);
}
#endif
#endregion

#region UInt64
Expand All @@ -235,7 +257,11 @@ public static long Or(ref long target, long value)
/// <returns>The new value stored as a result of the operation.</returns>
public static ulong Add(ref ulong target, ulong value)
{
#if NET6_0_OR_GREATER
return Interlocked.Add(ref target, value);
#else
return unchecked((ulong)Interlocked.Add(ref Unsafe.As<ulong, long>(ref target), Unsafe.As<ulong, long>(ref value)));
#endif
}

/// <summary>
Expand All @@ -247,7 +273,11 @@ public static ulong Add(ref ulong target, ulong value)
public static ulong Sub(ref ulong target, ulong value)
{
ulong neg = ~value + 1;
#if NET6_0_OR_GREATER
return Interlocked.Add(ref target, neg);
#else
return unchecked((ulong)Interlocked.Add(ref Unsafe.As<ulong, long>(ref target), Unsafe.As<ulong, long>(ref neg)));
#endif
}

/// <summary>
Expand All @@ -257,7 +287,11 @@ public static ulong Sub(ref ulong target, ulong value)
/// <returns>The new value stored as a result of the operation.</returns>
public static ulong Inc(ref ulong target)
{
#if NET6_0_OR_GREATER
return Interlocked.Increment(ref target);
#else
return unchecked((ulong)Interlocked.Increment(ref Unsafe.As<ulong, long>(ref target)));
#endif
}

/// <summary>
Expand All @@ -267,9 +301,14 @@ public static ulong Inc(ref ulong target)
/// <returns>The new value stored as a result of the operation.</returns>
public static ulong Dec(ref ulong target)
{
#if NET6_0_OR_GREATER
return Interlocked.Decrement(ref target);
#else
return unchecked((ulong)Interlocked.Decrement(ref Unsafe.As<ulong, long>(ref target)));
#endif
}

#if NET6_0_OR_GREATER
/// <summary>
/// Performs a bitwise And operation on two specified 64-bit unsigned integers and replaces the first integer with the result, as an atomic operation.
/// </summary>
Expand All @@ -291,6 +330,7 @@ public static ulong Or(ref ulong target, ulong value)
{
return Interlocked.Or(ref target, value);
}
#endif
#endregion
}
}
11 changes: 4 additions & 7 deletions DotMP/DependencyGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
using System.Collections.Generic;
using System.Threading;

#nullable enable

namespace DotMP
{
/// <summary>
Expand Down Expand Up @@ -48,7 +46,6 @@ internal IntWrapper(int @int)
/// </summary>
internal class DAG<T, U> : IDisposable
where T : struct
where U : class?
{
/// <summary>
/// Counter for remaining tasks in queue.
Expand Down Expand Up @@ -113,9 +110,9 @@ internal void AddItem(T id, U item, T[] dependencies)
else
satisfies_dependency[d].Add(id);

unmet_dependencies.TryAdd(id, new IntWrapper(dependency_count));
unmet_dependencies.Add(id, new IntWrapper(dependency_count));

satisfies_dependency.TryAdd(id, new List<T>());
satisfies_dependency.Add(id, new List<T>());

if (dependency_count == 0)
no_dependencies.Add(id);
Expand All @@ -130,7 +127,7 @@ internal void AddItem(T id, U item, T[] dependencies)
/// <param name="id">The ID of the item returned from the DAG.</param>
/// <param name="tasks_remaining">The number of tasks remaining in the queue.</param>
/// <returns>Whether or not there was an item to be returned.</returns>
internal bool GetNextItem(out U? item, out T id, out int tasks_remaining)
internal bool GetNextItem(out U item, out T id, out int tasks_remaining)
{
tasks_remaining = this.tasks_remaining;

Expand All @@ -140,7 +137,7 @@ internal bool GetNextItem(out U? item, out T id, out int tasks_remaining)
}
else
{
item = null;
item = default;
return false;
}
}
Expand Down
4 changes: 3 additions & 1 deletion DotMP/DotMP.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<TargetFrameworks>netstandard2.1;net471;net6.0;net7.0;net8.0</TargetFrameworks>
<RootNamespace>DotMP</RootNamespace>
<PackageId>DotMP</PackageId>
<Version>2.0-pre1</Version>
Expand All @@ -24,6 +24,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="6.0.0" />
<PackageReference Include="ILGPU" Version="1.5.1" />
<PackageReference Include="T4.Build" Version="0.2.4" />

Expand Down
9 changes: 7 additions & 2 deletions DotMP/ForkedRegion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,15 @@ internal Thread CreateThread(Action omp_fn, int tid, uint num_threads)
}
catch (Exception ex)
{
#if NET6_0_OR_GREATER
this.ex ??= ex;
#else
if (this.ex == null)
this.ex = ex;
#endif
Parallel.canceled = true;

if (ex is not ThreadInterruptedException)
if (!(ex is ThreadInterruptedException))
for (int i = 0; i < num_threads; i++)
if (i != tid)
threads[i].Interrupt();
Expand Down Expand Up @@ -182,7 +187,7 @@ internal void StartThreadpool()

in_parallel = false;

if (reg.ex is not null)
if (reg.ex != null)
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Capture(reg.ex).Throw();
}
}
Expand Down
Loading