通过 mpi 发送 mpz_t 数组

发布于 2024-10-21 13:36:00 字数 368 浏览 8 评论 0原文

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

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

所以我认为 mpz_t 就像指针。

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不及他 2024-10-28 13:36:00

使用 mpz_import()mpz_export()mpz_tchar 数组之间进行转换,然后您可以通过 MPI 发送/接收这些数组。请注意正确设置与字节顺序等相关的参数。

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.

这是代码:

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文