LBFGS.c 11.6 KB
Newer Older
1
/*------------------------------------------------------------------------
Florian Wittkamp's avatar
Florian Wittkamp committed
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
    extern float LBFGS_SCALE_GRADIENTS;
38 39
    
    /* local */
40
    int w=0;
Florian Wittkamp's avatar
Florian Wittkamp committed
41
    int i,j,l;
42 43
    float *q_LBFGS,*alpha_LBFGS,*r_LBFGS;
    char jac[225];
Florian Wittkamp's avatar
Florian Wittkamp committed
44
    FILE *FP_JAC = NULL;
45
    
Florian Wittkamp's avatar
Florian Wittkamp committed
46
    
47 48 49
    /*---------------------*/
    /*      DEBUGGING      */
    /*---------------------*/
Florian Wittkamp's avatar
Florian Wittkamp committed
50 51 52 53 54
    
    if(!ACOUSTIC) {
        sprintf(jac,"%s_grad1_vs_it%d",JACOBIAN,iteration);
        write_matrix_disk(grad_vs, jac);
    }
55 56 57 58
    
    sprintf(jac,"%s_grad1_rho_it%d",JACOBIAN,iteration);
    write_matrix_disk(grad_rho, jac);
    
59 60 61 62 63
    if(WAVETYPE==1||WAVETYPE==3) {
        sprintf(jac,"%s_grad1_vp_it%d",JACOBIAN,iteration);
        write_matrix_disk(grad_vp, jac);
    }
    
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
    /*---------------------*/
    /*      Experimental   */
    /*---------------------*/
    /* Scale the gradients with a constant factor */
    /* This is to avoid numerical instabilities if the gradients are to small (absolute value) */
    /* Do not use this feature, unless you have to */
    if(LBFGS_SCALE_GRADIENTS!=1) {
        if(MYID==0) printf("\n\n Scaling the gradients to ensure L-BFGS stability");
        if(MYID==0) printf("\n Scaling with %f",LBFGS_SCALE_GRADIENTS);
        if(MYID==0) printf("\n This is an experimental feature.");

        for (i=1;i<=NX;i++){
            for (j=1;j<=NY;j++){
                if(!ACOUSTIC) grad_vs[j][i]=grad_vs[j][i]*LBFGS_SCALE_GRADIENTS;
                if(NPAR_LBFGS>1) grad_rho[j][i]=grad_rho[j][i]*LBFGS_SCALE_GRADIENTS;
                if(NPAR_LBFGS>2) grad_vp[j][i]=grad_vp[j][i]*LBFGS_SCALE_GRADIENTS;
            }
        }
    }
    
84 85 86 87 88 89 90 91 92 93 94
    /*-------------------------------------------------*/
    /*      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++;
Florian Wittkamp's avatar
Florian Wittkamp committed
95
                if(!ACOUSTIC) y_LBFGS[w][l]=-grad_vs[j][i]*Vs_avg; /* VS */
96 97 98
                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
99 100
        }
    }
101 102 103 104 105 106 107 108 109 110 111 112 113 114
    
    /*------------------------*/
    /*      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);
        
        w=(iteration-1)%N_LBFGS;
        if(w==0) w=N_LBFGS;
        
        /* Debugging */
Florian Wittkamp's avatar
Florian Wittkamp committed
115
        if(!ACOUSTIC) {
116
            sprintf(jac,"%s_y_LBFGS_vs_it%d.bin.%i.%i",JACOBIAN,iteration,POS[1],POS[2]);
Florian Wittkamp's avatar
Florian Wittkamp committed
117 118
            FP_JAC=fopen(jac,"wb");
        }
119 120 121 122 123 124 125 126 127 128 129 130
        
        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 */
Florian Wittkamp's avatar
Florian Wittkamp committed
131 132 133 134
                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 */
                }
135 136 137 138 139 140 141 142 143 144 145 146 147 148
                
                /* 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 */
