Matrix Vector Multiplication in Parallel (mpi) [on hold]
up vote
-2
down vote
favorite
I wrote a program to calculate y = A*b. and I use MPI_Scatter to scatter the rows of the matrix A among processors and MPI_Gather to collect the calculated data and then print them. I don't know where I went wrong, and why the code prints some non-sense numbers. please help. Thank you.
here is my code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "mpi.h"
int main(int argc, char *argv){
int m = 8,n = 8, i, j, num = 0;
int rank, size;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
//!Allocating and generating matrix A.
int *A = (int*) malloc(m * n * sizeof(int));
if (rank == 0) {
for (i = 0; i < m; ++i) {
for (j = 0; j < n; ++j) {
A[i*n+j] = ++num;
}
}
}
//Scatter the matrix A into processores
MPI_Scatter(A, n/size, MPI_INT, A, n/size, MPI_INT, 0, MPI_COMM_WORLD);
//Allocating and generating vector b.
int *b = (int*) malloc(n * sizeof(int));
for (i = 0; i < n; ++i) {
b[i] = ++num;
}
//Allocating the vector y
int *y = (int*) calloc (m, sizeof(int));
//calculation of y.
for (i = 0; i < m; ++i) {
for (j = 0; j < n; ++j) {
y[j] += A[i*n+j] * b[i];
}
}
//collect all data from processors.
MPI_Gather(&y, 1, MPI_INT, y, 1, MPI_INT, 0, MPI_COMM_WORLD);
if (rank == 0) {
for (i = 0; i < n; ++i) {
printf("%dn", y[i]);
}
}
//free the allocated memory.
free (A);
free (b);
free (y);
MPI_Finalize();
return 0;
}
c matrix vector row mpi
put on hold as off-topic by Zulan, High Performance Mark, Mitch Wheat, gnat, Jimi 23 hours ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Zulan, Jimi
If this question can be reworded to fit the rules in the help center, please edit the question.
|
show 1 more comment
up vote
-2
down vote
favorite
I wrote a program to calculate y = A*b. and I use MPI_Scatter to scatter the rows of the matrix A among processors and MPI_Gather to collect the calculated data and then print them. I don't know where I went wrong, and why the code prints some non-sense numbers. please help. Thank you.
here is my code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "mpi.h"
int main(int argc, char *argv){
int m = 8,n = 8, i, j, num = 0;
int rank, size;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
//!Allocating and generating matrix A.
int *A = (int*) malloc(m * n * sizeof(int));
if (rank == 0) {
for (i = 0; i < m; ++i) {
for (j = 0; j < n; ++j) {
A[i*n+j] = ++num;
}
}
}
//Scatter the matrix A into processores
MPI_Scatter(A, n/size, MPI_INT, A, n/size, MPI_INT, 0, MPI_COMM_WORLD);
//Allocating and generating vector b.
int *b = (int*) malloc(n * sizeof(int));
for (i = 0; i < n; ++i) {
b[i] = ++num;
}
//Allocating the vector y
int *y = (int*) calloc (m, sizeof(int));
//calculation of y.
for (i = 0; i < m; ++i) {
for (j = 0; j < n; ++j) {
y[j] += A[i*n+j] * b[i];
}
}
//collect all data from processors.
MPI_Gather(&y, 1, MPI_INT, y, 1, MPI_INT, 0, MPI_COMM_WORLD);
if (rank == 0) {
for (i = 0; i < n; ++i) {
printf("%dn", y[i]);
}
}
//free the allocated memory.
free (A);
free (b);
free (y);
MPI_Finalize();
return 0;
}
c matrix vector row mpi
put on hold as off-topic by Zulan, High Performance Mark, Mitch Wheat, gnat, Jimi 23 hours ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Zulan, Jimi
If this question can be reworded to fit the rules in the help center, please edit the question.
What are those nonsense numbers?
– Osiris
Nov 21 at 20:37
y[j] += A[i*n+j] * b[i];should bey[i] += A[i*n+j] * b[j];
– Osiris
Nov 21 at 20:39
@Osiris the output is :1691269472 -837220448 1518966688 -1231147392 by the way I changed what you said.
– daryush shiri
Nov 21 at 20:46
1
At that stage, so many things are wrong and on so many levels, and it really looks like a homework dump. I'd rather suggest you take a paper and a pencil, make a sketch on how you scatter the matrix (2 MPI tasks are enough), what you compute on each task, and how you gather the result.
– Gilles Gouaillardet
Nov 21 at 22:47
1
good for you, you can now either post your answer or delete the question.
– Gilles Gouaillardet
Nov 22 at 0:07
|
show 1 more comment
up vote
-2
down vote
favorite
up vote
-2
down vote
favorite
I wrote a program to calculate y = A*b. and I use MPI_Scatter to scatter the rows of the matrix A among processors and MPI_Gather to collect the calculated data and then print them. I don't know where I went wrong, and why the code prints some non-sense numbers. please help. Thank you.
here is my code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "mpi.h"
int main(int argc, char *argv){
int m = 8,n = 8, i, j, num = 0;
int rank, size;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
//!Allocating and generating matrix A.
int *A = (int*) malloc(m * n * sizeof(int));
if (rank == 0) {
for (i = 0; i < m; ++i) {
for (j = 0; j < n; ++j) {
A[i*n+j] = ++num;
}
}
}
//Scatter the matrix A into processores
MPI_Scatter(A, n/size, MPI_INT, A, n/size, MPI_INT, 0, MPI_COMM_WORLD);
//Allocating and generating vector b.
int *b = (int*) malloc(n * sizeof(int));
for (i = 0; i < n; ++i) {
b[i] = ++num;
}
//Allocating the vector y
int *y = (int*) calloc (m, sizeof(int));
//calculation of y.
for (i = 0; i < m; ++i) {
for (j = 0; j < n; ++j) {
y[j] += A[i*n+j] * b[i];
}
}
//collect all data from processors.
MPI_Gather(&y, 1, MPI_INT, y, 1, MPI_INT, 0, MPI_COMM_WORLD);
if (rank == 0) {
for (i = 0; i < n; ++i) {
printf("%dn", y[i]);
}
}
//free the allocated memory.
free (A);
free (b);
free (y);
MPI_Finalize();
return 0;
}
c matrix vector row mpi
I wrote a program to calculate y = A*b. and I use MPI_Scatter to scatter the rows of the matrix A among processors and MPI_Gather to collect the calculated data and then print them. I don't know where I went wrong, and why the code prints some non-sense numbers. please help. Thank you.
here is my code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "mpi.h"
int main(int argc, char *argv){
int m = 8,n = 8, i, j, num = 0;
int rank, size;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
//!Allocating and generating matrix A.
int *A = (int*) malloc(m * n * sizeof(int));
if (rank == 0) {
for (i = 0; i < m; ++i) {
for (j = 0; j < n; ++j) {
A[i*n+j] = ++num;
}
}
}
//Scatter the matrix A into processores
MPI_Scatter(A, n/size, MPI_INT, A, n/size, MPI_INT, 0, MPI_COMM_WORLD);
//Allocating and generating vector b.
int *b = (int*) malloc(n * sizeof(int));
for (i = 0; i < n; ++i) {
b[i] = ++num;
}
//Allocating the vector y
int *y = (int*) calloc (m, sizeof(int));
//calculation of y.
for (i = 0; i < m; ++i) {
for (j = 0; j < n; ++j) {
y[j] += A[i*n+j] * b[i];
}
}
//collect all data from processors.
MPI_Gather(&y, 1, MPI_INT, y, 1, MPI_INT, 0, MPI_COMM_WORLD);
if (rank == 0) {
for (i = 0; i < n; ++i) {
printf("%dn", y[i]);
}
}
//free the allocated memory.
free (A);
free (b);
free (y);
MPI_Finalize();
return 0;
}
c matrix vector row mpi
c matrix vector row mpi
edited Nov 22 at 0:28
Nirvedh Meshram
17111
17111
asked Nov 21 at 20:23
daryush shiri
12
12
put on hold as off-topic by Zulan, High Performance Mark, Mitch Wheat, gnat, Jimi 23 hours ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Zulan, Jimi
If this question can be reworded to fit the rules in the help center, please edit the question.
put on hold as off-topic by Zulan, High Performance Mark, Mitch Wheat, gnat, Jimi 23 hours ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Zulan, Jimi
If this question can be reworded to fit the rules in the help center, please edit the question.
What are those nonsense numbers?
– Osiris
Nov 21 at 20:37
y[j] += A[i*n+j] * b[i];should bey[i] += A[i*n+j] * b[j];
– Osiris
Nov 21 at 20:39
@Osiris the output is :1691269472 -837220448 1518966688 -1231147392 by the way I changed what you said.
– daryush shiri
Nov 21 at 20:46
1
At that stage, so many things are wrong and on so many levels, and it really looks like a homework dump. I'd rather suggest you take a paper and a pencil, make a sketch on how you scatter the matrix (2 MPI tasks are enough), what you compute on each task, and how you gather the result.
– Gilles Gouaillardet
Nov 21 at 22:47
1
good for you, you can now either post your answer or delete the question.
– Gilles Gouaillardet
Nov 22 at 0:07
|
show 1 more comment
What are those nonsense numbers?
– Osiris
Nov 21 at 20:37
y[j] += A[i*n+j] * b[i];should bey[i] += A[i*n+j] * b[j];
– Osiris
Nov 21 at 20:39
@Osiris the output is :1691269472 -837220448 1518966688 -1231147392 by the way I changed what you said.
– daryush shiri
Nov 21 at 20:46
1
At that stage, so many things are wrong and on so many levels, and it really looks like a homework dump. I'd rather suggest you take a paper and a pencil, make a sketch on how you scatter the matrix (2 MPI tasks are enough), what you compute on each task, and how you gather the result.
– Gilles Gouaillardet
Nov 21 at 22:47
1
good for you, you can now either post your answer or delete the question.
– Gilles Gouaillardet
Nov 22 at 0:07
What are those nonsense numbers?
– Osiris
Nov 21 at 20:37
What are those nonsense numbers?
– Osiris
Nov 21 at 20:37
y[j] += A[i*n+j] * b[i]; should be y[i] += A[i*n+j] * b[j];– Osiris
Nov 21 at 20:39
y[j] += A[i*n+j] * b[i]; should be y[i] += A[i*n+j] * b[j];– Osiris
Nov 21 at 20:39
@Osiris the output is :1691269472 -837220448 1518966688 -1231147392 by the way I changed what you said.
– daryush shiri
Nov 21 at 20:46
@Osiris the output is :1691269472 -837220448 1518966688 -1231147392 by the way I changed what you said.
– daryush shiri
Nov 21 at 20:46
1
1
At that stage, so many things are wrong and on so many levels, and it really looks like a homework dump. I'd rather suggest you take a paper and a pencil, make a sketch on how you scatter the matrix (2 MPI tasks are enough), what you compute on each task, and how you gather the result.
– Gilles Gouaillardet
Nov 21 at 22:47
At that stage, so many things are wrong and on so many levels, and it really looks like a homework dump. I'd rather suggest you take a paper and a pencil, make a sketch on how you scatter the matrix (2 MPI tasks are enough), what you compute on each task, and how you gather the result.
– Gilles Gouaillardet
Nov 21 at 22:47
1
1
good for you, you can now either post your answer or delete the question.
– Gilles Gouaillardet
Nov 22 at 0:07
good for you, you can now either post your answer or delete the question.
– Gilles Gouaillardet
Nov 22 at 0:07
|
show 1 more comment
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
What are those nonsense numbers?
– Osiris
Nov 21 at 20:37
y[j] += A[i*n+j] * b[i];should bey[i] += A[i*n+j] * b[j];– Osiris
Nov 21 at 20:39
@Osiris the output is :1691269472 -837220448 1518966688 -1231147392 by the way I changed what you said.
– daryush shiri
Nov 21 at 20:46
1
At that stage, so many things are wrong and on so many levels, and it really looks like a homework dump. I'd rather suggest you take a paper and a pencil, make a sketch on how you scatter the matrix (2 MPI tasks are enough), what you compute on each task, and how you gather the result.
– Gilles Gouaillardet
Nov 21 at 22:47
1
good for you, you can now either post your answer or delete the question.
– Gilles Gouaillardet
Nov 22 at 0:07