From 238924e4276f240a1a38c6e847c6bb16bec0000b Mon Sep 17 00:00:00 2001 From: Shahram Najm Date: Tue, 1 May 2018 14:24:42 +0100 Subject: [PATCH] ECC-604: Pass file to test. Fix memory leaks --- tests/grib_ecc-604.c | 62 ++++++++++++++++++++----------------------- tests/grib_ecc-604.sh | 11 +++++++- 2 files changed, 39 insertions(+), 34 deletions(-) diff --git a/tests/grib_ecc-604.c b/tests/grib_ecc-604.c index 3ed17e8c9..433108834 100644 --- a/tests/grib_ecc-604.c +++ b/tests/grib_ecc-604.c @@ -7,43 +7,35 @@ #include "grib_api.h" #define NUM_THREADS 8 -#define FILES_PER_ITERATION 300 -#define TEMPLATE "../../share/eccodes/samples/gg_sfc_grib2.tmpl" - +#define FILES_PER_ITERATION 200 +static char* INPUT_FILE = NULL; static int encode_file(char *template_file, char *output_file) { - FILE *in = NULL; - FILE *out = NULL; + FILE *in, *out; grib_handle *source_handle = NULL; const void *buffer = NULL; size_t size = 0; int err = 0; + double *values; in = fopen(template_file,"r"); assert(in); out = fopen(output_file,"w"); assert(out); /* loop over the messages in the source GRIB and clone them */ - while ((source_handle = grib_handle_new_from_file(0, in, &err))!=NULL) - { - grib_handle *clone_handle = grib_handle_clone(source_handle); - - if (clone_handle == NULL) { - perror("ERROR: could not clone field"); - return 1; - } - - //GRIB_CHECK(grib_set_long(clone_handle, "centre", 250),0); - - size_t values_len= 0; - GRIB_CHECK(grib_get_size(clone_handle, "values", &values_len),0); - + while ((source_handle = grib_handle_new_from_file(0, in, &err))!=NULL) { int i; - double *values = NULL; - values = (double*)malloc(values_len*sizeof(double)); long count; double d,e; + size_t values_len= 0; + grib_handle *clone_handle = grib_handle_clone(source_handle); + assert(clone_handle); + + /*GRIB_CHECK(grib_set_long(clone_handle, "centre", 250),0);*/ + GRIB_CHECK(grib_get_size(clone_handle, "values", &values_len),0); + + values = (double*)malloc(values_len*sizeof(double)); d=10e-8; e=d; count=1; @@ -67,6 +59,7 @@ static int encode_file(char *template_file, char *output_file) } grib_handle_delete(clone_handle); grib_handle_delete(source_handle); + free(values); } fclose(out); @@ -79,7 +72,7 @@ void do_stuff(void *data); /* Structure for passing data to threads */ struct v { - int number; + int number; char *data; }; @@ -89,8 +82,14 @@ int main(int argc, char **argv) { int i; int thread_counter = 0; - int parallel = !(argc>1 && strcmp(argv[1],"seq")==0); - + int parallel = 1; + if (argc<2) { + return 1; + } + INPUT_FILE= argv[1]; + if (argc>2 && strcmp(argv[2],"seq")==0) { + parallel = 0; + } if (parallel) { printf("Running parallel in %d threads.\n", NUM_THREADS); } else { @@ -101,7 +100,6 @@ int main(int argc, char **argv) /* We have to create M * N worker threads */ for (i = 0; i < NUM_THREADS; i++) { - struct v *data = (struct v *) malloc(sizeof(struct v)); data->number = i; data->data = NULL; @@ -116,10 +114,8 @@ int main(int argc, char **argv) } } - /* Waiting for threads to complete */ if (parallel) { - for (i = 0; i < NUM_THREADS; i++) - { + for (i = 0; i < NUM_THREADS; i++) { pthread_join(workers[i], NULL); } } @@ -128,8 +124,8 @@ int main(int argc, char **argv) } void *runner(void *ptr) -{ - do_stuff(ptr); +{ + do_stuff(ptr); pthread_exit(0); } @@ -142,7 +138,7 @@ void do_stuff(void *ptr) for (int i=0; inumber,i); - encode_file(TEMPLATE,output_file); + encode_file(INPUT_FILE,output_file); } time_t ltime; @@ -151,8 +147,8 @@ void do_stuff(void *ptr) ltime = time(NULL); localtime_r(<ime, &result); - strftime(stime, 32, "%H:%M:%S", &result); // Try to get milliseconds here too - // asctime_r(&result, stime); + strftime(stime, 32, "%H:%M:%S", &result); /* Try to get milliseconds here too*/ + /* asctime_r(&result, stime); */ printf("%s: Worker %d finished.\n", stime,data->number); } diff --git a/tests/grib_ecc-604.sh b/tests/grib_ecc-604.sh index e8bae65c0..3fe6e14b9 100755 --- a/tests/grib_ecc-604.sh +++ b/tests/grib_ecc-604.sh @@ -19,8 +19,17 @@ cd $temp_dir OUTPUT=output mkdir -p $OUTPUT -time ${test_dir}/grib_ecc-604 +input=$ECCODES_SAMPLES_PATH/gg_sfc_grib2.tmpl +time ${test_dir}/grib_ecc-604 $input +# Run with forge +# ----------------- +# module swap forge/18.0.1 +# map ${test_dir}/grib_ecc-604 $input & +# + +# Validate results +# ----------------- #num=0 #for ofile in $OUTPUT/output_file_*grib; do # ${tools_dir}/grib_compare -H $ECCODES_SAMPLES_PATH/gg_sfc_grib2.tmpl $ofile