mpf_get_str trouble

Cesar Delgado cdelgad2@bigred.unl.edu
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. =20

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. =20

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 =3D 4 * (4 * atan(1/5) - atan(1/239) )
//	atan(x) =3D (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 =3D5 ;
        const int twoThirtyNine =3D 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
=09
	MPI_Init(&argc, &argv) ;

	MPI_Comm_rank(MPI_COMM_WORLD, &my_rank) ;
	MPI_Comm_size(MPI_COMM_WORLD, &size) ;
=09
	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 =3D my_rank ;

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

		number =3D (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 !=3D 0) {
			if ((i%2) =3D=3D 1) {
				mpf_sub(atan1, atan1, tmp) ;
			} else {
				mpf_add(atan1, atan1, tmp) ;
			}
		} else {
			mpf_add(atan1, atan1, tmp) ;
		}
	=09
		number =3D (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) ;

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

		i +=3D size ;
	}

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

		MPI_Send(&my_rank, 1, MPI_INT, 0, 0, MPI_COMM_WORLD)
;//So the master knows who I am
		word =3D mpf_get_str(NULL, &exponent, 10, 0, atan1) ;
		length =3D strlen(word) ;
	=09
		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 =3D %i\n", my_rank,
(int)time2-time1) ;
		free(word) ;//Get ready to get a new number

		word =3D mpf_get_str(NULL, &exponent, 10, 0, atan2) ;
		length =3D 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=3D1; 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)) ;
		=09
			MPI_Recv(&length, 1, MPI_INT, node, 0,
MPI_COMM_WORLD, &status) ;
			printf(". ") ;
			word =3D (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
		=09
			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") ;
	=09
		for (i=3D1; 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 =3D (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
		=09
			mpf_set_str(tmp, word, 10) ;
			mpf_mul(tmp, tmp, rExponent) ;
			printf("All info received from node.\n") ;
		=09
			mpf_add(atan2, atan2, tmp) ;
			free(word) ;
		}

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

//		gmp_printf("Estimate of pi =3D %1.*Ff\n", 20, atan1) ;
		printf("Changing number to a string starting with =3D")
;=20
		word =3D 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() ;=09
	return 0 ;
}

-Cesar Delgado
---------------------------------------------
Secure Distributed Information @ UNL
http://rcf.unl.edu
cdelgad2@bigred.unl.edu, beettlle@hotmail.com

> -----Original Message-----
> From: tege@king.swox.se [mailto:tege@king.swox.se] On Behalf Of
Torbjorn
> Granlund
> Sent: Wednesday, December 18, 2002 4:15 AM
> To: Cesar Delgado
> Cc: bug-gmp@gnu.org
> Subject: Re: mpf_get_str trouble
>=20
> "Cesar Delgado" <cdelgad2@bigred.unl.edu> writes:
>=20
>   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.
>=20
> I strongly suspect you're not picking up the patched code.
>=20
> Please insert something that odd near the place of the patch.
> For example:
>=20
>         puts ("*** i am here ***");
>=20
> 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.
>=20
> --
> Torbj=F6rn