Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Vector& SquareMatrix::MPImulVector(const Vector& vector) const {
- double* arr = this->array;
- double result[size];
- double* buffer;
- int amount, rank;
- MPI_Init(&argc, &argv);
- MPI_Comm_size(MPI_COMM_WORLD, &amount);
- MPI_Comm_rank(MPI_COMM_WORLD, &rank);
- unsigned lines = size / amount;
- unsigned remLines = size - amount * lines;
- double res[lines];
- if (rank == 0) {
- buffer = arr;
- for (unsigned it = 1; it < amount - 1; ++it)
- MPI_Send(arr + it * lines * size, lines * size, MPI_DOUBLE, it, 0, MPI_COMM_WORLD);
- if (amount > 1)
- MPI_Send(arr + (amount - 1) * lines * size, (lines + remLines) * size, MPI_DOUBLE, amount - 1, 0, MPI_COMM_WORLD);
- }
- else if (rank != amount - 1) {
- double buffer[lines * size];
- MPI_Recv(buffer, lines * size, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
- }
- else {
- lines += remLines;
- double buffer[lines * size];
- MPI_Recv(buffer, lines * size, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
- }
- for (unsigned it = 0; it != lines; ++it)
- res[it] = 0;
- for (unsigned it = 0; it != lines; ++it)
- for (unsigned j = 0; j != size; ++j)
- res[it] += buffer[it * size + j] * vector[j];
- if (rank != 0) {
- MPI_Send(res, lines, MPI_DOUBLE, 0, 1, MPI_COMM_WORLD);
- }
- else {
- for (unsigned it = 0; it != lines; ++it)
- result[it] = res[it];
- for (unsigned it = 1; it < amount - 1; ++it)
- MPI_Recv(result + it * lines, lines, MPI_DOUBLE, it, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
- if (amount > 1)
- MPI_Recv(result + (amount - 1) * lines, lines + remLines, MPI_DOUBLE, amount - 1, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
- }
- Vector& New = *(new Vector(result, size));
- MPI_Finalize();
- return New;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement