GRIB-884: OpenMP Support

This commit is contained in:
Shahram Najm 2015-12-30 14:39:02 +00:00
parent 6624c7b404
commit 52ab4d1ba3
21 changed files with 364 additions and 52 deletions

View File

@ -82,11 +82,11 @@ endif()
# advanced options (not visible in cmake-gui )
ecbuild_add_option( FEATURE MEMORY_MANAGEMENT DESCRIPTION "enable memory management" DEFAULT OFF ADVANCED )
ecbuild_add_option( FEATURE ALIGN_MEMORY DESCRIPTION "enable memory alignment" DEFAULT OFF ADVANCED )
ecbuild_add_option( FEATURE GRIB_TIMER DESCRIPTION "enable timer" DEFAULT OFF ADVANCED )
ecbuild_add_option( FEATURE ECCODES_THREADS DESCRIPTION "enable threads" DEFAULT OFF ADVANCED )
ecbuild_add_option( FEATURE MEMORY_MANAGEMENT DESCRIPTION "enable memory management" DEFAULT OFF ADVANCED )
ecbuild_add_option( FEATURE ALIGN_MEMORY DESCRIPTION "enable memory alignment" DEFAULT OFF ADVANCED )
ecbuild_add_option( FEATURE GRIB_TIMER DESCRIPTION "enable timer" DEFAULT OFF ADVANCED )
ecbuild_add_option( FEATURE ECCODES_THREADS DESCRIPTION "enable POSIX threads" DEFAULT OFF ADVANCED )
ecbuild_add_option( FEATURE ECCODES_OMP_THREADS DESCRIPTION "enable OMP threads" DEFAULT OFF ADVANCED )
###############################################################################
# macro processing
@ -166,6 +166,7 @@ if( NOT ${CMAKE_USE_PTHREADS_INIT} )
endif()
set( GRIB_PTHREADS 0 )
set( GRIB_OMP_THREADS 0 )
set( GRIB_LINUX_PTHREADS 0 )
#if( HAVE_ECCODES_THREADS AND CMAKE_THREAD_LIBS_INIT )
if( HAVE_ECCODES_THREADS )
@ -175,8 +176,12 @@ if( HAVE_ECCODES_THREADS )
set( GRIB_LINUX_PTHREADS 1 )
endif()
endif()
elseif(HAVE_ECCODES_OMP_THREADS)
ecbuild_enable_omp()
set( GRIB_OMP_THREADS 1 )
endif()
message(STATUS " GRIB_PTHREADS=${GRIB_PTHREADS}")
message(STATUS " GRIB_OMP_THREADS=${GRIB_OMP_THREADS}")
set( GRIB_MEM_ALIGN 0 )
if( ENABLE_ALIGN_MEMORY )

View File

@ -32,6 +32,7 @@
#define GRIB_MEM_ALIGN @GRIB_MEM_ALIGN@
#define GRIB_PTHREADS @GRIB_PTHREADS@
#define GRIB_OMP_THREADS @GRIB_OMP_THREADS@
#define ECCODES_SAMPLES_PATH "@ECCODES_SAMPLES_PATH@"

View File

