#include <stdio.h> #include <stdlib.h> #include <time.h> #include <mpi.h> #include <assert.h> // Average method int compute_avg(int *array,int num_elements) { int sum = 0; int i; for (i = 0; i < num_elements; i++) { sum += array[i]; } return sum / num_elements; } int main(int argc,char** argv) { if (argc != 2) { fprintf(stderr,"Usage: avg num_elements_per_procn"); exit(1); } int num_elements_per_proc = atoi(argv[1]); MPI_Init(NULL,NULL); int world_rank; MPI_Comm_rank(MPI_COMM_WORLD,&world_rank); int world_size; MPI_Comm_size(MPI_COMM_WORLD,&world_size); // Create array with integers int *nums = NULL; if (world_rank == 0) { for (int i =0; i<5; i++){ nums[i] = i; } } // Subtable from any processes int *sub_nums = (int *)malloc(sizeof(int) * num_elements_per_proc); assert(sub_nums != NULL); // distribution numbers for all processes MPI_Scatter(nums,num_elements_per_proc,MPI_INT,sub_nums,MPI_COMM_WORLD); // Count avg subtable int sub_avg = compute_avg(sub_nums,num_elements_per_proc); // Collectiong averages int *sub_avgs = NULL; if (world_rank == 0) { sub_avgs = (int *)malloc(sizeof(int) * world_size); assert(sub_avgs != NULL); } MPI_Gather(&sub_avg,1,sub_avgs,MPI_COMM_WORLD); // Calculates the overall average if (world_rank == 0) { int avg = compute_avg(sub_avgs,world_size); printf("Avg of all elements is %dn",avg); // Obliczenie średniej na danych oryginalnych i wyświetlenie. int original_data_avg = compute_avg(nums,num_elements_per_proc * world_size); printf("Avg computed across original data is %dn",original_data_avg); } // free memory if (world_rank == 0) { free(nums); free(sub_avgs); } free(sub_nums); MPI_Barrier(MPI_COMM_WORLD); MPI_Finalize(); return 0; }
当我尝试运行这个( mpirun -c 4 avg 4 ),我得到错误列表:
处理接收到的信号*
信号:分段错误(11)
Netbeans MPI c ++如何启动?
什么是“信号15收到”
MPI程序挂起
networking上所有机器的主机名发现
简单的MPI程序失败,并有大量的进程
信号码:地址未映射(1)
(mangeke-mpi-2431940:03372)地址失败:(无)
[mangeke-mpi-2431940:03372] *错误消息结束*
我该如何解决这个问题?
“集群”软件与MPI有什么关系?
链接错误:未定义的引用到Windows 7上的`MPI_Init'
fork:重试:资源暂时不可用
mpi4py返回排名差异
尝试安装openmpi的分段错误
正如Hristo所述,数字被初始化为NULL。 如果您浏览使用调试器生成的核心文件,则会引发以下语句
核心是由`./m 4'生成的。 程序终止信号SIGSEGV,分段故障。 #0 0x00000000008809 in main(argc = 2,argv = 0x7ffd4fc87e68)at m.cxx:36 36 nums [i] = i;
如果你改变下面的代码,如下所示,你会得到运行没有segfaulting。
.... // Create array with integers int nums[num_elements_per_proc]; // <<-- change here if (world_rank == 0) { for (int i =0; i<5; i++){ nums[i] = i; } } .... // free memory if (world_rank == 0) { // free(nums); // <<-- change here,free not needed free(sub_avgs); }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。