~ubuntu-branches/debian/stretch/adios/stretch

« back to all changes in this revision

Viewing changes to .pc/mpi4py_fix.patch/wrappers/numpy/adios.pyx

  • Committer: Package Import Robot
  • Author(s): Alastair McKinstry
  • Date: 2014-06-16 23:06:38 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20140616230638-5a0z7ylxx8i0edrg
Tags: 1.7.0-1
* New upstream release.
* Add adios.pc pkgconfig file. adios_config now uses this.

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
 
5
5
 Copyright (c) 2008 - 2009.  UT-BATTELLE, LLC. All rights reserved.
6
6
"""
 
7
"""
 
8
 This is a cython file. To generate a CPP file, use the following command:
 
9
 $ cython --cplus adios.pyx
 
10
"""
7
11
 
8
12
import numpy as np
9
13
cimport numpy as np
10
14
 
11
 
import mpi4py.MPI as MPI 
12
 
cimport mpi4py.MPI as MPI
 
15
 
 
16
## This is a serial version of Adios. No use of MPI.
 
17
##import mpi4py.MPI as MPI 
 
18
##cimport mpi4py.MPI as MPI
13
19
 
14
20
import cython
15
21
cimport cython
16
22
 
17
 
## ==========
 
23
## ====================
18
24
## ADIOS Exported Functions
19
 
## ==========
 
25
## ====================
20
26
 
21
27
from libc.stdint cimport uint32_t, int64_t, uint64_t
22
28
from libc.stdlib cimport malloc, free
48
54
        pass
49
55
 
50
56
cdef extern from "adios.h":
51
 
    ctypedef struct MPI_Comm:
52
 
        pass
 
57
    ctypedef int MPI_Comm
 
58
    int MPI_COMM_WORLD
53
59
 
54
60
    ctypedef char* const_char_ptr "const char*"
55
61
 
61
67
                         char * group_name,
62
68
                         char * name, 
63
69
                         char * mode,
64
 
                         void * comm)
 
70
                         MPI_Comm comm)
65
71
    
66
72
    cdef int adios_group_size (int64_t fd_p,
67
73
                               uint64_t data_size,
108
114
                                  char * parameters,
109
115
                                  char * base_path)
110
116
 
 
117
cdef extern from "adios_selection.h":
 
118
    ctypedef enum ADIOS_SELECTION_TYPE:
 
119
        ADIOS_SELECTION_BOUNDINGBOX
 
120
        ADIOS_SELECTION_POINTS
 
121
        ADIOS_SELECTION_WRITEBLOCK
 
122
        ADIOS_SELECTION_AUTO
 
123
 
 
124
    ctypedef struct ADIOS_SELECTION_BOUNDINGBOX_STRUCT:
 
125
        int       ndim
 
126
        uint64_t *start
 
127
        uint64_t *count
 
128
 
 
129
    ctypedef struct ADIOS_SELECTION_POINTS_STRUCT:
 
130
        pass
 
131
 
 
132
    ctypedef struct ADIOS_SELECTION_WRITEBLOCK_STRUCT:
 
133
        pass
 
134
    
 
135
    ctypedef struct ADIOS_SELECTION_AUTO_STRUCT:
 
136
        pass
 
137
 
 
138
    cdef union ADIOS_SELECTION_UNION:
 
139
        ADIOS_SELECTION_BOUNDINGBOX_STRUCT bb
 
140
        ADIOS_SELECTION_POINTS_STRUCT points
 
141
        ADIOS_SELECTION_WRITEBLOCK_STRUCT block
 
142
        ADIOS_SELECTION_AUTO_STRUCT autosel
 
143
 
 
144
    ctypedef struct ADIOS_SELECTION:
 
145
        ADIOS_SELECTION_TYPE    type
 
146
        ADIOS_SELECTION_UNION   u
 
147
 
 
148
    cdef ADIOS_SELECTION * adios_selection_boundingbox (int ndim,
 
149
                                                        const uint64_t *start,
 
150
                                                        const uint64_t *count)
 
151
 
111
152
cdef extern from "adios_read.h":
112
 
    ctypedef struct MPI_Comm:
 
153
    ctypedef enum ADIOS_READ_METHOD:
 
154
        ADIOS_READ_METHOD_BP
 
155
        ADIOS_READ_METHOD_BP_AGGREGATE
113
156
        pass
114
157
 
 
158
    ctypedef enum ADIOS_LOCKMODE:
 
159
        ADIOS_LOCKMODE_NONE
 
160
        ADIOS_LOCKMODE_CURRENT
 
161
        ADIOS_LOCKMODE_ALL
 
162
 
115
163
    ctypedef struct ADIOS_FILE:
116
 
        uint64_t fh
117
 
        int      groups_count
118
 
        int      vars_count
119
 
        int      attrs_count
120
 
        int      tidx_start
121
 
        int      ntimesteps
122
 
        int      version
 
164
        uint64_t fh               
 
165
        int      nvars            
 
166
        char     ** var_namelist  
 
167
        int      nattrs           
 
168
        char     ** attr_namelist 
 
169
        int      nmeshes          
 
170
        char     ** mesh_namelist 
 
171
        int      current_step     
 
172
        int      last_step        
 
173
        char     *path            
 
174
        int      endianness       
 
175
        int      version          
123
176
        uint64_t file_size
124
 
        int      endianness
125
 
        char     ** group_namelist
126
 
        void     * internal_data
127
 
 
128
 
    ctypedef struct ADIOS_GROUP:
129
 
        uint64_t gh
130
 
        int      grpid
131
 
        int      vars_count
132
 
        char     ** var_namelist
133
 
        int      attrs_count
134
 
        char     ** attr_namelist
135
 
        ADIOS_FILE * fp
136
 
        int      timestep
137
 
        int      lasttimestep
138
177
 
139
178
    ctypedef struct ADIOS_VARINFO:
140
 
        int        grpid
141
179
        int        varid
142
 
        ADIOS_DATATYPES   type
143
 
        int        ndim
144
 
        uint64_t * dims
145
 
        int        timedim
146
 
        int        characteristics_count
147
 
        void     * value
148
 
        void     * gmin
149
 
        void     * gmax
150
 
        double   * gavg
151
 
        double   * gstd_dev
152
 
        void     ** mins
153
 
        void     ** maxs
154
 
        double   ** avgs
155
 
        double   ** std_devs
156
 
 
157
 
 
158
 
    cdef ADIOS_FILE * adios_fopen (char *, MPI_Comm)
159
 
    cdef int adios_fclose (ADIOS_FILE *)
160
 
 
161
 
    cdef ADIOS_GROUP * adios_gopen (ADIOS_FILE *, char *)
162
 
    cdef ADIOS_GROUP * adios_gopen_byid (ADIOS_FILE *, int)
163
 
    cdef int adios_gclose (ADIOS_GROUP *)
164
 
 
165
 
    cdef ADIOS_VARINFO * adios_inq_var (ADIOS_GROUP *, char *)
166
 
    cdef ADIOS_VARINFO * adios_inq_var_byid (ADIOS_GROUP *, int)
167
 
    cdef void adios_free_varinfo(ADIOS_VARINFO *)
168
 
    cdef int64_t adios_read_var_byid (ADIOS_GROUP *, int,
169
 
                             uint64_t *, uint64_t *,
170
 
                             void *)
171
 
 
172
 
    cdef char * adios_errmsg()
173
 
    cdef int adios_errno
174
 
 
175
 
 
176
 
## ==========
177
 
## ADIOS Enum
178
 
## ==========
179
 
 
180
 
class DATATYPE(object):
 
180
        ADIOS_DATATYPES type  
 
181
        int        ndim       
 
182
        uint64_t * dims       
 
183
        int        nsteps     
 
184
        void     * value      
 
185
        int      * nblocks    
 
186
        int        sum_nblocks
 
187
 
 
188
    cdef int adios_read_init_method (ADIOS_READ_METHOD method, 
 
189
                                     MPI_Comm comm, 
 
190
                                     char * parameters)
 
191
    cdef int adios_read_finalize_method(ADIOS_READ_METHOD method)
 
192
    cdef ADIOS_FILE * adios_read_open (const char * fname, 
 
193
                                       ADIOS_READ_METHOD method, 
 
194
                                       MPI_Comm comm, 
 
195
                                       ADIOS_LOCKMODE lock_mode,
 
196
                                       float timeout_sec)
 
197
    cdef ADIOS_FILE * adios_read_open_file (const char * fname, 
 
198
                                            ADIOS_READ_METHOD method, 
 
199
                                            MPI_Comm comm)
 
200
    cdef int adios_read_close (ADIOS_FILE *fp)
 
201
    cdef int adios_advance_step (ADIOS_FILE *fp, int last, float timeout_sec)
 
202
    cdef void adios_release_step (ADIOS_FILE *fp)
 
203
    cdef ADIOS_VARINFO * adios_inq_var (ADIOS_FILE *fp, const char * varname)
 
204
    cdef ADIOS_VARINFO * adios_inq_var_byid (ADIOS_FILE *fp, int varid)
 
205
    cdef void adios_free_varinfo (ADIOS_VARINFO *cp)
 
206
    cdef int adios_schedule_read (const ADIOS_FILE * fp,
 
207
                                  const ADIOS_SELECTION * sel,
 
208
                                  const char * varname,
 
209
                                  int from_steps,
 
210
                                  int nsteps,
 
211
                                  void * data)
 
212
    cdef int adios_schedule_read_byid (const ADIOS_FILE * fp, 
 
213
                                       const ADIOS_SELECTION * sel,
 
214
                                       int varid,
 
215
                                       int from_steps,
 
216
                                       int nsteps,
 
217
                                       void * data)
 
218
    cdef int adios_perform_reads (const ADIOS_FILE *fp, int blocking)
 
219
 
 
220
 
 
221
## ====================
 
222
## ADIOS Enum (public)
 
223
## ====================
 
224
 
 
225
class DATATYPE:
181
226
    unknown = -1
182
227
    byte = 0
183
228
    short = 1
194
239
    complex = 10
195
240
    double_complex = 11
196
241
 
197
 
class FLAG(object):
 
242
class FLAG:
198
243
    UNKNOWN = 0
199
244
    YES = 1
200
245
    NO = 2
201
246
    
202
 
class BUFFER_ALLOC_WHEN(object):
 
247
class BUFFER_ALLOC_WHEN:
203
248
    UNKNOWN = 0
204
249
    NOW = 1
205
250
    LATER = 2
206
251
    
207
 
## ==========
 
252
## ====================
208
253
## ADIOS Write API
209
 
## ==========
 
254
## ====================
210
255
 
211
 
cpdef init(char * config, MPI.Comm comm = MPI.COMM_WORLD):
212
 
    return adios_init(config, comm.ob_mpi)
 
256
cpdef init(char * config, MPI_Comm comm = MPI_COMM_WORLD):
 
257
    return adios_init(config, comm)
213
258
 
214
259
cpdef int64_t open(char * group_name,
215
260
                   char * name,
216
261
                   char * mode,
217
 
                   MPI.Comm comm = MPI.COMM_WORLD):
 
262
                   MPI_Comm comm = MPI_COMM_WORLD):
218
263
    cdef int64_t fd
219
264
    cdef int result
220
 
    result = adios_open(&fd, group_name, name, mode, comm.ob_mpi)
 
265
    result = adios_open(&fd, group_name, name, mode, comm)
221
266
    return fd
222
267
 
223
268
cpdef int64_t set_group_size(int64_t fd_p, uint64_t data_size):
255
300
cpdef finalize(int mype = 0):
256
301
    return adios_finalize(mype)
257
302
 
258
 
## ==========
 
303
## ====================
259
304
## ADIOS No-XML API
260
 
## ==========
261
 
cpdef int init_noxml(MPI.Comm comm = MPI.COMM_WORLD):
262
 
    return adios_init_noxml(comm.ob_mpi)
 
305
## ====================
 
306
cpdef int init_noxml(MPI_Comm comm = MPI_COMM_WORLD):
 
307
    return adios_init_noxml(comm)
263
308
 
264
309
cpdef int allocate_buffer(int when,
265
310
                          uint64_t buffer_size):
313
358
                                base_path)
314
359
 
315
360
 
316
 
## ==========
317
 
## ADIOS Read API
318
 
## ==========
 
361
## ====================
 
362
## ADIOS Read API (V2)
 
363
## ====================
319
364
 
320
 
cpdef type adios2nptype(ADIOS_DATATYPES t):
 
365
cdef type adios2nptype(ADIOS_DATATYPES t):
321
366
    cdef type ntype = None
322
367
    if t == adios_byte:
323
368
        ntype = np.int8
350
395
 
351
396
    return ntype
352
397
 
353
 
"""
354
 
