Thread: Help with argv

  1. #1
    Registered User
    Join Date
    Dec 2009
    Location
    Colorado
    Posts
    41

    Help with argv

    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

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Try using strtod() instead of atof() -- it is easier to debug errors, as well as safer for user input.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Dec 2009
    Location
    Colorado
    Posts
    41
    Quote Originally Posted by MK27 View Post
    Try using strtod() instead of atof() -- it is easier to debug errors, as well as safer for user input.
    I made the change and I still get the same problem but it also seems to mess with the MPI stuff. The result after only changing atof() to strtod() is:

    Code:
    argc: 2
    Notice: mesh size set to 0.000000
    Process 32767 calculated: 0.000000
    argc: 2
    Notice: mesh size set to 0.000000
    Process 32767 calculated: 0.000000
    argc: 2
    Notice: mesh size set to 0.000000
    Process 32767 calculated: 0.000000
    argc: 2
    Notice: mesh size set to 0.000000
    Process 32767 calculated: 0.000000
    The processes should be 0,1,2,3.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Okay, well here's a simple test and demonstration:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[]) {
    	double n = strtod(argv[1],NULL);
    
    	printf("The number: %f\n",n);
    
    	return 0;
    }
    You should be able to compile this and run it thus:
    ./a.out .001
    The number: 0.001000


    If not, your computer is broken.

    However, most likely that is not the case I had not really considered this:
    Code:
    MPI_Init(&argc, &argv);
    Because I am unfamiliar with MPI. However, I can tell you that other toolkits/libraries which are intialized and passed argc/argv this way (it's not usual) may modify them!

    Try setting h before you initialize MPI.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Dec 2009
    Location
    Colorado
    Posts
    41
    Quote Originally Posted by MK27 View Post
    Okay, well here's a simple test and demonstration:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[]) {
    	double n = strtod(argv[1],NULL);
    
    	printf("The number: %f\n",n);
    
    	return 0;
    }
    You should be able to compile this and run it thus:
    ./a.out .001
    The number: 0.001000


    If not, your computer is broken.

    However, most likely that is not the case I had not really considered this:
    Code:
    MPI_Init(&argc, &argv);
    Because I am unfamiliar with MPI. However, I can tell you that other toolkits/libraries which are intialized and passed argc/argv this way (it's not usual) may modify them!

    Try setting h before you initialize MPI.
    I just realized I was forgetting the stdlib.h heading. Whoops! It is working now - thanks a lot for the help. In case you were curious, moving the conversion code before the MPI initialization didn't affect the outcome (at least from what I could tell). Thanks again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. argc, argv
    By Martas in forum C Programming
    Replies: 6
    Last Post: 11-19-2009, 09:39 AM
  2. argv
    By taurus in forum C Programming
    Replies: 15
    Last Post: 10-14-2007, 08:57 AM
  3. Using argc and argv data
    By Rad_Turnip in forum C Programming
    Replies: 4
    Last Post: 03-31-2006, 06:09 AM
  4. how do i? re: argc - argv
    By luigi40 in forum C Programming
    Replies: 2
    Last Post: 06-11-2004, 10:17 AM
  5. more argv and argc
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 09-08-2001, 11:04 PM