@ -32,28 +32,48 @@
#define MIN_FILE_ID 50000
#if GRIB_PTHREADS
static pthread_once_t once = PTHREAD_ONCE_INIT;
static pthread_mutex_t handle_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t index_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t multi_handle_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t iterator_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t keys_iterator_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_once_t once = PTHREAD_ONCE_INIT;
static pthread_mutex_t handle_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t index_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t multi_handle_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t iterator_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t keys_iterator_mutex = PTHREAD_MUTEX_INITIALIZER;
static void init() {
pthread_mutexattr_t attr;
static void init() {
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&handle_mutex,&attr);
pthread_mutex_init(&index_mutex,&attr);
pthread_mutex_init(&multi_handle_mutex,&attr);
pthread_mutex_init(&iterator_mutex,&attr);
pthread_mutex_init(&keys_iterator_mutex,&attr);
pthread_mutexattr_destroy(&attr);
}
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&handle_mutex,&attr);
pthread_mutex_init(&index_mutex,&attr);
pthread_mutex_init(&multi_handle_mutex,&attr);
pthread_mutex_init(&iterator_mutex,&attr);
pthread_mutex_init(&keys_iterator_mutex,&attr);
pthread_mutexattr_destroy(&attr);
}
#elif GRIB_OMP_THREADS
static int once = 0;
static omp_nest_lock_t handle_mutex;
static omp_nest_lock_t index_mutex;
static omp_nest_lock_t multi_handle_mutex;
static omp_nest_lock_t iterator_mutex;
static omp_nest_lock_t keys_iterator_mutex;
static void init()
{
GRIB_OMP_CRITICAL(lock_fortran)
{
if (once == 0)
{
omp_init_nest_lock(&handle_mutex);
omp_init_nest_lock(&index_mutex);
omp_init_nest_lock(&multi_handle_mutex);
omp_init_nest_lock(&iterator_mutex);
omp_init_nest_lock(&keys_iterator_mutex);
once = 1;
}
}
}
#endif
int GRIB_NULL=-1;

View File

@ -26,28 +26,49 @@
#include <ctype.h>
#if GRIB_PTHREADS
static pthread_once_t once = PTHREAD_ONCE_INIT;
static pthread_mutex_t handle_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t index_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t multi_handle_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t iterator_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t keys_iterator_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_once_t once = PTHREAD_ONCE_INIT;
static pthread_mutex_t handle_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t index_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t multi_handle_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t iterator_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t keys_iterator_mutex = PTHREAD_MUTEX_INITIALIZER;
static void init() {
pthread_mutexattr_t attr;
static void init() {
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&handle_mutex,&attr);
pthread_mutex_init(&index_mutex,&attr);
pthread_mutex_init(&multi_handle_mutex,&attr);
pthread_mutex_init(&iterator_mutex,&attr);
pthread_mutex_init(&keys_iterator_mutex,&attr);
pthread_mutexattr_destroy(&attr);
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&handle_mutex,&attr);
pthread_mutex_init(&index_mutex,&attr);
pthread_mutex_init(&multi_handle_mutex,&attr);
pthread_mutex_init(&iterator_mutex,&attr);
pthread_mutex_init(&keys_iterator_mutex,&attr);
pthread_mutexattr_destroy(&attr);
}
}
#elif GRIB_OMP_THREADS
static int once = 0;
static omp_nest_lock_t handle_mutex;
static omp_nest_lock_t index_mutex;
static omp_nest_lock_t multi_handle_mutex;
static omp_nest_lock_t iterator_mutex;
static omp_nest_lock_t keys_iterator_mutex;
static void init()
{
GRIB_OMP_CRITICAL(lock_fortran)
{
if (once == 0)
{
omp_init_nest_lock(&handle_mutex);
omp_init_nest_lock(&index_mutex);
omp_init_nest_lock(&multi_handle_mutex);
omp_init_nest_lock(&iterator_mutex);
omp_init_nest_lock(&keys_iterator_mutex);
once = 1;
}
}
}
#endif
int GRIB_NULL=-1;
@ -1855,7 +1876,6 @@ int grib_c_set_real8_array(int* gid, char* key, double*val, int* size)
if(!h) return GRIB_INVALID_GRIB;
return grib_set_double_array(h, key, val, lsize);
}
int grib_c_set_double_array(int* gid, char* key, double*val, int* size)
@ -1867,7 +1887,6 @@ int grib_c_set_double_array(int* gid, char* key, double*val, int* size)
if(!h) return GRIB_INVALID_GRIB;
return grib_set_double_array(h, key, val, lsize);
}
int grib_c_get_string(int* gid, char* key, char* val, size_t *lsize)
@ -1955,11 +1974,9 @@ int grib_c_copy_message(int* gid, void* mess,size_t* len)
return GRIB_BUFFER_TOO_SMALL;
}
memcpy(mess,h->buffer->data,h->buffer->ulength);
*len=h->buffer->ulength;
return GRIB_SUCCESS;
}
void grib_c_check(int* err,char* call,char* str)

