LBFGS.c 10.9 KB
Newer Older
1
/*------------------------------------------------------------------------
2
 * Copyright (C) 2016 For the list of authors, see file AUTHORS.
Tilman Steinweg's avatar
Tilman Steinweg committed
3
 *
Florian Wittkamp's avatar
Florian Wittkamp committed
4
 * This file is part of IFOS.
5
 *
Florian Wittkamp's avatar
Florian Wittkamp committed
6
 * IFOS is free software: you can redistribute it and/or modify
Tilman Steinweg's avatar
Tilman Steinweg committed
7 8
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 2.0 of the License only.
9
 *
Florian Wittkamp's avatar
Florian Wittkamp committed
10
 * IFOS is distributed in the hope that it will be useful,
Tilman Steinweg's avatar
Tilman Steinweg committed
11 12 13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
14
 *
Tilman Steinweg's avatar
Tilman Steinweg committed
15
 * You should have received a copy of the GNU General Public License
16 17 18
 * along with 3D-AWAIT. See file COPYING and/or
 * <http://www.gnu.org/licenses/gpl-2.0.html>.
 --------------------------------------------------------------------------*/
Tilman Steinweg's avatar
Tilman Steinweg committed
19

20 21 22
/*-------------------------------------------------------------------------
 * Calculatipon of L-BFGS update
 --------------------------------------------------------------------------*/
Tilman Steinweg's avatar
Tilman Steinweg committed
23 24

#include "fd.h"
25 26 27 28 29 30 31 32 33 34 35
void lbfgs_reset(int iter, int N_LBFGS, int NPAR_LBFGS,float ** s_LBFGS1, float ** y_LBFGS1, float * rho_LBFGS1);
void lbfgs_core(int iteration, int N_LBFGS, int NPAR_LBFGS,float ** s_LBFGS, float ** y_LBFGS, float * rho_LBFGS,float *q_LBFGS,float *alpha_LBFGS,float *r_LBFGS);

