I am trying to take in command line arguments with argv. I am using the command line argument to define a mesh size for a numerical integration program. Running the program looks like this
Code:
mpiexec -n 4 ./ParallelSimpson .001
However, my program sets the mesh size to 0. I am sure I am screwing something up in the conversion of the string argv[1] to a double. Here is the relevant code:
Code:
/*
* ParallelSimpson.c
* Parallel Simpson's Rule
*
* Created by waterborne on 1/22/10.
* Copyright 2010 __MyCompanyName__. All rights reserved.
*
*/
#include "ParallelSimpson.h"
int main(int argc, char* argv[]){
/* MPI constants */
int my_rank; // Rank of process
int p; // Number of processes
int source; // Rank of source
int dest; // Rank of receiver
int tag = 0; // Tag for messages
float I_n; // Storage for integral value
MPI_Status status; // Return status for receive
/* Math constants */
float S_n = 0; // Partial sum for integral approximation
float A, B; // Left and right endpoints respectively
double h; // Mesh size
float x1, x2, x3; // X values for each subinterval
float sub_int_size; // (B - A) / p
int n; // Iteration count
int i; // Loop counter
/* Start up MPI */
MPI_Init(&argc, &argv);
/* Find out process rank */
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
/* Find out number of processes */
MPI_Comm_size(MPI_COMM_WORLD, &p);
/* Set mesh size */
if (argv[1] == NULL) {
h = .000001; // Set default mesh size
}
else {
h = atof( &argv[1]);
printf("argc: %d\n", argc);
printf("Notice: mesh size set to %f\n", h);
}
/* Constant definitions */
A = -1.2;
B = .7;
if (A > B) {
printf("Error: invalid interval\nQuitting..."); // Check interval
return 1;
}
sub_int_size = (B - A) / p;
n = (B - A) / (h * p);
if (n % 2 == 0) { // Make sure n is odd
n += 1;
}
/* Simpson's rule loop */
for (i = 0; i < n ; i += 2) {
x1 = A + (sub_int_size * my_rank) + i * h;
x2 = A + (sub_int_size * my_rank) + (i + 1) * h;
x3 = A + (sub_int_size * my_rank) + (i + 2) * h;
S_n += SimpEval(x1, x2, x3);
}
S_n *= h / 3.0;
printf("Process %d calculated:: %f\n", my_rank, S_n);
/* Sum results into I_n */
MPI_Reduce( &S_n, &I_n, 1, MPI_FLOAT, MPI_SUM, 0, MPI_COMM_WORLD);
if (my_rank == 0) {
printf("Approximation is: %f\n", I_n);
}
/* Shut down MPI */
MPI_Finalize();
return 0;
}
float SimpEval( float x1, float x2, float x3){
float y; // Returned value of Simpson rule over interval [x1,x3]
y = f(x1) + 4 * f(x2) + f(x3);
return y;
}
float f( float x){
float y; // Returned value of function
y = pow(x, 3.0); // f(x) = x^3
return y;
}
And here is the output:
Code:
argc: 2
Notice: mesh size set to 0.000000
Process 0 calculated:: 0.000000
argc: 2
Notice: mesh size set to 0.000000
Process 1 calculated:: 0.000000
Approximation is: 0.000000
argc: 2
Notice: mesh size set to 0.000000
Process 2 calculated:: 0.000000
argc: 2
Notice: mesh size set to 0.000000
Process 3 calculated:: 0.000000
Where am I screwing up? Thanks