Commit 7f895397 authored by Taddeüs Kroes's avatar Taddeüs Kroes

ModSim ass4 taddeus: Fixed MPI implementation (finally...).

parent 09b18960
...@@ -14,19 +14,18 @@ FILE *file; ...@@ -14,19 +14,18 @@ FILE *file;
* receiving the outer borders calculated by the adjacent nodes * receiving the outer borders calculated by the adjacent nodes
*/ */
void interact(int state) { void interact(int state) {
int prev = (rank - 1 + tasks) % tasks, next = (rank + 1) % tasks;
MPI_Status status; MPI_Status status;
// Left border // Left border
if( rank ) { if( rank ) {
MPI_Sendrecv(y[state] + 1, 1, MPI_DOUBLE, prev, 0, y[state], 1, MPI_Sendrecv(y[state] + 1, 1, MPI_DOUBLE, rank - 1, 0, y[state], 1,
MPI_DOUBLE, prev, MPI_ANY_TAG, MPI_COMM_WORLD, &status); MPI_DOUBLE, rank - 1, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
} }
// Right border // Right border
if( rank < tasks - 1 ) { if( rank < tasks - 1 ) {
MPI_Sendrecv(y[state] + steps, 1, MPI_DOUBLE, next, 0, MPI_Sendrecv(y[state] + steps, 1, MPI_DOUBLE, rank + 1, 0,
y[state] + steps + 1, 1, MPI_DOUBLE, next, MPI_ANY_TAG, y[state] + steps + 1, 1, MPI_DOUBLE, rank + 1, MPI_ANY_TAG,
MPI_COMM_WORLD, &status); MPI_COMM_WORLD, &status);
} }
} }
...@@ -38,17 +37,14 @@ void init_borders() { ...@@ -38,17 +37,14 @@ void init_borders() {
else if( rank == tasks - 1 ) else if( rank == tasks - 1 )
y[2][steps] = y[1][steps] = y[0][steps]; y[2][steps] = y[1][steps] = y[0][steps];
//interact(0); interact(0);
} }
/* /*
* Fill the assigned string section with a sinus shape * Fill the assigned string section with a sinus shape
*/ */
void init_sinus(double l, double dx, double n) { void init_sinus(double l, double dx, double n) {
int i; for( int i = 0; i < steps; i++ )
printf("start: %d\n", start);
for( i = 0; i < steps; i++ )
y[0][i + 1] = sin(n * M_PI * (i + start) * dx / l); y[0][i + 1] = sin(n * M_PI * (i + start) * dx / l);
init_borders(); init_borders();
...@@ -59,9 +55,8 @@ void init_sinus(double l, double dx, double n) { ...@@ -59,9 +55,8 @@ void init_sinus(double l, double dx, double n) {
*/ */
void init_plucked(double l, double dx, double xp) { void init_plucked(double l, double dx, double xp) {
double x; double x;
int i;
for( i = 0; i < steps; i++ ) { for( int i = 0; i < steps; i++ ) {
x = (i + start) * dx; x = (i + start) * dx;
y[0][i + 1] = x < xp ? x / xp : (l - x) / (l - xp); y[0][i + 1] = x < xp ? x / xp : (l - x) / (l - xp);
} }
...@@ -75,8 +70,8 @@ void init_plucked(double l, double dx, double xp) { ...@@ -75,8 +70,8 @@ void init_plucked(double l, double dx, double xp) {
#define PRINT_FORMAT "%e" #define PRINT_FORMAT "%e"
void print(int index) { void print(int index) {
for( int i = 0; i < steps; i++ ) { for( int i = 1; i <= steps; i++ ) {
if( i ) if( i > 1 )
fprintf(file, " "); fprintf(file, " ");
fprintf(file, PRINT_FORMAT, y[index][i]); fprintf(file, PRINT_FORMAT, y[index][i]);
...@@ -91,16 +86,19 @@ void print(int index) { ...@@ -91,16 +86,19 @@ void print(int index) {
*/ */
void calculate_steps(int time, double dx, double tau) { void calculate_steps(int time, double dx, double tau) {
double x; double x;
int i, t, prev = 0, current = 1, next = 2; int i, t, prev = 0, current = 1, next = 2,
s = rank ? 1 : 2, e = rank == tasks - 1 ? steps - 1 : steps;
// Calculate the position over the entire string at time dt using the // Calculate the position over the entire string at time dt using the
// position at t = 0 and the information that y(x, -dt) == y(x, dt) // position at t = 0 and the information that y(x, -dt) == y(x, dt)
for( i = 1; i <= steps; i++ ) { for( i = s; i <= e; i++ ) {
x = i * dx; x = i * dx;
y[1][i] = y[0][i] + .5 * tau * tau * (y[0][i-1] - 2 * y[0][i] y[current][i] = y[0][i] + .5 * tau * tau * (y[0][i - 1] - 2 * y[0][i]
+ y[0][i+1]); + y[0][i + 1]);
} }
interact(current);
#ifdef VERBOSE #ifdef VERBOSE
// Print x values (which are the same for every y-series) // Print x values (which are the same for every y-series)
for( i = 0; i < steps; i++ ) { for( i = 0; i < steps; i++ ) {
...@@ -119,11 +117,13 @@ void calculate_steps(int time, double dx, double tau) { ...@@ -119,11 +117,13 @@ void calculate_steps(int time, double dx, double tau) {
// Iterate over the length of the string for each time interval step // Iterate over the length of the string for each time interval step
for( t = 2; t < time; t++ ) { for( t = 2; t < time; t++ ) {
for( i = 1; i <= steps; i++ ) { for( i = s; i <= e; i++ ) {
y[next][i] = 2 * y[current][i] - y[prev][i] + tau * tau y[next][i] = 2 * y[current][i] - y[prev][i] + tau * tau
* (y[current][i-1] - 2 * y[current][i] + y[current][i+1]); * (y[current][i-1] - 2 * y[current][i] + y[current][i+1]);
} }
interact(next);
#ifdef VERBOSE #ifdef VERBOSE
print(next); print(next);
#endif #endif
...@@ -131,8 +131,6 @@ void calculate_steps(int time, double dx, double tau) { ...@@ -131,8 +131,6 @@ void calculate_steps(int time, double dx, double tau) {
prev = current; prev = current;
current = next; current = next;
next = (next + 1) % 3; next = (next + 1) % 3;
interact(next);
} }
} }
...@@ -187,7 +185,7 @@ int main(int argc, char **argv) { ...@@ -187,7 +185,7 @@ int main(int argc, char **argv) {
// allocate the left and right outer border values that are calculated by // allocate the left and right outer border values that are calculated by
// other nodes // other nodes
if( rank == tasks - 1 ) if( rank == tasks - 1 )
steps += total_steps - steps * (tasks - 1); steps += total_steps % steps;
// Allocate a 3xN two-dimensional area for this part of the string that // Allocate a 3xN two-dimensional area for this part of the string that
// functions as a cyclic buffer for three states of the string (previous, // functions as a cyclic buffer for three states of the string (previous,
......
...@@ -6,7 +6,7 @@ PROGRAM_EXEC=./par ...@@ -6,7 +6,7 @@ PROGRAM_EXEC=./par
ARGS=sinus 1001 1 .01 1 1 ARGS=sinus 1001 1 .01 1 1
#mpirun -np $NUM_PROCESSES ./par sinus 100 1 .01 1 1 #mpirun -np $NUM_PROCESSES ./par sinus 100 1 .01 1 1
mpirun -np $NUM_PROCESSES ./par sinus 3 1 .01 1 1 mpirun -np $NUM_PROCESSES ./par sinus 50 1 .01 1 1
#mpirun -np $NUM_PROCESSES $PROGRAM_EXEC $ARGS #mpirun -np $NUM_PROCESSES $PROGRAM_EXEC $ARGS
#mpirun -np $NUM_PROCESSES --mca pls_rsh_agent $RSH_AGENT \ #mpirun -np $NUM_PROCESSES --mca pls_rsh_agent $RSH_AGENT \
# --hostfile $MPI_HOSTFILE $PROGRAM_EXEC # --hostfile $MPI_HOSTFILE $PROGRAM_EXEC
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment