2018-04-25 12:58:19 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "grib_api.h"
|
|
|
|
|
|
|
|
#define NUM_THREADS 8
|
2018-05-01 13:24:42 +00:00
|
|
|
#define FILES_PER_ITERATION 200
|
|
|
|
static char* INPUT_FILE = NULL;
|
2018-04-25 12:58:19 +00:00
|
|
|
|
|
|
|
static int encode_file(char *template_file, char *output_file)
|
|
|
|
{
|
2018-05-01 13:24:42 +00:00
|
|
|
FILE *in, *out;
|
2018-04-25 12:58:19 +00:00
|
|
|
grib_handle *source_handle = NULL;
|
|
|
|
const void *buffer = NULL;
|
|
|
|
size_t size = 0;
|
|
|
|
int err = 0;
|
2018-05-01 13:24:42 +00:00
|
|
|
double *values;
|
2018-04-27 11:54:55 +00:00
|
|
|
|
2018-04-25 12:58:19 +00:00
|
|
|
in = fopen(template_file,"r"); assert(in);
|
|
|
|
out = fopen(output_file,"w"); assert(out);
|
2018-04-27 11:54:55 +00:00
|
|
|
|
|
|
|
/* loop over the messages in the source GRIB and clone them */
|
2018-05-01 13:24:42 +00:00
|
|
|
while ((source_handle = grib_handle_new_from_file(0, in, &err))!=NULL) {
|
|
|
|
int i;
|
|
|
|
long count;
|
|
|
|
double d,e;
|
|
|
|
size_t values_len= 0;
|
2018-04-27 11:54:55 +00:00
|
|
|
|
2018-05-01 13:24:42 +00:00
|
|
|
grib_handle *clone_handle = grib_handle_clone(source_handle);
|
|
|
|
assert(clone_handle);
|
2018-04-25 12:58:19 +00:00
|
|
|
|
2018-05-01 13:24:42 +00:00
|
|
|
/*GRIB_CHECK(grib_set_long(clone_handle, "centre", 250),0);*/
|
2018-04-25 12:58:19 +00:00
|
|
|
GRIB_CHECK(grib_get_size(clone_handle, "values", &values_len),0);
|
|
|
|
|
2018-04-27 11:54:55 +00:00
|
|
|
values = (double*)malloc(values_len*sizeof(double));
|
2018-04-25 12:58:19 +00:00
|
|
|
d=10e-8;
|
|
|
|
e=d;
|
|
|
|
count=1;
|
|
|
|
for (i=0;i<values_len;i++) {
|
|
|
|
if (count>100) {e*=10; count=1;}
|
|
|
|
values[i]=d;
|
|
|
|
/*printf("%g \n",values[i]);*/
|
|
|
|
d+=e;
|
|
|
|
count++;
|
|
|
|
}
|
2018-04-27 11:54:55 +00:00
|
|
|
|
2018-04-25 12:58:19 +00:00
|
|
|
GRIB_CHECK(grib_set_long(clone_handle,"bitsPerValue",16),0);
|
2018-04-27 11:54:55 +00:00
|
|
|
|
2018-04-25 12:58:19 +00:00
|
|
|
/* set data values */
|
|
|
|
GRIB_CHECK(grib_set_double_array(clone_handle,"values",values,values_len),0);
|
2018-04-27 11:54:55 +00:00
|
|
|
|
2018-04-25 12:58:19 +00:00
|
|
|
GRIB_CHECK(grib_get_message(clone_handle,&buffer,&size),0);
|
|
|
|
if(fwrite(buffer,1,size,out) != size) {
|
|
|
|
perror(output_file);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
grib_handle_delete(clone_handle);
|
|
|
|
grib_handle_delete(source_handle);
|
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-04-25 12:58:19 +00:00
|
|
|
fclose(out);
|
|
|
|
fclose(in);
|
2018-04-27 11:54:55 +00:00
|
|
|
|
2018-04-25 12:58:19 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-04-25 17:45:27 +00:00
|
|
|
void do_stuff(void *data);
|
2018-04-25 12:58:19 +00:00
|
|
|
|
|
|
|
/* Structure for passing data to threads */
|
2018-04-27 11:54:55 +00:00
|
|
|
struct v {
|
2018-05-01 13:24:42 +00:00
|
|
|
int number;
|
2018-04-27 11:54:55 +00:00
|
|
|
char *data;
|
2018-04-25 12:58:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void *runner(void *ptr); /* the thread */
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int thread_counter = 0;
|
2018-05-01 13:24:42 +00:00
|
|
|
int parallel = 1;
|
|
|
|
if (argc<2) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
INPUT_FILE= argv[1];
|
|
|
|
if (argc>2 && strcmp(argv[2],"seq")==0) {
|
|
|
|
parallel = 0;
|
|
|
|
}
|
2018-04-25 12:58:19 +00:00
|
|
|
if (parallel) {
|
|
|
|
printf("Running parallel in %d threads.\n", NUM_THREADS);
|
|
|
|
} else {
|
|
|
|
printf("Running sequentially in %d runs.\n", NUM_THREADS);
|
|
|
|
}
|
2018-04-27 11:54:55 +00:00
|
|
|
|
2018-04-25 12:58:19 +00:00
|
|
|
pthread_t workers[NUM_THREADS];
|
2018-04-27 11:54:55 +00:00
|
|
|
|
2018-04-25 12:58:19 +00:00
|
|
|
/* We have to create M * N worker 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) {
|
2018-04-27 11:54:55 +00:00
|
|
|
/* Now we will create the thread passing it data as an argument */
|
2018-04-25 12:58:19 +00:00
|
|
|
pthread_create(&workers[thread_counter], NULL, runner, data);
|
2018-04-27 11:54:55 +00:00
|
|
|
/*pthread_join(workers[thread_counter], NULL);*/
|
2018-04-25 12:58:19 +00:00
|
|
|
thread_counter++;
|
|
|
|
} else {
|
|
|
|
do_stuff(data);
|
|
|
|
}
|
|
|
|
}
|
2018-04-27 11:54:55 +00:00
|
|
|
|
2018-04-25 12:58:19 +00:00
|
|
|
if (parallel) {
|
2018-05-01 13:24:42 +00:00
|
|
|
for (i = 0; i < NUM_THREADS; i++) {
|
2018-04-25 12:58:19 +00:00
|
|
|
pthread_join(workers[i], NULL);
|
|
|
|
}
|
|
|
|
}
|
2018-04-27 11:54:55 +00:00
|
|
|
|
2018-04-25 12:58:19 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *runner(void *ptr)
|
2018-05-01 13:24:42 +00:00
|
|
|
{
|
|
|
|
do_stuff(ptr);
|
2018-04-25 12:58:19 +00:00
|
|
|
pthread_exit(0);
|
|
|
|
}
|
|
|
|
|
2018-04-25 17:45:27 +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 */
|
2018-04-25 12:58:19 +00:00
|
|
|
struct v *data = ptr;
|
|
|
|
|
|
|
|
char output_file[50];
|
|
|
|
|
|
|
|
for (int i=0; i<FILES_PER_ITERATION;i++) {
|
|
|
|
sprintf(output_file,"output/output_file_%d-%d.grib",data->number,i);
|
2018-05-01 13:24:42 +00:00
|
|
|
encode_file(INPUT_FILE,output_file);
|
2018-04-25 12:58:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
time_t ltime;
|
|
|
|
struct tm result;
|
|
|
|
char stime[32];
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
printf("%s: Worker %d finished.\n", stime,data->number);
|
|
|
|
}
|