I do laboratory work at the university. The task is quite stupid and in fact you just need to familiarize yourself with MPI technology. It is necessary to transfer all the elements of the square matrix above the side diagonal into a homogeneous array. everything works correctly, but for some reason the MPI_Gather function gives an error in the penultimate process and then the last one, apparently trying to stuff data not of its size into 0, the process is mistaken, explain what I’m doing wrong.
<code>#include <iostream>
int main(int argc, char** argv) {
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
cout << "Enter matrix lenght: ";
MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
int* arr = (int*)calloc(n * n, sizeof(int));
int countofwrigthelements = (n - 1) * n / 2;
cout << countofwrigthelements << endl;
int* odn = (int*)calloc(countofwrigthelements, sizeof(int));
int* rankarr;//ссылка на отдельный подмассив каждого процесса;
//количество элементов,которые будут помещенны в массив в этои процессе
int coutOfElementsInThisRank;
int countOfLinesInOneProcess = n / size;
coutOfElementsInThisRank = ((n - 1) - (rank * countOfLinesInOneProcess) + (n - (rank + 1) * countOfLinesInOneProcess)) * countOfLinesInOneProcess / 2;
coutOfElementsInThisRank = ((n - (rank * countOfLinesInOneProcess)) * (countOfLinesInOneProcess+n%size-1)) / 2;
cout << "rank" << rank << " coutOfElementsInThisRank: " << coutOfElementsInThisRank << endl;
rankarr = (int*)calloc(coutOfElementsInThisRank, sizeof(int));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
*(arr + i * n + j) = rand() % 128 * (pow(-1, rand() % 2));
MPI_Bcast(arr, n * n, MPI_INT, 0, MPI_COMM_WORLD);
double time1 = MPI_Wtime();
start = rank * countOfLinesInOneProcess;
end = (rank + 1) * countOfLinesInOneProcess;
cout << "rank" << rank << " start: " << start << " end: " << end << endl;
for (int i = start; i < end; i++) {
curlineelements = n - i - 1;
for (int j = 0; j < curlineelements; j++) {
*(rankarr + countofprevious) = *(arr + i * n + j);
double time2 = MPI_Wtime();
MPI_Reduce(&coutOfElementsInThisRank, &reduce, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
MPI_Gather(rankarr, coutOfElementsInThisRank,MPI_INT,odn, coutOfElementsInThisRank,MPI_INT,0,MPI_COMM_WORLD);
cout << "reduce: " << reduce << endl;
printf("The total running time of the algorithm in seconds: %lfn", time2 - time1);
<code>#include <iostream>
#include <mpi.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main(int argc, char** argv) {
MPI_Init(&argc, &argv);
int rank, size;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
int n;
int reduce;
if (rank == 0) {
cout << "Enter matrix lenght: ";
cin >> n;
}
MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
int* arr = (int*)calloc(n * n, sizeof(int));
int countofwrigthelements = (n - 1) * n / 2;
cout << countofwrigthelements << endl;
int* odn = (int*)calloc(countofwrigthelements, sizeof(int));
int* rankarr;//ссылка на отдельный подмассив каждого процесса;
//количество элементов,которые будут помещенны в массив в этои процессе
int coutOfElementsInThisRank;
int countOfLinesInOneProcess = n / size;
if (rank < size - 1) {
coutOfElementsInThisRank = ((n - 1) - (rank * countOfLinesInOneProcess) + (n - (rank + 1) * countOfLinesInOneProcess)) * countOfLinesInOneProcess / 2;
}
else {
coutOfElementsInThisRank = ((n - (rank * countOfLinesInOneProcess)) * (countOfLinesInOneProcess+n%size-1)) / 2;
}
cout << "rank" << rank << " coutOfElementsInThisRank: " << coutOfElementsInThisRank << endl;
rankarr = (int*)calloc(coutOfElementsInThisRank, sizeof(int));
if (rank == 0) {
srand(time(NULL));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
*(arr + i * n + j) = rand() % 128 * (pow(-1, rand() % 2));
}
}
}
MPI_Bcast(arr, n * n, MPI_INT, 0, MPI_COMM_WORLD);
int curlineelements;
int countofprevious = 0;
double time1 = MPI_Wtime();
int start;
int end;
start = rank * countOfLinesInOneProcess;
if (rank < size - 1) {
end = (rank + 1) * countOfLinesInOneProcess;
}
else {
end = n;
}
cout << "rank" << rank << " start: " << start << " end: " << end << endl;
for (int i = start; i < end; i++) {
curlineelements = n - i - 1;
for (int j = 0; j < curlineelements; j++) {
*(rankarr + countofprevious) = *(arr + i * n + j);
countofprevious++;
}
}
double time2 = MPI_Wtime();
MPI_Reduce(&coutOfElementsInThisRank, &reduce, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
MPI_Gather(rankarr, coutOfElementsInThisRank,MPI_INT,odn, coutOfElementsInThisRank,MPI_INT,0,MPI_COMM_WORLD);
if (rank == 0) {
cout << "reduce: " << reduce << endl;
}
if (rank == 0) {
printf("The total running time of the algorithm in seconds: %lfn", time2 - time1);
}
free(arr);
free(rankarr);
free(odn);
MPI_Finalize();
return 0;
}
</code>
#include <iostream>
#include <mpi.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main(int argc, char** argv) {
MPI_Init(&argc, &argv);
int rank, size;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
int n;
int reduce;
if (rank == 0) {
cout << "Enter matrix lenght: ";
cin >> n;
}
MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
int* arr = (int*)calloc(n * n, sizeof(int));
int countofwrigthelements = (n - 1) * n / 2;
cout << countofwrigthelements << endl;
int* odn = (int*)calloc(countofwrigthelements, sizeof(int));
int* rankarr;//ссылка на отдельный подмассив каждого процесса;
//количество элементов,которые будут помещенны в массив в этои процессе
int coutOfElementsInThisRank;
int countOfLinesInOneProcess = n / size;
if (rank < size - 1) {
coutOfElementsInThisRank = ((n - 1) - (rank * countOfLinesInOneProcess) + (n - (rank + 1) * countOfLinesInOneProcess)) * countOfLinesInOneProcess / 2;
}
else {
coutOfElementsInThisRank = ((n - (rank * countOfLinesInOneProcess)) * (countOfLinesInOneProcess+n%size-1)) / 2;
}
cout << "rank" << rank << " coutOfElementsInThisRank: " << coutOfElementsInThisRank << endl;
rankarr = (int*)calloc(coutOfElementsInThisRank, sizeof(int));
if (rank == 0) {
srand(time(NULL));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
*(arr + i * n + j) = rand() % 128 * (pow(-1, rand() % 2));
}
}
}
MPI_Bcast(arr, n * n, MPI_INT, 0, MPI_COMM_WORLD);
int curlineelements;
int countofprevious = 0;
double time1 = MPI_Wtime();
int start;
int end;
start = rank * countOfLinesInOneProcess;
if (rank < size - 1) {
end = (rank + 1) * countOfLinesInOneProcess;
}
else {
end = n;
}
cout << "rank" << rank << " start: " << start << " end: " << end << endl;
for (int i = start; i < end; i++) {
curlineelements = n - i - 1;
for (int j = 0; j < curlineelements; j++) {
*(rankarr + countofprevious) = *(arr + i * n + j);
countofprevious++;
}
}
double time2 = MPI_Wtime();
MPI_Reduce(&coutOfElementsInThisRank, &reduce, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
MPI_Gather(rankarr, coutOfElementsInThisRank,MPI_INT,odn, coutOfElementsInThisRank,MPI_INT,0,MPI_COMM_WORLD);
if (rank == 0) {
cout << "reduce: " << reduce << endl;
}
if (rank == 0) {
printf("The total running time of the algorithm in seconds: %lfn", time2 - time1);
}
free(arr);
free(rankarr);
free(odn);
MPI_Finalize();
return 0;
}
I dont know whats wrong with that so i cant do anything.