C# で Parallel プレフィックス合計を書きたいと思います。私はこのアルゴリズムを使用しました:
initial condition: list of n >= 1 elements stored in A[0...(n-1)]
final condition: each element A[i] contains A[0]+A[1]+...+A[i]
begin
spawn (p1,p2,...,p(n-1))
foe all pi where 1 <= i <= n-1 do
for j←0 to ⌈logn⌉-1 do
if i - 2^j >= 0 then
A[i] ← A[i] + A[i - 2^j]
end if
end for
end for
end
そして、c# での私の最終的なコードは次のとおりです。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MPI;
namespace prefixsum3
{
class Program
{
static void Main(string[] args)
{
int[] A = new int[] { 4, 3, 8, 2, 9, 1, 3, 5, 6, 3 };
using (new MPI.Environment(ref args))
{
Intracommunicator comm = Communicator.world;
int size, rank, n, i;
size = comm.Size;
i = comm.Rank + 1;
n = A.Length;
int[] B = new int[10];
for (int j = 0; j <= (Math.Ceiling(Math.Log(n))) - 1; j++)
{
int t = Convert.ToInt32(Math.Pow(2, j));
if ( i - t >= 0)
{
B[i] = A[i] + A[i - t];
}
comm.Barrier();
A[i] = B[i];
comm.Barrier();
}
if (comm.Rank == 0)
{
for (int z = 0; z < n; z++)
{
Console.Write(A[z].ToString() + ",");
}
}
}
}
}
}
ライトの出力は [4,7,15,17,26,27,30,35,41,44] です
が、私の出力コードは [4,7,8,2,9,1,3,5, 6,3]
私のコードの何が問題なのか知っている人はいますか?
編集:
すべてのプロセッサがローカルで配列 A を参照していることがわかりました。問題は、すべてのプロセッサが 1 つの配列を参照できるように、配列 A をグローバルに定義する方法です。