MPI for loop in c
up vote
0
down vote
favorite
I need to loop through a bi-dimensional array executing an operation on all the elements of the array, for some number of iterations. Here is my code:
for (for_iters=0;for_iters<ITERS;for_iters++)
{
diff = 0.0;
for (i=1;i<n;i++)
{
for (j=1;j<n;j++)
{
tmp = A[i][j];
A[i][j] = 0.3*(A[I][j] + A[i][j-1] + A[i-1][j] + A[i][j+1] + A[i+1][j]);
}
}
iters++;
} /*for*/
}
The problem is to translate the above code to MPI. I think I could divide the elements of the array to make every process deal with a part of the array, but I have no idea how to make the outer loop if every line of the code its executed once per process? if I have 3 processes, would I create 3 external loops??
c mpi
add a comment |
up vote
0
down vote
favorite
I need to loop through a bi-dimensional array executing an operation on all the elements of the array, for some number of iterations. Here is my code:
for (for_iters=0;for_iters<ITERS;for_iters++)
{
diff = 0.0;
for (i=1;i<n;i++)
{
for (j=1;j<n;j++)
{
tmp = A[i][j];
A[i][j] = 0.3*(A[I][j] + A[i][j-1] + A[i-1][j] + A[i][j+1] + A[i+1][j]);
}
}
iters++;
} /*for*/
}
The problem is to translate the above code to MPI. I think I could divide the elements of the array to make every process deal with a part of the array, but I have no idea how to make the outer loop if every line of the code its executed once per process? if I have 3 processes, would I create 3 external loops??
c mpi
By outer loop do you mean thefor (for_iters=0;for_iters<ITERS;for_iters++)
? You can easily split the matrix computation by for instance making different processes perform computations on different matrix rows (and all associated columns). The actual first loop could be left as it is. You can also split the outer loop and have each process compute all matrix values.
– atru
Nov 19 at 20:15
Come to think of it, it makes little sense to parallelize the iterative outermost loop - you need previous solution in order to compute the current. In that case a simple parallelization is the first way i.e. the second loop.
– atru
Nov 19 at 20:17
But the n iteration nes all the results of n-1 iterations,if I split the outer loop I think I won't get this result
– afs2
Nov 19 at 20:19
That's the second comment, also your current code has some typos. I suggest you first make a serial one, run it and confirm its validity, then move on to MPI. So if I were you I would simply split it using rows - like process 0 gets rows 0 to some Ni etc. and then computes across all columns - I would just split them evenly taking care of uneven-ness using like modulo. There are online resources for this one. You will also need to communicate the outer-boundary rows at the end of each iteration (i,j are known on the current process but i-1 and i+1 are not if on the boundary).
– atru
Nov 19 at 20:23
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I need to loop through a bi-dimensional array executing an operation on all the elements of the array, for some number of iterations. Here is my code:
for (for_iters=0;for_iters<ITERS;for_iters++)
{
diff = 0.0;
for (i=1;i<n;i++)
{
for (j=1;j<n;j++)
{
tmp = A[i][j];
A[i][j] = 0.3*(A[I][j] + A[i][j-1] + A[i-1][j] + A[i][j+1] + A[i+1][j]);
}
}
iters++;
} /*for*/
}
The problem is to translate the above code to MPI. I think I could divide the elements of the array to make every process deal with a part of the array, but I have no idea how to make the outer loop if every line of the code its executed once per process? if I have 3 processes, would I create 3 external loops??
c mpi
I need to loop through a bi-dimensional array executing an operation on all the elements of the array, for some number of iterations. Here is my code:
for (for_iters=0;for_iters<ITERS;for_iters++)
{
diff = 0.0;
for (i=1;i<n;i++)
{
for (j=1;j<n;j++)
{
tmp = A[i][j];
A[i][j] = 0.3*(A[I][j] + A[i][j-1] + A[i-1][j] + A[i][j+1] + A[i+1][j]);
}
}
iters++;
} /*for*/
}
The problem is to translate the above code to MPI. I think I could divide the elements of the array to make every process deal with a part of the array, but I have no idea how to make the outer loop if every line of the code its executed once per process? if I have 3 processes, would I create 3 external loops??
c mpi
c mpi
edited Nov 19 at 23:21
dmcgrandle
958315
958315
asked Nov 19 at 20:05
afs2
12
12
By outer loop do you mean thefor (for_iters=0;for_iters<ITERS;for_iters++)
? You can easily split the matrix computation by for instance making different processes perform computations on different matrix rows (and all associated columns). The actual first loop could be left as it is. You can also split the outer loop and have each process compute all matrix values.
– atru
Nov 19 at 20:15
Come to think of it, it makes little sense to parallelize the iterative outermost loop - you need previous solution in order to compute the current. In that case a simple parallelization is the first way i.e. the second loop.
– atru
Nov 19 at 20:17
But the n iteration nes all the results of n-1 iterations,if I split the outer loop I think I won't get this result
– afs2
Nov 19 at 20:19
That's the second comment, also your current code has some typos. I suggest you first make a serial one, run it and confirm its validity, then move on to MPI. So if I were you I would simply split it using rows - like process 0 gets rows 0 to some Ni etc. and then computes across all columns - I would just split them evenly taking care of uneven-ness using like modulo. There are online resources for this one. You will also need to communicate the outer-boundary rows at the end of each iteration (i,j are known on the current process but i-1 and i+1 are not if on the boundary).
– atru
Nov 19 at 20:23
add a comment |
By outer loop do you mean thefor (for_iters=0;for_iters<ITERS;for_iters++)
? You can easily split the matrix computation by for instance making different processes perform computations on different matrix rows (and all associated columns). The actual first loop could be left as it is. You can also split the outer loop and have each process compute all matrix values.
– atru
Nov 19 at 20:15
Come to think of it, it makes little sense to parallelize the iterative outermost loop - you need previous solution in order to compute the current. In that case a simple parallelization is the first way i.e. the second loop.
– atru
Nov 19 at 20:17
But the n iteration nes all the results of n-1 iterations,if I split the outer loop I think I won't get this result
– afs2
Nov 19 at 20:19
That's the second comment, also your current code has some typos. I suggest you first make a serial one, run it and confirm its validity, then move on to MPI. So if I were you I would simply split it using rows - like process 0 gets rows 0 to some Ni etc. and then computes across all columns - I would just split them evenly taking care of uneven-ness using like modulo. There are online resources for this one. You will also need to communicate the outer-boundary rows at the end of each iteration (i,j are known on the current process but i-1 and i+1 are not if on the boundary).
– atru
Nov 19 at 20:23
By outer loop do you mean the
for (for_iters=0;for_iters<ITERS;for_iters++)
? You can easily split the matrix computation by for instance making different processes perform computations on different matrix rows (and all associated columns). The actual first loop could be left as it is. You can also split the outer loop and have each process compute all matrix values.– atru
Nov 19 at 20:15
By outer loop do you mean the
for (for_iters=0;for_iters<ITERS;for_iters++)
? You can easily split the matrix computation by for instance making different processes perform computations on different matrix rows (and all associated columns). The actual first loop could be left as it is. You can also split the outer loop and have each process compute all matrix values.– atru
Nov 19 at 20:15
Come to think of it, it makes little sense to parallelize the iterative outermost loop - you need previous solution in order to compute the current. In that case a simple parallelization is the first way i.e. the second loop.
– atru
Nov 19 at 20:17
Come to think of it, it makes little sense to parallelize the iterative outermost loop - you need previous solution in order to compute the current. In that case a simple parallelization is the first way i.e. the second loop.
– atru
Nov 19 at 20:17
But the n iteration nes all the results of n-1 iterations,if I split the outer loop I think I won't get this result
– afs2
Nov 19 at 20:19
But the n iteration nes all the results of n-1 iterations,if I split the outer loop I think I won't get this result
– afs2
Nov 19 at 20:19
That's the second comment, also your current code has some typos. I suggest you first make a serial one, run it and confirm its validity, then move on to MPI. So if I were you I would simply split it using rows - like process 0 gets rows 0 to some Ni etc. and then computes across all columns - I would just split them evenly taking care of uneven-ness using like modulo. There are online resources for this one. You will also need to communicate the outer-boundary rows at the end of each iteration (i,j are known on the current process but i-1 and i+1 are not if on the boundary).
– atru
Nov 19 at 20:23
That's the second comment, also your current code has some typos. I suggest you first make a serial one, run it and confirm its validity, then move on to MPI. So if I were you I would simply split it using rows - like process 0 gets rows 0 to some Ni etc. and then computes across all columns - I would just split them evenly taking care of uneven-ness using like modulo. There are online resources for this one. You will also need to communicate the outer-boundary rows at the end of each iteration (i,j are known on the current process but i-1 and i+1 are not if on the boundary).
– atru
Nov 19 at 20:23
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381858%2fmpi-for-loop-in-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
By outer loop do you mean the
for (for_iters=0;for_iters<ITERS;for_iters++)
? You can easily split the matrix computation by for instance making different processes perform computations on different matrix rows (and all associated columns). The actual first loop could be left as it is. You can also split the outer loop and have each process compute all matrix values.– atru
Nov 19 at 20:15
Come to think of it, it makes little sense to parallelize the iterative outermost loop - you need previous solution in order to compute the current. In that case a simple parallelization is the first way i.e. the second loop.
– atru
Nov 19 at 20:17
But the n iteration nes all the results of n-1 iterations,if I split the outer loop I think I won't get this result
– afs2
Nov 19 at 20:19
That's the second comment, also your current code has some typos. I suggest you first make a serial one, run it and confirm its validity, then move on to MPI. So if I were you I would simply split it using rows - like process 0 gets rows 0 to some Ni etc. and then computes across all columns - I would just split them evenly taking care of uneven-ness using like modulo. There are online resources for this one. You will also need to communicate the outer-boundary rows at the end of each iteration (i,j are known on the current process but i-1 and i+1 are not if on the boundary).
– atru
Nov 19 at 20:23