From 47b5cab33083557a52ffea167742db1c9877cf20 Mon Sep 17 00:00:00 2001 From: Gertjan van den Burg Date: Wed, 2 Jul 2014 13:21:20 +0200 Subject: moved realloc around, fix for kernels in trainmsvmmaj and fix memory leak consistency repeats --- src/msvmmaj_init.c | 79 +++++++++++++++++++++++++++++++++++++++++++ src/msvmmaj_train_dataset.c | 81 +-------------------------------------------- src/trainMSVMMaj.c | 9 +++-- src/trainMSVMMajdataset.c | 5 --- 4 files changed, 86 insertions(+), 88 deletions(-) (limited to 'src') diff --git a/src/msvmmaj_init.c b/src/msvmmaj_init.c index dd1fd8f..3950cae 100644 --- a/src/msvmmaj_init.c +++ b/src/msvmmaj_init.c @@ -141,6 +141,85 @@ void msvmmaj_allocate_model(struct MajModel *model) } } +/** + * @brief Reallocate memory for MajModel + * + * @details + * This function can be used to reallocate existing memory for a MajModel, + * upon a change in the model dimensions. This is used in combination with + * kernels. + * + * @param[in] model MajModel to reallocate + * @param[in] n new value of MajModel->n + * @param[in] m new value of MajModel->m + * + */ +void msvmmaj_reallocate_model(struct MajModel *model, long n, long m) +{ + long K = model->K; + + if (model->n == n && model->m == m) + return; + if (model->n != n) { + model->UU = (double *) realloc(model->UU, + n*K*(K-1)*sizeof(double)); + if (model->UU == NULL) { + fprintf(stderr, "Failed to reallocate UU\n"); + exit(1); + } + + model->Q = (double *) realloc(model->Q, n*K*sizeof(double)); + if (model->Q == NULL) { + fprintf(stderr, "Failed to reallocate Q\n"); + exit(1); + } + + model->H = (double *) realloc(model->H, n*K*sizeof(double)); + if (model->H == NULL) { + fprintf(stderr, "Failed to reallocate H\n"); + exit(1); + } + + model->R = (double *) realloc(model->R, n*K*sizeof(double)); + if (model->R == NULL) { + fprintf(stderr, "Failed to reallocate R\n"); + exit(1); + } + + model->rho = (double *) realloc(model->rho, n*sizeof(double)); + if (model->rho == NULL) { + fprintf(stderr, "Failed to reallocte rho\n"); + exit(1); + } + + model->n = n; + } + if (model->m != m) { + model->W = (double *) realloc(model->W, + m*(K-1)*sizeof(double)); + if (model->W == NULL) { + fprintf(stderr, "Failed to reallocate W\n"); + exit(1); + } + + model->V = (double *) realloc(model->V, + (m+1)*(K-1)*sizeof(double)); + if (model->V == NULL) { + fprintf(stderr, "Failed to reallocate V\n"); + exit(1); + } + + model->Vbar = (double *) realloc(model->Vbar, + (m+1)*(K-1)*sizeof(double)); + if (model->Vbar == NULL) { + fprintf(stderr, "Failed to reallocate Vbar\n"); + exit(1); + } + + model->m = m; + } +} + /** * @brief Free allocated MajModel struct * diff --git a/src/msvmmaj_train_dataset.c b/src/msvmmaj_train_dataset.c index 3f8cb74..8d23684 100644 --- a/src/msvmmaj_train_dataset.c +++ b/src/msvmmaj_train_dataset.c @@ -294,7 +294,7 @@ void consistency_repeats(struct Queue *q, long repeats, TrainType traintype) double p, pi, pr, pt, boundary, *time, *std, *mean, *perf; struct Queue *nq = Malloc(struct Queue, 1); struct MajModel *model = msvmmaj_init_model(); - struct Task *task = Malloc(struct Task, 1); + struct Task *task; clock_t loop_s, loop_e; // calculate the performance percentile (Matlab style) @@ -407,7 +407,6 @@ void consistency_repeats(struct Queue *q, long repeats, TrainType traintype) free(nq->tasks); free(nq); - free(task); free(model); free(perf); free(std); @@ -450,7 +449,6 @@ double cross_validation(struct MajModel *model, struct MajData *data, msvmmaj_make_cv_split(data->n, folds, cv_idx); for (f=0; fK; - - if (model->n == n && model->m == m) - return; - if (model->n != n) { - model->UU = (double *) realloc(model->UU, - n*K*(K-1)*sizeof(double)); - if (model->UU == NULL) { - fprintf(stderr, "Failed to reallocate UU\n"); - exit(1); - } - - model->Q = (double *) realloc(model->Q, n*K*sizeof(double)); - if (model->Q == NULL) { - fprintf(stderr, "Failed to reallocate Q\n"); - exit(1); - } - - model->H = (double *) realloc(model->H, n*K*sizeof(double)); - if (model->H == NULL) { - fprintf(stderr, "Failed to reallocate H\n"); - exit(1); - } - - model->R = (double *) realloc(model->R, n*K*sizeof(double)); - if (model->R == NULL) { - fprintf(stderr, "Failed to reallocate R\n"); - exit(1); - } - - model->rho = (double *) realloc(model->rho, n*sizeof(double)); - if (model->rho == NULL) { - fprintf(stderr, "Failed to reallocte rho\n"); - exit(1); - } - - model->n = n; - } - if (model->m != m) { - model->W = (double *) realloc(model->W, - m*(K-1)*sizeof(double)); - if (model->W == NULL) { - fprintf(stderr, "Failed to reallocate W\n"); - exit(1); - } - - model->V = (double *) realloc(model->V, - (m+1)*(K-1)*sizeof(double)); - if (model->V == NULL) { - fprintf(stderr, "Failed to reallocate V\n"); - exit(1); - } - - model->Vbar = (double *) realloc(model->Vbar, - (m+1)*(K-1)*sizeof(double)); - if (model->Vbar == NULL) { - fprintf(stderr, "Failed to reallocate Vbar\n"); - exit(1); - } - - model->m = m; - } -} - /** * @brief Run the grid search for a train/test dataset * @@ -707,24 +639,13 @@ void start_training_tt(struct Queue *q) */ void free_queue(struct Queue *q) { - printf("\there 0\n"); long i; - printf("\there 1\n"); for (i=0; iN; i++) { - printf("\there 2\n"); - fflush(stdout); free(q->tasks[i]->kernelparam); - printf("\there 3\n"); - fflush(stdout); free(q->tasks[i]); - printf("\there 4\n"); - fflush(stdout); } - printf("\there 5\n"); free(q->tasks); - printf("\there 6\n"); free(q); - printf("\there 7\n"); } /** diff --git a/src/trainMSVMMaj.c b/src/trainMSVMMaj.c index 66f6450..af07fe7 100644 --- a/src/trainMSVMMaj.c +++ b/src/trainMSVMMaj.c @@ -95,12 +95,15 @@ int main(int argc, char **argv) model->m = data->m; model->K = data->K; model->data_file = input_filename; - + + // allocate model + msvmmaj_allocate_model(model); + // initialize kernel (if necessary) msvmmaj_make_kernel(model, data); - // allocate model and initialize weights - msvmmaj_allocate_model(model); + // reallocate model and initialize weights + msvmmaj_reallocate_model(model, data->n, data->m); msvmmaj_initialize_weights(data, model); // seed the random number generator (only place in programs is in diff --git a/src/trainMSVMMajdataset.c b/src/trainMSVMMajdataset.c index a6f3e87..b9d9180 100644 --- a/src/trainMSVMMajdataset.c +++ b/src/trainMSVMMajdataset.c @@ -111,15 +111,10 @@ int main(int argc, char **argv) consistency_repeats(q, training->repeats, training->traintype); } - printf("here 0\n"); free_queue(q); - printf("here 1\n"); free(training); - printf("here 2\n"); msvmmaj_free_data(train_data); - printf("here 3\n"); msvmmaj_free_data(test_data); - printf("here 4\n"); note("Done.\n"); return 0; -- cgit v1.2.3