bug-gmp
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: mpf_get_str trouble


From: Cesar Delgado
Subject: RE: mpf_get_str trouble
Date: Sat, 21 Dec 2002 14:01:08 -0600

Sorry it took me so long to get back to you.  I was finishing up finals
@ the Uni.  

After playing with the linking for a while I got it to work with the
patch and I'm getting the right answer in the example that I sent
before.

Even though that part is working my program is still not giving me the
correct answer.  I'm using GMP and MPI (Message Passing Interface) to
calculate PI.  I'm enclosing the source hopping that someone knows MPI
and can lend me a hand.  

Thanks a lot,

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>

#include <gmp.h>
#include <mpi.h>

// Using the Taylor series for atan with some modifications
//      pi = 4 * (4 * atan(1/5) - atan(1/239) )
//      atan(x) = (x - (x^3/3) + (x^5/5) - (x^7/7) + ... )
//It's supposed to converge quickliy

int main(int argc, char* argv[]) {

        //normal variables
        unsigned long int i, number  ;
        const int five =5 ;
        const int twoThirtyNine = 239 ;
        char *word ;
        time_t time1, time2 ;
        //GMP variables
        mpf_t atan1, atan2, rExponent, tmp ;
        mp_exp_t exponent ;
        //MPI variables ;
        int my_rank, size, node, length ;
        long int lExponent ;
        MPI_Status status ;
        MPI_Request *request ;

        //Start the program
        
        MPI_Init(&argc, &argv) ;

        MPI_Comm_rank(MPI_COMM_WORLD, &my_rank) ;
        MPI_Comm_size(MPI_COMM_WORLD, &size) ;
        
        mpf_init(atan1) ;
                mpf_set_prec(atan1, 2000000) ;
        mpf_init(atan2) ;
                mpf_set_prec(atan2, 2000000) ;
        mpf_init(tmp) ;
                mpf_set_prec(tmp, 2000000) ;
        mpf_init2(rExponent, 100000) ;
        i = my_rank ;

/*Starting the math
*/      
        while (i < 30ul) {
                if (my_rank ==0) {
                        if ((i%50) == 0) {
                                printf("%lu from 50\n", i) ;
                        }
                        printf(".") ;
                        fflush(stdout) ;        
                }

                number = (i*2)+1 ;
                mpf_set_ui(tmp, 1) ;
                mpf_div_ui(tmp, tmp, five) ;
                mpf_pow_ui(tmp, tmp, number) ;
                mpf_div_ui(tmp, tmp, number) ;

                if (i != 0) {
                        if ((i%2) == 1) {
                                mpf_sub(atan1, atan1, tmp) ;
                        } else {
                                mpf_add(atan1, atan1, tmp) ;
                        }
                } else {
                        mpf_add(atan1, atan1, tmp) ;
                }
                
                number = (i*2)+1 ;
                mpf_set_ui(tmp, 1) ;
                mpf_div_ui(tmp, tmp, twoThirtyNine) ;
                mpf_pow_ui(tmp, tmp, number) ;
                mpf_div_ui(tmp, tmp, number) ;

                
                if (i != 0) {
                        if ((i%2) == 1) {
                                mpf_sub(atan2, atan2, tmp) ;
                        } else {
                                mpf_add(atan2, atan2, tmp) ;
                        }
                } else {
                        mpf_add(atan2, atan2, tmp) ;
                }

                i += size ;
        }

/*Sending messages
 */
        if (my_rank != 0) {
                mpf_clear(tmp) ;

                MPI_Send(&my_rank, 1, MPI_INT, 0, 0, MPI_COMM_WORLD)
;//So the master knows who I am
                word = mpf_get_str(NULL, &exponent, 10, 0, atan1) ;
                length = strlen(word) ;
                
                MPI_Send(&exponent, 1, MPI_LONG, 0, 0, MPI_COMM_WORLD) ;
//Send the exponent
                MPI_Send(&length, 1, MPI_INT, 0, 0, MPI_COMM_WORLD)
;//Send the length of the string
                time(&time1) ;
                MPI_Send(word, strlen(word)+1, MPI_CHAR, 0, 0,
MPI_COMM_WORLD) ;//Send the string
                time(&time2) ;
//              printf("Time for MPI_Send from node %i = %i\n", my_rank,
(int)time2-time1) ;
                free(word) ;//Get ready to get a new number

                word = mpf_get_str(NULL, &exponent, 10, 0, atan2) ;
                length = strlen(word) ;

                MPI_Send(&my_rank, 1, MPI_INT, 0, 1, MPI_COMM_WORLD) ;

                MPI_Send(&exponent, 1, MPI_LONG, 0, 1, MPI_COMM_WORLD)
;//Send the exponent
                MPI_Send(&length, 1, MPI_INT, 0, 1, MPI_COMM_WORLD)
;//Send the length of the string
                MPI_Send(word, strlen(word)+1, MPI_CHAR, 0, 1,
MPI_COMM_WORLD) ;//Send the string
                free(word) ;
        } else {
                printf("\n") ;
                for (i=1; i < size; i++) {
                        MPI_Recv(&node, 1, MPI_INT, MPI_ANY_SOURCE, 0,
MPI_COMM_WORLD, &status) ;
                        printf("Node %i just called! ", node) ;

                        MPI_Recv(&lExponent, 1, MPI_LONG, node, 0,
MPI_COMM_WORLD, &status) ;
                        printf(".(%li) ", lExponent) ; //print out the
exponent
                        mpf_set_d(rExponent, pow(10.0,
(double)lExponent)) ;
                        
                        MPI_Recv(&length, 1, MPI_INT, node, 0,
MPI_COMM_WORLD, &status) ;
                        printf(". ") ;
                        word = (char *)malloc(sizeof(char)*length+1) ;
                        MPI_Recv(word, length+1, MPI_CHAR, node, 0,
MPI_COMM_WORLD, &status) ;
                        printf(".(%c%c) ", word[0], word[1]) ; //print
out the first 2 characters of the string received
                        
                        mpf_set_str(tmp, word, 10) ;
                        mpf_mul(tmp, tmp, rExponent) ;
                        printf("All info received from node.\n") ;

                        mpf_add(atan1, atan1, tmp) ;
                        free(word) ;
                }

                printf("-------------------------------\n") ;
                printf("atan1 has been receved from all\n") ;
                printf("-------------------------------\n") ;
                
                for (i=1; i < size; i++) {
                        MPI_Recv(&node, 1, MPI_INT, MPI_ANY_SOURCE, 1,
MPI_COMM_WORLD, &status) ;
                        printf("Node %i just called! ", node) ;

                        MPI_Recv(&lExponent, 1, MPI_LONG, node, 1,
MPI_COMM_WORLD, &status) ;
                        printf(".(%li) ", lExponent) ; //print out
exponent
                        mpf_set_d(rExponent, pow(10.0,
(double)lExponent)) ;

                        MPI_Recv(&length, 1, MPI_INT, node, 1,
MPI_COMM_WORLD, &status) ;
                        printf(". ") ;
                        word = (char *)malloc(sizeof(char)*length) ;
                        MPI_Recv(word, length+1, MPI_CHAR, node, 1,
MPI_COMM_WORLD, &status) ;
                        printf(".(%c%c) ", word[0], word[1]) ; //print
out first 2 characters of string received
                        
                        mpf_set_str(tmp, word, 10) ;
                        mpf_mul(tmp, tmp, rExponent) ;
                        printf("All info received from node.\n") ;
                        
                        mpf_add(atan2, atan2, tmp) ;
                        free(word) ;
                }

                printf("-------------------------------\n") ;
                printf("atan2 has been receved from all\n") ;
                printf("-------------------------------\n") ;
                
                printf("atan1 = atan1 * 4\n") ;
                mpf_mul_ui(atan1, atan1, 4) ;
                printf("atan1 = atan1 - atan2\n") ;
                mpf_sub(atan1, atan1, atan2) ;
                printf("atan1 = atan1 * 4\n") ;
                mpf_mul_ui(atan1, atan1, 4) ;

//              gmp_printf("Estimate of pi = %1.*Ff\n", 20, atan1) ;
                printf("Changing number to a string starting with =")
; 
                word = mpf_get_str(NULL, &exponent, 10, 0, atan1) ;
                printf("%c%c does that look @ all like PI?", word[0],
word[1]) ; //print oit first 2 characters from string
        }

        MPI_Finalize() ;        
        return 0 ;
}

-Cesar Delgado
---------------------------------------------
Secure Distributed Information @ UNL
http://rcf.unl.edu
address@hidden, address@hidden

> -----Original Message-----
> From: address@hidden [mailto:address@hidden On Behalf Of
Torbjorn
> Granlund
> Sent: Wednesday, December 18, 2002 4:15 AM
> To: Cesar Delgado
> Cc: address@hidden
> Subject: Re: mpf_get_str trouble
> 
> "Cesar Delgado" <address@hidden> writes:
> 
>   I'm sorry to say that I'm still getting the same results when I work
>   with the program bellow.  I downloaded gmp-4.1.1 from the website,
>   patched it, compiled it and installed it.  I'm running the code on a
>   PentiumIII running RH7.3.
> 
> I strongly suspect you're not picking up the patched code.
> 
> Please insert something that odd near the place of the patch.
> For example:
> 
>         puts ("*** i am here ***");
> 
> Also, check that the patch is really there, and that you recompiled
> after you patched things, and that you relionked your binary
> with the new library.
> 
> --
> Torbjörn




reply via email to

[Prev in Thread] Current Thread [Next in Thread]