Skip to content

Instantly share code, notes, and snippets.

@Terminus-IMRC
Created September 17, 2013 13:06
Show Gist options
  • Save Terminus-IMRC/6594077 to your computer and use it in GitHub Desktop.
Save Terminus-IMRC/6594077 to your computer and use it in GitHub Desktop.
A beautiful MPI clip from terminus-p.
void probe_len_and_gather_total()
{
int i;
int len;
char *str, **allstr, *allstr_1dim;
len=mpz_sizeinbase(eachtotal, BASE);
MPI_Allreduce(&len, &i, 1, MPI_INT, MPI_MAX, MPI_COMM_WORLD);
len=i+1; /*core + '\0'*/
if(!commrank)
printf("len is %d.\n", len);
str=(char*)malloc(len*sizeof(char));
assert(str);
if(!commrank){
allstr=(char**)malloc(commsize*sizeof(char*));
assert(allstr);
allstr_1dim=(char*)malloc(commsize*len*sizeof(char));
assert(allstr_1dim);
for(i=0; i<commsize; i++)
allstr[i]=allstr_1dim+i*len;
}
mpz_get_str(str, BASE, eachtotal);
MPI_Gather(str, len, MPI_CHAR, allstr_1dim, len, MPI_CHAR, 0, MPI_COMM_WORLD);
if(!commrank){
mpz_set(total, eachtotal);
for(i=1; i<commsize; i++){
mpz_set_str(eachtotal, allstr[i], BASE);
mpz_add(total, total, eachtotal);
}
free(allstr);
free(allstr_1dim);
}
free(str);
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment