通过mpi发送mpz_t数组

时间:2011-03-12 03:32:14

标签: data-structures mpi gmp

我使用libgmp(GMP)处理非常长的整数,存储为mpz_thttp://gmplib.org/manual/Integer-Internals.html#Integer-Internals

  

mpz_t变量表示使用符号和幅度的整数,在动态分配和重新分配的空间中。

所以我认为mpz_t就像指针一样。

如何通过MPI发送包含数据的mpz_t变量数组?

2 个答案:

答案 0 :(得分:3)

使用mpz_import() and mpz_export()mpz_t之间进行转换,例如char数组,然后您可以通过MPI发送/接收。小心获取与endianness等相关的参数。

答案 1 :(得分:2)

以下是代码:

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);