Florian Wittkamp's avatar
Florian Wittkamp committed
149
                if(!ACOUSTIC) fwrite(&y_LBFGS[w][l],sizeof(float),1,FP_JAC);
Tilman Steinweg's avatar
Tilman Steinweg committed
150 151 152
            }
        }
        
153 154
        /*---------------------*/
        /*      DEBUGGING      */
Florian Wittkamp's avatar
Florian Wittkamp committed
155 156 157 158
        /*---------------------*/
        if(!ACOUSTIC) {
            fclose(FP_JAC);
            MPI_Barrier(MPI_COMM_WORLD);
159
            sprintf(jac,"%s_y_LBFGS_vs_it%d.bin",JACOBIAN,iteration);
Florian Wittkamp's avatar
Florian Wittkamp committed
160 161
            if (MYID==0) mergemod(jac,3);
            MPI_Barrier(MPI_COMM_WORLD);
162
            sprintf(jac,"%s_y_LBFGS_vs_it%d.bin.%i.%i",JACOBIAN,iteration,POS[1],POS[2]);
Florian Wittkamp's avatar
Florian Wittkamp committed
163 164
            remove(jac);
        }
Tilman Steinweg's avatar
Tilman Steinweg committed
165
        
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
        /*----------------------------------*/
        /*      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 */
Florian Wittkamp's avatar
Florian Wittkamp committed
184 185 186 187
                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 */
                }
188 189 190 191 192 193 194 195 196 197 198 199
                
                /* 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
200 201
            }
        }
202 203 204 205 206 207 208 209 210 211
        
        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      */
    /*---------------------*/
Florian Wittkamp's avatar
Florian Wittkamp committed
212 213 214 215 216
    if(!ACOUSTIC){
        sprintf(jac,"%s_grad2_vs_it%d",JACOBIAN,iteration);
        write_matrix_disk(grad_vs, jac);
    }
    
217 218 219
    sprintf(jac,"%s_grad2_rho_it%d",JACOBIAN,iteration);
    write_matrix_disk(grad_rho, jac);
    
220 221 222 223
    if(WAVETYPE==1||WAVETYPE==3) {
        sprintf(jac,"%s_grad2_vp_it%d",JACOBIAN,iteration);
        write_matrix_disk(grad_vp, jac);
    }
224
}
Tilman Steinweg's avatar
Tilman Steinweg committed
225

226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
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;
    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++) {
Florian Wittkamp's avatar
Florian Wittkamp committed
261
            fprintf(FP,"\n rho_LBFGS(%2d)=%e",w,rho_LBFGS[w]);
Tilman Steinweg's avatar
Tilman Steinweg committed
262
        }
263 264 265 266 267 268 269 270
        fprintf(FP,"\n h0=%e\n",h0);
    }
    
    /*----------------------------------*/
    /*       L-BFGS loop 1              */
    /*----------------------------------*/
		  
    for(v=iteration-1; v>=m;v--){
Tilman Steinweg's avatar
Tilman Steinweg committed
271
        
272 273
        w=v%N_LBFGS;
        if(w==0) w=N_LBFGS;
Tilman Steinweg's avatar
Tilman Steinweg committed
274
        
275 276 277 278
        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
279
        }
280 281 282 283 284 285 286 287 288
        
        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
289
        }
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
    }
    
    /*----------------------------------*/
    /*       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
311 312
        }
        
313 314 315 316
        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
317
        
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
        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 ---------------");
Florian Wittkamp's avatar
Florian Wittkamp committed
334
    if(MYID==0&&iter>1) printf("\n Reset L-BFGS at iteration %d",iter);
Florian Wittkamp's avatar
Florian Wittkamp committed
335
    if(MYID==0&&iter==1) printf("\n L-BFGS will be used from iteration %d on",iter+1);
336 337 338 339 340 341 342 343
    
    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
344
}