2016-05-24 13:27:46 +00:00
|
|
|
/*
|
2020-01-28 14:32:34 +00:00
|
|
|
* (C) Copyright 2005- ECMWF.
|
2016-05-24 13:27:46 +00:00
|
|
|
*
|
|
|
|
* This software is licensed under the terms of the Apache Licence Version 2.0
|
|
|
|
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
|
|
|
|
*
|
|
|
|
* In applying this licence, ECMWF does not waive the privileges and immunities granted to it by
|
|
|
|
* virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction.
|
|
|
|
*/
|
2020-12-10 23:21:51 +00:00
|
|
|
|
2016-05-24 13:27:46 +00:00
|
|
|
#include <pthread.h>
|
2020-12-10 23:21:51 +00:00
|
|
|
#include "grib_api_internal.h"
|
2016-05-24 13:27:46 +00:00
|
|
|
|
|
|
|
#include "eccodes.h"
|
|
|
|
#define NUM_THREADS 4
|
|
|
|
|
2019-08-12 12:19:21 +00:00
|
|
|
/* Return 0 if numbers considered equal, otherwise 1 */
|
2020-01-22 14:57:43 +00:00
|
|
|
static int compare_doubles(double a, double b, double tolerance)
|
2019-08-12 12:19:21 +00:00
|
|
|
{
|
2020-01-22 14:57:43 +00:00
|
|
|
int ret = 0;
|
|
|
|
double d = fabs(a - b);
|
2019-08-12 12:19:21 +00:00
|
|
|
if (d > tolerance) {
|
2020-01-22 14:57:43 +00:00
|
|
|
ret = 1;
|
2019-08-12 12:19:21 +00:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-05-24 13:27:46 +00:00
|
|
|
static void* process_grib(void* threadID)
|
|
|
|
{
|
|
|
|
const long tid = (long)threadID;
|
2019-08-12 12:19:21 +00:00
|
|
|
size_t str_len = 20, i = 0;
|
2016-05-24 13:27:46 +00:00
|
|
|
long indicatorOfUnitOfTimeRange = 1, step = 0;
|
|
|
|
char mystring[100];
|
2020-01-22 14:57:43 +00:00
|
|
|
double* values = NULL;
|
2016-05-24 13:27:46 +00:00
|
|
|
size_t values_len = 0;
|
2020-01-22 14:57:43 +00:00
|
|
|
double min = 0, max = 0, avg = 0;
|
|
|
|
const double tol = 1e-6;
|
|
|
|
double pv[4] = { 1, 2, 3, 4 };
|
|
|
|
const size_t pvsize = 4;
|
2019-05-17 14:02:58 +00:00
|
|
|
ProductKind prod_kind = 0;
|
2016-05-24 13:27:46 +00:00
|
|
|
|
2016-07-19 10:52:55 +00:00
|
|
|
codes_handle* h = codes_grib_handle_new_from_samples(0, "regular_ll_pl_grib2");
|
2020-12-10 23:21:51 +00:00
|
|
|
Assert(h);
|
2019-05-17 14:02:58 +00:00
|
|
|
CODES_CHECK(codes_get_product_kind(h, &prod_kind), 0);
|
2020-12-10 23:21:51 +00:00
|
|
|
Assert(prod_kind == PRODUCT_GRIB);
|
2016-05-24 15:21:55 +00:00
|
|
|
printf("Thread %ld running\n", tid);
|
2016-05-24 13:27:46 +00:00
|
|
|
|
2020-01-22 14:57:43 +00:00
|
|
|
CODES_CHECK(codes_set_long(h, "indicatorOfUnitOfTimeRange", indicatorOfUnitOfTimeRange), 0);
|
|
|
|
CODES_CHECK(codes_set_string(h, "indicatorOfUnitOfTimeRange", "s", &str_len), 0);
|
|
|
|
CODES_CHECK(codes_set_string(h, "stepUnits", "s", &str_len), 0);
|
2016-05-24 13:27:46 +00:00
|
|
|
CODES_CHECK(codes_set_long(h, "endStep", 86400), 0);
|
2020-01-22 14:57:43 +00:00
|
|
|
CODES_CHECK(codes_set_long(h, "centre", 80), 0);
|
2016-05-24 13:27:46 +00:00
|
|
|
|
2020-01-22 14:57:43 +00:00
|
|
|
CODES_CHECK(codes_get_long(h, "endStep", &step), 0);
|
|
|
|
CODES_CHECK(codes_get_string(h, "indicatorOfUnitOfTimeRange", mystring, &str_len), 0);
|
2016-05-24 13:27:46 +00:00
|
|
|
|
2020-01-22 14:57:43 +00:00
|
|
|
CODES_CHECK(codes_set_long(h, "PVPresent", 1), 0);
|
|
|
|
CODES_CHECK(codes_set_double_array(h, "pv", pv, pvsize), 0);
|
2016-05-24 13:27:46 +00:00
|
|
|
|
2020-01-22 14:57:43 +00:00
|
|
|
CODES_CHECK(codes_get_size(h, "values", &values_len), 0);
|
|
|
|
values = (double*)malloc(values_len * sizeof(double));
|
|
|
|
CODES_CHECK(codes_get_double_array(h, "values", values, &values_len), 0);
|
|
|
|
for (i = 0; i < values_len; i++) {
|
|
|
|
if (i % 2)
|
|
|
|
values[i] *= 0.94;
|
|
|
|
else if (i % 3)
|
|
|
|
values[i] *= 0.84;
|
2019-08-12 12:19:21 +00:00
|
|
|
}
|
2020-01-22 14:57:43 +00:00
|
|
|
GRIB_CHECK(grib_set_double_array(h, "values", values, values_len), 0);
|
2016-05-24 13:27:46 +00:00
|
|
|
free(values);
|
|
|
|
|
2020-01-22 14:57:43 +00:00
|
|
|
CODES_CHECK(codes_get_double(h, "min", &min), 0);
|
|
|
|
CODES_CHECK(codes_get_double(h, "max", &max), 0);
|
|
|
|
CODES_CHECK(codes_get_double(h, "avg", &avg), 0);
|
2019-08-12 12:19:21 +00:00
|
|
|
printf("Thread %ld: min=%g max=%g avg=%g\n", tid, min, max, avg);
|
2020-12-10 23:21:51 +00:00
|
|
|
Assert(compare_doubles(min, 0.84, tol) == 0);
|
|
|
|
Assert(compare_doubles(max, 1.00, tol) == 0);
|
|
|
|
Assert(compare_doubles(avg, 0.916774, tol) == 0);
|
2016-05-24 13:27:46 +00:00
|
|
|
|
|
|
|
codes_handle_delete(h);
|
|
|
|
pthread_exit(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
pthread_t threads[NUM_THREADS];
|
|
|
|
int error = 0;
|
2020-01-22 14:57:43 +00:00
|
|
|
long i = 0;
|
|
|
|
for (i = 0; i < NUM_THREADS; ++i) {
|
2016-05-24 13:27:46 +00:00
|
|
|
printf("Creating thread %ld\n", i);
|
2020-01-22 14:57:43 +00:00
|
|
|
error = pthread_create(&threads[i], NULL, process_grib, (void*)i);
|
2016-05-24 13:27:46 +00:00
|
|
|
if (error) {
|
2020-12-10 23:21:51 +00:00
|
|
|
Assert(0);
|
2016-05-24 13:27:46 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2020-01-22 14:57:43 +00:00
|
|
|
for (i = 0; i < NUM_THREADS; ++i) {
|
2016-11-21 14:03:17 +00:00
|
|
|
pthread_join(threads[i], NULL);
|
|
|
|
}
|
|
|
|
|
2016-05-24 13:27:46 +00:00
|
|
|
return 0;
|
|
|
|
}
|