Created
May 9, 2023 05:17
-
-
Save kleinron/33421ef7048aef59731d604d99f4df83 to your computer and use it in GitHub Desktop.
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
using System.Threading; | |
using System.Threading.Tasks; | |
namespace Bronze.Lib | |
{ | |
public class ParallelWithInterlock | |
{ | |
public int Sum(int[,] matrix) | |
{ | |
var outerUpperBound = matrix.GetLength(0); | |
var commonSum = 0; | |
var innerUpperBound = matrix.GetLength(1); | |
Parallel.For(0, outerUpperBound, i => | |
{ | |
var localSum = 0; | |
for (var j = 0; j < innerUpperBound; j++) | |
localSum += matrix[i, j]; | |
Interlocked.Add(ref commonSum, localSum); | |
}); | |
return commonSum; | |
} | |
} | |
} |
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
using System.Linq; | |
using System.Threading.Tasks; | |
namespace Bronze.Lib | |
{ | |
public class ParallelAggregatorWithLocalSum | |
{ | |
public int Sum(int[,] matrix) | |
{ | |
var outerUpperBound = matrix.GetLength(0); | |
var partialSums = new int[outerUpperBound]; | |
var innerUpperBound = matrix.GetLength(1); | |
Parallel.For(0, outerUpperBound, i => | |
{ | |
var localSum = 0; | |
for (var j = 0; j < innerUpperBound; j++) | |
localSum += matrix[i, j]; | |
partialSums[i] = localSum; | |
}); | |
var result = partialSums.Sum(); | |
return result; | |
} | |
} | |
} |
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
using System.Linq; | |
using System.Threading.Tasks; | |
namespace Bronze.Lib | |
{ | |
public class ParallelAggregator | |
{ | |
public int Sum(int[,] matrix) | |
{ | |
var outerUpperBound = matrix.GetLength(0); | |
var partialSums = new int[outerUpperBound]; | |
var innerUpperBound = matrix.GetLength(1); | |
Parallel.For(0, outerUpperBound, i => | |
{ | |
partialSums[i] = 0; | |
for (var j = 0; j < innerUpperBound; j++) | |
partialSums[i] += matrix[i, j]; | |
}); | |
var result = partialSums.Sum(); | |
return result; | |
} | |
} | |
} |
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
namespace Bronze.Lib | |
{ | |
public class SimpleAggregator | |
{ | |
public int Sum(int[,] matrix) | |
{ | |
var result = 0; | |
for (var i = 0; i < matrix.GetLength(0); i++) | |
for (var j = 0; j < matrix.GetLength(1); j++) | |
result += matrix[i, j]; | |
return result; | |
} | |
} | |
} |
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
1. common_sum = 0 | |
2. Parallel: for each row in matrix: | |
2.1 temp = sum(row) | |
2.2 append temp to common_sum |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment