Created
November 12, 2012 00:07
-
-
Save Toshakins/4056831 to your computer and use it in GitHub Desktop.
Parallel programming course ass.1 sem.2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include "mpi.h" | |
int id; | |
const int n = 3, MSG = 99; | |
int a[n][n] = {{1, 2, 3}, | |
{4, 5, 6}, | |
{7, 8, 9}}, | |
b[n][n] = {{1, 2, 3}, | |
{4, 5, 6}, | |
{7, 8, 9}}; | |
struct Pair{ | |
int x, y; | |
}task, pairs[n * n * n]; | |
int buf[n]; | |
int main(int argc, char* argv[]) | |
{ | |
MPI_Init(&argc, &argv); | |
MPI_Comm_rank(MPI_COMM_WORLD,&id); | |
int count = 2; | |
int lengths[2] = {1, 1}; | |
MPI_Aint offsets[2] = {0, sizeof(int) * (&(pairs[0].y) - &(pairs[0].x))}; | |
MPI_Datatype types[2] = {MPI_INT, MPI_INT}; | |
MPI_Datatype barDatatype; | |
MPI_Barrier(MPI_COMM_WORLD); | |
if (id == 0) | |
{ | |
/*for (int i = 0; i < n; ++i) | |
{ | |
for (int j = 0; j < n; j ++) | |
{ | |
cin >> a[i][j]; | |
} | |
for (int i = 0; i < n; ++i) | |
for (int j = 0; j < n; j ++) | |
{ | |
cin >> b[i][j]; | |
} | |
}*/ | |
for (int i = 0; i < n; ++i) | |
{ | |
for (int j = 0; j < n; ++j) | |
{ | |
for (int k = 0; k < n; ++k) | |
{ | |
//>_< | |
std::cout << "Attention! " << &(pairs[0]) - &(pairs[1]) << std::endl; | |
std::cout << "Attention! " << sizeof(Pair) << std::endl; | |
pairs[i * n * n + k + j * n].x = a[i][k], pairs[i * n * n + k + j * n].y = b[k][j]; | |
//std::cout << "Index " << i * n * n + k + j * n << std::endl; | |
//std::cout << "i j k " << i << " " << j << " " << k << std::endl; | |
//std::cout << i << ' ' << k << k << ' ' << j << std::endl; | |
//std::cout << "Org " << a[i][k] << " " << b[k][j] << std::endl; | |
//std::cout << "Vals " << pairs[i * n * n + k + j * n].x << " " << pairs[i * n * n + k + j * n].y << std::endl; | |
} | |
} | |
} | |
} | |
//MPI_Address( pairs - pairs, offsets); | |
//MPI_Address( &(pairs[0].y) - pairs, &offsets[1]); | |
//MPI_Type_contiguous(2, MPI_INT, &barDatatype); | |
MPI_Type_struct(2, lengths, offsets, types, &barDatatype); | |
MPI_Type_commit(&barDatatype); | |
MPI_Barrier(MPI_COMM_WORLD); | |
//mul | |
//MPI_Scatter(pairs, 1, barDatatype, &task, 1, barDatatype, id, MPI_COMM_WORLD); | |
//MPI_Scatter(&pairs, 1, MPI_2INT, &task, 1, MPI_2INT, id, MPI_COMM_WORLD); | |
if (id == 0) | |
{ | |
task.x = pairs[0].x, task.y = pairs[0].y; | |
for (int i = 1; i < n * n * n; i++) | |
{ | |
MPI_Send(&pairs[i], 2, MPI_INT, i, MSG, MPI_COMM_WORLD); | |
} | |
} | |
MPI_Status s; | |
if (id > 0) | |
{ | |
MPI_Recv(&task, 2, MPI_INT, 0, MSG, MPI_COMM_WORLD, &s); | |
} | |
std::cout << "Here " << task.x << " " << task.y; | |
std::cout << " got " << id << std::endl; | |
std::cout.flush(); | |
MPI_Barrier(MPI_COMM_WORLD); | |
int r = task.x * task.y; | |
int muls[n * n * n]; | |
MPI_Barrier(MPI_COMM_WORLD); | |
MPI_Gather(&r, 1, MPI_INT, muls, 1, MPI_INT, 0, MPI_COMM_WORLD); | |
//sum | |
if (id == 0) | |
for (int i = 0; i < n * n * n; i++) | |
{ | |
std::cout << ' ' << muls[i]; | |
} | |
std::cout << std::endl; | |
MPI_Barrier(MPI_COMM_WORLD); | |
//new comm | |
MPI_Group sums_grp, wrld_grp; | |
MPI_Comm sums_comm; | |
int ranks[n * n]; | |
for (int i = 0; i < n * n; i++) | |
{ | |
ranks[i] = i; | |
} | |
MPI_Comm_group(MPI_COMM_WORLD, &wrld_grp); | |
MPI_Group_incl(wrld_grp, 9, ranks, &sums_grp); | |
MPI_Comm_create(MPI_COMM_WORLD, sums_grp, &sums_comm); | |
if (id < (n * n)) | |
{ | |
MPI_Barrier(sums_comm); | |
//MPI_Scatter(muls, n, MPI_INT, buf, n, MPI_INT, id, sums_comm); | |
if (id == 0) | |
{ | |
for (int i = 0; i < n; i++) | |
{ | |
buf[i] = muls[i]; | |
} | |
for (int i = 1; i < n * n * n; i++) | |
{ | |
MPI_Send(&muls[i * n], n, MPI_INT, i, MSG, MPI_COMM_WORLD); | |
} | |
} | |
if (id > 0) | |
{ | |
MPI_Recv(&buf, n, MPI_INT, 0, MSG, MPI_COMM_WORLD, &s); | |
}// | |
for (int i = 0; i < n; i++) | |
{ | |
std::cout << " " << buf[i]; | |
} | |
std::cout << std::endl; | |
r = 0; | |
MPI_Barrier(sums_comm); | |
for (int i = 0; i < n; ++i) | |
{ | |
r += buf[i]; | |
} | |
MPI_Barrier(sums_comm); | |
int res[n * n]; | |
MPI_Barrier(sums_comm); | |
MPI_Gather(&r, 1, MPI_INT, res, 1, MPI_INT, 0, sums_comm); | |
if (id == 0) | |
{ | |
for (int i = 0; i < n; ++i) | |
{ | |
for (int j = 0; j < n; ++j) | |
{ | |
std::cout << res[i * n + j] << ' '; | |
std::cout.flush(); | |
} | |
std::cout << std::endl; | |
std::cout.flush(); | |
} | |
} | |
} | |
MPI_Finalize(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment