你好,游客 登录 注册 搜索
背景:
阅读新闻

MPI 并行解方程

[日期:2012-08-09] 来源:Linux社区  作者:pouloghost [字体:大 中 小]

基本算法 逐步缩小函数值异号的范围 最后逼近最终解

所有线程计算中地位相同 计算范围与self号相应的区段值 把x较小值做为解 只支持单个解

lx做为计算范围和终止条件 最后 由主线程显示结果

  1. #include "mpi.h"   
  • #include <stdio.h>   
  • #include <stdlib.h>   
  •   
  • #define END 999999   
  • #define CON 1   
  • #define RES 2   
  • //calculation values   
  • #define OST 0.000001   
  • #define IL 0.5   
  • #define IH 1.5   
  • #define THD 0.0001   
  • float func(float x) {  
  •     //any function   
  •     return (x*x-1);  
  • }  
  • int main(int argc,char *argv[]) {     
  •     int self,size;  
  •     MPI_Init(&argc,&argv);  
  •     MPI_Comm_rank(MPI_COMM_WORLD,&self);  
  •     MPI_Comm_size(MPI_COMM_WORLD,&size);  
  •     MPI_Request r;  
  •     MPI_Status s;  
  •     float lx=IL,hx=IH;//end point value   
  •     float res=END;  
  •     float *data=(float *)malloc(size*sizeof(float));//for gather   
  •     while(((hx-lx)/size>THD)&&(END!=lx)) {  
  •         res=END;  
  •         float step=(hx-lx)/size;  
  •         lx=lx+step*(self);  
  •         hx=lx+step-OST;  
  •         float lv=func(lx);  
  •         float hv=func(hx);  
  •         if(lv*hv<0) {  
  •             //continue calculation   
  •         } else {  
  •             if(0==lv) {  
  •                 //end and mark to pass low to root   
  •                 res=lx;  
  •             } else if(0==hv) {  
  •                 //end and mark to pass high to root   
  •                 res=hx;  
  •             } else {  
  •                 //wait for a new lx hx   
  •             }  
  •             lx=END;  
  •         }  
  •         //gather all lx   
  •         MPI_Allgather(&lx,1,MPI_FLOAT,data,1,MPI_FLOAT,MPI_COMM_WORLD);  
  •         int prc=END;  
  •         for(int i=0;i<size;++i) {  
  •             if(END!=data[i]) {  
  •                 prc=i;  
  •                 lx=data[i];  
  •             }  
  •         }  
  •         if(END==prc) {//all ends   
  •             if(END!=res) {//send res to root   
  •                 MPI_Ssend(&res,1,MPI_FLOAT,0,0,MPI_COMM_WORLD);  
  •             }  
  •         } else {  
  •             MPI_Bcast(&hx,1,MPI_FLOAT,prc,MPI_COMM_WORLD);  
  •         }  
  •     }  
  •     if(0==self) {//show result   
  •         if(END==lx) {  
  •             MPI_Recv(&lx,1,MPI_FLOAT,MPI_ANY_SOURCE,0,MPI_COMM_WORLD,&s);  
  •             for(int i=0;i<size;++i) {  
  •                 if(END!=data[i]) {  
  •                     lx=data[i];  
  •                 }  
  •             }  
  •         } else {  
  •             lx=(hx+lx)/2;  
  •         }  
  •         printf("result %f \n",lx);  
  •     }  
  •     free(data);  
  •     MPI_Finalize();  
  •     return 0;  
  • }  
  • linux
    相关资讯       MPI 
    本文评论   查看全部评论 (0)
    表情: 表情 姓名: 字数

           

    评论声明
    • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
    • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
    • 本站管理人员有权保留或删除其管辖留言中的任意内容
    • 本站有权在网站内转载或引用您的评论
    • 参与本评论即表明您已经阅读并接受上述条款