void lbfgs(float **grad_vs, float **grad_rho, float **grad_vp,float Vs_avg,float rho_avg,float Vp_avg, float *rho_LBFGS, float **s_LBFGS, float **y_LBFGS,int N_LBFGS,int NPAR_LBFGS, int iteration, int *LBFGS_iter_start){
    
    /* global */
    extern int NX,NY,MYID;
    extern FILE *FP;
    extern int POS[3];
    extern char JACOBIAN[STRING_SIZE];
    extern int WAVETYPE;
36
    extern int ACOUSTIC;
37 38 39 40 41 42 43 44 45 46 47
    
    /* local */
    int m=0,v=0,w=0;
    int i,j,k,l;
    float beta_LBFGS=0.0;
    float dum1=0.0, dum2=0.0, buf1=0.0, buf2=0.0;
    float *q_LBFGS,*alpha_LBFGS,*r_LBFGS;
    float h0;
    char jac[225];
    FILE *FP_JAC;
    
48
    
49 50 51
    /*---------------------*/
    /*      DEBUGGING      */
    /*---------------------*/
52 53 54 55 56
    
    if(!ACOUSTIC) {
        sprintf(jac,"%s_grad1_vs_it%d",JACOBIAN,iteration);
        write_matrix_disk(grad_vs, jac);
    }
57 58 59 60
    
    sprintf(jac,"%s_grad1_rho_it%d",JACOBIAN,iteration);
    write_matrix_disk(grad_rho, jac);
    
61 62 63 64 65
    if(WAVETYPE==1||WAVETYPE==3) {
        sprintf(jac,"%s_grad1_vp_it%d",JACOBIAN,iteration);
        write_matrix_disk(grad_vp, jac);
    }
    
66 67 68 69 70 71 72 73 74 75 76
    /*-------------------------------------------------*/
    /*      Init L-BFGS at iter==LBFGS_iter_start      */
    /*-------------------------------------------------*/
    if(iteration==*LBFGS_iter_start) {
        w=iteration%N_LBFGS;
        if(w==0) w=N_LBFGS;
        
        l=0;
        for (i=1;i<=NX;i++){
            for (j=1;j<=NY;j++){
                l++;
77
                if(!ACOUSTIC) y_LBFGS[w][l]=-grad_vs[j][i]*Vs_avg; /* VS */
78 79 80
                if(NPAR_LBFGS>1) y_LBFGS[w][l+NX*NY]=-grad_rho[j][i]*rho_avg; /* RHO */
                if(NPAR_LBFGS>2) y_LBFGS[w][l+2*NX*NY]=-grad_vp[j][i]*Vp_avg; /* VP */
            }
Tilman Steinweg's avatar
Tilman Steinweg committed
81 82
        }
    }
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
    
    /*------------------------*/
    /*      Start L-BFGS      */
    /*------------------------*/
    if(iteration>*LBFGS_iter_start) {
        
        alpha_LBFGS = vector(1,N_LBFGS);
        q_LBFGS = vector(1,NPAR_LBFGS*NX*NY);
        r_LBFGS = vector(1,NPAR_LBFGS*NX*NY);
        
        m=iteration-N_LBFGS;
        if(m<1) m=1;
        
        w=(iteration-1)%N_LBFGS;
        if(w==0) w=N_LBFGS;
        
        /* Debugging */
100
        if(!ACOUSTIC) {
101
            sprintf(jac,"%s_y_LBFGS_vs_it%d.bin.%i.%i",JACOBIAN,iteration,POS[1],POS[2]);
102 103
            FP_JAC=fopen(jac,"wb");
        }
104 105 106 107 108 109 110 111 112 113 114 115
        
        if(MYID==0) printf("\n\n ------------ L-BFGS ---------------");
        if(MYID==0) printf("\n Start calculation L-BFGS update");
        if(MYID==0) printf("\n At Iteration %i in L-BFGS vector %i\n",iteration,w);
        
        l=0;
        for (i=1;i<=NX;i++){
            for (j=1;j<=NY;j++){
                
                l++;
                
                /* VS */
116 117 118 119
                if(!ACOUSTIC){
                    y_LBFGS[w][l]+=grad_vs[j][i]*Vs_avg; /* add grad(i) to build grad(i)-grad(i-1) */
                    q_LBFGS[l]=grad_vs[j][i]*Vs_avg; /* Normalisation */
                }
120 121 122 123 124 125 126 127 128 129 130 131 132 133
                
                /* RHO */
                if(NPAR_LBFGS>1) {
                    y_LBFGS[w][l+NY*NX]+=grad_rho[j][i]*rho_avg; /* add grad(i) to build grad(i)-grad(i-1) */
                    q_LBFGS[l+NY*NX]=grad_rho[j][i]*rho_avg; /* Normalisation */
                }
                
                /* VP */
                if(NPAR_LBFGS>2) {
                    y_LBFGS[w][l+2*NY*NX]+=grad_vp[j][i]*Vp_avg; /* add grad(i) to build grad(i)-grad(i-1) */
                    q_LBFGS[l+2*NY*NX]=grad_vp[j][i]*Vp_avg; /* Normalisation */
                }
                
                /* Debugging */
134
                if(!ACOUSTIC) fwrite(&y_LBFGS[w][l],sizeof(float),1,FP_JAC);
Tilman Steinweg's avatar
Tilman Steinweg committed
135 136 137
            }
        }
        
138 139
        /*---------------------*/
        /*      DEBUGGING      */
140 141 142 143
        /*---------------------*/
        if(!ACOUSTIC) {
            fclose(FP_JAC);
            MPI_Barrier(MPI_COMM_WORLD);
144
            sprintf(jac,"%s_y_LBFGS_vs_it%d.bin",JACOBIAN,iteration);
145 146
            if (MYID==0) mergemod(jac,3);
            MPI_Barrier(MPI_COMM_WORLD);
147
            sprintf(jac,"%s_y_LBFGS_vs_it%d.bin.%i.%i",JACOBIAN,iteration,POS[1],POS[2]);
148 149
            remove(jac);
        }
Tilman Steinweg's avatar
Tilman Steinweg committed
150
        
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
        /*----------------------------------*/
        /*      call L-BFGS Algorithm       */
        /*----------------------------------*/
        lbfgs_core(iteration,N_LBFGS, NX*NY*NPAR_LBFGS,s_LBFGS,y_LBFGS,rho_LBFGS,q_LBFGS,alpha_LBFGS,r_LBFGS);
        
        /*-------------------------------------------------------------*/
        /* Save model pertubation and save gradient for next iteration */
        /*-------------------------------------------------------------*/
        w=iteration%N_LBFGS;
        if(w==0) w=N_LBFGS;
        
        l=0;
        for (i=1;i<=NX;i++){
            for (j=1;j<=NY;j++){
                
                l++;
                
                /* VS */
169 170 171 172
                if(!ACOUSTIC) {
                    y_LBFGS[w][l]=-grad_vs[j][i]*Vs_avg; /* add -grad(i-1) to build grad(i)-grad(i-1) */
                    grad_vs[j][i]=r_LBFGS[l]*Vs_avg; /* Denormalization */
                }
173 174 175 176 177 178 179 180 181 182 183 184
                
                /* RHO */
                if(NPAR_LBFGS>1) {
                    y_LBFGS[w][l+NY*NX]=-grad_rho[j][i]*rho_avg; /* add -grad(i-1) to build grad(i)-grad(i-1) */
                    grad_rho[j][i]=r_LBFGS[l+NY*NX]*rho_avg; /* Denormalization */
                }
                
                /* VP */
                if(NPAR_LBFGS>2) {
                    y_LBFGS[w][l+2*NY*NX]=-grad_vp[j][i]*Vp_avg; /* add -grad(i-1) to build grad(i)-grad(i-1) */
                    grad_vp[j][i]=r_LBFGS[l+2*NY*NX]*Vp_avg; /* Denormalization */
                }
Tilman Steinweg's avatar
Tilman Steinweg committed
185 186
            }
        }
187 188 189 190 191 192 193 194 195 196
        
        free_vector(r_LBFGS,1,NPAR_LBFGS*NX*NY);
        free_vector(q_LBFGS,1,NPAR_LBFGS*NX*NY);
        free_vector(alpha_LBFGS, 1, N_LBFGS);
        
    }
    
    /*---------------------*/
    /*      DEBUGGING      */
    /*---------------------*/
197 198 199 200 201
    if(!ACOUSTIC){
        sprintf(jac,"%s_grad2_vs_it%d",JACOBIAN,iteration);
        write_matrix_disk(grad_vs, jac);
    }
    
202 203 204
    sprintf(jac,"%s_grad2_rho_it%d",JACOBIAN,iteration);
    write_matrix_disk(grad_rho, jac);
    
205 206 207 208
    if(WAVETYPE==1||WAVETYPE==3) {
        sprintf(jac,"%s_grad2_vp_it%d",JACOBIAN,iteration);
        write_matrix_disk(grad_vp, jac);
    }
209
}
Tilman Steinweg's avatar
Tilman Steinweg committed
210

