SLIM  1.0
Sparse Linear Methods (SLIM) for top-n recommender systems
 All Data Structures Files Functions Variables Typedefs Macros Pages
Functions
util.c File Reference

This file contains all the utility routines. More...

#include <slim.h>

Go to the source code of this file.

Functions

ctrl_tcreate_ctrl ()
 Create a ctrl structure wich contains all the default parameters for SLIM.
 
void free_ctrl (ctrl_t *ctrl)
 Free a ctrl structure.
 
void start_timer (ctimer_t *ctimer)
 Start a timer to record current time.
 
void end_timer (ctimer_t *ctimer)
 End a timer to record a length of a duration.
 
void display_timer (ctimer_t *ctimer, char *msg)
 Display a user-defined message and a duration length recorded by a timer.
 
int count_nnz (double *array, int narray)
 Count the number of non-zero values in an array.
 
void find_topk (double *w, int n, int topk, double *map, int *topk2)
 Find the top-k values from an array.
 
void get_column (gk_csr_t *constraint, int i, double *w)
 Get a column from a csr matrix.
 

Detailed Description

This file contains all the utility routines.

Definition in file util.c.

Function Documentation

int count_nnz ( double *  array,
int  narray 
)

Count the number of non-zero values in an array.

Parameters
[in]arrayAn array whose non-zero values will be counted
[in]narrayThe length of the array
Returns
int The number of non-zero values in the array

Definition at line 134 of file util.c.

References EPSILON2.

Referenced by slim_fs_learn().

{
int nnz = 0;
for (int i = 0; i < narray; i ++){
if (array[i] > EPSILON2 || array[i] < -EPSILON2)
nnz ++;
}
return nnz;
}
ctrl_t* create_ctrl ( )

Create a ctrl structure wich contains all the default parameters for SLIM.

Returns
ctrl_t* A pointer to a created ctrl structure

Definition at line 21 of file util.c.

References ctrl_t::beta, ctrl_t::bl, ctrl_t::bsize, ctrl_t::bu, ctrl_t::dbglvl, ctrl_t::endi, ctrl_t::fs, ctrl_t::fs_file, ctrl_t::k, ctrl_t::lambda, ctrl_t::max_bcls_niters, ctrl_t::model_file, ctrl_t::nratings, ctrl_t::optTol, ctrl_t::pred_file, ctrl_t::starti, ctrl_t::test_file, ctrl_t::topn, ctrl_t::train_file, and ctrl_t::transpose.

Referenced by main(), and parse_cmdline().

{
ctrl_t * ctrl = gk_malloc(sizeof(ctrl_t), "malloc ctrl");
ctrl->train_file = NULL;
ctrl->test_file = NULL;
ctrl->model_file = NULL;
ctrl->fs_file = NULL;
ctrl->pred_file = NULL;
ctrl->dbglvl = 0;
ctrl->beta = 1.0;
ctrl->lambda = 1.0;
ctrl->starti = -1;
ctrl->endi = -1;
ctrl->optTol = 1e-5;
ctrl->max_bcls_niters = 100000;
ctrl->bl = 0;
ctrl->bu = 1e20;
ctrl->fs = 0;
ctrl->k = 50;
ctrl->bsize = 1000;
ctrl->nratings = 5;
ctrl->topn = 10;
ctrl->transpose = 0;
return ctrl;
}
void display_timer ( ctimer_t ctimer,
char *  msg 
)

Display a user-defined message and a duration length recorded by a timer.

Parameters
[in]ctimerA timer with a length of a duration recorded
[in]msgA user-defined message to display

Definition at line 116 of file util.c.

References ctimer_t::end, and ctimer_t::start.

Referenced by slim_learn(), and slim_test().

{
printf("----- elapsed CPU time for %s: %f s\n", msg,
(double)(ctimer->end - ctimer->start)/CLOCKS_PER_SEC);
fflush(stdout);
}
void end_timer ( ctimer_t ctimer)

End a timer to record a length of a duration.

Parameters
[in]ctimerA timer to end

Definition at line 101 of file util.c.

References ctimer_t::end.

Referenced by slim_learn(), and slim_test().

{
ctimer->end = clock();
}
void find_topk ( double *  w,
int  n,
int  topk,
double *  map,
int *  topk2 
)

Find the top-k values from an array.

Parameters
[in]wThe array whose top-k values will be found
[in]nThe length of the array w
[in]topkThe number of top values to be found
[out]mapThe array of indices that correspond to the top-k values in the input array
[out]topk2The actual number of top values that are found

Definition at line 159 of file util.c.

Referenced by slim_fs_learn().

{
gk_dkv_t * wkv = gk_malloc(sizeof(gk_dkv_t)*n, "malloc wkv");
int k2 = 0;
for (int i = 0; i < n; i ++){
wkv[i].key = w[i];
wkv[i].val = i;
if (w[i] > 1e-10) k2 ++;
}
/* sort */
gk_dkvsortd(n, wkv);
for (int i = 0; i < ((topk <= k2)? topk:k2); i ++){
map[i] = wkv[i].val;
}
*topk2 = ((topk <= k2)? topk:k2);
gk_free((void **)&wkv, LTERM);
}
void free_ctrl ( ctrl_t ctrl)

Free a ctrl structure.

Parameters
[in]ctrlA pointer to a ctrl structure to be freed

Definition at line 68 of file util.c.

References ctrl_t::fs_file, ctrl_t::model_file, ctrl_t::pred_file, ctrl_t::test_file, and ctrl_t::train_file.

Referenced by main().

{
gk_free((void **)&ctrl->model_file, LTERM);
gk_free((void **)&ctrl->train_file, LTERM);
gk_free((void **)&ctrl->test_file, LTERM);
gk_free((void **)&ctrl->pred_file, LTERM);
gk_free((void **)&ctrl->fs_file, LTERM);
gk_free((void **)&ctrl, LTERM);
}
void get_column ( gk_csr_t *  constraint,
int  i,
double *  w 
)

Get a column from a csr matrix.

Parameters
[in]constraintA matrix from which one column is to be retrievd
[in]iThe index of the column to be retrieved
[out]wThe output vector which saves the retrieved column

Definition at line 194 of file util.c.

Referenced by slim_learn().

{
if (i > constraint->ncols){
gk_dset(constraint->nrows, 0, w);
}
else{
int nnz = constraint->colptr[i+1] - constraint->colptr[i];
for (int j = 0; j < nnz; j ++){
int k = *(constraint->colptr[i] + j + constraint->colind);
w[k] = *(constraint->colptr[i] + j + constraint->colval);
}
}
}
void start_timer ( ctimer_t ctimer)

Start a timer to record current time.

Parameters
[in]ctimerA timer to start

Definition at line 88 of file util.c.

References ctimer_t::start.

Referenced by slim_learn(), and slim_test().

{
ctimer->start = clock();
}