Skip to content

Instantly share code, notes, and snippets.

@kleinron
Created May 9, 2023 05:17
Show Gist options
  • Save kleinron/33421ef7048aef59731d604d99f4df83 to your computer and use it in GitHub Desktop.
Save kleinron/33421ef7048aef59731d604d99f4df83 to your computer and use it in GitHub Desktop.
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;
}
}
}
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;
}
}
}
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;
}
}
}
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;
}
}
}
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