在 MPI_IScatterv() 问题中设置位移数组
Setting displacement array in MPI_IScatterv() issue
嘿,我在 MPI_IScatterv() 函数中设置位移数组时遇到问题。
我需要将部分图片分散到不同的进程。我自己做了 MPI_Datatype:
private: MPI_Datatype createMPI_PixelType(){
MPI_Datatype new_type;
int count = 3;
int blocklens[] = { 1,1,1 };
MPI_Aint indices[3];
indices[0] = (MPI_Aint)offsetof(struct Pixel, Red);
indices[1] = (MPI_Aint)offsetof(struct Pixel, Green);
indices[2] = (MPI_Aint)offsetof(struct Pixel, Blue);
MPI_Datatype old_types[] = { MPI_INT, MPI_INT, MPI_INT };
MPI_Type_create_struct(count, blocklens, indices, old_types, &new_type);
MPI_Type_commit(&new_type);
return new_type;}
结构像素
struct Pixel {
int Red;
int Green;
int Blue; }
作为函数参数,我需要传递位移数组。
我的理解是每个进程都从指定为 disp[proc_id] 的不同位置开始读取 sendBuffer。例如进程0 disp = 0,
进程 1 disp = sizeof 0 元素,
进程 2 disp = sizeof 1 + 0 元素等。
这是代码
...
//specify arrays for scathering
sendCountArray = calcSendCounts(workTable);
displacementArray = calcDisplacementArray(workTable);
Pixel *fullPicture = pictureToPixelsArray();
cout << endl;
for (int i = 0; i < pixelsOnPicture; i++) {
cout <<
" r:" << fullPicture[i].Red <<
" g:" << fullPicture[i].Green <<
" b:" << fullPicture[i].Blue << endl;
}
cout << endl << "sendArray:" << endl;
for (int i = 0; i < worldSize; i++) {
cout << "i:" << i << " " << sendCountArray[i] << endl;
}
cout << endl << "displacementArray:" << endl;
for (int i = 0; i < worldSize; i++) {
cout << "i:" << i << " " << displacementArray[i] << endl;
}
}
cout << endl;
//
MPI_Scatter(sendCountArray, 1, MPI_INT, &pixelsSizeBuffer, 1, MPI_INT, 0, MPI_COMM_WORLD);
//sending part of picture
MPI_Request request;
MPI_Datatype mMPI_PIXEL = createMPI_PixelType();
partPicture = new Pixel[pixelsSizeBuffer];
MPI_Iscatterv(pictureToPixelsArray(), sendCountArray, displacementArray, mMPI_PIXEL, partPicture, pixelsSizeBuffer, mMPI_PIXEL, 0, MPI_COMM_WORLD, &request);
for (int i = 0; i < pixelsSizeBuffer; i++) {
cout << "proc:" << procID <<
" r:" << partPicture[i].Red <<
" g:" << partPicture[i].Green <<
" b:" << partPicture[i].Blue << endl;
}
这是测试输出(6 个进程)
pixelsOnPicture:9
workTableSize: 6
r:255 g:242 b:0
r:63 g:72 b:204
r:237 g:28 b:36
r:0 g:162 b:232
r:163 g:73 b:164
r:63 g:11 b:15
r:34 g:177 b:76
r:255 g:242 b:0
r:63 g:10 b:16
sendArray:
i:0 2
i:1 2
i:2 2
i:3 1
i:4 1
i:5 1
displacementArray:
i:0 0
i:1 2
i:2 4
i:3 6
i:4 7
i:5 8
proc:4 r:-842150451 g:-842150451 b:-842150451
proc:0 r:255 g:242 b:0
proc:2 r:-842150451 g:-842150451 b:-842150451
proc:5 r:-842150451 g:-842150451 b:-842150451
proc:0 r:63 g:72 b:204
proc:2 r:-842150451 g:-842150451 b:-842150451
proc:1 r:-842150451 g:-842150451 b:-842150451
proc:3 r:-842150451 g:-842150451 b:-842150451
proc:1 r:-842150451 g:-842150451 b:-842150451
前 2 个元素发送正确..
您尚未等待请求,因此无法保证 Iscatterv 在您检查输出时已完成。调用普通的旧 Scatterv 将确保它在调用后完成,这样您就可以检查是否正确设置了所有参数和数据类型。或者在调用 Iscatterv 后添加一个 MPI_Wait(&request),但请注意,您在这里所做的只是重新实现阻塞调用。
嘿,我在 MPI_IScatterv() 函数中设置位移数组时遇到问题。
我需要将部分图片分散到不同的进程。我自己做了 MPI_Datatype:
private: MPI_Datatype createMPI_PixelType(){
MPI_Datatype new_type;
int count = 3;
int blocklens[] = { 1,1,1 };
MPI_Aint indices[3];
indices[0] = (MPI_Aint)offsetof(struct Pixel, Red);
indices[1] = (MPI_Aint)offsetof(struct Pixel, Green);
indices[2] = (MPI_Aint)offsetof(struct Pixel, Blue);
MPI_Datatype old_types[] = { MPI_INT, MPI_INT, MPI_INT };
MPI_Type_create_struct(count, blocklens, indices, old_types, &new_type);
MPI_Type_commit(&new_type);
return new_type;}
结构像素
struct Pixel {
int Red;
int Green;
int Blue; }
作为函数参数,我需要传递位移数组。 我的理解是每个进程都从指定为 disp[proc_id] 的不同位置开始读取 sendBuffer。例如进程0 disp = 0, 进程 1 disp = sizeof 0 元素, 进程 2 disp = sizeof 1 + 0 元素等。 这是代码
...
//specify arrays for scathering
sendCountArray = calcSendCounts(workTable);
displacementArray = calcDisplacementArray(workTable);
Pixel *fullPicture = pictureToPixelsArray();
cout << endl;
for (int i = 0; i < pixelsOnPicture; i++) {
cout <<
" r:" << fullPicture[i].Red <<
" g:" << fullPicture[i].Green <<
" b:" << fullPicture[i].Blue << endl;
}
cout << endl << "sendArray:" << endl;
for (int i = 0; i < worldSize; i++) {
cout << "i:" << i << " " << sendCountArray[i] << endl;
}
cout << endl << "displacementArray:" << endl;
for (int i = 0; i < worldSize; i++) {
cout << "i:" << i << " " << displacementArray[i] << endl;
}
}
cout << endl;
//
MPI_Scatter(sendCountArray, 1, MPI_INT, &pixelsSizeBuffer, 1, MPI_INT, 0, MPI_COMM_WORLD);
//sending part of picture
MPI_Request request;
MPI_Datatype mMPI_PIXEL = createMPI_PixelType();
partPicture = new Pixel[pixelsSizeBuffer];
MPI_Iscatterv(pictureToPixelsArray(), sendCountArray, displacementArray, mMPI_PIXEL, partPicture, pixelsSizeBuffer, mMPI_PIXEL, 0, MPI_COMM_WORLD, &request);
for (int i = 0; i < pixelsSizeBuffer; i++) {
cout << "proc:" << procID <<
" r:" << partPicture[i].Red <<
" g:" << partPicture[i].Green <<
" b:" << partPicture[i].Blue << endl;
}
这是测试输出(6 个进程)
pixelsOnPicture:9
workTableSize: 6
r:255 g:242 b:0
r:63 g:72 b:204
r:237 g:28 b:36
r:0 g:162 b:232
r:163 g:73 b:164
r:63 g:11 b:15
r:34 g:177 b:76
r:255 g:242 b:0
r:63 g:10 b:16
sendArray:
i:0 2
i:1 2
i:2 2
i:3 1
i:4 1
i:5 1
displacementArray:
i:0 0
i:1 2
i:2 4
i:3 6
i:4 7
i:5 8
proc:4 r:-842150451 g:-842150451 b:-842150451
proc:0 r:255 g:242 b:0
proc:2 r:-842150451 g:-842150451 b:-842150451
proc:5 r:-842150451 g:-842150451 b:-842150451
proc:0 r:63 g:72 b:204
proc:2 r:-842150451 g:-842150451 b:-842150451
proc:1 r:-842150451 g:-842150451 b:-842150451
proc:3 r:-842150451 g:-842150451 b:-842150451
proc:1 r:-842150451 g:-842150451 b:-842150451
前 2 个元素发送正确..
您尚未等待请求,因此无法保证 Iscatterv 在您检查输出时已完成。调用普通的旧 Scatterv 将确保它在调用后完成,这样您就可以检查是否正确设置了所有参数和数据类型。或者在调用 Iscatterv 后添加一个 MPI_Wait(&request),但请注意,您在这里所做的只是重新实现阻塞调用。