From be28a23d6126d2fc286caad7c3bdca44bf1aaea2 Mon Sep 17 00:00:00 2001 From: Sancar Adali Date: Tue, 5 Sep 2017 13:07:58 -0400 Subject: [PATCH 1/3] set of commits for valgrind fixes. test may be broken --- src/graphm/algorithm.cpp | 27 ++++++++++++- src/graphm/algorithm.h | 4 +- src/graphm/algorithm_path.cpp | 6 ++- src/graphm/algorithm_qcv.cpp | 73 ++++++++++++++++++----------------- 4 files changed, 68 insertions(+), 42 deletions(-) diff --git a/src/graphm/algorithm.cpp b/src/graphm/algorithm.cpp index 57d3fdc..0cbcaa1 100644 --- a/src/graphm/algorithm.cpp +++ b/src/graphm/algorithm.cpp @@ -22,12 +22,35 @@ algorithm::algorithm(std::string fconfig) : rpc(fconfig) { -gm_ldh=NULL;dalpha_ldh=0;bnosymm=false; + gm_ldh=NULL; + dalpha_ldh=0; + bnosymm=false; + df_norm=0; + N=0; + cdesc_matrix='A'; + cscore_matrix='A'; + bverbose=false; + sverbfile=std::string(""); + + N=0; + df_norm=0.0; + } algorithm::algorithm() : rpc() { -gm_ldh=NULL;dalpha_ldh=0;bnosymm=false;df_norm=0;N=0;cdesc_matrix='A';cscore_matrix='A'; + gm_ldh=NULL; + dalpha_ldh=0; + bnosymm=false; + df_norm=0; + N=0; + cdesc_matrix='A'; + cscore_matrix='A'; + bverbose=false; + sverbfile=std::string(""); + + N=0; + df_norm=0.0; } //common framework for graph matching algorithm diff --git a/src/graphm/algorithm.h b/src/graphm/algorithm.h index 933e7a6..34a1834 100644 --- a/src/graphm/algorithm.h +++ b/src/graphm/algorithm.h @@ -47,7 +47,7 @@ class match_result match_result() { gm_P=NULL; salgo = ""; - gm_P_exact=NULL; + gm_P_exact=NULL; inum_iteration=-1; dres = 0.0; dtime = 0.0; @@ -75,7 +75,7 @@ class algorithm : public rpc { public: algorithm(std::string ); - algorithm(); + algorithm() ; match_result gmatch(graph& g, graph& h,gsl_matrix* gm_P_i=NULL, gsl_matrix* gm_ldh=NULL,double dalpha_ldh=-1);//common stuff, virtual match_result match(graph& g, graph& h, gsl_matrix* gm_P_i=NULL, gsl_matrix* gm_ldh=NULL,double dalpha_ldh=-1)=0;//particular method implementation double graph_dist(graph &g,graph &h,gsl_matrix* gm_P,char cscore_matrix); diff --git a/src/graphm/algorithm_path.cpp b/src/graphm/algorithm_path.cpp index c192e8a..dd251d0 100644 --- a/src/graphm/algorithm_path.cpp +++ b/src/graphm/algorithm_path.cpp @@ -21,8 +21,10 @@ match_result algorithm_path::match(graph& g, graph& h,gsl_matrix* gm_P_i, gsl_matrix* _gm_ldh, double dalpha_ldh) { - bool bblast_match=(get_param_i("blast_match")==1); - bool bblast_match_end=(get_param_i("blast_match_proj")==1); + bool bblast_match=false; + bblast_match=(get_param_i("blast_match")==1); + bool bblast_match_end=false; + bblast_match_end=(get_param_i("blast_match_proj")==1); bool bbest_path_proj=(get_param_i("best_path_proj_sol")==1); bool bbest_path_blast_proj=(get_param_i("best_path_blast_proj_sol")==1); bool bbest_path_greedy=(get_param_i("best_path_greedy_sol")==1); diff --git a/src/graphm/algorithm_qcv.cpp b/src/graphm/algorithm_qcv.cpp index e6bdfde..627b209 100644 --- a/src/graphm/algorithm_qcv.cpp +++ b/src/graphm/algorithm_qcv.cpp @@ -23,7 +23,8 @@ match_result algorithm_qcv::match(graph& g, graph& h,gsl_matrix* gm_P_i,gsl_matr { if (bverbose) *gout<<"QCV matching"<data,N*N); gsl_matrix* gm_dP_2=gsl_matrix_alloc(N,N); //gsl_vector_view gvv_dP_2=gsl_vector_view_array(gm_dP_2->data,N*N); gsl_matrix_set_zero(gm_dP_2); - + if (gm_P_i==NULL) gsl_matrix_set_all(gm_P,1.0/N); else @@ -79,7 +80,7 @@ match_result algorithm_qcv::match(graph& g, graph& h,gsl_matrix* gm_P_i,gsl_matr gsl_matrix_memcpy(gm_P_prev,gm_P); //perm matrix transformation into vector gvv_P=gsl_vector_view_array(gm_P->data,N*N); - + //and in opposite direction for gradient gmv_C=gsl_matrix_view_vector(gv_C,N,N); C=&gmv_C.matrix; @@ -89,7 +90,7 @@ match_result algorithm_qcv::match(graph& g, graph& h,gsl_matrix* gm_P_i,gsl_matr gsl_matrix * gm_ddP_prev=gsl_matrix_alloc(N,N); gsl_vector_view gvv_ldh; if (dalpha_ldh>0) - { + { gvv_ldh=gsl_vector_view_array(gm_ldh->data,N*N); if (pdebug.ivalue) gsl_matrix_printout(gm_ldh,"gm_ldh",pdebug.strvalue); }; @@ -107,10 +108,10 @@ match_result algorithm_qcv::match(graph& g, graph& h,gsl_matrix* gm_P_i,gsl_matr dt1=clock(); //double r=(dt1-dt0)/CLOCKS_PER_SEC; if (pdebug.ivalue) gsl_matrix_printout(gv_C,"gv_C",pdebug.strvalue); - + //result save gsl_matrix_transpose(C); - if (pdebug.ivalue) gsl_matrix_printout(C,"C=gradient",pdebug.strvalue); + if (pdebug.ivalue) gsl_matrix_printout(C,"C=gradient",pdebug.strvalue); gsl_matrix_scale(C,2); update_C_hungarian(C); double dscale_factor =gsl_matrix_max_abs(C); @@ -118,8 +119,8 @@ match_result algorithm_qcv::match(graph& g, graph& h,gsl_matrix* gm_P_i,gsl_matr dscale_factor=dhung_max/dscale_factor; gsl_matrix_scale(C,dscale_factor); //gsl_matrix_transpose(C); - if (pdebug.ivalue) gsl_matrix_printout(C,"scale(C)",pdebug.strvalue); - + if (pdebug.ivalue) gsl_matrix_printout(C,"scale(C)",pdebug.strvalue); + //hungarian, before the true C matrix must be transposed gsl_matrix_transpose(C); dt1=clock(); @@ -130,15 +131,15 @@ match_result algorithm_qcv::match(graph& g, graph& h,gsl_matrix* gm_P_i,gsl_matr update_C_hungarian(C,1,true);//return to the original value gsl_matrix_scale(C,0.5); - if (pdebug.ivalue) gsl_matrix_printout(gm_P,"gm_P",pdebug.strvalue); - if (pdebug.ivalue) gsl_matrix_printout(gm_P_prev,"gm_P_prev",pdebug.strvalue); - + if (pdebug.ivalue) gsl_matrix_printout(gm_P,"gm_P",pdebug.strvalue); + if (pdebug.ivalue) gsl_matrix_printout(gm_P_prev,"gm_P_prev",pdebug.strvalue); + //line search gsl_matrix_memcpy(gm_dP,gm_P); gsl_matrix_sub(gm_dP,gm_P_prev); - - if (pdebug.ivalue) gsl_matrix_printout(gm_dP,"gm_dP",pdebug.strvalue); - + + if (pdebug.ivalue) gsl_matrix_printout(gm_dP,"gm_dP",pdebug.strvalue); + double a,b1,b2,bldh; gsl_matrix_transpose(gm_dP); gsl_matrix_transpose(gm_P_prev); @@ -164,7 +165,7 @@ match_result algorithm_qcv::match(graph& g, graph& h,gsl_matrix* gm_P_i,gsl_matr gsl_matrix_transpose(gm_P_prev); gsl_matrix_transpose(C); if ((alpha<1) and (alpha>0)) - { + { gsl_matrix_scale(gm_dP,(1-alpha)); gsl_matrix_sub(gm_P,gm_dP); }; @@ -176,16 +177,16 @@ match_result algorithm_qcv::match(graph& g, graph& h,gsl_matrix* gm_P_i,gsl_matr else { dfvalue=f_qcv(gm_Ag_d,gm_Ah_d,gm_P,gm_temp,true); - if (dfvalue>dfvalue_prev) + if (dfvalue>dfvalue_prev) {gsl_matrix_memcpy(gm_P,gm_P_prev); dfvalue=dfvalue_prev;}; }; - if (pdebug.ivalue) gsl_matrix_printout(gm_P,"gm_P_step_finish",pdebug.strvalue); //sparsity + if (pdebug.ivalue) gsl_matrix_printout(gm_P,"gm_P_step_finish",pdebug.strvalue); //sparsity long lsparse=0; for (long j=0;jdata[j]<1e-7); if (bverbose) *gout<<"#zeros="< Date: Tue, 5 Sep 2017 13:09:56 -0400 Subject: [PATCH 2/3] make sure N of graph is set to graph's size --- src/graphm/algorithm.cpp | 9 +-------- src/graphm/algorithm_ca.cpp | 1 + src/graphm/algorithm_ext.cpp | 6 +++--- src/graphm/graph.cpp | 7 +++++++ 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/graphm/algorithm.cpp b/src/graphm/algorithm.cpp index 0cbcaa1..79cc186 100644 --- a/src/graphm/algorithm.cpp +++ b/src/graphm/algorithm.cpp @@ -27,14 +27,9 @@ algorithm::algorithm(std::string fconfig) bnosymm=false; df_norm=0; N=0; - cdesc_matrix='A'; - cscore_matrix='A'; bverbose=false; sverbfile=std::string(""); - N=0; - df_norm=0.0; - } algorithm::algorithm() : rpc() @@ -49,8 +44,6 @@ algorithm::algorithm() bverbose=false; sverbfile=std::string(""); - N=0; - df_norm=0.0; } //common framework for graph matching algorithm @@ -125,7 +118,7 @@ double algorithm::graph_dist(graph &g,graph &h,gsl_matrix* gm_P,char cscore_matr bool print_debug = false; if ((pdebug.ivalue != -1) && (pdebug.ivalue)) print_debug = true; - long N=g.getN(); + N=g.getN(); gsl_matrix* gm_Ag=g.get_descmatrix(cscore_matrix); gsl_matrix* gm_At=gsl_matrix_alloc(N,N); gsl_matrix* gm_Ah=gsl_matrix_alloc(N,N); diff --git a/src/graphm/algorithm_ca.cpp b/src/graphm/algorithm_ca.cpp index 9e485c0..abe2ba6 100644 --- a/src/graphm/algorithm_ca.cpp +++ b/src/graphm/algorithm_ca.cpp @@ -22,6 +22,7 @@ match_result algorithm_ca::match(graph &g, graph &h, gsl_matrix *gm_P_i, gsl_matrix *_gm_ldh, double dalpha_ldh) { double dhung_max = get_param_d("hungarian_max"); + N=g.getN(); //bool bblast_match_end = (get_param_i("blast_match_proj") == 1); bool bblast_match = (get_param_i("blast_match") == 1); //double dfw_xeps = get_param_d("algo_fw_xeps"); diff --git a/src/graphm/algorithm_ext.cpp b/src/graphm/algorithm_ext.cpp index fd041f4..2e77967 100644 --- a/src/graphm/algorithm_ext.cpp +++ b/src/graphm/algorithm_ext.cpp @@ -21,13 +21,13 @@ match_result algorithm_NEW::match(graph& g, graph& h,gsl_matrix* gm_P_i,gsl_matrix* gm_ldh,double dalpha_ldh) { -if (bverbose) *gout<<"The best matching algorithm"<size1; set_adjmatrix(_gm_A); + } graph::graph(std::string fconfig) @@ -46,14 +48,18 @@ graph::graph(graph &gr) : rpc() gm_A = NULL; N = 0; const gsl_matrix *gm_t = gr.get_adjmatrix(); + N=gm_t->size1; set_adjmatrix(gm_t); + } graph &graph::operator=(graph &gh) { if (&gh != this) { + N=gh.get_adjmatrix()->size1; set_adjmatrix(gh.get_adjmatrix()); + } return (*this); } @@ -87,6 +93,7 @@ int graph::load_graph(std::string fgraph_name, char ftype, char cformat, std::st fclose(f); gsl_set_error_handler(NULL); set_adjmatrix(gm_A_l); + this->N = gm_A_l->size1; gsl_matrix_free(gm_A_l); }; if (ierror != 0) From 8b91dc8d044cfaffab2b9df4e1ea11ed8a56f7d3 Mon Sep 17 00:00:00 2001 From: Sancar Adali Date: Tue, 5 Sep 2017 13:18:33 -0400 Subject: [PATCH 3/3] make sure specific algo implementations get N from first graph --- src/graphm/algorithm_fsol.cpp | 7 ++-- src/graphm/algorithm_iden.cpp | 3 +- src/graphm/algorithm_lp.cpp | 17 +++++----- src/graphm/algorithm_path.cpp | 1 + src/graphm/algorithm_qcv.cpp | 1 + src/graphm/algorithm_rand.cpp | 3 +- src/graphm/algorithm_rank.cpp | 55 ++++++++++++++++---------------- src/graphm/algorithm_sch.cpp | 13 ++++---- src/graphm/algorithm_umeyama.cpp | 25 ++++++++------- src/graphm/algorithm_unif.cpp | 1 + 10 files changed, 68 insertions(+), 58 deletions(-) diff --git a/src/graphm/algorithm_fsol.cpp b/src/graphm/algorithm_fsol.cpp index 0ce6d66..c941733 100644 --- a/src/graphm/algorithm_fsol.cpp +++ b/src/graphm/algorithm_fsol.cpp @@ -22,18 +22,19 @@ match_result algorithm_fsol::match(graph& g, graph& h,gsl_matrix* gm_P_i, gsl_matrix* gm_ldh,double dalpha_ldh) { if (bverbose) - *gout<<"FSol matching"<0) - { + { for (int i=0;idata[i]); for (int i=0;i<2*N*N;i++) @@ -115,7 +116,7 @@ match_result algorithm_lp::match(graph& g, graph& h,gsl_matrix* gm_P_i, gsl_matr for (int j=0;jdata,N*N); gsl_vector_view gvv_R=gsl_vector_view_array(C->data,N*N); - - + + gsl_vector* gv_deg_g=gsl_vector_alloc(N); gsl_vector* gv_deg_h=gsl_vector_alloc(N); gsl_matrix_sum(gm_Ag_d,1, gv_deg_g); @@ -53,38 +54,38 @@ match_result algorithm_rank::match(graph& g, graph& h,gsl_matrix* gm_P_i, gsl_ma if (gv_deg_g->data[j]>0) gm_Ag_d->data[i*N+j]/=gv_deg_g->data[j]; if (gv_deg_h->data[j]>0) gm_Ah_d->data[i*N+j]/=gv_deg_h->data[j]; }; - if (pdebug.ivalue) gsl_matrix_printout(gm_Ag_d,"gm_Ag_d",pdebug.strvalue); - if (pdebug.ivalue) gsl_matrix_printout(gm_Ah_d,"gm_Ah_d",pdebug.strvalue); + if (pdebug.ivalue) gsl_matrix_printout(gm_Ag_d,"gm_Ag_d",pdebug.strvalue); + if (pdebug.ivalue) gsl_matrix_printout(gm_Ah_d,"gm_Ah_d",pdebug.strvalue); gsl_vector_free(gv_deg_g); gsl_vector_free(gv_deg_h); double ddiff;bool bcontinue=true;double dm_old; - if (dalpha_ldh>0) { + if (dalpha_ldh>0) { gsl_matrix_scale(gm_ldh,dalpha_ldh); gsl_matrix_memcpy(C,gm_ldh); } else gsl_matrix_set_all(C,1.0/N); - - if (pdebug.ivalue) gsl_matrix_printout(C,"C",pdebug.strvalue); + + if (pdebug.ivalue) gsl_matrix_printout(C,"C",pdebug.strvalue); bool biter_algo=true; if (biter_algo) { while (bcontinue) - { + { gsl_matrix_memcpy(C_old,C); gsl_blas_dgemm(CblasNoTrans,CblasNoTrans, 1, gm_Ag_d,C,0,gm_temp); - if (pdebug.ivalue) gsl_matrix_printout(gm_temp,"gm_temp",pdebug.strvalue); + if (pdebug.ivalue) gsl_matrix_printout(gm_temp,"gm_temp",pdebug.strvalue); //gsl_blas_dgemm(CblasNoTrans,CblasTrans, 1-dalpha_ldh, gm_temp,gm_Ah_d,0,C); gsl_matrix_transpose(gm_temp); gsl_blas_dgemm(CblasNoTrans,CblasNoTrans, 1-dalpha_ldh,gm_Ah_d, gm_temp,0,C); gsl_matrix_transpose(C); - if (pdebug.ivalue) gsl_matrix_printout(C,"C",pdebug.strvalue); + if (pdebug.ivalue) gsl_matrix_printout(C,"C",pdebug.strvalue); if (dalpha_ldh>0) gsl_matrix_add(C,gm_ldh); //normalization dm_old=gsl_blas_dnrm2(&gvv_R.vector); gsl_matrix_scale(C,1.0/dm_old); gsl_matrix_sub(C_old,C); - if (pdebug.ivalue) gsl_matrix_printout(C_old,"C_old",pdebug.strvalue); + if (pdebug.ivalue) gsl_matrix_printout(C_old,"C_old",pdebug.strvalue); ddiff=gsl_blas_dnrm2(&gvv_C_old.vector); bcontinue=!(ddiff<1e-2); bcontinue=(bcontinue && (num_it++data,N,N); @@ -118,9 +119,9 @@ match_result algorithm_rank::match(graph& g, graph& h,gsl_matrix* gm_P_i, gsl_ma gsl_matrix_scale(C_old,-1); gsl_matrix_add_constant(C_old,1); - if (pdebug.ivalue) gsl_matrix_printout(&gmv_Lg.matrix,"gmv_Lg.matrix",pdebug.strvalue); - if (pdebug.ivalue) gsl_matrix_printout(&gmv_Lh.matrix,"gmv_Lh.matrix",pdebug.strvalue); - if (pdebug.ivalue) gsl_matrix_printout(C_old,"C_old",pdebug.strvalue); + if (pdebug.ivalue) gsl_matrix_printout(&gmv_Lg.matrix,"gmv_Lg.matrix",pdebug.strvalue); + if (pdebug.ivalue) gsl_matrix_printout(&gmv_Lh.matrix,"gmv_Lh.matrix",pdebug.strvalue); + if (pdebug.ivalue) gsl_matrix_printout(C_old,"C_old",pdebug.strvalue); gsl_matrix_memcpy(C,gm_ldh); gsl_matrix_transpose(C); //multiplcation chain @@ -128,42 +129,42 @@ match_result algorithm_rank::match(graph& g, graph& h,gsl_matrix* gm_P_i, gsl_ma gsl_matrix_transpose(gm_Ag_d); gsl_blas_dgemm(CblasNoTrans,CblasNoTrans, 1, gm_Ag_d,gm_temp,0,C); gsl_matrix_transpose(gm_Ag_d); - + gsl_matrix_div_elements(C,C_old); - + //gsl_matrix_transpose(gm_Ah_d); //gsl_blas_dgemm(CblasNoTrans,CblasNoTrans, 1, C,gm_Ah_d,0,gm_temp); gsl_matrix_transpose(C); gsl_blas_dgemm(CblasNoTrans,CblasNoTrans, 1, gm_Ah_d,C,0,gm_temp); gsl_matrix_transpose(gm_temp); gsl_blas_dgemm(CblasNoTrans,CblasNoTrans, 1, gm_Ag_d,gm_temp,0,C); - + gsl_matrix_free(gm_Ag_d); - gsl_matrix_free(gm_Ah_d); + gsl_matrix_free(gm_Ah_d); gsl_vector_free(eval_g); gsl_vector_free(eval_h); gsl_matrix_free(evec_g); gsl_matrix_free(evec_h); - gsl_matrix_free(C); + gsl_matrix_free(C); }; if (dalpha_ldh>0) gsl_matrix_scale(gm_ldh,1/dalpha_ldh); - if (pdebug.ivalue) gsl_matrix_printout(C,"C=rank matrix",pdebug.strvalue); + if (pdebug.ivalue) gsl_matrix_printout(C,"C=rank matrix",pdebug.strvalue); gsl_matrix_free(gm_Ag_d); gsl_matrix_free(gm_Ah_d); gsl_matrix_free(C_old); gsl_matrix_free(gm_temp); - + //scaling for hungarian double dscale_factor =gsl_matrix_max_abs(C); dscale_factor=(dscale_factor>EPSILON)?dscale_factor:EPSILON; dscale_factor=10000/dscale_factor; gsl_matrix_scale(C,-dscale_factor); gsl_matrix_transpose(C); - if (pdebug.ivalue) gsl_matrix_printout(C,"scale(C)",pdebug.strvalue); + if (pdebug.ivalue) gsl_matrix_printout(C,"scale(C)",pdebug.strvalue); gsl_matrix* gm_P=gsl_matrix_alloc(N,N); gsl_matrix_hungarian(C,gm_P,NULL,NULL,false,(bblast_match_end?gm_ldh:NULL),false); if (pdebug.ivalue) gsl_matrix_printout(gm_P,"gm_P",pdebug.strvalue); - + match_result mres; mres.gm_P=gm_P; gsl_matrix_free(C); diff --git a/src/graphm/algorithm_sch.cpp b/src/graphm/algorithm_sch.cpp index aae7f33..72a3271 100644 --- a/src/graphm/algorithm_sch.cpp +++ b/src/graphm/algorithm_sch.cpp @@ -25,11 +25,12 @@ match_result algorithm_sch::match(graph& g, graph& h,gsl_matrix* gm_P_i,gsl_matr bool bgreedy=(get_param_i("hungarian_greedy")==1); match_result mres=algorithm_qcv::match(g,h,gm_P_i,gm_ldh,dalpha_ldh); if (bverbose) - *gout<<"SCH algorithm"<data[i+N*j]+=pow(gm_Ag_d->data[i+k*N]-gm_Ah_d->data[j+m*N],2); };*/ - + gsl_matrix_free(gm_Ag_d); gsl_matrix_free(gm_Ah_d); - + // gsl_matrix_transpose(C); //gsl_matrix_scale(C,-1); gsl_matrix_printout(C,"C",pdebug.strvalue); diff --git a/src/graphm/algorithm_umeyama.cpp b/src/graphm/algorithm_umeyama.cpp index 9597021..67093c2 100644 --- a/src/graphm/algorithm_umeyama.cpp +++ b/src/graphm/algorithm_umeyama.cpp @@ -22,14 +22,15 @@ match_result algorithm_umeyama::match(graph& g, graph& h,gsl_matrix* gm_P_i,gsl_matrix* gm_ldh,double dalpha_ldh) { if (bverbose) - *gout<<"Umeyama algorithm"<