41 #include "matrixmarket/mmio.h"
42 #include "matrixmarket/mmio.c"
49 #include "Eigen/Dense"
50 #define EIGEN_YES_I_KNOW_SPARSE_MODULE_IS_NOT_STABLE_YET
51 #include "Eigen/Sparse"
52 #include "Eigen/Cholesky"
53 #include "Eigen/Eigenvalues"
55 using namespace Eigen;
60 typedef VectorXi ivec;
61 typedef MatrixXi imat;
62 typedef SparseVector<double> sparse_vec;
64 using namespace graphchi;
68 #define NLATENT 5 // Dimension of the latent factors. You can specify this in compile time as well (in make).
71 double LAMBDA = 0.065;
80 vid_t max_left_vertex =0 ;
81 vid_t max_right_vertex = 0;
90 for(
int k=0; k < NLATENT; k++) d[k] = 0.001 * (std::rand() % 1000);
93 double & operator[] (
int idx) {
97 for(
int i=0; i<NLATENT; i++) {
if (d[i] != oth.d[i])
return true; }
103 for(
int i=0; i<NLATENT; i++) x+= oth.d[i]*d[i];
130 template <
typename als_edge_type>
142 if ((nshards = find_shards<als_edge_type>(base_filename, get_option_string(
"nshards",
"auto")))) {
143 logstream(
LOG_INFO) <<
"File " << base_filename <<
" was already preprocessed, won't do it again. " << std::endl;
144 logstream(
LOG_INFO) <<
"If this is not intended, please delete the shard files and try again. " << std::endl;
152 if ((f = fopen(base_filename.c_str(),
"r")) == NULL) {
153 logstream(
LOG_ERROR) <<
"Could not open file: " << base_filename <<
", error: " << strerror(errno) << std::endl;
158 if (mm_read_banner(f, &matcode) != 0)
160 logstream(
LOG_ERROR) <<
"Could not process Matrix Market banner. File: " << base_filename << std::endl;
161 logstream(
LOG_ERROR) <<
"Matrix must be in the Matrix Market format. " << std::endl;
169 if (mm_is_complex(matcode) || !mm_is_sparse(matcode))
171 logstream(
LOG_ERROR) <<
"Sorry, this application does not support complex values and requires a sparse matrix." << std::endl;
172 logstream(
LOG_ERROR) <<
"Market Market type: " << mm_typecode_to_str(matcode) << std::endl;
178 if ((ret_code = mm_read_mtx_crd_size(f, &M, &N, &nz)) !=0) {
179 logstream(
LOG_ERROR) <<
"Failed reading matrix size: error=" << ret_code << std::endl;
184 logstream(
LOG_INFO) <<
"Starting to read matrix-market input. Matrix dimensions: "
185 << M <<
" x " << N <<
", non-zeros: " << nz << std::endl;
187 if (M < 5 || N < 5 || nz < 10) {
188 logstream(
LOG_ERROR) <<
"File is suspiciously small. Something wrong? File: " << base_filename << std::endl;
189 assert(M < 5 || N < 5 || nz < 10);
194 for (
int i=0; i<nz; i++)
198 fscanf(f,
"%d %d %lg\n", &I, &J, &val);
207 logstream(
LOG_INFO) <<
"Matrix already preprocessed, just run sharder." << std::endl;
209 if (f !=stdin) fclose(f);
212 logstream(
LOG_INFO) <<
"Now creating shards." << std::endl;
215 nshards = sharderobj.
execute_sharding(get_option_string(
"nshards",
"auto"));
224 mm_initialize_typecode(&matcode);
225 mm_set_matrix(&matcode);
226 mm_set_array(&matcode);
227 mm_set_real(&matcode);
229 outf = fopen(fname.c_str(),
"w");
230 assert(outf != NULL);
231 mm_write_banner(outf, matcode);
232 mm_write_mtx_array_size(outf, NLATENT, nvertices);
236 for(
int i=0; i < NLATENT; i++) {
237 fprintf(outf,
"%lf\n", vec.d[i]);
242 if (outf != NULL) fclose(outf);
247 void output_als_result(std::string filename, vid_t numvertices, vid_t max_left_vertex) {
248 MMOutputter mmoutput_left(filename +
"_U.mm", max_left_vertex + 1);
249 foreach_vertices<latentvec_t>(filename, 0, max_left_vertex + 1, mmoutput_left);
252 MMOutputter mmoutput_right(filename +
"_V.mm", numvertices - max_left_vertex - 1);
253 foreach_vertices<latentvec_t>(filename, max_left_vertex + 1, numvertices, mmoutput_right);
254 logstream(
LOG_INFO) <<
"ALS output files (in matrix market format): " << filename +
"_U.mm" <<
255 ", " << filename +
"_V.mm" << std::endl;