c-在MPI中使用Cblacs初始化网格的问题



我正在尝试使用以下代码设置一个非常简单的1*2网格:


int nprow, npcol, myrow, mycol, myid;
char rowcol[1] = "R";
nprow = 1;
npcol = size / nprow;
if(npcol * nprow != size){
printf("Error");
MPI_Finalize();
exit(1);
}
Cblacs_pinfo(&myid, &size);
Cblacs_get(0, 0, &ictxt);
Cblacs_gridinit(&ictxt, rowcol, nprow, npcol);
Cblacs_pcoord(ictxt, myid, &myrow, &mycol);
printf("rank = %d, nprow = %d, npcol = %d, myrow = %d, mycol = %dn", rank, nprow, npcol, myrow, mycol);                                                                    }

问题是,无论最初设置为什么,Cblacs_pcoord函数似乎都在将nprow更改为0,而这反过来又为每个myrow提供0,而npcol和mycol变量对于任何数量的处理器都是正确的。我很困惑,因为这个函数不应该接触nprow,但我在每一行代码之后都打印了nprow,直到调用该函数之后,它才是正确的值。

如果我缺少任何有助于你回答我问题的信息,请告诉我,我会相应地更新。

简短回答:使用Cblacs_gridinfo而不是Cblacs_pcoord

长答案:请给出一个完整的、最少的例子。我填写了一堆细节来编译一些东西。

使用const char* rowcol = "R";而不是char rowcol[1] = "R";,这会产生编译错误:

blacs.cc:22:22: error: initializer-string for char array is too long
char rowcol[1] = "R";
^~~

将列声明为1字符数组可能会导致堆栈损坏,因为";R〃;是2个字符,包括终止的null字符。

我不确定你得到了什么错误的输出。你的代码似乎对我有效。但我仍然建议使用gridinfo而不是pcord,因为ScaLAPACK在任何地方都是这样做的,我认为pcord在col-major网格中都是坏的。

这是完整的代码。

#include <mpi.h>
#include <stdlib.h>
extern "C" void Cblacs_pinfo( int* mypnum, int* nprocs );
extern "C" void Cblacs_get( int context, int request, int* value );
extern "C" int  Cblacs_gridinit( int* context, const char* order, int np_row, int np_col );
extern "C" void Cblacs_gridinfo( int context, int*  np_row, int* np_col, int*  my_row, int*  my_col );
extern "C" void Cblacs_gridexit( int context );
extern "C" void Cblacs_exit( int error_code );
extern "C" void Cblacs_abort( int context, int error_code );
extern "C" void Cblacs_abort( int context, int error_code );
extern "C" void Cblacs_pcoord( int icontxt, int pnum, int* prow, int* pcol );
int main( int argc, char** argv )
{
MPI_Init( &argc, &argv );
int rank;
MPI_Comm_rank( MPI_COMM_WORLD, &rank );
int nprow, npcol, myrow, mycol, myid;
//char rowcol[1] = "R";
const char* rowcol = "R";
int size;
Cblacs_pinfo( &myid, &size );
nprow = 1;
npcol = size / nprow;
if (npcol * nprow != size) {
printf( "Error" );
MPI_Finalize();
exit( 1 );
}
int ictxt;
Cblacs_get( 0, 0, &ictxt );
Cblacs_gridinit( &ictxt, rowcol, nprow, npcol );
//Cblacs_pcoord( ictxt, myid, &myrow, &mycol );
Cblacs_gridinfo( ictxt, &nprow, &npcol, &myrow, &mycol );
printf( "rank = %d, nprow = %d, npcol = %d, myrow = %d, mycol = %dn",
rank, nprow, npcol, myrow, mycol );
MPI_Finalize();
return 0;
}

编译并运行:

> mpicxx -o blacs blacs.cc -lscalapack -lgfortran
> mpirun -np 4 ./blacs
rank = 0, nprow = 1, npcol = 4, myrow = 0, mycol = 0
rank = 1, nprow = 1, npcol = 4, myrow = 0, mycol = 1
rank = 2, nprow = 1, npcol = 4, myrow = 0, mycol = 2
rank = 3, nprow = 1, npcol = 4, myrow = 0, mycol = 3

最新更新