Changeset 18457


Ignore:
Timestamp:
08/21/14 16:06:57 (11 years ago)
Author:
abuzzi
Message:

MODIF:final version of atsu cpy multithread

File:
1 edited

Legend:

Unmodified
Added
Removed
  • issm/trunk-jpl/src/m/contrib/gravity/vfsa.cpp

    r18454 r18457  
    88#include <cassert>
    99#include <gsl/gsl_multifit.h>
     10#include <pthread.h>
    1011
    1112using namespace std;
     
    126127void vec2grid(Matrix *V,Matrix *V1,Matrix *V2,int nx, int ny);
    127128void msplit( Matrix *m, Matrix *m1,Matrix *m2,double dlevel);
    128 void plouff(Matrix *g,Matrix *Pobs,Matrix *Pp,Matrix * mesh,Matrix *rho,int dx,int dy, int dn,int m,int n,int l);
     129void* plouffT(void* vpthread_handle);
    129130void vec2gridsimple(Matrix *V,Matrix *V1,int nx, int ny);
    130131void reshape(Matrix* V,Matrix* V1,int nx,int ny);
     
    135136void newmodelgen(Matrix* m0,Matrix* m1,Matrix* bathy,Matrix* icethick,int mx,int my,double T,double ptval,double mmax,double mmax2,double ctr,double sd);
    136137double coolshed(double T0,int k,double c,double D);
     138void   LaunchThread(void* function(void*), void* usr,int num_threads);
     139/*}}}*/
     140
     141/*Multithreading structures {{{*/
     142typedef struct{
     143        void* usr;
     144        int   my_thread;
     145        int   num_threads;
     146} pthread_handle;
     147
     148typedef struct{
     149        Matrix *g;
     150        Matrix *Pobs;
     151        Matrix *Pp;
     152        Matrix *mesh;
     153        Matrix *rho;
     154        int dx;
     155        int dy;
     156        int dn;
     157        int m;
     158        int n;
     159        int l;
     160} AppStruct;
    137161/*}}}*/
    138162
     
    391415                        }
    392416                }
     417        cout<<totaliter<<endl;
    393418        }
    394419
     
    400425                savefile2 << m_min->GetValue(i,0)<<"   "<< m_min->GetValue(i,1)<<"   "<< m_min->GetValue(i,2)<<endl;
    401426        }
    402         savefile1.close;
    403         savefile2.close;
     427        savefile1.close();
     428        savefile2.close();
    404429                        /*}}}*/
    405430        return 0;
     
    484509        m2->AddNumber(dlevel);
    485510}/*}}}*/
    486 void plouff(Matrix *g,Matrix *Pobs,Matrix *Pp,Matrix * mesh,Matrix *rho,int dx,int dy, int dn,int m,int n,int l){/*{{{*/
    487                 double gg=6.673e-11;
    488                 int si,sj,id,s;
    489                 double R,Q,P;
    490                 Matrix *xp= new Matrix(1,2);
    491                 Matrix *yp= new Matrix(1,2);
    492                 Matrix *xpp= new Matrix(1,2);
    493                 Matrix *ypp= new Matrix(1,2);
    494                 Matrix *U= new Matrix(l,4);
    495                 Matrix *U1=new Matrix(1,4);
    496                 Matrix *U2=new Matrix(1,4);
    497                 Matrix *gl= new Matrix(1,l-1);
    498                 bool test=true;
    499                 for(int c=0;c<n;c++){
    500                         g->SetValue(c,0,0);
    501                         for(int a=0;a<m;a++){
    502                                 test=true;
    503                                 xp->SetValue(0,0,Pp->GetValue(a,0)-Pobs->GetValue(c,0));
    504                                 xp->SetValue(0,1,Pp->GetValue(a,0)-Pobs->GetValue(c,0)+dx);
    505                                 if(xp->GetValue(0,0)<0 && xp->GetValue(0,0)<xp->GetValue(0,1) && xp->GetValue(0,0)*xp->GetValue(0,1)>=0){
    506                                         xpp->SetValue(0,0,xp->GetValue(0,1));
    507                                         xpp->SetValue(0,1,xp->GetValue(0,0));
    508                                         xp->MatrixAbs(xpp);
    509                                 }
    510                                 yp->SetValue(0,0,Pp->GetValue(a,1)-Pobs->GetValue(c,1));
    511                                 yp->SetValue(0,1,Pp->GetValue(a,1)-Pobs->GetValue(c,1)+dy);
    512                                 if(yp->GetValue(0,0)<0 && yp->GetValue(0,0)<yp->GetValue(0,1) && yp->GetValue(0,0)*yp->GetValue(0,1)>=0){
    513                                         ypp->SetValue(0,0,yp->GetValue(0,1));
    514                                         ypp->SetValue(0,1,yp->GetValue(0,0));
    515                                         yp->MatrixAbs(ypp);
    516                                 }
    517                                 P=sqrt(xp->GetValue(0,0)*xp->GetValue(0,0)+yp->GetValue(0,0)*yp->GetValue(0,0));
    518                                 if(P>dn){
    519                                         test=false;
    520                                         for(int i=0;i<l-1;i++){
    521                                                 gl->SetValue(0,i,0);
    522                                         }
    523                                 }
    524                                 if(test==true){
    525                                         si=1;
    526                                         sj=1;
    527                                         id=0;
    528                                         for(int i=0;i<2;i++){
    529                                                 si*=-1;
    530                                                 for(int j=0;j<2;j++){
    531                                                         si*=-1;
    532                                                         s=si*sj;
    533                                                         for(int k=0;k<l;k++){
    534                                                                 R=sqrt(xp->GetValue(0,i)*xp->GetValue(0,i)+yp->GetValue(0,j)*yp->GetValue(0,j)+mesh->GetValue(a,k)*mesh->GetValue(a,k));
    535                                                                 Q=atan(xp->GetValue(0,i)*yp->GetValue(0,j)/(mesh->GetValue(a,k)*R));
    536                                                                 U->SetValue(k,id,s*(mesh->GetValue(a,k)*Q-xp->GetValue(0,i)*log(R+yp->GetValue(0,j))-yp->GetValue(0,j)*log(R+xp->GetValue(0,i))));
    537                                                         }
    538                                                         id++;
    539                                                 }
    540                                         }
    541                                         for(int b=0;b<l-1;b++){
    542                                                 U->ExtractLine(U1,b);
    543                                                 U->ExtractLine(U2,b+1);
    544                                                 gl->SetValue(0,b,rho->GetValue(0,b)*(U1->MatrixInternSum()*(-1)+U2->MatrixInternSum()));
    545                                         }
    546                                 }
    547                                 g->SetValue(c,0,g->GetValue(c,0)+gg*gl->MatrixInternSum());
    548                         }
    549                 }
    550                 delete xp;
    551                 delete yp;
    552                 delete xpp;
    553                 delete ypp;
    554                 delete gl;
    555                 delete U;
    556                 delete U1;
    557                 delete U2;
    558         }/*}}}*/
    559511void vec2gridsimple(Matrix *V,Matrix *V1,int nx, int ny){/*{{{*/
    560512        for(int i=0;i<ny;i++){
     
    585537        double e=0;
    586538        msplit(m0,m1,m2,dlevel);
    587    plouff(g1,Pobs,Pp,m1,rho1,dx,dy,dn,mx*my,nx*ny,4);
    588    plouff(g2,Pobs,Pp,m2,rho2,dx,dy,dn,mx*my,nx*ny,3);
     539       
     540        /*Multithreaded core*/
     541        int       num_threads = 4;
     542        AppStruct usr;
     543        usr.g = g1;
     544        usr.Pobs = Pobs;
     545        usr.Pp=Pp;
     546        usr.mesh= m1;
     547        usr.rho= rho1;
     548        usr.dx=dx;
     549        usr.dy=dy;
     550        usr.dn=dn;
     551        usr.m=mx*my;
     552        usr.n=nx*ny;
     553        usr.l=4;
     554        LaunchThread(plouffT,(void*)&usr,num_threads);
     555        usr.g = g2;
     556        usr.mesh= m2;
     557        usr.rho= rho2;
     558        usr.l=3;
     559        LaunchThread(plouffT,(void*)&usr,num_threads);
    589560        g->MatrixSum(g1,g2);
    590561        vec2gridsimple(g,gcalgr,nx,ny);
     
    696667        return T1;
    697668}/*}}}*/
     669void* plouffT(void* vpthread_handle){/*{{{*/
     670
     671        /*recover this thread info*/
     672        pthread_handle *handle = (pthread_handle*)vpthread_handle;
     673        int my_thread   = handle->my_thread;
     674        int num_threads = handle->num_threads;
     675
     676        /*Recover struct*/
     677        AppStruct *usr = (AppStruct*)handle->usr;
     678        Matrix *g = usr->g;
     679        Matrix *Pobs = usr->Pobs;
     680        Matrix *Pp = usr->Pp;
     681        Matrix *mesh = usr->mesh;
     682        Matrix *rho = usr->rho;
     683        int dx =usr->dx;
     684        int dy =usr->dy;
     685        int dn =usr->dn;
     686        int m =usr->m;
     687        int n =usr->n;
     688        int l =usr->l;
     689
     690        double gg=6.673e-11;
     691        int si,sj,id,s;
     692        double R,Q,P;
     693        Matrix *xp= new Matrix(1,2);
     694        Matrix *yp= new Matrix(1,2);
     695        Matrix *xpp= new Matrix(1,2);
     696        Matrix *ypp= new Matrix(1,2);
     697        Matrix *U= new Matrix(l,4);
     698        Matrix *U1=new Matrix(1,4);
     699        Matrix *U2=new Matrix(1,4);
     700        Matrix *gl= new Matrix(1,l-1);
     701        bool test=true;
     702        for(int c=my_thread;c<n;c+=num_threads){
     703                g->SetValue(c,0,0);
     704                for(int a=0;a<m;a++){
     705                        test=true;
     706                        xp->SetValue(0,0,Pp->GetValue(a,0)-Pobs->GetValue(c,0));
     707                        xp->SetValue(0,1,Pp->GetValue(a,0)-Pobs->GetValue(c,0)+dx);
     708                        if(xp->GetValue(0,0)<0 && xp->GetValue(0,0)<xp->GetValue(0,1) && xp->GetValue(0,0)*xp->GetValue(0,1)>=0){
     709                                xpp->SetValue(0,0,xp->GetValue(0,1));
     710                                xpp->SetValue(0,1,xp->GetValue(0,0));
     711                                xp->MatrixAbs(xpp);
     712                        }
     713                        yp->SetValue(0,0,Pp->GetValue(a,1)-Pobs->GetValue(c,1));
     714                        yp->SetValue(0,1,Pp->GetValue(a,1)-Pobs->GetValue(c,1)+dy);
     715                        if(yp->GetValue(0,0)<0 && yp->GetValue(0,0)<yp->GetValue(0,1) && yp->GetValue(0,0)*yp->GetValue(0,1)>=0){
     716                                ypp->SetValue(0,0,yp->GetValue(0,1));
     717                                ypp->SetValue(0,1,yp->GetValue(0,0));
     718                                yp->MatrixAbs(ypp);
     719                        }
     720                        P=sqrt(xp->GetValue(0,0)*xp->GetValue(0,0)+yp->GetValue(0,0)*yp->GetValue(0,0));
     721                        if(P>dn){
     722                                test=false;
     723                                for(int i=0;i<l-1;i++){
     724                                        gl->SetValue(0,i,0);
     725                                }
     726                        }
     727                        if(test==true){
     728                                si=1;
     729                                sj=1;
     730                                id=0;
     731                                for(int i=0;i<2;i++){
     732                                        si*=-1;
     733                                        for(int j=0;j<2;j++){
     734                                                si*=-1;
     735                                                s=si*sj;
     736                                                for(int k=0;k<l;k++){
     737                                                        R=sqrt(xp->GetValue(0,i)*xp->GetValue(0,i)+yp->GetValue(0,j)*yp->GetValue(0,j)+mesh->GetValue(a,k)*mesh->GetValue(a,k));
     738                                                        Q=atan(xp->GetValue(0,i)*yp->GetValue(0,j)/(mesh->GetValue(a,k)*R));
     739                                                        U->SetValue(k,id,s*(mesh->GetValue(a,k)*Q-xp->GetValue(0,i)*log(R+yp->GetValue(0,j))-yp->GetValue(0,j)*log(R+xp->GetValue(0,i))));
     740                                                }
     741                                                id++;
     742                                        }
     743                                }
     744                                for(int b=0;b<l-1;b++){
     745                                        U->ExtractLine(U1,b);
     746                                        U->ExtractLine(U2,b+1);
     747                                        gl->SetValue(0,b,rho->GetValue(0,b)*(U1->MatrixInternSum()*(-1)+U2->MatrixInternSum()));
     748                                }
     749                        }
     750                        g->SetValue(c,0,g->GetValue(c,0)+gg*gl->MatrixInternSum());
     751                }
     752        }
     753        delete xp;
     754        delete yp;
     755        delete xpp;
     756        delete ypp;
     757        delete gl;
     758        delete U;
     759        delete U1;
     760        delete U2;
     761
     762        return NULL;
     763}/*}}}*/
     764void LaunchThread(void* function(void*), void* usr,int num_threads){/*{{{*/
     765
     766        int i;
     767        int            *status  = NULL;
     768        pthread_t      *threads = NULL;
     769        pthread_handle *handles = NULL;
     770
     771        /*dynamically allocate: */
     772        threads=(pthread_t*)malloc(num_threads*sizeof(pthread_t));
     773        handles=(pthread_handle*)malloc(num_threads*sizeof(pthread_handle));
     774
     775        for(i=0;i<num_threads;i++){
     776                handles[i].usr=usr;
     777                handles[i].my_thread  =i;
     778                handles[i].num_threads=num_threads;
     779        }
     780        for(i=0;i<num_threads;i++){
     781                if(pthread_create(threads+i,NULL,function,(void*)(handles+i))){
     782                        std::cerr<<"pthread_create error";
     783                }
     784        }
     785        for(i=0;i<num_threads;i++){
     786                if(pthread_join(threads[i],(void**)&status)){
     787                        std::cerr<<"pthread_join error";
     788                }
     789        }
     790
     791        /*Free ressources:*/
     792        delete threads;
     793        delete handles;
     794}/*}}}*/
Note: See TracChangeset for help on using the changeset viewer.