ECC-604: Pass file to test. Fix memory leaks

This commit is contained in:
Shahram Najm 2018-05-01 14:24:42 +01:00
parent 91efbc51b9
commit 238924e427
2 changed files with 39 additions and 34 deletions

View File

@ -7,43 +7,35 @@
#include "grib_api.h" #include "grib_api.h"
#define NUM_THREADS 8 #define NUM_THREADS 8
#define FILES_PER_ITERATION 300 #define FILES_PER_ITERATION 200
#define TEMPLATE "../../share/eccodes/samples/gg_sfc_grib2.tmpl" static char* INPUT_FILE = NULL;
static int encode_file(char *template_file, char *output_file) static int encode_file(char *template_file, char *output_file)
{ {
FILE *in = NULL; FILE *in, *out;
FILE *out = NULL;
grib_handle *source_handle = NULL; grib_handle *source_handle = NULL;
const void *buffer = NULL; const void *buffer = NULL;
size_t size = 0; size_t size = 0;
int err = 0; int err = 0;
double *values;
in = fopen(template_file,"r"); assert(in); in = fopen(template_file,"r"); assert(in);
out = fopen(output_file,"w"); assert(out); out = fopen(output_file,"w"); assert(out);
/* loop over the messages in the source GRIB and clone them */ /* loop over the messages in the source GRIB and clone them */
while ((source_handle = grib_handle_new_from_file(0, in, &err))!=NULL) 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);
int i; int i;
double *values = NULL;
values = (double*)malloc(values_len*sizeof(double));
long count; long count;
double d,e; 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; d=10e-8;
e=d; e=d;
count=1; count=1;
@ -67,6 +59,7 @@ static int encode_file(char *template_file, char *output_file)
} }
grib_handle_delete(clone_handle); grib_handle_delete(clone_handle);
grib_handle_delete(source_handle); grib_handle_delete(source_handle);
free(values);
} }
fclose(out); fclose(out);
@ -89,8 +82,14 @@ int main(int argc, char **argv)
{ {
int i; int i;
int thread_counter = 0; 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) { if (parallel) {
printf("Running parallel in %d threads.\n", NUM_THREADS); printf("Running parallel in %d threads.\n", NUM_THREADS);
} else { } else {
@ -101,7 +100,6 @@ int main(int argc, char **argv)
/* We have to create M * N worker threads */ /* We have to create M * N worker threads */
for (i = 0; i < NUM_THREADS; i++) { for (i = 0; i < NUM_THREADS; i++) {
struct v *data = (struct v *) malloc(sizeof(struct v)); struct v *data = (struct v *) malloc(sizeof(struct v));
data->number = i; data->number = i;
data->data = NULL; data->data = NULL;
@ -116,10 +114,8 @@ int main(int argc, char **argv)
} }
} }
/* Waiting for threads to complete */
if (parallel) { if (parallel) {
for (i = 0; i < NUM_THREADS; i++) for (i = 0; i < NUM_THREADS; i++) {
{
pthread_join(workers[i], NULL); pthread_join(workers[i], NULL);
} }
} }
@ -142,7 +138,7 @@ void do_stuff(void *ptr)
for (int i=0; i<FILES_PER_ITERATION;i++) { for (int i=0; i<FILES_PER_ITERATION;i++) {
sprintf(output_file,"output/output_file_%d-%d.grib",data->number,i); sprintf(output_file,"output/output_file_%d-%d.grib",data->number,i);
encode_file(TEMPLATE,output_file); encode_file(INPUT_FILE,output_file);
} }
time_t ltime; time_t ltime;
@ -151,8 +147,8 @@ void do_stuff(void *ptr)
ltime = time(NULL); ltime = time(NULL);
localtime_r(&ltime, &result); localtime_r(&ltime, &result);
strftime(stime, 32, "%H:%M:%S", &result); // Try to get milliseconds here too strftime(stime, 32, "%H:%M:%S", &result); /* Try to get milliseconds here too*/
// asctime_r(&result, stime); /* asctime_r(&result, stime); */
printf("%s: Worker %d finished.\n", stime,data->number); printf("%s: Worker %d finished.\n", stime,data->number);
} }

View File

@ -19,8 +19,17 @@ cd $temp_dir
OUTPUT=output OUTPUT=output
mkdir -p $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 #num=0
#for ofile in $OUTPUT/output_file_*grib; do #for ofile in $OUTPUT/output_file_*grib; do
# ${tools_dir}/grib_compare -H $ECCODES_SAMPLES_PATH/gg_sfc_grib2.tmpl $ofile # ${tools_dir}/grib_compare -H $ECCODES_SAMPLES_PATH/gg_sfc_grib2.tmpl $ofile