MPI筆記 派生資料型別相關例子

2021-10-21 00:27:50 字數 3672 閱讀 1159

/*

檔名:

mpi_structs.c

目的:該**用於測試向量型別、結構型別、打包解包的傳輸效率

測試傳輸的資料是100000個mpi_double型別,且以24為間隔儲存的資料

mpi_type_vector

mpi_type_create_struct(新)/mpi_type_struct(棄)

mpi_type_commit

mpi_type_extent

執行:mpicc -g mpi_structs.c -o mpi_structs -wall

mpirun -n 2 ./mpi_structs

*/#include

#include

#include

#include

"mpi.h"

#define number_of_tests 10

// 重複執行的次數

intmain

(int argc,

char

*ar**)

lbuf =

(double*)

malloc

(n*sizeof

(double))

;if(!buf)

if(rank ==0)

printf

("kind\t\t n\t stride\t time(sec)\t rate(mb/sec)\n");

/* 向量型別的傳輸 */

mpi_type_vector

(n,1

, stride, mpi_double,

&vec1)

;mpi_type_commit

(&vec1)

;/* 列印派生資料型別的跨度 */

if(rank ==0)

tmin =

1000

;// 重複計算10遍時間,取其中執行的最短時間為tmin

for(k=

0; k) t2 =

(mpi_wtime()

- t1)

/ nloop;

// 結束計時,並求每個迴圈的平均時間

if(t2 < tmin)

tmin = t2;

// 取10次測試中最短時間作為參考值

}else

if(rank ==1)

}}/* 取往返時間的一半 */

tmin = tmin /

2.0;

// 取之前10次運算的最短參考值的一半作為一次傳輸的最短參考時間

if(rank ==0)

printf

("vector\t\t %d\t %d\t %f\t %f\n"

, n, stride, tmin, n*

sizeof

(double)*

1.0e-6

/ tmin)

;mpi_type_free

(&vec1)

;/* 結構型別的傳輸 */

blocklens[0]

=1; blocklens[1]

=1;// 每個塊中所含元素的數目

indices[0]

=0; indices[1]

= stride*

sizeof

(double);

// 每個塊起始位置的偏移量

old_types[0]

= mpi_double;

old_types[1]

= mpi_ub;

// 每個塊中元素的型別

mpi_type_create_struct(2

, blocklens, indices, old_types,

&vec_n)

;mpi_type_commit

(&vec_n)

;/* 列印派生資料型別的跨度 */

if(rank ==0)

tmin =

1000

;for

(k=0

; k) t2 =

(mpi_wtime()

- t1)

/ nloop;

if(t2 < tmin)

tmin = t2;

}else

if(rank ==1)

}}/* 取往返時間的一半 */

tmin = tmin /

2.0;

if(rank ==0)

printf

("struct\t\t %d\t %d\t %f\t %f\n"

, n, stride, tmin, n*

sizeof

(double)*

1.0e-6

/ tmin)

;mpi_type_free

(&vec_n)

;/* use user-packing with known stride */

tmin =

1000

;for

(k=0

; k)

t2 =

(mpi_wtime()

- t1)

/ nloop;

if(t2 < tmin)

tmin = t2;

}else

if(rank ==1)

}}tmin = tmin /

2.0;

if(rank ==0)

printf

("user\t\t %d\t %d\t %f\t %f\n"

, n, stride, tmin, n*

sizeof

(double)*

1.0e-6

/ tmin)

;/* use user-packing with known stride, using addition in the user copy code */

tmin =

1000

;for

(k=0

; k)mpi_send

(lbuf, n, mpi_double,

1, k, mpi_comm_world)

;mpi_recv

(lbuf, n, mpi_double,

1, k, mpi_comm_world,

&status)

; out_p = buf;

in_p = lbuf;

for(i=

0; it2 =

(mpi_wtime()

- t1)

/ nloop;

if(t2 < tmin)

tmin = t2;

}else

if(rank ==1)

out_p = lbuf;

in_p = buf;

for(i=

0; i)mpi_send

(lbuf, n, mpi_double,

0, k, mpi_comm_world);}

}}tmin = tmin /

2.0;

if(rank ==0)

mpi_finalize()

;}

MPI之資料型別

我們知道,比較基本的mpi點對點通訊具有無法同時傳送不同資料型別 當然前面提到了可以使用mpi packed,但是這樣會造成效能的極大損耗 因此mpi提供說明更通用的,混合的非連續通訊緩衝區的機制.直到執行 implementation 時再決定資料應該在傳送之前打包到連續緩衝中,還是直接從資料儲存...

golang 派生資料型別

資料型別概覽 指標型別 引用型別 var 變數 type 陣列 值型別 var 變數名 大小 型別 切片 引用型別 var 變數 型別 map 引用型別 var 變數名 map key type value type map切片 var 變數名 map key type value type str...

資料型別相關

整型常量 十進位制 18 31 long int型常量 123l 123l 123456l 123456l unsigned int型常量 123u 123u 根據實際資料大小確定int型還是long型 l以數字 0 開始的整型常量是八進位制數 022 037 010和10大小不一樣 因為八進位制並...