Skip to content

Commit

Permalink
Fix memory leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
Jana Pazurikova committed Jun 2, 2016
1 parent 3e444df commit adfc0fa
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
17 changes: 11 additions & 6 deletions src/diffevolution.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void run_diff_evolution(struct subset * const ss) {

//minimize part of population
int minimized_initial = 0;
int* good_indices = (int*) malloc(s.population_size*sizeof(int));
int* good_indices = (int*) calloc(s.population_size, sizeof(int));
for (i = 0; i < s.population_size; i++)
good_indices[i] = -1;
if (s.polish > 2) {
Expand Down Expand Up @@ -114,8 +114,8 @@ void run_diff_evolution(struct subset * const ss) {
printf(".");
}
/* Select randomly two points from population */
int rand1 = good_indices[(int)(floor(get_random_float(0, (float) minimized_initial)))-1];
int rand2 = good_indices[(int)(floor(get_random_float(0, (float) minimized_initial)))-1];
int rand1 = good_indices[(int)(floor(get_random_float(0, (float) minimized_initial)))];
int rand2 = good_indices[(int)(floor(get_random_float(0, (float) minimized_initial)))];

struct kappa_data* a = &(ss->data[rand1]);
struct kappa_data* b = &(ss->data[rand2]);
Expand Down Expand Up @@ -160,18 +160,18 @@ void run_diff_evolution(struct subset * const ss) {
minimize_locally(min_trial, 500);
calculate_charges(de_ss, min_trial);
calculate_statistics(de_ss, min_trial);
int cond = 0;
/* If better, swap for so_far_best */
#pragma omp critical
{
if (kd_sort_by_is_better(min_trial, trial) && compare_and_set(min_trial, so_far_best)) {
cond = (kd_sort_by_is_better(min_trial, trial) && compare_and_set(min_trial, so_far_best));
if (cond) {
calculate_charges(ss, so_far_best);
calculate_statistics(ss, so_far_best);
if(s.verbosity >= VERBOSE_KAPPA) {
printf("\n");
kd_print_results(so_far_best);
}
}
}
kd_destroy(min_trial);
free(min_trial);
}
Expand All @@ -194,6 +194,8 @@ void run_diff_evolution(struct subset * const ss) {
if (s.verbosity >= VERBOSE_KAPPA) {
printf("Out of %d iterations, we minimized %d trials.\n", s.limit_de_iters, minimized);
}
kd_destroy(so_far_best);
free(so_far_best);
}

/* Generate random population by Latin HyperCube Sampling */
Expand Down Expand Up @@ -222,6 +224,7 @@ void generate_random_population(struct subset* ss, float *bounds, int size) {
else
ss->data[i].kappa = s.fixed_kappa;
}
free(random_lhs);

}

Expand Down Expand Up @@ -321,6 +324,8 @@ void minimize_locally(struct kappa_data* t, int max_calls) {
//call fortran code NEWUOA for local minimization
newuoa_(&n, &npt, x, &rhobeg, &rhoend, &iprint, &maxfun, w);
double_array_to_kappa_data(x, t);
free(w);
free(x);
}

/* Used by NEWUOA algorithm. Evaluates the vector in the local minimization: converts it to kappa_data, computes charges, computes statistics and return the fitness score that should be minimized */
Expand Down
5 changes: 3 additions & 2 deletions src/eem.c
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,9 @@ void calculate_charges(struct subset * const ss, struct kappa_data * const kd) {
starts[0] = 0;
for(int i = 1; i < ts.molecules_count; i++)
starts[i] = starts[i - 1] + ts.molecules[i - 1].atoms_count;

#pragma omp parallel for num_threads(ts.molecules_count < s.max_threads/s.de_threads ? ts.molecules_count : s.max_threads/s.de_threads)
int nt = s.max_threads/s.de_threads;
int nthreads = ts.molecules_count < nt ? ts.molecules_count : nt;
#pragma omp parallel for num_threads(nthreads)
for(int i = 0; i < ts.molecules_count; i++) {
#define MOLECULE ts.molecules[i]
const long int n = MOLECULE.atoms_count;
Expand Down

0 comments on commit adfc0fa

Please sign in to comment.