gpt4 book ai didi

c - MPI_Scatterv 段错误

转载 作者:太空宇宙 更新时间:2023-11-04 03:50:23 26 4
gpt4 key购买 nike

我刚刚开始使用 MPI 编程,并决定使用 OpenMPI 制作一个简单的分布式 qsort。为了分配我想要排序的数组部分,我正在尝试使用 MPI_Scatterv,但是以下代码对我造成段错误:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <mpi.h>

#define ARRAY_SIZE 26
#define BUFFER_SIZE 2048

int main(int argc, char** argv) {
int my_rank, nr_procs;

int* data_in, *data_out;
int* sizes;
int* offsets;

srand(time(0));

MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &nr_procs);
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);

// everybody generates the control tables
int nr_workers = nr_procs-1;
sizes = malloc(sizeof(int)*nr_workers);
offsets = malloc(sizeof(int)*nr_workers);

int nr_elems = ARRAY_SIZE/nr_workers;

// basic distribution
for (int i = 0; i < nr_workers; ++i) {
sizes[i] = nr_elems;
}
// distribute the remainder
int left = ARRAY_SIZE%nr_workers;
int curr_worker = 0;
while (left) {
++sizes[curr_worker];
curr_worker = (++curr_worker)%nr_workers;
--left;
}
// offsets
int curr_offset = 0;
for (int i = 0; i < nr_workers; ++i) {
offsets[i] = curr_offset;
curr_offset += sizes[i];
}


if (my_rank == 0) {
// root
data_in = malloc(sizeof(int)*ARRAY_SIZE);
data_out = malloc(sizeof(int)*ARRAY_SIZE);
for (int i = 0; i < ARRAY_SIZE; ++i) {
data_in[i] = rand();
}

for (int i = 0; i < nr_workers; ++i) {
printf("%d at %d\n", sizes[i], offsets[i]);
}

MPI_Scatterv (data_in, sizes, offsets, MPI_INT, data_out, ARRAY_SIZE, MPI_INT, 0, MPI_COMM_WORLD);


} else {
// worker
printf("%d has %d elements!\n",my_rank, sizes[my_rank-1]);
// alloc the input buffer
data_in = malloc(sizeof(int)*sizes[my_rank-1]);

MPI_Scatterv(NULL, NULL, NULL, MPI_INT, data_in, sizes[my_rank-1], MPI_INT, 0, MPI_COMM_WORLD);

printf("%d got:\n", my_rank);
for (int i = 0; i < sizes[my_rank-1]; ++i) {
printf("%d ", data_in[i]);
}
printf("\n");
}

MPI_Finalize();
return 0;
}

我将如何使用 Scatterv?从工作代码内部分配我的输入缓冲区是不是我做错了什么?

最佳答案

我更改了您代码中的某些部分以使其正常工作。

MPI_Scatter() 会将数据发送到每个处理器,包括他自己。根据您的程序,处理器 0 需要 ARRAY_SIZE 整数,但 sizes[0] 小得多。其他进程还有其他问题:MPI_Scatter 将发送 sizes[my_rank] 整数,但 sizes[my_rank-1] 将被期望...

这是将 data_in 从 0 分散到所有处理器(包括 0)的代码。因此我将 1 添加到 nr_workers :

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <mpi.h>

#define ARRAY_SIZE 26
#define BUFFER_SIZE 2048

int main(int argc, char** argv) {
int my_rank, nr_procs;

int* data_in, *data_out;
int* sizes;
int* offsets;

srand(time(0));

MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &nr_procs);
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);

// everybody generates the control tables
int nr_workers = nr_procs;
sizes = malloc(sizeof(int)*nr_workers);
offsets = malloc(sizeof(int)*nr_workers);

int nr_elems = ARRAY_SIZE/nr_workers;

// basic distribution
for (int i = 0; i < nr_workers; ++i) {
sizes[i] = nr_elems;
}
// distribute the remainder
int left = ARRAY_SIZE%nr_workers;
int curr_worker = 0;
while (left) {
++sizes[curr_worker];
curr_worker = (++curr_worker)%nr_workers;
--left;
}
// offsets
int curr_offset = 0;
for (int i = 0; i < nr_workers; ++i) {
offsets[i] = curr_offset;
curr_offset += sizes[i];
}

if (my_rank == 0) {
// root
data_in = malloc(sizeof(int)*ARRAY_SIZE);

for (int i = 0; i < ARRAY_SIZE; ++i) {
data_in[i] = rand();
printf("%d %d \n",i,data_in[i]);
}

for (int i = 0; i < nr_workers; ++i) {
printf("%d at %d\n", sizes[i], offsets[i]);
}


} else {
printf("%d has %d elements!\n",my_rank, sizes[my_rank]);
}
data_out = malloc(sizeof(int)*sizes[my_rank]);

MPI_Scatterv (data_in, sizes, offsets, MPI_INT, data_out, sizes[my_rank], MPI_INT, 0, MPI_COMM_WORLD);

printf("%d got:\n", my_rank);
for (int i = 0; i < sizes[my_rank]; ++i) {
printf("%d ", data_out[i]);
}
printf("\n");
free(data_out);
if(my_rank==0){
free(data_in);
}
MPI_Finalize();
return 0;
}

关于内存管理,data_indata_out 应该在代码末尾释放。

这是你想做的吗?祝 qsort 好运!我认为您不是第一个使用 MPI 对整数进行排序的人。参见 parallel sort using mpi .您在 0 进程上生成随机数然后分散它们的方法是正确的方法。我想你会对他的 TD_Trier() 通信功能感兴趣。即使您将 tri_fusion(T, 0, size - 1); 更改为 qsort(...)...

再见,

弗朗西斯

关于c - MPI_Scatterv 段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21076044/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com