1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
|
/*
* (C) Copyright 2005- ECMWF.
*
* 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.
*/
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#undef NDEBUG
#include <assert.h>
/*#include "grib_api_internal.h"*/
#include "eccodes.h"
#define NUM_THREADS 4
/* Return 0 if numbers considered equal, otherwise 1 */
static int compare_doubles(double a, double b, double tolerance)
{
int ret = 0;
double d = fabs(a - b);
if (d > tolerance) {
ret = 1;
}
return ret;
}
static void* process_grib(void* threadID)
{
const long tid = (long)threadID;
size_t str_len = 20, i = 0;
long indicatorOfUnitOfTimeRange = 1, step = 0;
char mystring[100];
double* values = NULL;
size_t values_len = 0;
double min = 0, max = 0, avg = 0;
const double tol = 1e-4;
double pv[4] = { 1, 2, 3, 4 };
const size_t pvsize = 4;
ProductKind prod_kind = PRODUCT_ANY;
codes_handle* h = codes_grib_handle_new_from_samples(0, "regular_ll_pl_grib2");
assert(h != NULL);
CODES_CHECK(codes_get_product_kind(h, &prod_kind), 0);
assert(prod_kind == PRODUCT_GRIB);
printf("Thread %ld running\n", tid);
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);
CODES_CHECK(codes_set_long(h, "endStep", 86400), 0);
CODES_CHECK(codes_set_long(h, "centre", 80), 0);
CODES_CHECK(codes_get_long(h, "endStep", &step), 0);
CODES_CHECK(codes_get_string(h, "indicatorOfUnitOfTimeRange", mystring, &str_len), 0);
CODES_CHECK(codes_set_long(h, "PVPresent", 1), 0);
CODES_CHECK(codes_set_double_array(h, "pv", pv, pvsize), 0);
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;
}
GRIB_CHECK(grib_set_double_array(h, "values", values, values_len), 0);
free(values);
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);
printf("\nThread %ld: min=%.8f max=%.8f avg=%.8f\n", tid, min, max, avg);
assert(compare_doubles(min, 229.4459, tol) == 0);
assert(compare_doubles(max, 273.1499, tol) == 0);
assert(compare_doubles(avg, 250.4168, tol) == 0);
codes_handle_delete(h);
pthread_exit(NULL);
}
int main(int argc, char** argv)
{
pthread_t threads[NUM_THREADS];
int error = 0;
long i = 0;
for (i = 0; i < NUM_THREADS; ++i) {
printf("Creating thread %ld\n", i);
error = pthread_create(&threads[i], NULL, process_grib, (void*)i);
if (error) {
assert(0);
return 1;
}
}
for (i = 0; i < NUM_THREADS; ++i) {
pthread_join(threads[i], NULL);
}
return 0;
}
|