/** Validate every local entry of a matrix has a given value. */ void validate_mat(DistMat& mat, DataType expected) { for (int i = 0; i < mat.LocalHeight(); ++i) { for (int j = 0; j < mat.LocalWidth(); ++j) { ASSERT_EQ(mat.GetLocal(i, j), expected); } } }
static bool readDist(int fd, const char* filename, DistMat& M, uint64_t* bytes) { struct layer_header header; ssize_t read_rc = read(fd, &header, sizeof(header)); if (read_rc != sizeof(header)) { // error! } *bytes += read_rc; // check that header values match up Int height = header.height; Int width = header.width; M.Resize(height, width); if(M.ColStride() == 1 && M.RowStride() == 1) { if(M.Height() == M.LDim()) { void* buf = (void*) M.Buffer(); size_t bufsize = height * width * sizeof(DataType); read_rc = read(fd, buf, bufsize); if (read_rc != bufsize) { // error! } *bytes += read_rc; } else { for(Int j = 0; j < width; ++j) { void* buf = (void*) M.Buffer(0, j); size_t bufsize = height * sizeof(DataType); read_rc = read(fd, buf, bufsize); if (read_rc != bufsize) { // error! } *bytes += read_rc; } } } else { const Int localHeight = M.LocalHeight(); const Int localWidth = M.LocalWidth(); const Int lDim = M.LDim(); if(localHeight == lDim) { void* buf = (void*) M.Buffer(); size_t bufsize = localHeight * localWidth * sizeof(DataType); read_rc = read(fd, buf, bufsize); if (read_rc != bufsize) { // error! } *bytes += read_rc; } else { for(Int jLoc = 0; jLoc < localWidth; ++jLoc) { void* buf = (void*) M.Buffer(0, jLoc); size_t bufsize = localHeight * sizeof(DataType); read_rc = read(fd, buf, bufsize); if (read_rc != bufsize) { // error! } *bytes += read_rc; } } } return true; }
static bool writeDist(int fd, const char* filename, const DistMat& M, uint64_t* bytes) { struct layer_header header; header.rank = (uint64_t) M.Grid().Rank(); header.width = (uint64_t) M.Width(); header.height = (uint64_t) M.Height(); header.localwidth = (uint64_t) M.LocalWidth(); header.localheight = (uint64_t) M.LocalHeight(); header.ldim = (uint64_t) M.LDim(); ssize_t write_rc = write(fd, &header, sizeof(header)); if (write_rc != sizeof(header)) { // error! } *bytes += write_rc; const Int localHeight = M.LocalHeight(); const Int localWidth = M.LocalWidth(); const Int lDim = M.LDim(); if(localHeight == lDim) { void* buf = (void*) M.LockedBuffer(); size_t bufsize = localHeight * localWidth * sizeof(DataType); write_rc = write(fd, buf, bufsize); if (write_rc != bufsize) { // error! } *bytes += write_rc; } else { for(Int j = 0; j < localWidth; ++j) { void* buf = (void*) M.LockedBuffer(0, j); size_t bufsize = localHeight * sizeof(DataType); write_rc = write(fd, buf, bufsize); if (write_rc != bufsize) { // error! } *bytes += write_rc; } } return true; }
/** Create a new matrix. */ void create_mat(DistMat& mat, DataType def = 1.0) { mat.Resize(LBANN_COMM_TEST_NROWS, LBANN_COMM_TEST_NCOLS); El::Fill(mat, def); }
double cg(gl_types::core * _glcore, std::vector<double> & means, double &diff, advanced_config & config){ glcore = _glcore; int tmp=ps.n; ps.n=ps.m; ps.m = tmp; diff = NAN; init_row_cols(); DistMat A; DistVec b(0,true), prec(1,true), r(2, true), p(3,true), x(4,true), Ap(5), t(6,true); //initialize startng guess if (!config.square) x = ones(0.5,ps.m); else x = ones(0.5,ps.n); DistDouble rsold, rnew, alpha; /* r = -A*x+b; if (~sqaure) r=A'*r; end p = r; rsold = r'*r; */ r=-A*x+b; if (!config.square) r=A._transpose()*r; p = r; rsold = r._transpose()*r; /* for i=1:size(A,1) Ap=A*p; if (~config.square) Ap=A'*Ap; end alpha=rsold/(p'*Ap); x=x+alpha*p; r=r-alpha*Ap; rsnew=r'*r; if sqrt(rsnew)<1e-10 break; end p=r+rsnew/rsold*p; rsold=rsnew; end */ for (int i=1; i <= std::min(config.iter,size(A,1)); i++){ Ap=A*p; if (!config.square) Ap= A._transpose()*Ap; alpha = rsold/(p._transpose()*Ap); x=x+alpha*p; if (config.cg_resid){ t=A*x-b; logstream(LOG_INFO)<<"Iteration " << i << " approximated solution redidual is " << norm(t).toDouble() << std::endl; } r=r-alpha*Ap; rnew=r._transpose()*r; if (sqrt(rnew)<1e-10){ diff = sqrt(rnew).toDouble(); logstream(LOG_INFO)<<" Conjugate gradient converged in iteration "<<i<<" to an accuracy of " << diff << std::endl; break; } p=r+(rnew/rsold)*p; rsold=rnew; } x.to_vec(means); return runtime; }