-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e8ad5fe
commit f3b19ec
Showing
1 changed file
with
50 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace Task02 | ||
{ | ||
public class ParallelAlgoFloyd | ||
{ | ||
static int[,] dist; | ||
|
||
static void parallelProc(int n, int k, int i) | ||
{ | ||
for (int j = 0; j < n; j++) | ||
{ | ||
if (j != k) | ||
{ | ||
if (dist[i, j] > dist[i, k] + dist[k, j]) | ||
dist[i, j] = dist[i, k] + dist[k, j]; | ||
} | ||
} | ||
} | ||
|
||
public static int[,] Execute() | ||
{ | ||
int n = GeneralResources.n; | ||
dist = new int[n, n]; | ||
Task[] tasks = new Task[n - 1]; | ||
Array.Copy(GeneralResources.graphMatrix, dist, n * n); | ||
|
||
for (int k = 0; k < n; k++) | ||
{ | ||
for (int i = 0; i < n; i++) | ||
{ | ||
if (i != k) | ||
{ | ||
int newK = k; | ||
int newI = i; | ||
// передаём newK и newI для того, чтобы избежать замыкания | ||
tasks[i < k ? i : i - 1] = Task.Run(() => parallelProc(n, newK, newI)); | ||
} | ||
} | ||
// ждём выполнения всех задач для того, чтобы случайно не получилось так, что | ||
// задача с большим k не обогнала задачу с меньшим k (и чтобы нам всегда хватило | ||
// ровно n - 1 задачи) | ||
Task.WaitAll(tasks); | ||
} | ||
|
||
return dist; | ||
} | ||
} | ||
} |