diff options
| author | Gertjan van den Burg <burg@ese.eur.nl> | 2014-07-02 13:21:20 +0200 |
|---|---|---|
| committer | Gertjan van den Burg <burg@ese.eur.nl> | 2014-07-02 13:21:20 +0200 |
| commit | 47b5cab33083557a52ffea167742db1c9877cf20 (patch) | |
| tree | 04f906ea5c871d86966a01b1f634e4c9ea3491f6 | |
| parent | fix in grid creation (diff) | |
| download | gensvm-47b5cab33083557a52ffea167742db1c9877cf20.tar.gz gensvm-47b5cab33083557a52ffea167742db1c9877cf20.zip | |
moved realloc around, fix for kernels in trainmsvmmaj and fix memory leak consistency repeats
| -rw-r--r-- | include/msvmmaj_init.h | 1 | ||||
| -rw-r--r-- | include/msvmmaj_train_dataset.h | 2 | ||||
| -rw-r--r-- | src/msvmmaj_init.c | 79 | ||||
| -rw-r--r-- | src/msvmmaj_train_dataset.c | 81 | ||||
| -rw-r--r-- | src/trainMSVMMaj.c | 9 | ||||
| -rw-r--r-- | src/trainMSVMMajdataset.c | 5 |
6 files changed, 87 insertions, 90 deletions
diff --git a/include/msvmmaj_init.h b/include/msvmmaj_init.h index febfb4a..281214c 100644 --- a/include/msvmmaj_init.h +++ b/include/msvmmaj_init.h @@ -21,6 +21,7 @@ struct MajModel *msvmmaj_init_model(); struct MajData *msvmmaj_init_data(); void msvmmaj_allocate_model(struct MajModel *model); +void msvmmaj_reallocate_model(struct MajModel *model, long n, long m); void msvmmaj_free_model(struct MajModel *model); void msvmmaj_free_data(struct MajData *data); diff --git a/include/msvmmaj_train_dataset.h b/include/msvmmaj_train_dataset.h index 0889626..95e2c74 100644 --- a/include/msvmmaj_train_dataset.h +++ b/include/msvmmaj_train_dataset.h @@ -135,7 +135,5 @@ double cross_validation(struct MajModel *model, struct MajData *data, void make_model_from_task(struct Task *task, struct MajModel *model); void copy_model(struct MajModel *from, struct MajModel *to); -void msvmmaj_reallocate_model(struct MajModel *model, long n, long m); - void print_progress_string(struct Task *task, long N); #endif 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 @@ -142,6 +142,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 * * @details 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; f<folds; f++) { - note("."); msvmmaj_get_tt_split(data, train_data, test_data, cv_idx, f); msvmmaj_make_kernel(model, train_data); @@ -542,72 +540,6 @@ void start_training_cv(struct Queue *q) msvmmaj_free_model(model); } -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 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; i<q->N; 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; |
