SIONlib  2.0.0-rc.1
Scalable I/O library for parallel access to task-local files
sion_omp_gen.c
1 /****************************************************************************
2 ** SIONLIB http://www.fz-juelich.de/jsc/sionlib **
3 *****************************************************************************
4 ** Copyright (c) 2008-2018 **
5 ** Forschungszentrum Juelich, Juelich Supercomputing Centre **
6 ** **
7 ** See the file COPYRIGHT in the package base directory for details **
8 ****************************************************************************/
9 
10 #define _XOPEN_SOURCE 700
11 
12 #include <omp.h>
13 #include <stdint.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 
17 #include "sion_common.h"
18 #include "sion_const.h"
19 #include "sion_debug.h"
20 #include "sion_error_handler.h"
21 #include "sion_fd.h"
22 #include "sion_filedesc.h"
23 #include "sion_flags.h"
24 #include "sion_generic.h"
25 #include "sion_internal.h"
26 #include "sion_omp_cb_gen.h"
27 #include "sion_omp_internal_gen.h"
28 
29 #ifdef SION_OMP
30 
31 int32_t _sion_omp_api_aid = -1;
32 static omp_lock_t _sion_omp_lock_data;
33 
34 int _sion_omp_user_lock(void *data)
35 {
36  int rc = SION_SUCCESS;
37  omp_set_lock(&_sion_omp_lock_data);
38  return rc;
39 }
40 int _sion_omp_user_unlock(void *data)
41 {
42  int rc = SION_SUCCESS;
43  omp_unset_lock(&_sion_omp_lock_data);
44  return rc;
45 }
46 
47 int sion_paropen_omp(const char *fname, const char *file_mode, int64_t *chunksize, int32_t *fsblksize, int *globalrank,
48  FILE **fileptr, char **newfname)
49 {
50  int sid = SION_ID_UNDEF;
51  int filenumber, num_threads, thread_num;
52  int numFiles = 1, gtasks, gRank, lRank, lSize;
53  _omp_api_commdata *gen_gcomm;
54  _sion_flags_store *flags_store = NULL;
55 
56  thread_num = omp_get_thread_num();
57  num_threads = omp_get_num_threads();
58 
59 #pragma omp master
60  {
61  _sion_debug_set_query_thread_num_function(omp_get_thread_num);
62  _sion_error_set_query_thread_num_function(omp_get_thread_num);
63  omp_init_lock(&_sion_omp_lock_data);
64  sion_lock_register_lock_callbacks(_sion_omp_user_lock, _sion_omp_user_unlock, &_sion_omp_lock_data);
65  }
66  {
67 #pragma omp barrier
68  }
69 
70  DPRINTFP((1, "sion_paropen_omp", thread_num, "enter parallel open of file %s\n", fname));
71 
72  flags_store = _sion_parse_flags(file_mode);
73  if (!flags_store) {
74  return _sion_errorprint_omp(
75  SION_ID_NOT_VALID, _SION_ERROR_RETURN, "sion_paropen_omp: could not parse file mode in %s, aborting ...\n", file_mode);
76  }
77 
78  if (flags_store->mask & _SION_FMODE_WRITE) {
79  /* file mode WRITE */
80  *globalrank = thread_num;
81  }
82  _sion_flags_destroy_store(&flags_store);
83 
84 #pragma omp master
85  {
86  /* register callbacks for generic interface */
87  if (_sion_omp_api_aid < 0) {
88  _sion_omp_api_aid = _sion_register_callbacks_omp();
89  }
90  }
91 
92  /* create generic communicator container */
93  gen_gcomm = malloc(sizeof(_omp_api_commdata));
94  if (gen_gcomm == NULL) {
95  return _sion_errorprint(SION_ID_NOT_VALID, _SION_ERROR_RETURN,
96  "cannot allocate omp internal data structure of size %lu (_omp_api_commdata), aborting ...\n",
97  (unsigned long)sizeof(_omp_api_commdata));
98  }
99  gen_gcomm->commset = 1;
100  gen_gcomm->thread_num = thread_num;
101  gen_gcomm->num_threads = num_threads;
102 
103  /* sync to ensure that aid is accessible */
104  _sion_omp_barrier_cb(gen_gcomm);
105 
106  lRank = gRank = thread_num;
107  lSize = gtasks = num_threads;
108  filenumber = 0;
109  numFiles = 1;
110 
111  DPRINTFP(
112  (1, "sion_paropen_omp", gRank, "enter parallel open of %d files (current name %s) in %s mode\n", numFiles, fname, file_mode));
113  sid = sion_generic_paropen(_sion_omp_api_aid, fname, file_mode, chunksize, fsblksize, gen_gcomm, gRank, gtasks, &filenumber,
114  &numFiles, &lRank, &lSize, fileptr, newfname);
115  DPRINTFP((1, "sion_paropen_omp", gRank, "leave parallel open of %d files in %s mode #tasks=%d sid=%d\n", numFiles, file_mode,
116  lSize, sid));
117 
118  /* test return code from internal open */
119  if (sid == SION_ID_NOT_VALID) {
120  return _sion_errorprint_omp(
121  SION_ID_NOT_VALID, _SION_ERROR_RETURN, "sion_paropen_omp: invalid return code from internal open %d", sid);
122  }
123 
124  DPRINTFP((1, "sion_paropen_omp", thread_num, "leave parallel open of file %s sid=%d\n", fname, sid));
125 
126  return sid;
127 }
128 
129 int sion_parclose_omp(int sid)
130 {
131  int rc = SION_SUCCESS;
132  ONLY_DEBUG(int thread_num;)
133  _sion_filedesc *sion_filedesc;
134 
135  if ((sid < 0) || (_sion_vcdtype(sid) != SION_FILEDESCRIPTOR) || !(sion_filedesc = _sion_vcdtovcon(sid))) {
136  return _sion_errorprint_omp(SION_NOT_SUCCESS, _SION_ERROR_RETURN, "sion_parclose_omp: invalid sion_filedesc %d", sid);
137  }
138 
139  ONLY_DEBUG(thread_num = omp_get_thread_num();)
140 
141  if (omp_get_num_threads() != sion_filedesc->ntasks) {
142  return _sion_errorprint_omp(SION_NOT_SUCCESS, _SION_ERROR_RETURN,
143  "sion_parclose_omp: invalid number of OpenMP threads, %d <> %d", omp_get_num_threads(), sion_filedesc->ntasks);
144  }
145 
146  DPRINTFP((1, "sion_parclose_omp", thread_num, "enter parallel close of sid %d\n", sid));
147  DPRINTFP((1, "sion_parclose_omp", thread_num, "closing %d file(s)\n", sion_filedesc->nfiles));
148 
149  rc = sion_generic_parclose(sid);
150 
151  DPRINTFP((1, "sion_parclose_omp", thread_num, "leave parallel close of sid %d rc=%d\n", sid, rc));
152  return rc;
153 }
154 
155 /* end of ifdef OMP */
156 #endif
int sion_parclose_omp(int sid)
closes a SION file previously opened in OpenMP mode
Definition: sion_omp_gen.c:129
int sion_generic_paropen(int aid, const char *fname, const char *file_mode, int64_t *chunksize, int32_t *fsblksize, void *gcommgroup, int grank, int gsize, int *filenumber, int *numfiles, const int *lrank, const int *lsize, FILE **fileptr, char **newfname)
Open a sion file a generic interface.
Definition: sion_generic.c:362
int sion_lock_register_lock_callbacks(int lock(void *), int unlock(void *), void *lock_data)
Function which registers callback funtions for lock and unlock internal access to shared data structu...
Definition: sion_common.c:774
int sion_paropen_omp(const char *fname, const char *file_mode, int64_t *chunksize, int32_t *fsblksize, int *globalrank, FILE **fileptr, char **newfname)
Open a sion file using OpenMP.
Definition: sion_omp_gen.c:47