I am using GMP and MPI to calculate precision PI. Currently I have calculated PI in a worker process and need to send it back to the farmer process, to do this I have converted to a char array as so
mp_exp_t exp;
pi_str = mpf_get_str(pi_str,
&exp,
10,
(size_t)PRECISION,
pi);
char send_str[3026]= {0};
strcpy(send_str,pi_str);
MPI_Send(&send_str, len, MPI_CHAR, 0, 1, MPI_COMM_WORLD);
This works fine, now I receive the char array and I need to perform other calcualtions so the char array needs converting back to a float.
char recv_str[3026] = {0};
for(i=1; i<size ; i++)
{
printf("farmer**%d\n",size);
MPI_Recv(&recv_str,3026, MPI_CHAR, i, 1,MPI_COMM_WORLD, &status);
printf("pi after send %s", recv_str);
mpf_set_str(pi_ret, recv_str, 10);
gmp_printf("setting from string to float %.Ff\n", pi_ret, 20);
This gives me a memory allocation fault, but I can not see another way of converting back to a float unless I am missing something.
You need to initialize pi_ret
before assigning a value to it.
The easiesst way to do this would be with the mpf_init_set_str()
function, which simultaneously initializes a floating point number and assigns its value from a string of chars.