SLIM  1.0
Sparse Linear Methods (SLIM) for top-n recommender systems
 All Data Structures Files Functions Variables Typedefs Macros Pages
util.c
Go to the documentation of this file.
1 /**************************************************************/
2 /*! \file
3 
4  \brief This file contains all the utility routines.
5 */
6 /**************************************************************/
7 
8 #include<slim.h>
9 
10 
11 
12 
13 /**************************************************************/
14 /*!
15  \brief Create a ctrl structure wich contains all the default
16  parameters for SLIM
17 
18  \return ctrl_t* A pointer to a created ctrl structure
19 */
20 /**************************************************************/
22 
23  ctrl_t * ctrl = gk_malloc(sizeof(ctrl_t), "malloc ctrl");
24 
25  ctrl->train_file = NULL;
26  ctrl->test_file = NULL;
27  ctrl->model_file = NULL;
28  ctrl->fs_file = NULL;
29  ctrl->pred_file = NULL;
30 
31  ctrl->dbglvl = 0;
32 
33  ctrl->beta = 1.0;
34  ctrl->lambda = 1.0;
35 
36  ctrl->starti = -1;
37  ctrl->endi = -1;
38 
39  ctrl->optTol = 1e-5;
40  ctrl->max_bcls_niters = 100000;
41 
42  ctrl->bl = 0;
43  ctrl->bu = 1e20;
44 
45  ctrl->fs = 0;
46 
47  ctrl->k = 50;
48 
49  ctrl->bsize = 1000;
50 
51  ctrl->nratings = 5;
52 
53  ctrl->topn = 10;
54 
55  ctrl->transpose = 0;
56 
57  return ctrl;
58 
59 }
60 
61 /**************************************************************/
62 /*!
63  \brief Free a ctrl structure
64 
65  \param[in] ctrl A pointer to a ctrl structure to be freed
66 */
67 /**************************************************************/
68 void free_ctrl(ctrl_t * ctrl){
69 
70  gk_free((void **)&ctrl->model_file, LTERM);
71  gk_free((void **)&ctrl->train_file, LTERM);
72  gk_free((void **)&ctrl->test_file, LTERM);
73  gk_free((void **)&ctrl->pred_file, LTERM);
74  gk_free((void **)&ctrl->fs_file, LTERM);
75 
76  gk_free((void **)&ctrl, LTERM);
77 
78 }
79 
80 
81 /**************************************************************/
82 /*!
83  \brief Start a timer to record current time
84 
85  \param[in] ctimer A timer to start
86 */
87 /**************************************************************/
88 void start_timer(ctimer_t * ctimer){
89 
90  ctimer->start = clock();
91 
92 }
93 
94 /**************************************************************/
95 /*!
96  \brief End a timer to record a length of a duration
97 
98  \param[in] ctimer A timer to end
99  */
100 /**************************************************************/
101 void end_timer(ctimer_t * ctimer){
102 
103  ctimer->end = clock();
104 
105 }
106 
107 /**************************************************************/
108 /*!
109  \brief Display a user-defined message and a duration length
110  recorded by a timer
111 
112  \param[in] ctimer A timer with a length of a duration recorded
113  \param[in] msg A user-defined message to display
114 */
115 /**************************************************************/
116 void display_timer(ctimer_t * ctimer, char * msg){
117 
118  printf("----- elapsed CPU time for %s: %f s\n", msg,
119  (double)(ctimer->end - ctimer->start)/CLOCKS_PER_SEC);
120  fflush(stdout);
121 
122 }
123 
124 /**************************************************************/
125 /*!
126  \brief Count the number of non-zero values in an array
127 
128  \param[in] array An array whose non-zero values will be
129  counted
130  \param[in] narray The length of the array
131  \return int The number of non-zero values in the array
132 */
133 /**************************************************************/
134 int count_nnz(double * array, int narray){
135 
136  int nnz = 0;
137 
138  for (int i = 0; i < narray; i ++){
139  if (array[i] > EPSILON2 || array[i] < -EPSILON2)
140  nnz ++;
141  }
142 
143  return nnz;
144 
145 }
146 
147 /**************************************************************/
148 /*!
149  \brief Find the top-k values from an array
150 
151  \param[in] w The array whose top-k values will be found
152  \param[in] n The length of the array w
153  \param[in] topk The number of top values to be found
154  \param[out] map The array of indices that correspond to the
155  top-k values in the input array
156  \param[out] topk2 The actual number of top values that are found
157  */
158 /**************************************************************/
159 void find_topk(double * w, int n, int topk, double * map, int * topk2){
160 
161 
162  gk_dkv_t * wkv = gk_malloc(sizeof(gk_dkv_t)*n, "malloc wkv");
163  int k2 = 0;
164 
165  for (int i = 0; i < n; i ++){
166  wkv[i].key = w[i];
167  wkv[i].val = i;
168  if (w[i] > 1e-10) k2 ++;
169  }
170 
171  /* sort */
172  gk_dkvsortd(n, wkv);
173 
174  for (int i = 0; i < ((topk <= k2)? topk:k2); i ++){
175  map[i] = wkv[i].val;
176  }
177 
178  *topk2 = ((topk <= k2)? topk:k2);
179  gk_free((void **)&wkv, LTERM);
180 
181 }
182 
183 /**************************************************************/
184 /*!
185  \brief Get a column from a csr matrix
186 
187  \param[in] constraint A matrix from which one column is to
188  be retrievd
189  \param[in] i The index of the column to be retrieved
190  \param[out] w The output vector which saves the
191  retrieved column
192 */
193 /**************************************************************/
194 void get_column(gk_csr_t * constraint, int i, double * w){
195 
196  if (i > constraint->ncols){
197  gk_dset(constraint->nrows, 0, w);
198  }
199  else{
200  int nnz = constraint->colptr[i+1] - constraint->colptr[i];
201  for (int j = 0; j < nnz; j ++){
202  int k = *(constraint->colptr[i] + j + constraint->colind);
203  w[k] = *(constraint->colptr[i] + j + constraint->colval);
204  }
205  }
206 }
207