-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add dedicated wasm implementation for Parallel.Invoke and Parallel.For (
#57283) Introduces a simple single-threaded implementation for Parallel.Invoke, .For, and .ForEach that bypasses Task and wait primitives so that in browser environments parallel loop operations complete synchronously on the current thread
- Loading branch information
Showing
6 changed files
with
104 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
...teropServices.JavaScript/tests/System/Runtime/InteropServices/JavaScript/ParallelTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Runtime.InteropServices.JavaScript; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace System.Runtime.InteropServices.JavaScript.Tests | ||
{ | ||
public static class ParallelTests | ||
{ | ||
// The behavior of APIs like Invoke depends on how many items they are asked to invoke | ||
[Theory] | ||
[InlineData(0)] | ||
[InlineData(1)] | ||
[InlineData(2)] | ||
[InlineData(5)] | ||
[InlineData(32)] | ||
[InlineData(250)] | ||
public static void ParallelInvokeActionArray(int count) | ||
{ | ||
var actions = new List<Action>(); | ||
int sum = 0, expected = 0; | ||
for (int i = 0; i < count; i++) { | ||
int j = i; | ||
actions.Add(() => { | ||
sum += j; | ||
}); | ||
expected += j; | ||
} | ||
|
||
Parallel.Invoke(actions.ToArray()); | ||
Assert.Equal(expected, sum); | ||
} | ||
|
||
[Theory] | ||
[InlineData(0)] | ||
[InlineData(1)] | ||
[InlineData(32)] | ||
[InlineData(250)] | ||
public static void ParallelFor(int count) | ||
{ | ||
int sum = 0, expected = 0; | ||
for (int i = 0; i < count; i++) | ||
expected += i; | ||
Parallel.For(0, count, (i) => { sum += i; }); | ||
Assert.Equal(expected, sum); | ||
} | ||
|
||
[Theory] | ||
[InlineData(0)] | ||
[InlineData(1)] | ||
[InlineData(32)] | ||
[InlineData(250)] | ||
public static void ParallelForEach(int count) | ||
{ | ||
int sum = 0, expected = 0; | ||
var items = new List<int>(); | ||
for (int i = 0; i < count; i++) { | ||
items.Add(i); | ||
expected += i; | ||
} | ||
Parallel.ForEach(items, (i) => { sum += i; }); | ||
Assert.Equal(expected, sum); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters