Skip to content

Instantly share code, notes, and snippets.

@Zamony
Last active December 30, 2017 10:33
Show Gist options
  • Save Zamony/73a2dc8be3f01f5ae4557740eabd14c6 to your computer and use it in GitHub Desktop.
Save Zamony/73a2dc8be3f01f5ae4557740eabd14c6 to your computer and use it in GitHub Desktop.
Умножение матриц методом Фокса / Matrix Multiplication by Fox Method in C
/*
MIT License
Copyright (c) 2017 Mochalov Nikita Sergeevich
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include "mpi.h"
typedef long long ll;
void print_submatrix(ll *matrix, int n_size, int N){
int i, j;
for (i = 0; i < N; i++){
for (j = 0; j < N; j++){
if (i < n_size && j < n_size)
printf("%lld ", matrix[i*N + j]);
}
printf("\n");
}
}
void enter_matrix(ll *matrix, int n_size, int N){
int i, j;
for (i = 0; i < N; i++){
for (j = 0; j < N; j++){
if (i < n_size && j < n_size)
scanf("%lld", &matrix[i*N + j]);
else
matrix[i*N + j] = 0;
}
}
}
void get_submatrix(
ll *matrix,
ll *submatrix,
int _i,
int _j,
int block_size,
int N
)
{
int i, j;
for (i = 0; i < block_size; i++)
for (j = 0; j < block_size; j++)
submatrix[i*block_size + j] = matrix[(_i+i)*N+_j+j];
}
void replace_submatrix(
ll *matrix,
ll *submatrix,
int _i,
int _j,
int block_size,
int N
)
{
int i, j;
for (i = 0; i < block_size; i++)
for (j = 0; j < block_size; j++)
matrix[(_i+i)*N+_j+j] = submatrix[i*block_size + j];
}
int main(int argc, char** argv){
MPI_Init(&argc, &argv);
double start = MPI_Wtime();
int world_size;
MPI_Comm_size(MPI_COMM_WORLD, &world_size);
int rank;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
// Число процессоров для строки
const int p = (int) ceil(sqrt(world_size));
int block_size;
int n_size; // Только для процесса 0
if ( rank == 0 ){
// Ввод размера матрицы NxN
scanf("%d", &n_size);
// Расширяем матрицу нулями
const int N = (p < n_size && n_size % p != 0) ? (n_size + p - n_size % p) : n_size;
// Вычислили размер блока
block_size = N / p;
MPI_Barrier(MPI_COMM_WORLD);
MPI_Bcast(&block_size, 1, MPI_INT, 0, MPI_COMM_WORLD);
} else {
MPI_Barrier(MPI_COMM_WORLD);
MPI_Bcast(&block_size, 1, MPI_INT, 0, MPI_COMM_WORLD);
}
const int block_square = block_size * block_size;
const int N = p * block_size;
ll *wa = (ll *) calloc( block_square, sizeof(ll) );
ll *la = (ll *) calloc( block_square, sizeof(ll) );
ll *lb = (ll *) calloc( block_square, sizeof(ll) );
ll *lc = (ll *) calloc( block_square, sizeof(ll) );
ll *c; // Только для процесса 0
if ( rank == 0 ){
ll *a = (ll *) calloc( N * N, sizeof(ll) );
ll *b = (ll *) calloc( N * N, sizeof(ll) );
c = (ll *) calloc( N * N, sizeof(ll) );
enter_matrix(a, n_size, N);
enter_matrix(b, n_size, N);
int is_first = 1;
int destination = 1;
int i, j;
for (i = 0; i < N; i += block_size){
for (j = 0; j < N; j += block_size){
if ( is_first ){
is_first = 0;
continue;
}
get_submatrix(a, la, i, j, block_size, N);
get_submatrix(b, lb, i, j, block_size, N);
MPI_Send(la, block_square, MPI_LONG_LONG, destination, 0, MPI_COMM_WORLD);
MPI_Send(lb, block_square, MPI_LONG_LONG, destination, 0, MPI_COMM_WORLD);
destination++;
}
}
get_submatrix(a, la, 0, 0, block_size, N);
get_submatrix(b, lb, 0, 0, block_size, N);
MPI_Barrier(MPI_COMM_WORLD);
} else {
MPI_Recv(la, block_square, MPI_LONG_LONG, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
MPI_Recv(lb, block_square, MPI_LONG_LONG, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
MPI_Barrier(MPI_COMM_WORLD);
}
int l, dest, from;
for (l = 0; l < p; l++){
int row = rank / p;
int pivot_j = (row + l) % p;
int pivot_rank = p * row + pivot_j;
if ( rank == pivot_rank ){
get_submatrix(la, wa, 0, 0, block_size, block_size);
int j;
for (j = 0; j < p; j++){
int dest = row * p + j;
if ( j != pivot_j )
MPI_Send(la, block_square, MPI_LONG_LONG, dest, 0, MPI_COMM_WORLD);
}
} else {
MPI_Recv(wa, block_square, MPI_LONG_LONG, pivot_rank, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
}
// Перемножаем A и B и суммируем с прошлой матрицей C
int _i, _j, _k;
for (_i = 0; _i < block_size; _i++)
for (_j = 0; _j < block_size; _j++)
for (_k = 0; _k < block_size; _k++)
lc[_i*block_size+_j] = lc[_i*block_size+_j] + wa[_i*block_size+_k] * lb[_k*block_size+_j];
dest = row == 0 ? (p-1)*p + (rank % p) : (row-1)*p + (rank % p);
from = row == (p-1) ? rank % p : (row+1)*p + rank % p;
if ( row % 2 == 0 ){
MPI_Send(lb, block_square, MPI_LONG_LONG, dest, 0, MPI_COMM_WORLD);
MPI_Recv(lb, block_square, MPI_LONG_LONG, from, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
} else {
ll *wb = (ll *) calloc( block_square, sizeof(ll) );
get_submatrix(lb, wb, 0, 0, block_size, block_size);
MPI_Recv(lb, block_square, MPI_LONG_LONG, from, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
MPI_Send(wb, block_square, MPI_LONG_LONG, dest, 0, MPI_COMM_WORLD);
}
MPI_Barrier(MPI_COMM_WORLD);
}
if ( rank == 0 ){
replace_submatrix(c, lc, 0, 0, block_size, N);
int proc;
for (proc = 1; proc < world_size; proc++){
MPI_Recv(lc, block_square, MPI_LONG_LONG, proc, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
int i = proc / p;
int j = proc % p;
replace_submatrix(c, lc, i*block_size, j*block_size, block_size, N);
}
print_submatrix(c, n_size, N);
double finish = MPI_Wtime();
printf("Elapsed time: %f\n", finish - start);
} else {
MPI_Send(lc, block_square, MPI_LONG_LONG, 0, 0, MPI_COMM_WORLD);
}
MPI_Finalize();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment