From def2bcd3f41a36e52b179aa435c5bf29c94dab5e Mon Sep 17 00:00:00 2001 From: Joe Zhao Date: Mon, 18 May 2015 22:22:11 +0800 Subject: added openmp directive --- CMakeLists.txt | 2 +- model/rankaccu.cpp | 81 +++++++++++++++++++++------------------------------- model/rankaccu.h | 4 +-- model/ranksvm.h | 2 +- model/ranksvmtn.cpp | 26 ++++++++++------- model/ranksvmtn.h | 2 +- tools/dataProvider.h | 32 ++++++++++++--------- train.cpp | 29 +++++++++---------- 8 files changed, 84 insertions(+), 94 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 180456c..0e356fe 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,7 +6,7 @@ INCLUDE_DIRECTORIES ( "/usr/include/eigen3" ) # Use Random Library for Random Number Generation -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -fopenmp") FIND_PACKAGE( Boost COMPONENTS program_options REQUIRED ) INCLUDE_DIRECTORIES( ${Boost_INCLUDE_DIR}) diff --git a/model/rankaccu.cpp b/model/rankaccu.cpp index 726f2c8..f998e93 100644 --- a/model/rankaccu.cpp +++ b/model/rankaccu.cpp @@ -70,79 +70,64 @@ void rankmerge(int l,int r,vector &C,vector &rank,const vector } } -void rank_accu(DataList &D,const vector pred) +void rank_accu(RidList &D,const vector pred) { unsigned long n = D.getSize(); vector orig_rank(n),pred_rank(n),C(n); vector orig(n); - vector &dat = D.getData(); int i,j; - for (i=0;irank; + orig[i]=D.getL(i); } - int cnt=0; double accu_nDCG=0; double accu_AP=0; - i=j=0; - while (iqid!=dat[i+1]->qid) + i=j+D.getqSize()-1; + double Y=0,Z=0; + double AP=0; + ranksort(j,i,orig_rank,orig,pred); + ranksort(j,i,pred_rank,pred,orig); + for (int k = j;k<=i;++k) { - double Y=0,Z=0; - double AP=0; - ranksort(j,i,orig_rank,orig,pred); - ranksort(j,i,pred_rank,pred,orig); - for (int k = j;k<=i;++k) - { - Z += (pow(2,offset+orig[orig_rank[k]]) - 1)/log2(2+k-j); - Y += (pow(2,offset+orig[pred_rank[k]]) - 1)/log2(2+k-j); - } - accu_nDCG+=Y/Z; - rankmerge(j,i,C,orig_rank,pred,orig); - for (int k = j+1;k<=i;++k) - AP += ((double)C[k])/(k-j); - AP=AP*2/(i-j)-1; - accu_AP+=AP; - j = i+1; - ++cnt; + Z += (pow(2,offset+orig[orig_rank[k]]) - 1)/log2(2+k-j); + Y += (pow(2,offset+orig[pred_rank[k]]) - 1)/log2(2+k-j); } - ++i; + accu_nDCG+=Y/Z; + rankmerge(j,i,C,orig_rank,pred,orig); + for (int k = j+1;k<=i;++k) + AP += ((double)C[k])/(k-j); + AP=AP*2/(i-j)-1; + accu_AP+=AP; } - LOG(INFO)<<"over "<< cnt<< " queries. "<<"Average nDGC: "<< accu_nDCG/cnt<< " Average AP: "< pred,CMC & cmc) { +void rank_CMC(RidList &D,const std::vector pred,CMC & cmc) { unsigned long n = D.getSize(); vector orig_rank(n),pred_rank(n); vector orig(n); - vector &dat = D.getData(); int i,j; - for (i=0;irank; + orig[i]=D.getL(i); } - int cnt=0; - i=j=0; - while (iqid!=dat[i+1]->qid) - { - ranksort(j,i,pred_rank,pred,orig); - for (int k=j;k<=i;++k) - if (orig[pred_rank[k]]>0) - { - LOG(INFO)<<"qid:"<qid<<"; pred:"<0) + { + LOG(INFO)<<"qid:"< pred,CMC & cmc); +void rank_CMC(RidList &D,const std::vector pred,CMC & cmc); -void rank_accu(DataList &D,const std::vector pred); +void rank_accu(RidList &D,const std::vector pred); #endif //RANKSVM_RANKACCU_H diff --git a/model/ranksvm.h b/model/ranksvm.h index 9051343..20bb35a 100644 --- a/model/ranksvm.h +++ b/model/ranksvm.h @@ -26,7 +26,7 @@ protected: int fsize; public: virtual int train(RidList &D)=0; - virtual int predict(DataList &D,std::vector &res)=0; + virtual int predict(RidList &D,std::vector &res)=0; // TODO Not sure how to construct this // Possible solution: generate a nxn matrix each row contains the sorted list of ranker result. int saveModel(const std::string fname); diff --git a/model/ranksvmtn.cpp b/model/ranksvmtn.cpp index 1414c81..f904fdd 100644 --- a/model/ranksvmtn.cpp +++ b/model/ranksvmtn.cpp @@ -7,7 +7,7 @@ using namespace std; using namespace Eigen; // Main terminating criteria -const int maxiter = 40; // max iteration count +const int maxiter = 50; // max iteration count const double prec=1e-10; // precision // conjugate gradient const double cg_prec=1e-10; // precision @@ -17,14 +17,21 @@ const int ls_maxiter = 10; const double line_prec=1e-10; // precision const double line_turb=1e-15; // purturbation +void cal_Dw(RidList &D,const VectorXd &w, VectorXd &Dw) +{ + int n = D.getSize(); + #pragma omp parallel for + for (int i=0;i &rank,const VectorXd &corr,const VectorXd &alpha,const VectorXd s,VectorXd &Hs) { int n = D.getSize(); int q = D.getqSize(); Hs = VectorXd::Zero(s.rows()); VectorXd Ds(n); - for (int i=0;i &res){ +int RSVMTN::predict(RidList &D, vector &res){ res.clear(); for (int i=0;ifeature).dot(model.weight)); + res.push_back(D.getVec(i).dot(model.weight)); return 0; }; \ No newline at end of file diff --git a/model/ranksvmtn.h b/model/ranksvmtn.h index c98e581..97579b3 100644 --- a/model/ranksvmtn.h +++ b/model/ranksvmtn.h @@ -13,7 +13,7 @@ public: return "TN"; }; virtual int train(RidList &D); - virtual int predict(DataList &D,std::vector &res); + virtual int predict(RidList &D,std::vector &res); }; #endif \ No newline at end of file diff --git a/tools/dataProvider.h b/tools/dataProvider.h index 586965e..59b989a 100644 --- a/tools/dataProvider.h +++ b/tools/dataProvider.h @@ -82,9 +82,23 @@ public: else uniq.push_back(d); } + inline DataEntry* getU(int x) + { + return uniq[x]; + } + inline DataEntry* getO(int x) + { + return other[x]; + } + inline std::string getQid(int x) + { + int a,b,n=getqSize(); + a=x/n; + return getU(a)->qid; + } inline int getqSize() { - return (int)(uniq.size()+other.size()-1); + return (int)other.size(); } inline int getuSize() { @@ -99,24 +113,14 @@ public: a=x/n; b=x%n; Eigen::VectorXd vec; - if (bfeature-uniq[b]->feature; - else - if (bfeature-uniq[b+1]->feature; - else - vec=uniq[a]->feature-other[b-uniq.size()+1]->feature; - return vec.cwiseAbs(); + return (uniq[a]->feature-other[b]->feature).cwiseAbs(); }; inline double getL(int x){ int a,b,n=getqSize(); a=x/n; b=x%n; - if (brank - a) < 1e-5) - return 1; + if (std::fabs(other[b]->rank - a) < 1e-5) + return 1; return -1; }; }; diff --git a/train.cpp b/train.cpp index 039c93a..0b5b4d4 100644 --- a/train.cpp +++ b/train.cpp @@ -43,7 +43,7 @@ int predict(DataProvider &dp) { rsvm = RSVM::loadModel(vm["model"].as().c_str()); dp.open(); - DataList D; + RidList D; vector L; CMC cmc; LOG(INFO)<<"Prediction started"; @@ -59,24 +59,21 @@ int predict(DataProvider &dp) { else ot=&cout; - while (!dp.EOFile()) - { - dp.getDataSet(D); - LOG(INFO)<<"Read "<predict(D,L); - - if (vm.count("validate")) - { - rank_accu(D,L); - if (vm.count("cmc")) - rank_CMC(D,L,cmc); - } + dp.getAllDataSet(D); + LOG(INFO)<<"Read "<predict(D,L); - if (vm.count("output") && vm.count("predict")) - for (int i=0; i