我想送存儲在STL向量形式如何使用C++ STL向量通過Boost MPI發送矩陣的列?
vector < vector <double> > A (10, vector <double> (10));
矩陣的多列,而不滿足於升壓MPI複製到某些緩存(因爲計算時間是在這裏至關重要)。
我發現,MPI如何做到這一點。下面是示例代碼如何從一個進程(rank == 0)向另一個進程(rank == 1)發送10×10矩陣的第4,5,6列。 (即使我不知道爲什麼我必須在MPI_Typ_vector的第三個參數中添加'2',有人知道爲什麼?)。
int rank, size;
MPI_Init (&argc, &argv); /* starts MPI */
MPI_Comm_rank (MPI_COMM_WORLD, &rank); /* get current process id */
MPI_Comm_size (MPI_COMM_WORLD, &size); /* get number of processes */
// fill matrices
vector< vector <float> >A(10, vector <float> (10));
vector< vector <float> >A_copy(10, vector <float> (10));
for (int i=0; i!=10; i++)
{
for (int j=0; j!=10; j++)
{
A[i][j]=j+ i*10;
A_copy[i][j]=0.0;
}
}
int dest=1;
int tag=1;
// define new type = two columns
MPI_Datatype newtype;
MPI_Type_vector(10, /* # column elements */
3, /* 3 column only */
10+2, /* skip 10 elements */
MPI_FLOAT, /* elements are float */
&newtype); /* MPI derived datatype */
MPI_Type_commit(&newtype);
if (rank==0)
{
MPI_Send(&A[0][4], 1, newtype, dest, tag, MPI_COMM_WORLD);
}
if (rank==1)
MPI_Status status;
MPI_Recv(&A_copy[0][4], 1, newtype, 0, tag, MPI_COMM_WORLD, &status);
}
MPI_Finalize();
在升壓網頁,他們聲稱MPI_Type_vector是 「在Boost.MPI自動使用」(http://www.boost.org/doc/libs/1_47_0/doc/html/mpi/tutorial。 HTML#mpi.c_mapping)。
但我找不到一個例子如何做到這一點的細節。在只知道如何發送整個矩陣或每個元素與Boost後。
謝謝你在前進,
托比亞斯
感謝您的回答接收到結束。但是我需要在程序的另一點處理矩陣A [i]的行。例如。將行A [i]作爲參數傳遞給期望向量的函數。因此,我不能在不復制整個矩陣的情況下切換行和列。無論如何,應該有一種方法來做到這一點與提升mpi,因爲它是適用於mpi。 – tstollenw