0

Здравствуйте, помогите с MPI.NET C# кто сталкивался? Или изучал.

Я хочу сделать умножение матриц через MPI. Написал "нерабочий" код и целый день не могу разобраться, что не так, потому, что источников очень мало и они поверхностно описывают работу с процессами MPI.

Есть реализация умножения матриц на C++, пытался переписать на C#, но методы MPI_Send, MPI_Receive работают по разному.

public class Multiplicator
{
    private const int Root = 0;

    const int FROM_MASTER = 2;

    const int FROM_WORKER = 2;

    public int N { get { return n; } }

    public double[,] A { get { return a; } }
    public double[,] B { get { return b; } }

    public bool IsMaster { get; private set; }

    private int n;

    private double[,] a;

    private double[,] b;

    private double[,] c;

    public void SetMatrixes(double[,] a, double[,] b, int nn) //initializing matrixes
    {
        n = nn;
        this.a = a;
        this.b = b;
    }

    public void Solve(MPI.Intracommunicator mpi)
    {
        IsMaster = mpi.Rank == 0; // proccess with rank 0            

        int numtasks,              /* number of tasks in partition */
            taskid,                /* a task identifier */
            numworkers,            /* number of worker tasks */
            source,                /* task id of message source */
            dest,                  /* task id of message destination */
            mtype,                 /* message type */
            rows,                  /* rows of matrix A sent to each worker */
            averow, extra, offset, /* used to determine rows sent to each worker */
            i, j, k, rc;           /* misc */

        numworkers = mpi.Size - 1;

        if (IsMaster) //если ранг = 0
        {
            /* Send matrix data to the worker tasks */
            averow = N / numworkers;
            extra = N % numworkers;
            offset = 0;
            mtype = FROM_MASTER;
            for (dest = 1; dest <= numworkers; dest++)
            {
                rows = (dest <= extra) ? averow + 1 : averow;
                Console.WriteLine("Sending {0} rows to task {1} offset={2}\n", rows, dest, offset);
                mpi.Send(offset, dest, mtype);
                mpi.Send(rows, dest, mtype);
                mpi.Send(a[offset, 0], dest, mtype);
                mpi.Send(b, dest, mtype);
                offset = offset + rows;
            }

            /* Receive results from worker tasks */
            mtype = FROM_WORKER;
            for (i = 1; i <= numworkers; i++)
            {
                source = i;
                mpi.Receive(source, mtype, out offset);
                mpi.Receive(source, mtype, out rows);
                mpi.Receive(source, mtype, out c[offset, 0]);
                Console.WriteLine("Received results from task \n", source);
            }

            /* Print results */
            Console.WriteLine("******************************************************\n");
            Console.WriteLine("Result Matrix:\n");
            for (i = 0; i < N; i++)
            {
                Console.WriteLine("\n");
                for (j = 0; j < N; j++)
                    Console.Write(string.Format("{0} ", c[i, j]));
            }
            Console.WriteLine("\n******************************************************\n");

        }
        if (!IsMaster)
        {
            mtype = FROM_MASTER;
            mpi.Receive<int>(0, mtype);
            mpi.Receive(0, mtype, out rows);
            mpi.Receive(0, mtype, out a);
            mpi.Receive(0, mtype, out b);

            for (k = 0; k < N; k++)
                for (i = 0; i < rows; i++)
                {
                    c[i,k] = 0.0;
                    for (j = 0; j < N; j++)
                        c[i,k] = c[i,k] + a[i,j] * b[j,k];
                }
            mtype = FROM_WORKER;
            offset = 0;
            mpi.Send(offset, 0, mtype);
            mpi.Send(rows, 0, mtype);
            mpi.Send(c, 0, mtype);
        }

    }
}
2
  • Положи код в тело вопроса. 8 окт 2017 в 14:53
  • Здравствуйте, Владимир, вот код, он ужасен( 8 окт 2017 в 15:03

0

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Посмотрите другие вопросы с метками или задайте свой вопрос.