Send array of mpz_t over mpi

余生长醉 提交于 2019-12-23 03:41:39

问题


I use a libgmp (GMP) to work with very long integers, stored as mpz_t: http://gmplib.org/manual/Integer-Internals.html#Integer-Internals

mpz_t variables represent integers using sign and magnitude, in space dynamically allocated and reallocated.

So I think mpz_t is like pointer.

How can I send an array of mpz_t variables with data over MPI?


回答1:


Use mpz_import() and mpz_export() to convert between mpz_t and e.g. char arrays, which you can then send/receive over MPI. Be careful with getting the parameters related to endianness etc. right.




回答2:


Here is the code:

unsigned long *buf, *t; // pointers for ulong array for storing gmp data
unsigned long count, countc; // sizes of data and element sizes array 
unsigned long size = array_size; // size of array
size_t *bc,*tc; // pointers for size_t array to store element sizes;

buf=(unsigned long*)malloc(sizeof(unsigned long)*(size*limb_per_element));
bc=(size_t*)malloc(sizeof(size_t)*(size));

if(rank==SENDER_RANK) {
    t=buf;
    tc=bc;
    for(int i;i<size;i++) {
        mpz_export(t,tc,1,sizeof(unsigned long),0,0, ARRAY(i));
        t+=*tc;
    tc++;
    }
    count=t-buf;
    countc=tc-bc;
    MPI_Send(&count, 1, MPI_UNSIGNED_LONG, 0, 0, MPI_COMM_WORLD);
    MPI_Send(&countc, 1, MPI_UNSIGNED_LONG, 0, 0, MPI_COMM_WORLD);
    MPI_Send(bc, countc*(sizeof(size_t)), MPI_CHAR, 0, 0, MPI_COMM_WORLD);
    MPI_Send(buf, count, MPI_UNSIGNED_LONG, 0, 0, MPI_COMM_WORLD);
} else {
    status=MPI_Recv(&count, 1, MPI_UNSIGNED_LONG, SENDER_RANK, 0, MPI_COMM_WORLD, NULL); 
    status=MPI_Recv(&countc, 1, MPI_UNSIGNED_LONG, SENDER_RANK, 0, MPI_COMM_WORLD, NULL); 
    t=buf;
    tc=bc;
    status=MPI_Recv(bc, countc*(sizeof(size_t)), MPI_CHAR, SENDER_RANK, 0, MPI_COMM_WORLD, NULL); 
    status=MPI_Recv(buf, count, MPI_UNSIGNED_LONG, SENDER_RANK, 0, MPI_COMM_WORLD, NULL); 
    for(int i; i<size; i++) {
        mpz_import(ARRAY(i),*tc,1,sizeof(unsigned long),0,0, t);
        t+=*tc;
        tc++;
    }
}
free(buf);
free(bc);


来源:https://stackoverflow.com/questions/5280359/send-array-of-mpz-t-over-mpi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!