如何在MPI.NET for C#中指定1-D数组的子集作为发送或接收缓冲区?

时间:2017-06-05 23:32:53

标签: c# mpi

我正在使用MPI.NET学习C#并行编程。在下面给出的示例中,我在每个过程中定义了一维数组(x),然后我对分配给该过程的相应部分(即只有x的指定部分)进行了一些简单的计算,以获得其中的相应部分( Y)。我的主要兴趣是将所有这些分配的部分(在每个过程中计算的y的一部分)收集到根过程的y数组中,以便能够最终计算总和。我的意思是,我想从位于根进程的y-array上的相应部分的所有进程中复制每个已分配的部分。但是,我不能这样做;我唯一能做的就是将1-D数组收集到2-D数组中,或者将所有数组聚集在一个新定义的1-D数组中,其大小为“comm.size * y.length”。当我在C ++中使用“MPI_Gather($ sendbuf,sendcnt,sendtype,& recbuf,recvcount,root,comm)”关键字进行搜索时,我们能够执行此任务,因为它会向我发送“MPI.Gather”在C#中是不同的,它在C ++中没有MPI_Gather的灵活性。我需要将每个进程中y的所有计算部分收集到根进程的y数组中的相应位置。换句话说,如何在MPI.NET for C#中指定数组的子集作为发送或接收缓冲区。如果你在这件事上帮助我,我将不胜感激。

            using (new MPI.Environment(ref args))
        {

            double sumSerial = 0;
            double sumParallel = 0;
            int arraySize = 100000;
            double[] x = new double[arraySize];
            double[] y = new double[arraySize];

            Intracommunicator comm = Communicator.world;

            int numProc = comm.Size;

            int numItr = arraySize / numProc;


            for (int i = 0; i < x.Length; i++)
            {
                x[i] = i;
                sumSerial += i;
            }


            int firstInx = comm.Rank * numItr;
            int lastInx = firstInx + numItr;


            for (int i = firstInx; i < lastInx; i++)
            {

                y[i] = 5.0 * x[i];


            }


            //double[][] zz=comm.Gather<double[]>(y,0);
            double[] z = comm.GatherFlattened(y, 0);

            comm.Barrier();


            if (comm.Rank==0)
            {

                //for (int i = 0; i < numProc; i++)
                //{
                //    for (int j = 0; j < zz[0].Length; j++)
                //    {
                //        sumParallel += zz[i][j];
                //    }

                //}

                for (int i = 0; i < z.Length; i++)
                {
                    sumParallel += z[i];
                }



                Console.WriteLine("sum_Parallel: {0}; sum_Serial= {1}; 
                    Ratio: {2}; z_length: {3}", sumParallel, sumSerial,
                    sumParallel / sumSerial, z.Length);


            }



        }

0 个答案:

没有答案