cpdef int np2adiostype(np.dtype t):
355
 
    cdef int atype = -1
356
 
    if t.type == np.float64:
357
 
        atype = DATATYPE.double
358
 
    else:
359
 
        atype = -1
360
 
 
361
 
    return atype
362
 
"""
363
 
 
364
 
cdef printAdiosFile(ADIOS_FILE * f):
 
398
cdef printfile(ADIOS_FILE * f):
365
399
    print '%15s : %lu' % ('fh', f.fh)
366
 
    print '%15s : %d' % ('groups_count', f.groups_count)
367
 
    print '%15s : %d' % ('vars_count', f.vars_count)
368
 
    print '%15s : %d' % ('attrs_count', f.attrs_count)      
369
 
    print '%15s : %d' % ('tidx_start', f.tidx_start)       
370
 
    print '%15s : %d' % ('ntimesteps', f.ntimesteps)       
371
 
    print '%15s : %d' % ('version', f.version)          
 
400
    print '%15s : %d' % ('nvars', f.nvars)
 
401
    print '%15s : %s' % ('var_namelist', [f.var_namelist[i] for i in range(f.nvars)])
 
402
    print '%15s : %d' % ('nattrs', f.nattrs)
 
403
    print '%15s : %s' % ('attr_namelist', [f.attr_namelist[i] for i in range(f.nattrs)])
 
404
    print '%15s : %d' % ('current_step', f.current_step)       
 
405
    print '%15s : %d' % ('last_step', f.last_step)       
 
406
    print '%15s : %s' % ('path', f.path)
 
407
    print '%15s : %d' % ('endianness', f.endianness)       
 
408
    print '%15s : %d' % ('version', f.version)       
372
409
    print '%15s : %lu' % ('file_size', f.file_size)
373
 
    print '%15s : %d' % ('endianness', f.endianness)
374
 
    print '%15s : %s' % ('group_namelist', [f.group_namelist[i] for i in range(f.groups_count)])
375
 
 
376
 
cdef printAdiosGroup(ADIOS_GROUP * g):
377
 
    print '%15s : %lu' % ('gh', g.gh)
378
 
    print '%15s : %d' % ('grpid', g.grpid)
379
 
    print '%15s : %d' % ('vars_count', g.vars_count)
380
 
    print '%15s : %s' % ('var_namelist', [g.var_namelist[i] for i in range(g.vars_count)])
381
 
    print '%15s : %d' % ('attrs_count', g.attrs_count)      
382
 
    print '%15s : %s' % ('attr_namelist', [g.attr_namelist[i] for i in range(g.attrs_count)])
383
 
    print '%15s : %lu' % ('fp', <unsigned long> g.fp)
384
 
 
385
 
cdef printAdiosVariable(ADIOS_VARINFO * v):
386
 
    print '%15s : %d' % ('grpid', v.grpid)
 
410
 
 
411
cdef printvar(ADIOS_VARINFO * v):
387
412
    print '%15s : %d' % ('varid', v.varid)
388
413
    print '%15s : %s' % ('type', adios2nptype(v.type))
389
414
    print '%15s : %d' % ('ndim', v.ndim)
390
415
    print '%15s : %s' % ('dims', [v.dims[i] for i in range(v.ndim)])
391
 
    print '%15s : %d' % ('timedim', v.timedim)
392
 
 
393
 
cdef adios2scalar(ADIOS_DATATYPES t, void * val):
394
 
    if t == adios_byte :
395
 
        return (<char *> val)[0]
396
 
    elif t == adios_short:
397
 
        return (<short *> val)[0]
398
 
    elif t == adios_integer:
399
 
        return (<int *> val)[0]
400
 
    elif t == adios_long:
401
 
        return (<long *> val)[0]
402
 
    elif t == adios_unsigned_byte:
403
 
        return (<unsigned char *> val)[0]
404
 
    elif t == adios_unsigned_short:
405
 
        return (<unsigned short *> val)[0]
406
 
    elif t == adios_unsigned_integer:
407
 
        return (<unsigned int *> val)[0]
408
 
    elif t == adios_unsigned_long:
409
 
        return (<unsigned long *> val)[0]
410
 
    elif t == adios_real:
411
 
        return (<float *> val)[0]
412
 
    elif t == adios_double:
413
 
        return (<double *> val)[0]
414
 
    elif t == adios_long_double:
415
 
        return (<long double *> val)[0]
416
 
    else:
417
 
        return None
418
 
 
419
 
## ==========
420
 
## ADIOS Class Definition
421
 
## ==========
422
 
    
423
 
cdef class AdiosFile:
 
416
    print '%15s : %d' % ('nsteps', v.nsteps)
 
417
 
 
418
## ====================
 
419
## ADIOS Class Definitions for Read
 
420
## ====================
 
421
 
 
422
cpdef read_init(ADIOS_READ_METHOD method = ADIOS_READ_METHOD_BP,
 
423
                MPI_Comm comm = MPI_COMM_WORLD,
 
424
                char * parameters = ""):
 
425
    return adios_read_init_method (method, comm, parameters)
 
426
 
 
427
 
 
428
cpdef read_finalize(ADIOS_READ_METHOD method = ADIOS_READ_METHOD_BP):
 
429
    return adios_read_finalize_method (method)
 
430
 
 
431
cdef class file:
424
432
    """ Private Memeber """
425
433
    cpdef ADIOS_FILE * fp
426
434
 
427
435
    """ Public Memeber """
428
436
    cpdef public bytes name
429
 
    cpdef public int groups_count
430
 
    cpdef public int vars_count
431
 
    cpdef public int attrs_count
432
 
    cpdef public int tidx_start
433
 
    cpdef public int ntimesteps
 
437
    cpdef public int nvars
 
438
    cpdef public int nattrs
 
439
    cpdef public int current_step
 
440
    cpdef public int last_step
 
441
    cpdef public int endianness
434
442
    cpdef public int version
435
443
    cpdef public int file_size
436
 
    cpdef public int endianness
437
 
    
438
 
    cpdef public dict group
439
 
    
440
 
    def __init__(self, char * fname, MPI.Comm comm = MPI.COMM_WORLD):
 
444
    
 
445
    cpdef public dict var
 
446
    cpdef public dict attr
 
447
 
 
448
    def __init__(self, char * fname,
 
449
                 ADIOS_READ_METHOD method = ADIOS_READ_METHOD_BP,
 
450
                 MPI_Comm comm = MPI_COMM_WORLD):
441
451
        self.fp = NULL
442
 
        self.group = {}
443
 
        
444
 
        self.fp = adios_fopen(fname, comm.ob_mpi)
 
452
        self.var = {}
 
453
        self.attr = {}
 
454
 
 
455
        self.fp = adios_read_open_file(fname, method, comm)
445
456
        assert self.fp != NULL, 'Not an open file'
446
457
 
447
 
        self.name         = fname.split('/')[-1]  ## basename
448
 
        self.groups_count = self.fp.groups_count
449
 
        self.vars_count   = self.fp.vars_count  
450
 
        self.attrs_count  = self.fp.attrs_count 
451
 
        self.tidx_start   = self.fp.tidx_start  
452
 
        self.ntimesteps   = self.fp.ntimesteps  
453
 
        self.version      = self.fp.version     
454
 
        self.file_size    = self.fp.file_size   
455
 
        self.endianness   = self.fp.endianness  
 
458
        self.name = fname.split('/')[-1]  ## basename
 
459
        self.nvars = self.fp.nvars
 
460
        self.nattrs = self.fp.nattrs
 
461
        self.current_step = self.fp.current_step
 
462
        self.last_step = self.fp.last_step
 
463
        self.endianness = self.fp.endianness  
 
464
        self.version = self.fp.version     
 
465
        self.file_size = self.fp.file_size   
456
466
    
457
 
        cdef AdiosGroup g
458
 
        for grpname in [self.fp.group_namelist[i] for i in range(self.groups_count)]:
459
 
            g = AdiosGroup(self, grpname)
460
 
            self.group[grpname] = g
 
467
        for varname in [self.fp.var_namelist[i] for i in range(self.nvars)]:
 
468
            self.var[varname] = var(self, varname)
461
469
 
462
470
    def __del__(self):
463
 
        self.close()
 
471
            self.close()
464
472
            
465
473
    cpdef close(self):
466
474
        assert self.fp != NULL, 'Not an open file'
467
 
        for g in self.group.values():
468
 
            g.close()
469
 
        adios_fclose(self.fp)
 
475
        adios_read_close(self.fp)
470
476
        self.fp = NULL
471
477
        
472
478
    cpdef printself(self):
473
479
        assert self.fp != NULL, 'Not an open file'
474
480
        print '=== AdiosFile ==='
475
481
        print '%15s : %lu' % ('fp', <unsigned long> self.fp)
476
 
        printAdiosFile(self.fp)
477
 
 
478
 
 
479
 
cdef class AdiosGroup:
480
 
    """ Private Memeber """
481
 
    cdef AdiosFile file
482
 
    cdef ADIOS_GROUP * gp
483
 
 
484
 
    """ Public Memeber """
485
 
    cpdef public bytes name
486
 
    cpdef public int grpid
487
 
    cpdef public int vars_count
488
 
    cpdef public int attrs_count
489
 
    cpdef public int timestep
490
 
    cpdef public int lasttimestep
491
 
    
492
 
    cpdef public dict var
493
 
    
494
 
    def __init__(self, AdiosFile file, char * name):
495
 
        self.file = file
496
 
        self.var = {}
497
 
        
498
 
        self.gp = adios_gopen(self.file.fp, name)
499
 
        assert self.gp != NULL, 'Not an open group'
500
 
 
501
 
        self.name         = name
502
 
        self.grpid        = self.gp.grpid        
503
 
        self.vars_count   = self.gp.vars_count   
504
 
        self.attrs_count  = self.gp.attrs_count  
505
 
        self.timestep     = self.gp.timestep     
506
 
        self.lasttimestep = self.gp.lasttimestep 
507
 
        
508
 
        cdef AdiosVariable v
509
 
        for varname in [self.gp.var_namelist[i] for i in range(self.vars_count)]:
510
 
            v = AdiosVariable(self, varname)
511
 
            self.var[varname] = v
512
 
 
513
 
    def __del__(self):
514
 
        self.close()
515
 
 
516
 
    cpdef close(self):
517
 
        assert self.gp != NULL, 'Not an open file'
518
 
        for v in self.var.values():
519
 
            v.close()
520
 
        adios_gclose(self.gp)
521
 
        self.gp = NULL
522
 
        
523
 
    cpdef printself(self):
524
 
        assert self.gp != NULL, 'Not an open file'
525
 
        print '=== AdiosGroup ==='
526
 
        print '%15s : %lu' % ('gp', <unsigned long> self.gp)
527
 
        printAdiosGroup(self.gp)
528
 
        
529
 
cdef class AdiosVariable:
530
 
    """ Private Memeber """
531
 
    cdef AdiosGroup group
 
482
        printfile(self.fp)
 
483
 
 
484
cdef class var:
 
485
    """ Private Memeber """
 
486
    cdef file file
532
487
    cdef ADIOS_VARINFO * vp
533
488
 
534
489
    """ Public Memeber """
537
492
    cpdef public type type
538
493
    cpdef public int ndim
539
494
    cpdef public tuple dims
540
 
    cpdef public int timedim
541
 
    cpdef public int characteristics_count
542
 
    
543
 
    def __init__(self, AdiosGroup group, char * name):
544
 
        self.group = group
 
495
    cpdef public int nsteps
 
496
 
 
497
    def __init__(self, file file, char * name):
 
498
        self.file = file
545
499
        self.vp = NULL
546
500
 
547
 
        self.vp = adios_inq_var(self.group.gp, name)
548
 
        assert self.group.gp != NULL, 'Not an open group'
 
501
        assert self.file.fp != NULL, 'Not an open file'
 
502
        self.vp = adios_inq_var(self.file.fp, name)
 
503
        assert self.vp != NULL, 'Not a valid var'
549
504
 
550
 
        self.name                  = name
551
 
        self.varid                 = self.vp.varid                
552
 
        self.type                  = adios2nptype(self.vp.type)
553
 
        self.ndim                  = self.vp.ndim                 
554
 
        self.timedim               = self.vp.timedim              
555
 
        self.characteristics_count = self.vp.characteristics_count
556
 
        
 
505
        self.name = name
 
506
        self.varid = self.vp.varid                
 
507
        self.type = adios2nptype(self.vp.type)
 
508
        self.ndim = self.vp.ndim                 
557
509
        self.dims = tuple([self.vp.dims[i] for i in range(self.vp.ndim)])
 
510
        self.nsteps = self.vp.nsteps
558
511
        
559
512
    def __del__(self):
560
513
        self.close()
561
514
 
562
515
    cpdef close(self):
563
 
        assert self.vp != NULL, 'Not an open file'
 
516
        assert self.vp != NULL, 'Not an open var'
564
517
        adios_free_varinfo(self.vp)
565
518
        self.vp = NULL
566
 
        
567
 
    cpdef read(self, tuple offset = (), tuple count = ()):
568
 
        cdef type ntype = adios2nptype(self.vp.type)
569
 
        assert ntype is not None, 'Data type is not supported yet'
 
519
 
 
520
    cpdef read(self, tuple offset = (), tuple count = (), from_steps = 0, nsteps = 1):
 
521
        assert self.type is not None, 'Data type is not supported yet'
 
522
        assert from_steps + nsteps <= self.nsteps, 'Step index is out of range'
570
523
        
571
524
        cdef list lshape = [self.vp.dims[i] for i in range(self.vp.ndim)]
572
525
        cdef np.ndarray npshape = np.array(lshape, dtype=np.int64)
586
539
 
587
540
        assert npshape.ndim == npoffset.ndim, 'Offset dimension mismatch'
588
541
        assert npshape.ndim == npcount.ndim, 'Count dimension mismatch.'
589
 
        assert (npshape - npoffset > npcount).all(), 'Count is larger than shape.'
590
 
 
591
 
        cdef np.ndarray var = np.zeros(npcount, dtype=ntype)
592
 
        cdef int64_t nbytes = adios_read_var_byid(
593
 
            self.group.gp, 
594
 
            self.vp.varid, 
595
 
            <uint64_t *> npoffset.data, 
596
 
            <uint64_t *> npcount.data, 
597
 
            <void *> var.data
598
 
            )
599
 
 
600
 
        if nbytes < 0:
601
 
            print "[WARNING] bytes read :", nbytes
 
542
        assert (npshape - npoffset >= npcount).all(), 'Count is larger than shape.'
 
543
 
 
544
        shape = list(npcount)
 
545
        if (nsteps > 1):
 
546
            shape.insert(0, nsteps)
 
547
        cdef np.ndarray var = np.zeros(shape, dtype=self.type)
 
548
 
 
549
        cdef ADIOS_SELECTION * sel
 
550
        sel = adios_selection_boundingbox (self.vp.ndim, <uint64_t *> npoffset.data, <uint64_t *> npcount.data)
 
551
        
 
552
        adios_schedule_read_byid (self.file.fp, sel, self.vp.varid, from_steps, nsteps, <void *> var.data)
 
553
        adios_perform_reads(self.file.fp, 1)
602
554
 
603
555
        return var
604
556
 
605
557
    """ Print self """
606
558
    cpdef printself(self):
607
 
        assert self.vp != NULL, 'Not an open file'
 
559
        assert self.vp != NULL, 'Not an open variable'
608
560
        print '=== AdiosVariable ==='
609
561
        print '%15s : %lu' % ('vp', <unsigned long> self.vp)
610
 
        printAdiosVariable(self.vp)
 
562
        print '%15s : %lu' % ('fp', <unsigned long> self.file.fp)
 
563
        printvar(self.vp)
 
564
 
 
565
## ====================
 
566
## ADIOS Global functions
 
567
## ====================
 
568
 
 
569
def readvar(fname, varname):
 
570
    f = file(fname)
 
571
    if not f.var.has_key(varname):
 
572
        print "No valid variable"
 
573
        return
 
574
 
 
575
    v = f.var[varname]
 
576
    return v.read(from_steps=0, nsteps=v.nsteps)
 
577
 
 
578
def bpls(fname):
 
579
    f = file(fname)
 
580
    return {'nvars': f.nvars,
 
581
            'nattrs': f.nattrs,
 
582
            'vars': tuple([ k for k in f.var.iterkeys() ]),
 
583
            'attrs': tuple([ k for k in f.attr.iterkeys() ]),
 
584
            'time_steps': (f.current_step, f.last_step),
 
585
            'file_size': f.file_size}
 
586