From 321a06060dead18874841e44ef8ec335a82b01a2 Mon Sep 17 00:00:00 2001 From: vasiloglou Date: Mon, 7 Apr 2008 23:07:13 +0000 Subject: [PATCH] added the example for memory_manager --- fastlib2/contrib/nvasil/l_bfgs/build.py | 2 +- fastlib2/contrib/nvasil/l_bfgs/l_bfgs_impl.h | 17 +++++-- .../nvasil/l_bfgs/optimization_utils.h | 44 ++++++++++++++++++ fastlib2/contrib/nvasil/mvu/main.cc | 23 +++++++--- .../fastlib/mmanager/memory_manager_test.cc | 45 +++++++++++++++++++ fastlib2/fastlib/sparse/sparse_matrix.h | 5 +++ fastlib2/fastlib/sparse/sparse_matrix_impl.h | 2 +- fastlib2/mlpack/kernel_pca/kernel_pca_impl.h | 3 +- fastlib2/mlpack/kernel_pca/kernel_pca_test.cc | 8 ++-- 9 files changed, 132 insertions(+), 17 deletions(-) create mode 100644 fastlib2/fastlib/mmanager/memory_manager_test.cc diff --git a/fastlib2/contrib/nvasil/l_bfgs/build.py b/fastlib2/contrib/nvasil/l_bfgs/build.py index 77c3f83b3f..8d3c30fc18 100644 --- a/fastlib2/contrib/nvasil/l_bfgs/build.py +++ b/fastlib2/contrib/nvasil/l_bfgs/build.py @@ -1,5 +1,5 @@ librule(name="l_bfgs", - headers=["l_bfgs.h", "l_bfgs_impl.h"], + headers=["l_bfgs.h", "l_bfgs_impl.h", "optimization_utils.h"], tests=["test_l_bfgs.cc"], deplibs=["fastlib:fastlib", "mlpack/allknn:allknn", "contrib/nvasil/allkfn:allkfn", "contrib/nvasil/mvu:mvu"]) diff --git a/fastlib2/contrib/nvasil/l_bfgs/l_bfgs_impl.h b/fastlib2/contrib/nvasil/l_bfgs/l_bfgs_impl.h index 90078302e8..bea0ac76d7 100644 --- a/fastlib2/contrib/nvasil/l_bfgs/l_bfgs_impl.h +++ b/fastlib2/contrib/nvasil/l_bfgs/l_bfgs_impl.h @@ -88,11 +88,16 @@ void LBfgs::ComputeLocalOptimumBFGS() { s_bfgs_[0].ptr(), y_bfgs_[0].ptr()); double old_feasibility_error = feasibility_error; for(index_t i=0; iComputeGradient(coordinates_, &gradient_); UpdateBFGS_(); previous_gradient_.CopyValues(gradient_); @@ -163,6 +168,10 @@ void LBfgs::GetResults(Matrix *result) { result->Copy(coordinates_); } +template +void LBfgs::set_coordinates(Matrix &coordinates) { + coordinates_.CopyValues(coordinates); +} template Matrix *LBfgs::coordinates() { return &coordinates_; @@ -336,7 +345,7 @@ success_t LBfgs::UpdateBFGS_(index_t index_bfgs) { temp_y_bfgs.ptr()); double y_norm=la::Dot(temp_y_bfgs.n_elements(), temp_y_bfgs.ptr(), temp_y_bfgs.ptr()); - if (temp_ro<1e-10*y_norm) { + if (temp_ro<1e-70*y_norm) { fx_timer_stop(module_, "update_bfgs"); NONFATAL("Rejecting s, y they don't satisfy curvature condition"); return SUCCESS_FAIL; diff --git a/fastlib2/contrib/nvasil/l_bfgs/optimization_utils.h b/fastlib2/contrib/nvasil/l_bfgs/optimization_utils.h index 8394f11495..28b0ad9d52 100644 --- a/fastlib2/contrib/nvasil/l_bfgs/optimization_utils.h +++ b/fastlib2/contrib/nvasil/l_bfgs/optimization_utils.h @@ -33,6 +33,7 @@ class OptUtils { la::AddTo(dimension, mean.ptr(), data->GetColumnPtr(i)); } } + static void NonNegativeProjection(Matrix *data) { double *ptr=data->ptr(); for(index_t i=0; i<(index_t)data->n_elements(); i++) { @@ -41,6 +42,49 @@ class OptUtils { } } } + + static success_t SVDTransform(Matrix &input_mat, Matrix *output_mat, + index_t components_to_keep) { + Matrix temp; + temp.Copy(input_mat); + RemoveMean(&temp); + Vector s; + Matrix U, VT; + success_t success=la::SVDInit(temp, &s, &U, &VT); + if (success==SUCCESS_PASS) { + NOTIFY("PCA successful !! Printing requested %i eigenvalues...", + components_to_keep); + for(index_t i=0; iInit(components_to_keep, input_mat.n_cols()); + Matrix temp_reconstructed; + Matrix temp_S; + temp_S.Init(input_mat.n_rows(), input_mat.n_rows()); + temp_S.SetAll(0.0); + for(index_t i=0; in_cols(); i++) { + memcpy(output_mat->GetColumnPtr(i), + temp_reconstructed.GetColumnPtr(i), components_to_keep*sizeof(double)); + } + double error=0; + for(index_t i=0; i=0); diff --git a/fastlib2/contrib/nvasil/mvu/main.cc b/fastlib2/contrib/nvasil/mvu/main.cc index c8a2a504d6..0c86a28916 100644 --- a/fastlib2/contrib/nvasil/mvu/main.cc +++ b/fastlib2/contrib/nvasil/mvu/main.cc @@ -37,14 +37,25 @@ int main(int argc, char *argv[]){ l_bfgs_node=fx_submodule(NULL, "opts/l_bfgs", "l_bfgs"); optfun_node=fx_submodule(NULL, "opts/optfun", "optfun"); - bool pca_preprocess=fx_param_bool(NULL, "opts/pca", false); + bool pca_preprocess=fx_param_bool(NULL, "opts/pca_pre", false); + index_t pca_dimension=fx_param_int(NULL, "opts/pca_dim", 5); + bool pca_init=fx_param_bool(NULL, "opts/pca_init", false); Matrix *initial_data; if (pca_preprocess==true) { + NOTIFY("Preprocessing with pca"); + initial_data = new Matrix(); + Matrix temp; + OptUtils::SVDTransform(data_mat, &temp, pca_dimension); + data_mat.Destruct(); + data_mat.Own(&temp); + } + if (pca_init==true) { NOTIFY("Preprocessing with pca"); initial_data = new Matrix(); index_t new_dimension=fx_param_int(l_bfgs_node, "new_dimension", 2); OptUtils::SVDTransform(data_mat, initial_data, new_dimension); - } + } + //we need to insert the number of points char buffer[128]; @@ -58,7 +69,7 @@ int main(int argc, char *argv[]){ opt_function.Init(optfun_node, data_mat); LBfgs engine; engine.Init(&opt_function, l_bfgs_node); - if (pca_preprocess==true) { + if (pca_init==true) { engine.set_coordinates(*initial_data); } engine.ComputeLocalOptimumBFGS(); @@ -75,7 +86,7 @@ int main(int argc, char *argv[]){ opt_function.Init(optfun_node, data_mat); LBfgs engine; engine.Init(&opt_function, l_bfgs_node); - if (pca_preprocess==true) { + if (pca_init==true) { engine.set_coordinates(*initial_data); } engine.ComputeLocalOptimumBFGS(); @@ -90,10 +101,10 @@ int main(int argc, char *argv[]){ if (optimized_function == "mvfu"){ MaxFurthestNeighbors opt_function; opt_function.Init(optfun_node, data_mat); - opt_function.set_lagrange_mult(0.0); + //opt_function.set_lagrange_mult(0.0); LBfgs engine; engine.Init(&opt_function, l_bfgs_node); - if (pca_preprocess==true) { + if (pca_init==true) { engine.set_coordinates(*initial_data); } engine.ComputeLocalOptimumBFGS(); diff --git a/fastlib2/fastlib/mmanager/memory_manager_test.cc b/fastlib2/fastlib/mmanager/memory_manager_test.cc new file mode 100644 index 0000000000..11305ec85c --- /dev/null +++ b/fastlib2/fastlib/mmanager/memory_manager_test.cc @@ -0,0 +1,45 @@ +/* + * ===================================================================================== + * + * Filename: memory_manager_test.cc + * + * Description: + * + * Version: 1.0 + * Created: 04/07/2008 05:51:29 PM EDT + * Revision: none + * Compiler: gcc + * + * Author: Nikolaos Vasiloglou (NV), nvasil@ieee.org + * Company: Georgia Tech Fastlab-ESP Lab + * + * ===================================================================================== + */ +#include "memory_manager.h" + +class MemoryManagerTest { + void Init(){ + mmapmm::MemoryManager::allocator_ = new mmapmm::MemoryManager(); + mmapmm::MemoryManager::allocator_->set_capacity(65536*1000); + mmapmm::MemoryManager::allocator_->Init(); + } + + void Destruct() { + mmapmm::MemoryManager::allocator_->Destruct(); + delete mmapmm::MemoryManager::allocator_; + } + + void Test1() { + double *ptr=mmap::MemoryManager::allocator_->malloc(100000); + + } + + void TestAll() { + Test1(); + } +}; + +int main(int argc, char* argv[]) { + MemoryManagerTest test; + test.TestAll(); +} diff --git a/fastlib2/fastlib/sparse/sparse_matrix.h b/fastlib2/fastlib/sparse/sparse_matrix.h index f93d0809fb..30b6443145 100644 --- a/fastlib2/fastlib/sparse/sparse_matrix.h +++ b/fastlib2/fastlib/sparse/sparse_matrix.h @@ -212,6 +212,11 @@ class SparseMatrix { * Set Values */ void set(index_t r, index_t c, double v); + /** + * Returns the transpose of the matrix. + * if it is symmetric it just returns a copy of the same matrix + */ + void Transpose(SparseMatrix *transpose); /** * scales the matrix with a scalar */ diff --git a/fastlib2/fastlib/sparse/sparse_matrix_impl.h b/fastlib2/fastlib/sparse/sparse_matrix_impl.h index 9294ccd65c..668ea21724 100644 --- a/fastlib2/fastlib/sparse/sparse_matrix_impl.h +++ b/fastlib2/fastlib/sparse/sparse_matrix_impl.h @@ -447,7 +447,7 @@ void SparseMatrix::Eig(SparseMatrix &pencil_matrix, "it will fail\n"); } - index_t block_size=2*num_of_eigvalues; + index_t block_size=std::min(2*num_of_eigvalues, 10); Teuchos::RCP ivec = Teuchos::rcp(new Epetra_MultiVector(*map_, block_size)); // Fill it with random numbers diff --git a/fastlib2/mlpack/kernel_pca/kernel_pca_impl.h b/fastlib2/mlpack/kernel_pca/kernel_pca_impl.h index c7d97d433a..ad2352cb4a 100644 --- a/fastlib2/mlpack/kernel_pca/kernel_pca_impl.h +++ b/fastlib2/mlpack/kernel_pca/kernel_pca_impl.h @@ -197,9 +197,10 @@ void KernelPCA::ComputeLLE(index_t num_of_eigenvalues, kernel_matrix_.SetDiagonal(1.0); NONFATAL("Computing eigen values...\n"); SparseMatrix kernel_matrix1; + kernel_matrix_.ToFile("i_w.txt"); kernel_matrix_.EndLoading(); Sparsem::MultiplyT(kernel_matrix_, &kernel_matrix1); - kernel_matrix1.ToFile("lle_mat.txt"); + kernel_matrix1.ToFile("i_w_i_w.txt"); kernel_matrix1.EndLoading(); kernel_matrix1.Eig(num_of_eigenvalues, "SM", diff --git a/fastlib2/mlpack/kernel_pca/kernel_pca_test.cc b/fastlib2/mlpack/kernel_pca/kernel_pca_test.cc index 7a5a8eed6b..4f7195adda 100644 --- a/fastlib2/mlpack/kernel_pca/kernel_pca_test.cc +++ b/fastlib2/mlpack/kernel_pca/kernel_pca_test.cc @@ -41,7 +41,7 @@ class KernelPCATest { NOTIFY("Estimated bandwidth %lg ...\n", bandwidth); kernel_.set(bandwidth); engine_->LoadAffinityMatrix(); - engine_->ComputeGeneralKernelPCA(kernel_, 5, + engine_->ComputeGeneralKernelPCA(kernel_, 15, &eigen_vectors, &eigen_values); @@ -89,9 +89,9 @@ class KernelPCATest { NOTIFY("Test ComputeSpectralRegression passed...\n"); } void TestAll() { - TestGeneralKernelPCA(); - //TestLLE(); - TestSpectralRegression(); + //TestGeneralKernelPCA(); + TestLLE(); + //TestSpectralRegression(); } private: KernelPCA *engine_;