当我执行mpi prgramming并在单个进程中执行时,系统显示我"信号:分段错误(11)"和"信号代码:无效权限(2)" ,但是这个程序可以在没有MPI换行的情况下执行查找,我的意思是如果只执行没有MPI_Init()的人员,它工作正常
#include "mpi.h"
#include <stdio.h>
#include <stdlib.h>
#define BUF_SIZE 1024
int main(int argc, char **argv) {
int numprocs,myid,read,i,allocatedjobs,j,current_worker_uc,uc_master_total=0;//i is definded by teacher
int array[0];
char * buf;
char * worker_buf;
int worker_result;
int number_amount;
MPI_Status status;
MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD,&myid);
MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
if (numprocs==1){
// /* trivial single cpu case */
read=buf_read(buf,buf+BUF_SIZE);
while(read)
{
//printf("Actual number read is:%d\n",read);
if(read==0){break;}
int current_buf_uc=count_uc(buf,read);
uc_all=(uc_all+current_buf_uc)%10+(uc_all+current_buf_uc)/10;
if(read<BUF_SIZE){break;}
read=buf_read(buf,buf+BUF_SIZE);
}
fclose(stdin);
printf("%d\n",uc_all);
}
MPI_Finalize();
return 0;
}
int buf_read(char * buf, const char * end) {
int count;
count = fread(buf, 1, end - buf, stdin);
if (count > 0 && buf[count - 1] == '\n') {
--count;
}
return count;
}
int count_uc(char buf[],int size)
{
int a = buf[0] - '0';
int uc = -1;
int j = 0;
while (j<(size - 1))
{
int b = buf[j + 1] - '0';
uc = (a + b) % 10 + (a + b) / 10;
a = uc;
++j;
}
return uc;
}
答案 0 :(得分:2)
buf
在
read=buf_read(buf,buf+BUF_SIZE);
你需要一个数组
char buf[BUF_SIZE];
或指向已分配空间的指针
char *buf = malloc(BUF_SIZE);
为了存储由fread