LBFGS.c 10.5 KB
Newer Older
1 2
/*------------------------------------------------------------------------
 * Copyright (C) 2015 For the list of authors, see file AUTHORS.
Tilman Steinweg's avatar
Tilman Steinweg committed
3 4
 *
 * This file is part of DENISE.
5
 *
Tilman Steinweg's avatar
Tilman Steinweg committed
6 7 8
 * DENISE is free software: you can redistribute it and/or modify
 * 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
 *
Tilman Steinweg's avatar
Tilman Steinweg committed
10 11 12 13
 * DENISE is distributed in the hope that it will be useful,
 * 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 23
/*-------------------------------------------------------------------------
 * Calculatipon of L-BFGS update
 * F. Wittkamp 2015
 --------------------------------------------------------------------------*/
Tilman Steinweg's avatar
Tilman Steinweg committed
24 25

#include "fd.h"
26 27 28 29 30 31 32 33 34 35 36
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;
37
    extern int ACOUSTIC;
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
    
    /* 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;
    

    /*---------------------*/
    /*      DEBUGGING      */
    /*---------------------*/
53
    sprintf(jac,"%s_grad1_vs_it%d",JACOBIAN,iteration);
54 55 56 57 58
    write_matrix_disk(grad_vs, jac);
    
    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
    /*-------------------------------------------------*/
    /*      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++;
                y_LBFGS[w][l]=-grad_vs[j][i]*Vs_avg; /* VS */
                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
79 80
        }
    }
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
    
    /*------------------------*/
    /*      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 */
98
        sprintf(jac,"%s_y_LBFGS_vs_it%d_w%d.bin.%i.%i",JACOBIAN,iteration,w,POS[1],POS[2]);
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
        FP_JAC=fopen(jac,"wb");
        
        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 */
                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 */
                
                /* 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 */
                fwrite(&y_LBFGS[w][l],sizeof(float),1,FP_JAC);
Tilman Steinweg's avatar
Tilman Steinweg committed
129 130 131
            }
        }
        
132 133 134 135 136
        /*---------------------*/
        /*      DEBUGGING      */
        /*--------------------*/
        fclose(FP_JAC);
        MPI_Barrier(MPI_COMM_WORLD);
137
        sprintf(jac,"%s_y_LBFGS_vs_it%d_w%d.bin",JACOBIAN,iteration,w);
138
        if (MYID==0) mergemod(jac,3);
Tilman Steinweg's avatar
Tilman Steinweg committed
139
        MPI_Barrier(MPI_COMM_WORLD);
140
        sprintf(jac,"%s_y_LBFGS_vs_it%d_w%d.bin.%i.%i",JACOBIAN,iteration,w,POS[1],POS[2]);
141
        remove(jac);
Tilman Steinweg's avatar
Tilman Steinweg committed
142
        
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
        /*----------------------------------*/
        /*      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 */
                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 */
                
                /* 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
175 176
            }
        }
177 178 179 180 181 182 183 184 185 186
        
        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      */
    /*---------------------*/
187
    sprintf(jac,"%s_grad2_vs_it%d",JACOBIAN,iteration);
188 189 190 191 192
    write_matrix_disk(grad_vs, jac);
 
    sprintf(jac,"%s_grad2_rho_it%d",JACOBIAN,iteration);
    write_matrix_disk(grad_rho, jac);
    
193 194 195 196
    if(WAVETYPE==1||WAVETYPE==3) {
        sprintf(jac,"%s_grad2_vp_it%d",JACOBIAN,iteration);
        write_matrix_disk(grad_vp, jac);
    }
197
}
Tilman Steinweg's avatar
Tilman Steinweg committed
198

199 200 201 202 203 204 205 206 207 208 209 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
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++) {
            fprintf(FP,"\n rho_LBFGS(%d)=%e\n",w,rho_LBFGS[w]);
Tilman Steinweg's avatar
Tilman Steinweg committed
236
        }
237 238 239 240 241 242 243 244 245
        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
246
        
247 248
        w=v%N_LBFGS;
        if(w==0) w=N_LBFGS;
Tilman Steinweg's avatar
Tilman Steinweg committed
249
        
250 251 252 253
        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
254
        }
255 256 257 258 259 260 261 262 263
        
        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
264
        }
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
    }
    
    /*----------------------------------*/
    /*       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
286 287
        }
        
288 289 290 291
        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
292
        
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
        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 ---------------");
    if(MYID==0) printf("\n Reset L-BFGS at Iteration %d",iter);
    
    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
318
}