diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 0e758efcc..f7c1c7e93 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -217,9 +217,15 @@ if( ENABLE_EXTRA_TESTS AND HAVE_ECCODES_THREADS ) TYPE SCRIPT COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/grib_encode_pthreads.sh ) - ecbuild_add_executable( TARGET grib_ecc-604 + + ecbuild_add_executable( TARGET grib_ecc-604-1 NOINSTALL - SOURCES grib_ecc-604.c + SOURCES grib_ecc-604-1.c + LIBS eccodes + ) + ecbuild_add_executable( TARGET grib_ecc-604-2 + NOINSTALL + SOURCES grib_ecc-604-2.c LIBS eccodes ) ecbuild_add_test( TARGET eccodes_t_grib_ecc-604 diff --git a/tests/grib_ecc-604.c b/tests/grib_ecc-604-1.c similarity index 99% rename from tests/grib_ecc-604.c rename to tests/grib_ecc-604-1.c index f1e3a677b..fb4f72982 100644 --- a/tests/grib_ecc-604.c +++ b/tests/grib_ecc-604-1.c @@ -1,3 +1,6 @@ +/* + * Test for ECC-604: + */ #include #include #include diff --git a/tests/grib_ecc-604-2.c b/tests/grib_ecc-604-2.c new file mode 100644 index 000000000..331edbf5d --- /dev/null +++ b/tests/grib_ecc-604-2.c @@ -0,0 +1,156 @@ +/* + * Test for ECC-604: This version does not clone the GRIB handle + */ + +#include +#include +#include + +#include "grib_api.h" + +static size_t NUM_THREADS = 8; +static size_t FILES_PER_ITERATION = 300; +static char* INPUT_FILE = NULL; + +static int encode_file(char *template_file, char *output_file) +{ + 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 */ + while ((source_handle = grib_handle_new_from_file(0, in, &err))!=NULL) { + int i; + long count; + double d,e; + size_t values_len= 0; + + /*GRIB_CHECK(grib_set_long(source_handle, "centre", 250),0);*/ + GRIB_CHECK(grib_get_size(source_handle, "values", &values_len),0); + + values = (double*)malloc(values_len*sizeof(double)); + d=10e-8; + e=d; + count=1; + for (i=0;i100) {e*=10; count=1;} + values[i]=d; + d+=e; + count++; + } + + GRIB_CHECK(grib_set_long(source_handle,"bitsPerValue",16),0); + + /* set data values */ + GRIB_CHECK(grib_set_double_array(source_handle,"values",values,values_len),0); + + GRIB_CHECK(grib_get_message(source_handle,&buffer,&size),0); + if(fwrite(buffer,1,size,out) != size) { + perror(output_file); + return 1; + } + grib_handle_delete(source_handle); + free(values); + } + + fclose(out); + fclose(in); + + return 0; +} + +void do_stuff(void *data); + +/* Structure for passing data to threads */ +struct v { + size_t number; + char *data; +}; + +void *runner(void *ptr); /* the thread */ + +int main(int argc, char **argv) +{ + size_t i; + int thread_counter = 0; + int parallel = 1; + const char* prog = argv[0]; + char* mode; + if (argc!=5) { + fprintf(stderr, "Usage:\n\t%s seq file numRuns numIter\nOr\n\t%s par file numThreads numIter\n", prog, prog); + return 1; + } + mode = argv[1]; + INPUT_FILE= argv[2]; + NUM_THREADS = atol(argv[3]); + FILES_PER_ITERATION = atol(argv[4]); + + if (strcmp(mode,"seq")==0) { + parallel = 0; + } + if (parallel) { + printf("Running parallel in %ld threads. %ld iterations\n", NUM_THREADS, FILES_PER_ITERATION); + } else { + printf("Running sequentially in %ld runs. %ld iterations\n", NUM_THREADS, FILES_PER_ITERATION); + } + + pthread_t workers[NUM_THREADS]; + for (i = 0; i < NUM_THREADS; i++) { + struct v *data = (struct v *) malloc(sizeof(struct v)); + data->number = i; + data->data = NULL; + + if (parallel) { + /* Now we will create the thread passing it data as an argument */ + pthread_create(&workers[thread_counter], NULL, runner, data); + /*pthread_join(workers[thread_counter], NULL);*/ + thread_counter++; + } else { + do_stuff(data); + } + } + + if (parallel) { + for (i = 0; i < NUM_THREADS; i++) { + pthread_join(workers[i], NULL); + } + } + + return 0; +} + +void *runner(void *ptr) +{ + do_stuff(ptr); + pthread_exit(0); +} + +void do_stuff(void *ptr) +{ + /* Cast argument to struct v pointer */ + struct v *data = ptr; + size_t i; + char output_file[50]; + + for (i=0; inumber, i); + encode_file(INPUT_FILE,output_file); + } + + time_t ltime; + struct tm result; + char stime[32]; + + ltime = time(NULL); + localtime_r(<ime, &result); + strftime(stime, 32, "%H:%M:%S", &result); /* Try to get milliseconds here too*/ + /* asctime_r(&result, stime); */ + + printf("%s: Worker %ld finished.\n", stime, data->number); +} diff --git a/tests/grib_ecc-604.sh b/tests/grib_ecc-604.sh index 52c7be28d..3f0c3bf3f 100755 --- a/tests/grib_ecc-604.sh +++ b/tests/grib_ecc-604.sh @@ -11,41 +11,37 @@ . ./include.sh label="grib_ecc-604" - temp_dir=tempdir.${label} -rm -fr $temp_dir -mkdir -p $temp_dir -cd $temp_dir + +validate() +{ + echo "Checking every output file is identical..." + set +x + res=`cksum $OUTPUT/output_file_* | awk '{print $1}' | sort -u` + set -x + [ "$res" = "2572910830" ] +} NUM_THREADS=8 NUM_ITER=300 OUTPUT=output -mkdir -p $OUTPUT input=$ECCODES_SAMPLES_PATH/gg_sfc_grib2.tmpl -time ${test_dir}/grib_ecc-604 par $input $NUM_THREADS $NUM_ITER -# Run with forge -# ----------------- -# module swap forge/18.0.1 -# map ${test_dir}/grib_ecc-604 par $input $NUM_THREADS $NUM_ITER & -# +rm -fr $temp_dir +mkdir -p $temp_dir +cd $temp_dir + +# Test 01 +mkdir -p $OUTPUT +time ${test_dir}/grib_ecc-604-1 par $input $NUM_THREADS $NUM_ITER +validate + +# Test 02 +rm -fr $OUTPUT +mkdir -p $OUTPUT +time ${test_dir}/grib_ecc-604-2 par $input $NUM_THREADS $NUM_ITER +validate -# 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 -# num=`expr $num + 1` -# # If there are too many output files, comparing each one will take a long time! -# if [ $num -gt 500 ]; then -# break -# fi -#done -echo "Checking every output file is identical..." -set +x -res=`cksum $OUTPUT/output_file_* | awk '{print $1}' | sort -u` -set -x -[ "$res" = "2572910830" ] # Clean up cd $test_dir