2017-09-01 13:58:59 +00:00
|
|
|
/*
|
2020-01-28 14:32:34 +00:00
|
|
|
* (C) Copyright 2005- ECMWF.
|
2017-09-01 13:58:59 +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.
|
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <pthread.h>
|
2022-07-29 12:21:04 +00:00
|
|
|
|
|
|
|
#include "grib_api_internal.h"
|
2017-09-01 13:58:59 +00:00
|
|
|
|
|
|
|
#define NUM_THREADS 8
|
|
|
|
#define FILES_PER_ITERATION 10
|
|
|
|
|
|
|
|
char* INPUT_FILE = NULL;
|
2020-01-29 12:05:02 +00:00
|
|
|
void do_stuff(void* data);
|
2017-09-01 13:58:59 +00:00
|
|
|
|
2020-01-29 12:05:02 +00:00
|
|
|
void* runner(void* ptr); /* the thread function */
|
2017-09-01 13:58:59 +00:00
|
|
|
|
2020-01-29 12:05:02 +00:00
|
|
|
int main(int argc, char** argv)
|
2017-09-01 13:58:59 +00:00
|
|
|
{
|
2017-09-01 14:07:42 +00:00
|
|
|
long i;
|
2017-09-01 13:58:59 +00:00
|
|
|
int thread_counter = 0;
|
|
|
|
pthread_t workers[NUM_THREADS];
|
|
|
|
INPUT_FILE = argv[1];
|
2020-01-29 12:05:02 +00:00
|
|
|
|
2017-09-01 14:07:42 +00:00
|
|
|
/* Create worker threads */
|
2017-09-01 13:58:59 +00:00
|
|
|
for (i = 0; i < NUM_THREADS; i++) {
|
|
|
|
/* Now we will create the thread passing it data as a paramater*/
|
2017-09-01 14:07:42 +00:00
|
|
|
pthread_create(&workers[thread_counter], NULL, runner, (void*)i);
|
2017-09-01 13:58:59 +00:00
|
|
|
thread_counter++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Waiting for threads to complete */
|
2017-09-01 14:07:42 +00:00
|
|
|
for (i = 0; i < NUM_THREADS; i++) {
|
2020-01-29 12:05:02 +00:00
|
|
|
pthread_join(workers[i], NULL);
|
2017-09-01 13:58:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-01-29 12:05:02 +00:00
|
|
|
void* runner(void* ptr)
|
|
|
|
{
|
2017-09-01 13:58:59 +00:00
|
|
|
do_stuff(ptr);
|
|
|
|
pthread_exit(0);
|
|
|
|
}
|
|
|
|
|
2023-12-09 14:50:06 +00:00
|
|
|
static int encode_file(const char* input_file, const char* output_file)
|
2017-09-01 13:58:59 +00:00
|
|
|
{
|
2020-01-29 12:05:02 +00:00
|
|
|
grib_handle* source_handle = NULL;
|
|
|
|
const void* buffer = NULL;
|
|
|
|
int err = 0;
|
|
|
|
|
|
|
|
FILE* in = fopen(input_file, "rb");
|
|
|
|
FILE* out = fopen(output_file, "wb");
|
2024-12-20 12:58:07 +00:00
|
|
|
ECCODES_ASSERT(in);
|
|
|
|
ECCODES_ASSERT(out);
|
2017-09-01 13:58:59 +00:00
|
|
|
|
2020-01-29 12:05:02 +00:00
|
|
|
while ((source_handle = grib_handle_new_from_file(0, in, &err)) != NULL) {
|
|
|
|
size_t size = 0, values_len = 0;
|
2017-09-01 13:58:59 +00:00
|
|
|
int i;
|
2020-01-29 12:05:02 +00:00
|
|
|
double* values = NULL;
|
2017-09-01 13:58:59 +00:00
|
|
|
long count;
|
2020-01-29 12:05:02 +00:00
|
|
|
double d, e;
|
2017-09-01 13:58:59 +00:00
|
|
|
|
2020-01-29 12:05:02 +00:00
|
|
|
grib_handle* clone_handle = grib_handle_clone(source_handle);
|
2024-12-20 12:58:07 +00:00
|
|
|
ECCODES_ASSERT(clone_handle);
|
2020-01-29 12:05:02 +00:00
|
|
|
|
|
|
|
GRIB_CHECK(grib_get_size(clone_handle, "values", &values_len), 0);
|
|
|
|
values = (double*)malloc(values_len * sizeof(double));
|
|
|
|
|
|
|
|
d = 10e-10;
|
|
|
|
e = d;
|
|
|
|
count = 1;
|
|
|
|
for (i = 0; i < values_len; i++) {
|
|
|
|
if (count > 1000) {
|
|
|
|
e *= 2;
|
|
|
|
count = 1;
|
|
|
|
}
|
|
|
|
values[i] = d;
|
|
|
|
d += e;
|
2017-09-01 13:58:59 +00:00
|
|
|
count++;
|
|
|
|
}
|
2020-01-29 12:05:02 +00:00
|
|
|
GRIB_CHECK(grib_set_long(clone_handle, "bitsPerValue", 16), 0);
|
2021-01-14 15:40:23 +00:00
|
|
|
|
|
|
|
/*GRIB_CHECK(grib_set_string(clone_handle, "packingType", "grid_ccsds", &str_len), 0);*/
|
2022-02-28 16:08:16 +00:00
|
|
|
/*GRIB_CHECK(grib_set_string(clone_handle, "packingType", "grid_simple", &str_len), 0);*/
|
2021-01-14 15:40:23 +00:00
|
|
|
|
2020-01-29 12:05:02 +00:00
|
|
|
GRIB_CHECK(grib_set_double_array(clone_handle, "values", values, values_len), 0);
|
|
|
|
|
2017-09-01 13:58:59 +00:00
|
|
|
/* get the coded message in a buffer */
|
2020-01-29 12:05:02 +00:00
|
|
|
GRIB_CHECK(grib_get_message(clone_handle, &buffer, &size), 0);
|
2017-09-01 13:58:59 +00:00
|
|
|
/* write the buffer to a file */
|
2020-01-29 12:05:02 +00:00
|
|
|
if (fwrite(buffer, 1, size, out) != size) {
|
2017-09-01 13:58:59 +00:00
|
|
|
perror(output_file);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
grib_handle_delete(clone_handle);
|
|
|
|
grib_handle_delete(source_handle);
|
|
|
|
free(values);
|
|
|
|
}
|
2020-01-29 12:05:02 +00:00
|
|
|
|
2017-09-01 13:58:59 +00:00
|
|
|
fclose(out);
|
|
|
|
fclose(in);
|
2020-01-29 12:05:02 +00:00
|
|
|
|
2017-09-01 13:58:59 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-01-29 12:05:02 +00:00
|
|
|
void do_stuff(void* arg)
|
2017-09-01 13:58:59 +00:00
|
|
|
{
|
2017-09-01 14:07:42 +00:00
|
|
|
long number = (long)arg;
|
2017-09-01 13:58:59 +00:00
|
|
|
char output_file[50];
|
2017-09-01 14:27:04 +00:00
|
|
|
int i;
|
2017-09-01 13:58:59 +00:00
|
|
|
|
2020-01-29 12:05:02 +00:00
|
|
|
for (i = 0; i < FILES_PER_ITERATION; i++) {
|
2022-11-10 19:18:43 +00:00
|
|
|
snprintf(output_file, 50, "temp.grib_encode_pthreads_test.out_%d-%d.grib", (int)number, i);
|
2020-01-29 12:05:02 +00:00
|
|
|
encode_file(INPUT_FILE, output_file);
|
2017-09-01 13:58:59 +00:00
|
|
|
}
|
|
|
|
}
|