2022-11-04 16:55:18 +00:00
|
|
|
/*
|
|
|
|
* (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.
|
|
|
|
*/
|
2018-05-08 13:11:01 +00:00
|
|
|
/*
|
2019-07-04 15:58:13 +00:00
|
|
|
* Test for ECC-604: GRIB decoding/encoding sequentially and parallel with POSIX threads
|
2018-05-08 13:11:01 +00:00
|
|
|
*/
|
2018-04-25 12:58:19 +00:00
|
|
|
#include <time.h>
|
|
|
|
#include <pthread.h>
|
2018-07-25 13:48:42 +00:00
|
|
|
#include <unistd.h>
|
2018-04-25 12:58:19 +00:00
|
|
|
|
2022-07-29 12:21:04 +00:00
|
|
|
#include "grib_api_internal.h"
|
2018-04-25 12:58:19 +00:00
|
|
|
|
2018-05-11 10:48:23 +00:00
|
|
|
/* These are passed in via argv */
|
|
|
|
static size_t NUM_THREADS = 0;
|
|
|
|
static size_t FILES_PER_ITERATION = 0;
|
2020-01-29 12:05:02 +00:00
|
|
|
static char* INPUT_FILE = NULL;
|
|
|
|
int opt_dump = 0; /* If 1 then dump handle to /dev/null */
|
|
|
|
int opt_clone = 0; /* If 1 then clone source handle */
|
|
|
|
int opt_write = 0; /* If 1 write handle to file */
|
2018-04-25 12:58:19 +00:00
|
|
|
|
2023-12-09 14:50:06 +00:00
|
|
|
static int encode_file(const char* template_file, const char* output_file)
|
2018-04-25 12:58:19 +00:00
|
|
|
{
|
2020-01-29 12:05:02 +00:00
|
|
|
FILE *in, *out = NULL;
|
|
|
|
grib_handle* source_handle = NULL;
|
|
|
|
const void* buffer = NULL;
|
|
|
|
size_t size = 0;
|
|
|
|
int err = 0;
|
|
|
|
double* values;
|
|
|
|
|
|
|
|
in = fopen(template_file, "rb");
|
2024-12-20 12:58:07 +00:00
|
|
|
ECCODES_ASSERT(in);
|
2020-01-29 10:52:09 +00:00
|
|
|
if (opt_write && output_file) {
|
2020-01-29 12:05:02 +00:00
|
|
|
out = fopen(output_file, "wb");
|
2024-12-20 12:58:07 +00:00
|
|
|
ECCODES_ASSERT(out);
|
2018-07-25 13:48:42 +00:00
|
|
|
}
|
2018-04-27 11:54:55 +00:00
|
|
|
|
|
|
|
/* loop over the messages in the source GRIB and clone them */
|
2020-01-29 12:05:02 +00:00
|
|
|
while ((source_handle = grib_handle_new_from_file(0, in, &err)) != NULL) {
|
2018-05-01 13:24:42 +00:00
|
|
|
int i;
|
2018-05-11 10:48:23 +00:00
|
|
|
size_t values_len = 0;
|
2020-01-29 12:05:02 +00:00
|
|
|
size_t str_len = 20;
|
|
|
|
grib_handle* h = source_handle;
|
|
|
|
|
2018-07-25 13:48:42 +00:00
|
|
|
if (opt_clone) {
|
2020-01-29 12:05:02 +00:00
|
|
|
h = grib_handle_clone(source_handle);
|
2024-12-20 12:58:07 +00:00
|
|
|
ECCODES_ASSERT(h);
|
2018-07-25 13:48:42 +00:00
|
|
|
}
|
2018-04-27 11:54:55 +00:00
|
|
|
|
2020-01-29 12:05:02 +00:00
|
|
|
GRIB_CHECK(grib_get_size(h, "values", &values_len), 0);
|
2018-04-25 12:58:19 +00:00
|
|
|
|
2020-01-29 12:05:02 +00:00
|
|
|
values = (double*)malloc(values_len * sizeof(double));
|
|
|
|
GRIB_CHECK(grib_get_double_array(h, "values", values, &values_len), 0);
|
2018-05-18 13:36:06 +00:00
|
|
|
|
2020-01-29 12:05:02 +00:00
|
|
|
for (i = 0; i < values_len; i++) {
|
2018-05-18 13:36:06 +00:00
|
|
|
values[i] *= 0.9;
|
2018-04-25 12:58:19 +00:00
|
|
|
}
|
2018-04-27 11:54:55 +00:00
|
|
|
|
2020-01-29 12:05:02 +00:00
|
|
|
GRIB_CHECK(grib_set_string(h, "stepUnits", "s", &str_len), 0);
|
2018-07-25 13:48:42 +00:00
|
|
|
GRIB_CHECK(grib_set_long(h, "startStep", 43200), 0);
|
|
|
|
GRIB_CHECK(grib_set_long(h, "endStep", 86400), 0);
|
2020-01-29 12:05:02 +00:00
|
|
|
GRIB_CHECK(grib_set_long(h, "bitsPerValue", 16), 0);
|
2018-04-27 11:54:55 +00:00
|
|
|
|
2018-04-25 12:58:19 +00:00
|
|
|
/* set data values */
|
2020-01-29 12:05:02 +00:00
|
|
|
GRIB_CHECK(grib_set_double_array(h, "values", values, values_len), 0);
|
2018-04-27 11:54:55 +00:00
|
|
|
|
2020-01-29 12:05:02 +00:00
|
|
|
GRIB_CHECK(grib_get_message(h, &buffer, &size), 0);
|
2018-08-06 12:44:56 +00:00
|
|
|
if (opt_write) {
|
2020-01-29 12:05:02 +00:00
|
|
|
if (fwrite(buffer, 1, size, out) != size) {
|
2018-07-25 13:48:42 +00:00
|
|
|
perror(output_file);
|
|
|
|
return 1;
|
|
|
|
}
|
2018-04-25 12:58:19 +00:00
|
|
|
}
|
2018-08-06 12:44:56 +00:00
|
|
|
if (opt_dump) {
|
2020-01-29 12:05:02 +00:00
|
|
|
FILE* devnull = fopen("/dev/null", "w");
|
|
|
|
grib_dump_content(source_handle, devnull, "debug", 0, NULL);
|
2018-05-30 15:33:40 +00:00
|
|
|
}
|
2018-07-25 13:48:42 +00:00
|
|
|
|
2018-04-25 12:58:19 +00:00
|
|
|
grib_handle_delete(source_handle);
|
2020-01-29 12:05:02 +00:00
|
|
|
if (opt_clone) grib_handle_delete(h);
|
2018-05-01 13:24:42 +00:00
|
|
|
free(values);
|
2018-04-25 12:58:19 +00:00
|
|
|
}
|
2018-04-27 11:54:55 +00:00
|
|
|
|
2018-08-06 12:44:56 +00:00
|
|
|
if (opt_write) fclose(out);
|
2018-04-25 12:58:19 +00:00
|
|
|
fclose(in);
|
2018-04-27 11:54:55 +00:00
|
|
|
|
2018-04-25 12:58:19 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-01-29 12:05:02 +00:00
|
|
|
void do_stuff(void* data);
|
2018-04-25 12:58:19 +00:00
|
|
|
|
|
|
|
/* Structure for passing data to threads */
|
2020-01-29 12:05:02 +00:00
|
|
|
struct v
|
|
|
|
{
|
2018-05-01 14:44:34 +00:00
|
|
|
size_t number;
|
2020-01-29 12:05:02 +00:00
|
|
|
char* data;
|
2018-04-25 12:58:19 +00:00
|
|
|
};
|
|
|
|
|
2020-01-29 12:05:02 +00:00
|
|
|
void* runner(void* ptr); /* the thread */
|
2018-04-25 12:58:19 +00:00
|
|
|
|
2020-01-29 12:05:02 +00:00
|
|
|
int main(int argc, char** argv)
|
2018-04-25 12:58:19 +00:00
|
|
|
{
|
2018-05-01 14:44:34 +00:00
|
|
|
size_t i;
|
2018-04-25 12:58:19 +00:00
|
|
|
int thread_counter = 0;
|
2020-01-29 12:05:02 +00:00
|
|
|
int parallel = 1, index = 0, c = 0;
|
2018-05-01 14:44:34 +00:00
|
|
|
const char* prog = argv[0];
|
|
|
|
char* mode;
|
2020-01-29 12:05:02 +00:00
|
|
|
if (argc < 5 || argc > 8) {
|
2018-07-25 13:48:42 +00:00
|
|
|
fprintf(stderr, "Usage:\n\t%s [options] seq file numRuns numIter\nOr\n\t%s [options] par file numThreads numIter\n", prog, prog);
|
2018-05-01 13:24:42 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2020-01-29 12:05:02 +00:00
|
|
|
|
|
|
|
while ((c = getopt(argc, argv, "dcw")) != -1) {
|
2018-07-25 13:48:42 +00:00
|
|
|
switch (c) {
|
2020-01-29 12:05:02 +00:00
|
|
|
case 'd':
|
|
|
|
opt_dump = 1;
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
opt_clone = 1;
|
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
opt_write = 1;
|
|
|
|
break;
|
2018-07-25 13:48:42 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-29 12:05:02 +00:00
|
|
|
index = optind;
|
|
|
|
mode = argv[index];
|
|
|
|
INPUT_FILE = argv[index + 1];
|
|
|
|
NUM_THREADS = atol(argv[index + 2]);
|
|
|
|
FILES_PER_ITERATION = atol(argv[index + 3]);
|
2018-05-01 14:44:34 +00:00
|
|
|
|
2020-01-29 12:05:02 +00:00
|
|
|
if (strcmp(mode, "seq") == 0) {
|
2018-05-01 13:24:42 +00:00
|
|
|
parallel = 0;
|
|
|
|
}
|
2018-04-25 12:58:19 +00:00
|
|
|
if (parallel) {
|
2023-12-09 14:50:06 +00:00
|
|
|
printf("Running parallel in %zu threads. %zu iterations (prod=%zu)\n", NUM_THREADS, FILES_PER_ITERATION, NUM_THREADS * FILES_PER_ITERATION);
|
2018-08-06 12:44:56 +00:00
|
|
|
printf("Options: dump=%d, clone=%d, write=%d\n", opt_dump, opt_clone, opt_write);
|
2020-01-29 12:05:02 +00:00
|
|
|
}
|
|
|
|
else {
|
2023-12-09 14:50:06 +00:00
|
|
|
printf("Running sequentially in %zu runs. %zu iterations\n", NUM_THREADS, FILES_PER_ITERATION);
|
2018-04-25 12:58:19 +00:00
|
|
|
}
|
2018-04-27 11:54:55 +00:00
|
|
|
|
2018-07-20 13:16:37 +00:00
|
|
|
{
|
2022-10-20 16:20:13 +00:00
|
|
|
pthread_t* workers = (pthread_t*)malloc(NUM_THREADS * sizeof(pthread_t));
|
2018-07-20 13:16:37 +00:00
|
|
|
for (i = 0; i < NUM_THREADS; i++) {
|
2020-01-29 12:05:02 +00:00
|
|
|
struct v* data = (struct v*)malloc(sizeof(struct v));
|
|
|
|
data->number = i;
|
|
|
|
data->data = NULL;
|
2018-07-20 13:16:37 +00:00
|
|
|
|
|
|
|
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++;
|
2020-01-29 12:05:02 +00:00
|
|
|
}
|
|
|
|
else {
|
2018-07-20 13:16:37 +00:00
|
|
|
do_stuff(data);
|
|
|
|
}
|
2018-04-25 12:58:19 +00:00
|
|
|
}
|
2018-04-27 11:54:55 +00:00
|
|
|
|
2018-07-20 13:16:37 +00:00
|
|
|
if (parallel) {
|
2020-01-29 12:05:02 +00:00
|
|
|
for (i = 0; i < NUM_THREADS; i++) {
|
2018-07-20 13:16:37 +00:00
|
|
|
pthread_join(workers[i], NULL);
|
|
|
|
}
|
2018-04-25 12:58:19 +00:00
|
|
|
}
|
2020-01-29 12:05:02 +00:00
|
|
|
free(workers);
|
2018-04-25 12:58:19 +00:00
|
|
|
}
|
2018-04-27 11:54:55 +00:00
|
|
|
|
2018-04-25 12:58:19 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-01-29 12:05:02 +00:00
|
|
|
void* runner(void* ptr)
|
2018-05-01 13:24:42 +00:00
|
|
|
{
|
|
|
|
do_stuff(ptr);
|
2020-03-30 17:14:43 +00:00
|
|
|
free(ptr);
|
2018-04-25 12:58:19 +00:00
|
|
|
pthread_exit(0);
|
|
|
|
}
|
|
|
|
|
2020-01-29 12:05:02 +00:00
|
|
|
void do_stuff(void* ptr)
|
2018-04-25 12:58:19 +00:00
|
|
|
{
|
2018-04-27 11:54:55 +00:00
|
|
|
/* Cast argument to struct v pointer */
|
2022-10-20 16:20:13 +00:00
|
|
|
struct v* data = (struct v*)ptr;
|
2018-05-01 14:44:34 +00:00
|
|
|
size_t i;
|
2018-04-25 12:58:19 +00:00
|
|
|
char output_file[50];
|
2018-07-20 13:16:37 +00:00
|
|
|
time_t ltime;
|
|
|
|
struct tm result;
|
|
|
|
char stime[32];
|
2018-04-25 12:58:19 +00:00
|
|
|
|
2020-01-29 12:05:02 +00:00
|
|
|
for (i = 0; i < FILES_PER_ITERATION; i++) {
|
2018-07-25 13:48:42 +00:00
|
|
|
if (opt_write) {
|
2023-12-09 14:50:06 +00:00
|
|
|
snprintf(output_file, 50, "output/output_file_%zu-%zu.grib", data->number, i);
|
2020-01-29 12:05:02 +00:00
|
|
|
encode_file(INPUT_FILE, output_file);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
encode_file(INPUT_FILE, NULL);
|
2018-07-25 13:48:42 +00:00
|
|
|
}
|
2018-04-25 12:58:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ltime = time(NULL);
|
|
|
|
localtime_r(<ime, &result);
|
2018-05-01 13:24:42 +00:00
|
|
|
strftime(stime, 32, "%H:%M:%S", &result); /* Try to get milliseconds here too*/
|
|
|
|
/* asctime_r(&result, stime); */
|
2018-04-25 12:58:19 +00:00
|
|
|
|
2023-12-09 14:50:06 +00:00
|
|
|
printf("%s: Worker %zu finished.\n", stime, data->number);
|
2018-04-25 12:58:19 +00:00
|
|
|
}
|