211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
void lbfgs_core(int iteration, int N_LBFGS, int NPAR_LBFGS,float ** s_LBFGS, float ** y_LBFGS, float * rho_LBFGS,float *q_LBFGS,float *alpha_LBFGS,float *r_LBFGS) {
    
    extern FILE * FP;
    extern int VERBOSE;
    
    float beta_LBFGS=0.0;
    float dum1=0.0, dum2=0.0, buf1=0.0, buf2=0.0;
    float h0;
    int m=0,v=0,w=0,l=0;
    int VERBOSE_local=1;
    m=iteration-N_LBFGS;
    if(m<1) m=1;
    
    /*----------------------------------*/
    /* calculate H0 and rho_LBFGS       */
    /*----------------------------------*/
    w=(iteration-1)%N_LBFGS; if(w==0) w=N_LBFGS;
    dum1=0.0; dum2=0.0;
    
    for (l=1;l<=NPAR_LBFGS;l++){
        dum1+=y_LBFGS[w][l]*s_LBFGS[w][l];
        dum2+=y_LBFGS[w][l]*y_LBFGS[w][l];
    }
    
    buf1=0.0; buf2=0.0;
    MPI_Allreduce(&dum1,&buf1,1,MPI_FLOAT,MPI_SUM,MPI_COMM_WORLD);
    MPI_Allreduce(&dum2,&buf2,1,MPI_FLOAT,MPI_SUM,MPI_COMM_WORLD);
    
    rho_LBFGS[w]=1/buf1;
    
    h0=buf1/buf2;
    
    
    /* give output so stdout */
    if(VERBOSE || 1) {
        for(w=1;w<=N_LBFGS;w++) {
247
            fprintf(FP,"\n rho_LBFGS(%2d)=%e",w,rho_LBFGS[w]);
Tilman Steinweg's avatar
Tilman Steinweg committed
248
        }
249 250 251 252 253 254 255 256 257
        fprintf(FP,"\n h0=%e\n",h0);
    }
    
    /*----------------------------------*/
    /*       L-BFGS loop 1              */
    /*----------------------------------*/
		  
    l=0;
    for(v=iteration-1; v>=m;v--){
Tilman Steinweg's avatar
Tilman Steinweg committed
258
        
259 260
        w=v%N_LBFGS;
        if(w==0) w=N_LBFGS;
Tilman Steinweg's avatar
Tilman Steinweg committed
261
        
262 263 264 265
        alpha_LBFGS[w]=0.0;
        for (l=1;l<=NPAR_LBFGS;l++){
            alpha_LBFGS[w]+=rho_LBFGS[w]*s_LBFGS[w][l]*q_LBFGS[l];
            
Tilman Steinweg's avatar
Tilman Steinweg committed
266
        }
267 268 269 270 271 272 273 274 275
        
        buf1=0.0;
        dum2=alpha_LBFGS[w];
        MPI_Allreduce(&dum2,&buf1,1,MPI_FLOAT,MPI_SUM,MPI_COMM_WORLD);
        alpha_LBFGS[w]=buf1;
        
        for (l=1;l<=NPAR_LBFGS;l++){
            q_LBFGS[l]=q_LBFGS[l]-alpha_LBFGS[w]*y_LBFGS[w][l];
            
Tilman Steinweg's avatar
Tilman Steinweg committed
276
        }
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
    }
    
    /*----------------------------------*/
    /*       Apply H0^-1                */
    /*----------------------------------*/
    for (l=1;l<=NPAR_LBFGS;l++){
        r_LBFGS[l]=h0*q_LBFGS[l];
    }
    
    /*----------------------------------*/
    /*       L-BFGS loop 2              */
    /*----------------------------------*/
    for(v=m; v<=iteration-1;v++){
        
        w=v%N_LBFGS;
        if(w==0) w=N_LBFGS;
        
        beta_LBFGS=0.0;
        
        for (l=1;l<=NPAR_LBFGS;l++){
            beta_LBFGS+=rho_LBFGS[w]*y_LBFGS[w][l]*r_LBFGS[l];
Tilman Steinweg's avatar
Tilman Steinweg committed
298 299
        }
        
300 301 302 303
        buf1=0.0;
        buf2=beta_LBFGS;
        MPI_Allreduce(&buf2,&buf1,1,MPI_FLOAT,MPI_SUM,MPI_COMM_WORLD);
        beta_LBFGS=buf1;
Tilman Steinweg's avatar
Tilman Steinweg committed
304
        
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
        for (l=1;l<=NPAR_LBFGS;l++){
            r_LBFGS[l]=r_LBFGS[l]+s_LBFGS[w][l]*(alpha_LBFGS[w]-beta_LBFGS);
        }
    }
    
}

void lbfgs_reset(int iter, int N_LBFGS, int NPAR_LBFGS,float ** s_LBFGS1, float ** y_LBFGS1, float * rho_LBFGS1) {
    
    /* local variables */
    int l,m;
    
    /* global variables */
    extern int NX,NY,MYID;
    
    if(MYID==0) printf("\n\n ------------ L-BFGS ---------------");
321
    if(MYID==0&&iter>1) printf("\n Reset L-BFGS at iteration %d",iter);
322
    if(MYID==0&&iter==1) printf("\n L-BFGS will be used from iteration %d on",iter+1);
323 324 325 326 327 328 329 330
    
    for(l=1;l<=N_LBFGS;l++){
        for(m=1;m<=NPAR_LBFGS*NX*NY;m++){
            s_LBFGS1[l][m]=0.0;
            y_LBFGS1[l][m]=0.0;
        }
        rho_LBFGS1[l]=0.0;
    }
Tilman Steinweg's avatar
Tilman Steinweg committed
331
}