2011-03-12 90 views

回答

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

使用mpz_import() and mpz_export()mpz_t與例如char數組,您可以通過MPI發送/接收數組。請小心獲取與排序等相關的參數。

+0

我如何導出'mpz_t's的陣列? – osgx 2011-03-12 13:06:58

+0

我不知道是否有內置的支持,所以你可能需要分配一個足夠大的數組來保存所有的數字,然後重複調用mpz_export()並逐漸填充數組。如果有方法,你也可以使用['mpz_out_raw()'和'mpz_inp_raw()'](http://gmplib.org/manual/I_002fO-of-Integers.html#I_002fO-of-Integers)在C中創建內存流。 – 2011-03-12 13:29:03

+0

我剛剛發現我必須傳遞兩個數組 - 第一個是導出的輸出,第二個是由導出返回的「count」的數組! – osgx 2011-03-12 15:27:52