找回密码
 新注册用户
搜索
查看: 10730|回复: 3

[求助] MPI运行出错问题

[复制链接]
发表于 2009-12-1 10:32:42 | 显示全部楼层 |阅读模式
各位并行计算高手,小弟目前在学并行计算,用的框架是MPICH2。实现主从式并行遗传算法,我在单机上用3个进程通过mpich2提供的GUI界面可以得出结果,但利用命令行输入同样的命令却出现:process 0 exited without calling finalize   process 1 等等:123
我的主程序代码如下:
void main(int argc,char *argv[])
{
        int myid,numprocs,mysize;
        int i,j;
        double starttime,endtime;
        MPI_Status status;
        MPI_Request request;
        MPI_Init(&argc,&argv);
        MPI_Comm_rank(MPI_COMM_WORLD,&myid);
        MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
        if (numprocs>1)
                mysize = POPSIZE/(numprocs-1);
        else if (numprocs==1) mysize= POPSIZE;
        genotype * subpopulation = (genotype*)malloc(mysize*sizeof(genotype));
        if (!subpopulation)
        {
                printf("不能获得 %d 个大小的空间\n",mysize);
                MPI_Abort(MPI_COMM_WORLD,1);
        }
        MPI_Datatype mystruct;
        int     blocklens[6];
        MPI_Aint   indices[6];
        MPI_Datatype old_types[6];
        //各块中数据个数
        blocklens[0] = NVARS;
        blocklens[1] = NVARS;
        blocklens[2] = NVARS;
        blocklens[3] = 1;
        blocklens[4] = 1;
        blocklens[5] = 1;
        //数据类型
        for (i =0;i<6;i++) old_types=MPI_DOUBLE;
        //求地址和相对偏移
        MPI_Address(&population->gene,&indices[0]);
        MPI_Address(&population->upper,&indices[1]);
        MPI_Address(&population->lower,&indices[2]);
        MPI_Address(&population->fitness,&indices[3]);
        MPI_Address(&population->rfitness,&indices[4]);
        MPI_Address(&population->cfitness,&indices[5]);
        indices[5]=indices[5]-indices[0];
        indices[4]=indices[4]-indices[0];
        indices[3]=indices[3]-indices[0];
        indices[2]=indices[2]-indices[0];
        indices[1]=indices[1]-indices[0];
        indices[0]=0;
        MPI_Type_struct(6,blocklens,indices,old_types,&mystruct);
        MPI_Type_commit(&mystruct);
        if(myid == 0)
        {
                starttime=MPI_Wtime();
                if ((galog = fopen("galog.txt","w"))==NULL)
                        exit(1);
                fprintf(galog, "generation \t best \t \t average \t standard\n");
                fprintf(galog, "number     \t value\t \t fitness \t deviation\n");
                generation = 0;
                initialize();//编码
                evaluate();
                keep_the_best();
        }
        //开始进化
        while(generation<MAXGENS)
        {
                generation++;
                if (myid==0)//master
                {
                        select();   
                        crossover();
                       
                        for (i=1;i<= numprocs -1;i++) //发送
                        {
                                for(j=0;j<mysize;j++) //复制,其中mysize表示每一个处理区分配到的个体数
                                        subpopulation[j] = population[mysize*(i-1)+j];     
                                MPI_Ssend(subpopulation,mysize,mystruct,i,1,MPI_COMM_WORLD);//分配
                        }
                        for (i=1;i<= numprocs -1;i++) //接收
                        {
                                MPI_Recv(subpopulation,mysize,mystruct,i,2,MPI_COMM_WORLD,&status);//回收
                                for(j=0;j<mysize;j++)//拷回
                                        population[mysize*(i-1)+j] = subpopulation[j];
                        }
                        report();  
                        elitist();   
                }                //结束主进程
                else
                {
                        MPI_Recv(subpopulation,mysize,mystruct,0,1,MPI_COMM_WORLD,&status);
                        worker(subpopulation,mysize);
                        MPI_Ssend(subpopulation,mysize,mystruct,0,2,MPI_COMM_WORLD);
                }
                MPI_Barrier(MPI_COMM_WORLD);
        }
        if (myid==0)
        {
                fprintf(galog,"Simulation completed\n");
                fprintf(galog,"Best member:\n");
                for (i = 0; i < NVARS; i++)
                        fprintf (galog,"   var(%d) = %3.3f\n",i,population[POPSIZE].gene);
                fprintf(galog,"Best fitness = %3.3f",population[POPSIZE].fitness);
                printf("\nSuccess\n");
                endtime=MPI_Wtime();
                printf("mpi的时间: %f 秒",endtime-starttime);
                fprintf(galog,"mpi的时间: %f 秒",endtime-starttime);
                fclose(galog);
        }
        MPI_Type_free(&mystruct);
        MPI_Finalize();
}
望各路高手不吝赐教,谢谢了!
回复

使用道具 举报

发表于 2009-12-1 19:39:12 | 显示全部楼层
区分你们所学的“分布式计算”和我们参与的“分布式计算”。

我们都是志愿者,后台怎么编程的不了解。

另:还是尽量遵照规范格式,main函数用:
int main()
{

  return 0;
}


[ 本帖最后由 cuihao 于 2009-12-1 19:40 编辑 ]
回复

使用道具 举报

发表于 2009-12-1 21:37:22 | 显示全部楼层

回复 #2 cuihao 的帖子

并行计算库 在分布式计算社区问实在是~搞不懂~
回复

使用道具 举报

发表于 2009-12-1 22:13:11 | 显示全部楼层
所以说,咱们还是更名为“中国志愿计算总站”比较不容易混淆视听。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 新注册用户

本版积分规则

论坛官方淘宝店开业啦~
欢迎大家多多支持基金会~

Archiver|手机版|小黑屋|中国分布式计算总站 ( 沪ICP备05042587号 )

GMT+8, 2025-3-23 05:32

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表