View File

@ -27,6 +27,21 @@ static void init_mutex() {
pthread_mutexattr_destroy(&attr);
}
#elif GRIB_OMP_THREADS
static int once = 0;
static omp_nest_lock_t mutex1;
static void init_mutex()
{
GRIB_OMP_CRITICAL(lock_action_c)
{
if (once == 0)
{
omp_init_nest_lock(&mutex1);
once = 1;
}
}
}
#endif

View File

@ -105,6 +105,21 @@ static void init() {
pthread_mutex_init(&mutex,&attr);
pthread_mutexattr_destroy(&attr);
}
#elif GRIB_OMP_THREADS
static int once = 0;
static omp_nest_lock_t mutex;
static void init()
{
GRIB_OMP_CRITICAL(lock_action_class_concept_c)
{
if (once == 0)
{
omp_init_nest_lock(&mutex);
once = 1;
}
}
}
#endif
static grib_concept_value* get_concept(grib_handle* h,grib_action_concept* self);

View File

@ -95,6 +95,21 @@ static void init() {
pthread_mutex_init(&mutex,&attr);
pthread_mutexattr_destroy(&attr);
}
#elif GRIB_OMP_THREADS
static int once = 0;
static omp_nest_lock_t mutex;
static void init()
{
GRIB_OMP_CRITICAL(lock_action_class_when_c)
{
if (once == 0)
{
omp_init_nest_lock(&mutex);
once = 1;
}
}
}
#endif
grib_action* grib_action_create_when( grib_context* context,

View File

@ -28,6 +28,21 @@ static void init() {
pthread_mutexattr_destroy(&attr);
}
#elif GRIB_OMP_THREADS
static int once = 0;
static omp_nest_lock_t mutex1;
static void init()
{
GRIB_OMP_CRITICAL(lock_grib_accessor_class_c)
{
if (once == 0)
{
omp_init_nest_lock(&mutex1);
once = 1;
}
}
}
#endif
struct table_entry
@ -176,7 +191,8 @@ grib_accessor* grib_accessor_factory(grib_section* p, grib_action* creator,
return a;
}
static void link_same_attributes(grib_accessor* a,grib_accessor* b) {
static void link_same_attributes(grib_accessor* a,grib_accessor* b)
{
int i=0;
int idx=0;
grib_accessor* bAttribute=NULL;

View File

@ -173,6 +173,21 @@ static void thread_init() {
pthread_mutexattr_destroy(&attr);
}
#elif GRIB_OMP_THREADS
static int once = 0;
static omp_nest_lock_t mutex;
static void thread_init()
{
GRIB_OMP_CRITICAL(lock_grib_accessor_class_smart_table_c)
{
if (once == 0)
{
omp_init_nest_lock(&mutex);
once = 1;
}
}
}
#endif
static int grib_load_smart_table(grib_context* c,const char* filename,

View File

@ -127,10 +127,22 @@ extern "C" {
#define GRIB_MUTEX_INIT_ONCE(a,b) pthread_once(a,b);
#define GRIB_MUTEX_LOCK(a) pthread_mutex_lock(a);
#define GRIB_MUTEX_UNLOCK(a) pthread_mutex_unlock(a);
/*
#define GRIB_MUTEX_LOCK(a) {pthread_mutex_lock(a); printf("MUTEX LOCK %p %s line %d\n",(void*)a,__FILE__,__LINE__);}
#define GRIB_MUTEX_UNLOCK(a) {pthread_mutex_unlock(a);printf("MUTEX UNLOCK %p %s line %d\n",(void*)a,__FILE__,__LINE__);}
*/
/*
#define GRIB_MUTEX_LOCK(a) {pthread_mutex_lock(a); printf("MUTEX LOCK %p %s line %d\n",(void*)a,__FILE__,__LINE__);}
#define GRIB_MUTEX_UNLOCK(a) {pthread_mutex_unlock(a);printf("MUTEX UNLOCK %p %s line %d\n",(void*)a,__FILE__,__LINE__);}
*/
#elif GRIB_OMP_THREADS
#include <omp.h>
#ifdef _MSC_VER
#define GRIB_OMP_CRITICAL(a) __pragma(omp critical (a))
#else
#define GRIB_OMP_STR(a) #a
#define GRIB_OMP_XSTR(a) GRIB_OMP_STR(a)
#define GRIB_OMP_CRITICAL(a) _Pragma( GRIB_OMP_XSTR(omp critical (a) ) )
#endif
#define GRIB_MUTEX_INIT_ONCE(a,b) (*(b))();
#define GRIB_MUTEX_LOCK(a) omp_set_nest_lock(a);
#define GRIB_MUTEX_UNLOCK(a) omp_unset_nest_lock(a);
#else
#define GRIB_MUTEX_INIT_ONCE(a,b)
#define GRIB_MUTEX_LOCK(a)
@ -1031,6 +1043,8 @@ struct grib_context
grib_trie* lists;
#if GRIB_PTHREADS
pthread_mutex_t mutex;
#elif GRIB_OMP_THREADS
omp_nest_lock_t mutex;
#endif
};

View File

@ -40,7 +40,23 @@ static void init()
pthread_mutex_init(&mutex_mem,&attr);
pthread_mutexattr_destroy(&attr);
}
#elif GRIB_OMP_THREADS
static int once = 0;
static omp_nest_lock_t mutex_mem;
static omp_nest_lock_t mutex_c;
static void init()
{
GRIB_OMP_CRITICAL(lock_grib_context_c)
{
if (once == 0)
{
omp_init_nest_lock(&mutex_mem);
omp_init_nest_lock(&mutex_c);
once = 1;
}
}
}
#endif

View File

@ -29,6 +29,21 @@ static void init() {
pthread_mutexattr_destroy(&attr);
}
#elif GRIB_OMP_THREADS
static int once = 0;
static omp_nest_lock_t mutex1;
static void init()
{
GRIB_OMP_CRITICAL(lock_grib_filepool_c)
{
if (once == 0)
{
omp_init_nest_lock(&mutex1);
once = 1;
}
}
}
#endif
static short next_id=0;

View File

@ -30,6 +30,23 @@ static void init() {
pthread_mutexattr_destroy(&attr);
}
/* #elif GRIB_OMP_THREADS */
static int once = 0;
static omp_nest_lock_t mutex1;
static omp_nest_lock_t mutex2;
static void init()
{
GRIB_OMP_CRITICAL(lock_grib_handle_c)
{
if (once == 0)
{
omp_init_nest_lock(&mutex1);
omp_init_nest_lock(&mutex2);
once = 1;
}
}
}
#endif

View File

@ -7472,6 +7472,21 @@ static void init() {
pthread_mutexattr_destroy(&attr);
}
#elif GRIB_OMP_THREADS
static int once = 0;
static omp_nest_lock_t mutex;
static void init()
{
GRIB_OMP_CRITICAL(lock_grib_hash_keys_c)
{
if (once == 0)
{
omp_init_nest_lock(&mutex);
once = 1;
}
}
}
#endif
struct grib_itrie {
grib_itrie* next[SIZE];
@ -7542,7 +7557,6 @@ int grib_hash_keys_insert(grib_itrie* t,const char* key)
int* count;
GRIB_MUTEX_INIT_ONCE(&once,&init)
GRIB_MUTEX_LOCK(&mutex)
count=t->count;

View File

@ -21,6 +21,21 @@ static void init() {
pthread_mutex_init(&mutex,&attr);
pthread_mutexattr_destroy(&attr);
}
#elif GRIB_OMP_THREADS
static int once = 0;
static omp_nest_lock_t mutex;
static void init()
{
GRIB_OMP_CRITICAL(lock_grib_ibmfloat_c)
{
if (once == 0)
{
omp_init_nest_lock(&mutex);
once = 1;
}
}
}
#endif
typedef struct ibm_table_t ibm_table_t;

View File

@ -25,6 +25,21 @@ static void init() {
pthread_mutex_init(&mutex,&attr);
pthread_mutexattr_destroy(&attr);
}
#elif GRIB_OMP_THREADS
static int once = 0;
static omp_nest_lock_t mutex;
static void init()
{
GRIB_OMP_CRITICAL(lock_grib_ieeefloat_c)
{
if (once == 0)
{
omp_init_nest_lock(&mutex);
once = 1;
}
}
}
#endif
#if 1

View File

@ -38,6 +38,23 @@ static void init() {
pthread_mutexattr_destroy(&attr);
}
/* #elif GRIB_OMP_THREADS */
static int once = 0;
static omp_nest_lock_t mutex1;
static omp_nest_lock_t mutex2;
static void init()
{
GRIB_OMP_CRITICAL(lock_grib_index_c)
{
if (once == 0)
{
omp_init_nest_lock(&mutex1);
omp_init_nest_lock(&mutex2);
once = 1;
}
}
}
#endif

View File

@ -284,6 +284,21 @@ static void init() {
pthread_mutexattr_destroy(&attr);
}
#elif GRIB_OMP_THREADS
static int once = 0;
static omp_nest_lock_t mutex;
static void init()
{
GRIB_OMP_CRITICAL(lock_grib_itrie_c)
{
if (once == 0)
{
omp_init_nest_lock(&mutex);
once = 1;
}
}
}
#endif
struct grib_itrie {
grib_itrie* next[SIZE];

View File

@ -27,6 +27,21 @@ static void init() {
pthread_mutexattr_destroy(&attr);
}
#elif GRIB_OMP_THREADS
static int once = 0;
static omp_nest_lock_t mutex;
static void init()
{
GRIB_OMP_CRITICAL(lock_grib_memory_c)
{
if (once == 0)
{
omp_init_nest_lock(&mutex);
once = 1;
}
}
}
#endif
union align {

View File

@ -48,6 +48,31 @@ static void init()
pthread_mutex_init(&mutex_parse,&attr);
pthread_mutexattr_destroy(&attr);
}
#elif GRIB_OMP_THREADS
static int once = 0;
static omp_nest_lock_t mutex_file;
static omp_nest_lock_t mutex_rules;
static omp_nest_lock_t mutex_concept;
static omp_nest_lock_t mutex_hash_array;
static omp_nest_lock_t mutex_stream;
static omp_nest_lock_t mutex_parse;
static void init()
{
GRIB_OMP_CRITICAL(lock_grib_parse_utils_c)
{
if (once == 0)
{
omp_init_nest_lock(&mutex_file);
omp_init_nest_lock(&mutex_rules);
omp_init_nest_lock(&mutex_concept);
omp_init_nest_lock(&mutex_hash_array);
omp_init_nest_lock(&mutex_stream);
omp_init_nest_lock(&mutex_parse);
once = 1;
}
}
}
#endif
int grib_recompose_name(grib_handle* h, grib_accessor *observer, const char* uname, char* fname,int fail)

View File

@ -283,6 +283,21 @@ static void init() {
pthread_mutex_init(&mutex,&attr);
pthread_mutexattr_destroy(&attr);
}
#elif GRIB_OMP_THREADS
static int once = 0;
static omp_nest_lock_t mutex;
static void init()
{
GRIB_OMP_CRITICAL(lock_grib_trie_c)
{
if (once == 0)
{
omp_init_nest_lock(&mutex);
once = 1;
}
}
}
#endif
struct grib_trie {