GURLS_EXPORT void svd(const gMat2D<float>& A, gMat2D<float>& U, gVec<float>& W, gMat2D<float>& Vt) { char jobu = 'S', jobvt = 'S'; int m = A.rows(); int n = A.cols(); int k = std::min<int>(m, n); if ((int)W.getSize() < k) { throw gException("The length of vector W must be at least equal to the minimum dimension of the input matrix A"); } if ((int)U.rows() < m || (int)U.cols() < k) { throw gException("Please check the dimensions of the matrix U where to store the singular vectors"); } if ((int)Vt.rows() < k || (int)Vt.cols() < n) { throw gException("Please check the dimensions of the matrix Vt where to store the rigth singular vectors"); } int lda = A.cols(); int ldu = U.cols(); int ldvt = Vt.cols(); int info, lwork = std::max<int>(3*k+std::max<int>(m,n), 5*k); float* work = new float[lwork]; float* copy = new float[m*n]; A.asarray(copy, m*n); sgesvd_(&jobu, &jobvt, &n, &m, copy, &lda, W.getData(), Vt.getData(), &ldvt, U.getData(), &ldu, work, &lwork, &info); delete[] work; delete[] copy; }
GURLS_EXPORT void eig(const gMat2D<float>& A, gVec<float>& Wr, gVec<float>& Wi) { if (A.cols() != A.rows()) throw gException("The input matrix A must be squared"); float* Atmp = new float[A.getSize()]; copy(Atmp, A.getData(), A.getSize()); char jobvl = 'N', jobvr = 'N'; int n = A.cols(), lda = A.cols(), ldvl = 1, ldvr = 1; int info, lwork = 4*n; float* work = new float[lwork]; sgeev_(&jobvl, &jobvr, &n, Atmp, &lda, Wr.getData(), Wi.getData(), NULL, &ldvl, NULL, &ldvr, work, &lwork, &info); delete[] Atmp; delete[] work; if(info != 0) { std::stringstream str; str << "Eigenvalues/eigenVectors computation failed, error code " << info << ";" << std::endl; throw gException(str.str()); } }
void BigArray<T>::loadNC(const std::string &fileName) { dataFileName = fileName; connection.disconnect(); connection = releaseSignal().connect(boost::bind(&gurls::BigArray<T>::close, this)); std::string errorString = "Error opening file " + fileName + ":"; // Set up file access property list with parallel I/O access plist_id = H5Pcreate(H5P_FILE_ACCESS); if(plist_id == -1) throw gException(errorString); herr_t status; #ifdef USE_MPIIO status = H5Pset_fapl_mpio(plist_id, MPI_COMM_WORLD, MPI_INFO_NULL); #else status = H5Pset_fapl_mpiposix(plist_id, MPI_COMM_WORLD, false); #endif CHECK_HDF5_ERR(status, errorString) // Create a new file collectively and release property list identifier. file_id = H5Fopen(fileName.c_str(), H5F_ACC_RDWR, plist_id); CHECK_HDF5_ERR(file_id, errorString) status = H5Pclose(plist_id); CHECK_HDF5_ERR(status, errorString) dset_id = H5Dopen(file_id, "mat", H5P_DEFAULT); CHECK_HDF5_ERR(dset_id, errorString) hid_t filespace = H5Dget_space( dset_id ); CHECK_HDF5_ERR(filespace, errorString) hsize_t dims[2], maxDims[2]; status = H5Sget_simple_extent_dims(filespace, dims, maxDims); CHECK_HDF5_ERR(status, errorString) status = H5Sclose(filespace); CHECK_HDF5_ERR(status, errorString) this->numrows = static_cast<unsigned long>(dims[1]); this->numcols = static_cast<unsigned long>(dims[0]); // Create property list for collective dataset write. plist_id = H5Pcreate(H5P_DATASET_XFER); if(plist_id == -1) throw gException(errorString); status = H5Pset_dxpl_mpio(plist_id, H5FD_MPIO_INDEPENDENT); CHECK_HDF5_ERR(status, errorString) }
void BigArray<T>::getMatrix(unsigned long startingRow, unsigned long startingCol, gMat2D<T>&result) const { if(startingRow >= this->numrows || startingCol >= this->numcols) throw gException(Exception_Index_Out_of_Bound); if(startingRow+result.rows() > this->numrows || startingCol+result.cols() > this->numcols) throw gException(Exception_Index_Out_of_Bound); getMatrix(startingRow, startingCol, result.rows(), result.cols(), result.getData()); }
void BigArray<T>::setColumn(unsigned long col, const gVec<T>& value) { if(col >= this->numcols) throw gException(Exception_Index_Out_of_Bound); if(value.getSize() != this->numrows) throw gException(Exception_Inconsistent_Size); setMatrix(0, col, value.getData(), this->numrows, 1); }
void BigArray<T>::setRow(unsigned long row, const gVec<T>& value) { if(row >= this->numrows) throw gException(Exception_Index_Out_of_Bound); if(value.getSize() != this->numcols) throw gException(Exception_Inconsistent_Size); setMatrix(row, 0, value.getData(), 1, this->numcols); }
void BigArray<T>::getMatrix(unsigned long startingRow, unsigned long startingCol, unsigned long numRows, unsigned long numCols, gMat2D<T>&result) const { if(startingRow >= this->numrows || startingCol >= this->numcols) throw gException(Exception_Index_Out_of_Bound); if(startingRow+numRows > this->numrows || startingCol+numCols > this->numcols) throw gException(Exception_Index_Out_of_Bound); result.resize(numRows, numCols); getMatrix(startingRow, startingCol, numRows, numCols, result.getData()); }
const OptFunction* OptFunction::dynacast(const GurlsOption* opt) { if (opt->isA(FunctionOption) ) return static_cast<const OptFunction*>(opt); throw gException(gurls::Exception_Illegal_Dynamic_Cast); }
void BigArray<T>::setValue(unsigned long row, unsigned long col, T value) { if(row >= this->numrows || col >= this->numcols) throw gException(Exception_Index_Out_of_Bound); std::string errorString("Error writing BigArray value"); hsize_t dims[2] = {1, 1}; hid_t memspace = H5Screate_simple(2, dims, NULL); CHECK_HDF5_ERR(memspace, errorString) hsize_t point[2] = {col, row}; hid_t filespace = H5Dget_space(dset_id); CHECK_HDF5_ERR(filespace, errorString) herr_t status; status = H5Sselect_elements(filespace, H5S_SELECT_SET, 1, point); CHECK_HDF5_ERR(status, errorString) status = H5Dwrite(dset_id, getHdfType<T>(), memspace, filespace, plist_id, &value); CHECK_HDF5_ERR(status, errorString) status = H5Sclose(memspace); CHECK_HDF5_ERR(status, errorString) status = H5Sclose(filespace); CHECK_HDF5_ERR(status, errorString) }
const OptProcess* OptProcess::dynacast(const GurlsOption* opt) { if (opt->isA(ProcessOption) ) return static_cast<const OptProcess*>(opt); throw gException(gurls::Exception_Illegal_Dynamic_Cast); }
OptStringList* OptStringList::dynacast(GurlsOption* opt) { if (opt->isA(StringListOption) ) return static_cast<OptStringList*>(opt); throw gException(gurls::Exception_Illegal_Dynamic_Cast); }
const OptNumber* OptNumber::dynacast(const GurlsOption* opt) { if (opt->isA(NumberOption) ) return static_cast<const OptNumber*>(opt); throw gException(gurls::Exception_Illegal_Dynamic_Cast); }
const OptString* OptString::dynacast(const GurlsOption* opt) { if (opt->isA(StringOption) ) return static_cast<const OptString*>(opt); throw gException(gurls::Exception_Illegal_Dynamic_Cast); }
const OptTaskSequence *OptTaskSequence::dynacast(const GurlsOption *opt) { if (opt->isA(TaskSequenceOption) ) return static_cast<const OptTaskSequence*>(opt); throw gException(gurls::Exception_Illegal_Dynamic_Cast); }
GURLS_EXPORT void lu(gMat2D<float>& A, gVec<int>& pv) { unsigned int k = std::min(A.cols(), A.rows()); if (pv.getSize() != k) throw gException("The lenghth of pv must be equal to the minimun dimension of A"); int info; int m = A.rows(); int n = A.cols(); int lda = A.rows(); sgetrf_(&m, &n, A.getData(), &lda, pv.getData(), &info); if(info <0) throw gException("LU factorization failed"); }
GURLS_EXPORT float dot(const gVec<float>& x, const gVec<float>& y) { if ( x.getSize() != y.getSize() ) throw gException(gurls::Exception_Inconsistent_Size); int n = x.getSize(); int incr = 1; return sdot_(&n, const_cast<float*>(x.getData()), &incr, const_cast<float*>(y.getData()), &incr); }
gVec<T> BigArray<T>::operator() (unsigned long i) const { if(i >= this->numcols) throw gException(Exception_Index_Out_of_Bound); gVec<T> ret(this->numrows); getMatrix(0, i, this->numrows, 1, ret.getData()); return ret; }
GURLS_EXPORT double dot(const gVec<double>& x, const gVec<double>& y) { if ( x.getSize() != y.getSize() ) { throw gException(gurls::Exception_Inconsistent_Size); } int n = x.getSize(); int incr = 1; return ddot_(&n, const_cast<double*>(x.getData()), &incr, const_cast<double*>(y.getData()), &incr); }
void BaseArray<T>::resize(unsigned long n) { if (! this->isowner) { throw gException("It is not possible to resize an array that is not the owner of its data."); } else if (n != this->size) { T* tmp = this->data; unsigned long oldsize = this->size; this->alloc(n); this->set(tmp, std::min(n, oldsize)); if(tmp != NULL) delete[] tmp; }; }
GURLS_EXPORT void eig(const gMat2D<float>& A, gVec<float>& Wr, gVec<float>& Wi) { if (A.cols() != A.rows()) { throw gException("The input matrix A must be squared"); } char jobvl = 'N', jobvr = 'N'; int n = A.cols(), lda = A.cols(), ldvl = 1, ldvr = 1; int info, lwork = 4*n; float* work = new float[lwork]; sgeev_(&jobvl, &jobvr, &n, const_cast<gMat2D<float>&>(A).getData(), &lda, Wr.getData(), Wi.getData(), NULL, &ldvl, NULL, &ldvr, work, &lwork, &info); delete[] work; }
void BigArray<T>::setMatrix(unsigned long startingRow, unsigned long startingCol, const T* M, const unsigned long M_rows, const unsigned long M_cols) { if(startingRow >= this->numrows || startingCol >= this->numcols) throw gException(Exception_Index_Out_of_Bound); if(startingRow+M_rows > this->numrows || startingCol+M_cols > this->numcols) throw gException(Exception_Index_Out_of_Bound); std::string errorString("Error writing matrix data"); hsize_t dims[2] = {M_cols, M_rows}; hid_t memspace = H5Screate_simple(2, dims, NULL); CHECK_HDF5_ERR(memspace, errorString) hsize_t count[2] = {1, 1}; hsize_t stride[2] = {1, 1}; hsize_t block[2] = {dims[0], dims[1]}; hsize_t offset[2] = {startingCol, startingRow}; // Select hyperslab in the file. hid_t filespace = H5Dget_space(dset_id); CHECK_HDF5_ERR(filespace, errorString) herr_t status; status = H5Sselect_hyperslab(filespace, H5S_SELECT_SET, offset, stride, count, block); CHECK_HDF5_ERR(status, errorString) status = H5Dwrite(dset_id, getHdfType<T>(), memspace, filespace, plist_id, M); CHECK_HDF5_ERR(status, errorString) H5Sclose(memspace); CHECK_HDF5_ERR(status, errorString) H5Sclose(filespace); CHECK_HDF5_ERR(status, errorString) }
GURLS_EXPORT void eig(const gMat2D<float>& A, gMat2D<float>& V, gVec<float>& Wr, gVec<float>& Wi) { if (A.cols() != A.rows()) { throw gException("The input matrix A must be squared"); } char jobvl = 'N', jobvr = 'V'; int n = A.cols(), lda = A.cols(), ldvl = 1, ldvr = A.cols(); int info, lwork = 4*n; float* work = new float[lwork]; gMat2D<float> Atmp = A; gMat2D<float> Vtmp = V; sgeev_(&jobvl, &jobvr, &n, Atmp.getData(), &lda, Wr.getData(), Wi.getData(), NULL, &ldvl, Vtmp.getData(), &ldvr, work, &lwork, &info); Vtmp.transpose(V); delete[] work; }
gMat2D<T>* RecursiveRLSWrapper<T>::eval(const gMat2D<T> &X) { if(!this->trainedModel()) throw gException("Error, Train Model First"); gurls::PredPrimal<T> predTask; gMat2D<T> y; OptMatrix<gMat2D<T> >* result = predTask.execute(X, y, *(this->opt)); gMat2D<T>& pred_mat = result->getValue(); result->detachValue(); delete result; return &pred_mat; }
void OptFunction::setValue(std::string func_name) { name = func_name; if(func_name == "mean") f = new Mean(); else if(func_name == "min") f = new Min(); else if(func_name == "max") f = new Max(); else if(func_name == "median") f = new Median(); else { f = NULL; throw gException(Exception_Unknown_Function); } }
GURLS_EXPORT void dot(const gMat2D<float>& A, const gVec<float>& x, gVec<float>& y) { if ( (A.cols() != x.getSize()) || (A.rows() != y.getSize())) throw gException(Exception_Inconsistent_Size); // y = alpha*A*x + beta*y float alpha = 1.0f; float beta = 0.0f; char transA = 'N'; int m = A.rows(); int n = A.cols(); int lda = m; int inc = 1; sgemv_(&transA, &m, &n, &alpha, const_cast<float*>(A.getData()), &lda, const_cast<float*>(x.getData()), &inc, &beta, y.getData(), &inc); }
void RecursiveRLSCholUpdateWrapper<T>::update(const gMat2D<T> &X, const gMat2D<T> &y) { if(!this->trainedModel()) throw gException("Error, Train Model First"); RLSPrimalRecUpdateCholesky<T> optimizer; const unsigned long d = X.getSize(); const unsigned long t = y.getSize(); gMat2D<T>X_mat(1, d); copy(X_mat.getData(), X.getData(), d); gMat2D<T>y_mat(1, t); copy(y_mat.getData(), y.getData(), t); GurlsOptionsList* ret = optimizer.execute(X_mat, y_mat, *(this->opt)); this->opt->removeOpt("optimizer"); this->opt->addOpt("optimizer", ret); }
T GurlsWrapper<T>::eval(const gVec<T> &X, unsigned long *index) { if(!trainedModel()) throw gException("Error, Train Model First"); gMat2D<T>X_mat(1, X.getSize()); copy(X_mat.getData(), X.getData(), X.getSize()); gMat2D<T>* pred_mat = eval(X_mat); const T* pred = pred_mat->getData(); const unsigned long size = pred_mat->getSize(); const T* max = std::max_element(pred, pred+size); T ret = *max; if(index != NULL) *index = max-pred; delete pred_mat; return ret; }
GURLS_EXPORT void dot(const gMat2D<double>& A, const gVec<double>& x, gVec<double>& y){ if ( (A.cols() != x.getSize()) || (A.rows() != y.getSize())) throw gException(Exception_Inconsistent_Size); // y = alpha*A*x + beta*y double alpha = 1.0f; double beta = 0.0f; char transA = 'T'; // row major matrix int m = A.cols(); // row major matrix int n = A.rows(); // row major matrix int lda = m; // row major matrix int inc = 1; dgemv_(&transA, &m, &n, &alpha, const_cast<double*>(A.getData()), &lda, const_cast<double*>(x.getData()), &inc, &beta, y.getData(), &inc); }
void RecursiveRLSWrapper<T>::update(const gVec<T> &X, const gVec<T> &y) { if(!this->trainedModel()) throw gException("Error, Train Model First"); RLSPrimalRecUpdate<T> optimizer; const unsigned long d = X.getSize(); const unsigned long t = y.getSize(); gMat2D<T>X_mat(1, d); copy(X_mat.getData(), X.getData(), d); gMat2D<T>y_mat(1, t); copy(y_mat.getData(), y.getData(), t); GurlsOptionsList* ret = optimizer.execute(X_mat, y_mat, *(this->opt)); this->opt->removeOpt("optimizer"); this->opt->addOpt("optimizer", ret); ++nTot; gMat2D<T>* xtx = new gMat2D<T>(d,d); gMat2D<T>* xty = new gMat2D<T>(d,t); dot(X.getData(), X.getData(), xtx->getData(), 1, d, 1, d, d, d, CblasTrans, CblasNoTrans, CblasColMajor); dot(X.getData(), y.getData(), xty->getData(), 1, d, 1, t, d, t, CblasTrans, CblasNoTrans, CblasColMajor); GurlsOptionsList* kernel = this->opt->template getOptAs<GurlsOptionsList>("kernel"); const gMat2D<T>& XtX = kernel->getOptValue<OptMatrix<gMat2D<T> > >("XtX"); const gMat2D<T>& Xty = kernel->getOptValue<OptMatrix<gMat2D<T> > >("Xty"); axpy(d*d, (T)1.0, XtX.getData(), 1, xtx->getData(), 1); axpy(d*t, (T)1.0, Xty.getData(), 1, xty->getData(), 1); kernel->removeOpt("XtX"); kernel->addOpt("XtX", new OptMatrix<gMat2D<T> >(*xtx)); kernel->removeOpt("Xty"); kernel->addOpt("Xty", new OptMatrix<gMat2D<T> >(*xty)); unsigned long proportion = static_cast<unsigned long>(gurls::round(1.0/this->opt->getOptAsNumber("hoproportion"))); if(nTot % proportion == 0) { const gMat2D<T>& Xva = kernel->getOptValue<OptMatrix<gMat2D<T> > >("Xva"); const gMat2D<T>& yva = kernel->getOptValue<OptMatrix<gMat2D<T> > >("yva"); const unsigned long nva = Xva.rows(); const unsigned long nva_new = nva+1; gMat2D<T>* Xva_new = new gMat2D<T>(nva_new, d); const T* old_it = Xva.getData(); T* new_it = Xva_new->getData(); for(const T* end = new_it+(nva_new*d); new_it< end; old_it+=nva, new_it +=nva_new) copy(new_it, old_it, nva); copy(Xva_new->getData()+nva, X.getData(), d, nva_new, 1); kernel->removeOpt("Xva"); kernel->addOpt("Xva", new OptMatrix<gMat2D<T> >(*Xva_new)); gMat2D<T>* yva_new = new gMat2D<T>(nva_new, t); old_it = yva.getData(); new_it = yva_new->getData(); for(const T* end = new_it+(nva_new*t); new_it< end; old_it+=nva, new_it +=nva_new) copy(new_it, old_it, nva); copy(yva_new->getData()+nva, y.getData(), t, nva_new, 1); kernel->removeOpt("yva"); kernel->addOpt("yva", new OptMatrix<gMat2D<T> >(*yva_new)); } }
void KernelRLSWrapper<T>::train(const gMat2D<T> &X, const gMat2D<T> &y) { this->opt->removeOpt("split"); this->opt->removeOpt("optimizer"); const unsigned long nlambda = static_cast<unsigned long>(this->opt->getOptAsNumber("nlambda")); const unsigned long nsigma = static_cast<unsigned long>(this->opt->getOptAsNumber("nsigma")); OptTaskSequence *seq = new OptTaskSequence(); GurlsOptionsList * process = new GurlsOptionsList("processes", false); OptProcess* process1 = new OptProcess(); process->addOpt("one", process1); this->opt->addOpt("seq", seq); this->opt->addOpt("processes", process); if(this->kType == KernelWrapper<T>::LINEAR) { if(nlambda > 1ul) { *seq << "split:ho" << "kernel:linear" << "paramsel:hodual"; *process1 << GURLS::computeNsave << GURLS::computeNsave << GURLS::computeNsave; } else if(nlambda == 1ul) { if(this->opt->hasOpt("paramsel.lambdas")) { *seq << "kernel:linear"; *process1 << GURLS::computeNsave; } else throw gException("Please set a valid value for the regularization parameter, calling setParam(value)"); } else throw gException("Please set a valid value for NParam, calling setNParam(value)"); } else if(this->kType == KernelWrapper<T>::RBF) { if(nlambda > 1ul) { if(nsigma > 1ul) { *seq << "split:ho" << "paramsel:siglamho" << "kernel:rbf"; *process1 << GURLS::computeNsave << GURLS::computeNsave << GURLS::computeNsave; } else if(nsigma == 1ul) { if(this->opt->hasOpt("paramsel.sigma")) { *seq << "split:ho" << "kernel:rbf" << "paramsel:hodual"; *process1 << GURLS::computeNsave << GURLS::computeNsave << GURLS::computeNsave; } else throw gException("Please set a valid value for the kernel parameter, calling setSigma(value)"); } else throw gException("Please set a valid value for NSigma, calling setNSigma(value)"); } else if(nlambda == 1ul) { if(nsigma == 1ul) { if(this->opt->hasOpt("paramsel.sigma") && this->opt->hasOpt("paramsel.lambdas")) { *seq << "split:ho" << "kernel:rbf"; *process1 << GURLS::computeNsave << GURLS::computeNsave; } else throw gException("Please set a valid value for kernel and regularization parameters, calling setParam(value) and setSigma(value)"); } else throw gException("Please set a valid value for NSigma, calling setNSigma(value)"); } else throw gException("Please set a valid value for NParam, calling setNParam(value)"); } *seq << "optimizer:rlsdual"; *process1 << GURLS::computeNsave; GURLS G; G.run(X, y, *(this->opt), "one"); }