KTH framework for Nek5000 toolboxes; testing version  0.0.1
tsrs_IO.f
Go to the documentation of this file.
1 
8 !=======================================================================
14  subroutine tsrs_mfo_outfld(bff,lbff)
15  implicit none
16 
17  include 'SIZE'
18  include 'INPUT'
19  include 'PARALLEL'
20  include 'RESTART'
21  include 'TSTEP'
22  include 'TSRSD'
23 
24  ! argument list
25  integer lbff
26  real bff(lbff)
27 
28  ! local variables
29  real tiostart, tio ! timing
30  integer itmp
31  integer ierr ! error flag
32  character*3 prefix ! file prefix
33 
34  integer wdsizol ! store global wdsizo
35  integer nptsB ! running sum for npts
36  integer nelBl ! store global nelB
37 
38  integer*8 offs0, offs ! offset
39  integer*8 stride,stridebt,stridebf ! stride
40 
41  integer ioflds ! fields count
42 
43  integer wdsizet, wdsizef ! precission of writing coordinates/time and fields
44 
45 ! byte sum
46  real dnbyte
47 
48 ! functions
49  integer igl_running_sum
50  real dnekclock_sync, glsum
51 !----------------------------------------------------------------------
52  tiostart=dnekclock_sync()
53  call io_init
54  ! this is mesh speciffic, so some variables must be overwritten
55  ! how many ele are present up to rank nid
56  itmp = tsrs_npts
57  nptsb = igl_running_sum(itmp)
58  nptsb = nptsb - tsrs_npts
59  ! replace value
60  nelbl = nelb
61  nelb = nptsb
62 
63  ! force double precission for coordinates and time, but single for fileds
64  wdsizol = wdsizo
65  wdsizet = wdsize
66  wdsizef = 4
67  wdsizo = wdsizet
68 
69  ! open files on i/o nodes
70  prefix='pts'
71  ierr=0
72  if (nid.eq.pid0) call mfo_open_files(prefix,ierr)
73  call mntr_check_abort(tsrs_id,ierr,
74  $ 'Error openning file')
75 
76  ! write header
77  call tsrs_mfo_write_hdr(wdsizet,wdsizef)
78 
79  ! initial offset: header, test pattern, tiem list, global ordering
80  offs0 = iheadersize + 4 + wdsizo*tsrs_ntsnap + isize*tsrs_nptot
81  offs = offs0
82  ! stride
83  stridebt = nelb * wdsizet
84  stridebf = nelb * wdsizef
85  stride = tsrs_nptot * wdsizet
86 
87  ! count fields
88  ioflds = 0
89 
90  ! write coordinates in a single call
91  ! offset
92  offs = offs0 + stride*ioflds + stridebt*ndim
93  call byte_set_view(offs,ifh_mbyte)
94  itmp = tsrs_npts*ndim
95  call tsrs_mfo_outs(tsrs_pts,itmp,ierr)
96  call mntr_check_abort(tsrs_id,ierr,
97  $ 'Error writing point coordinates')
98  ioflds = ioflds + ndim
99 
100  ! write fields in a single call
101  wdsizo = wdsizef
102  ! offset
103  offs = offs0 + stride*ioflds + stridebf*tsrs_nfld*tsrs_ntsnap
104  call byte_set_view(offs,ifh_mbyte)
105  itmp = tsrs_npts*tsrs_nfld*tsrs_ntsnap
106  call tsrs_mfo_outs(bff,itmp,ierr)
107  call mntr_check_abort(tsrs_id,ierr,
108  $ 'Error writing interpolated field')
109  ioflds = ioflds + tsrs_nfld*tsrs_ntsnap
110 
111  if (nid.eq.pid0) then
112  if(ifmpiio) then
113  call byte_close_mpi(ifh_mbyte,ierr)
114  else
115  call byte_close(ierr)
116  endif
117  endif
118  call mntr_check_abort(tsrs_id,ierr,
119  $ 'Error closing file')
120 
121  ! count bytes
122  dnbyte = 1.*ioflds*tsrs_npts*wdsizo
123  tio = dnekclock_sync()-tiostart
124  if (tio.le.0) tio=1.
125 
126  ! this is not exact right now as there are different real precisons
127  dnbyte = glsum(dnbyte,1)
128  dnbyte = dnbyte + iheadersize + 4. + wdsizo * tsrs_ntsnap +
129  $ isize*tsrs_nptot
130 
131  dnbyte = dnbyte/1024/1024
132  if(nio.eq.0) write(6,7) istep,time,dnbyte,dnbyte/tio,
133  & nfileo
134  7 format(/,i9,1pe12.4,' done :: Write checkpoint',/,
135  & 30x,'file size = ',3pg12.2,'MB',/,
136  & 30x,'avg data-throughput = ',0pf7.1,'MB/s',/,
137  & 30x,'io-nodes = ',i5,/)
138 
139  ! restore old values
140  wdsizo = wdsizol
141  nelb = nelbl
142 
143  return
144  end subroutine
145 !=======================================================================
151  subroutine tsrs_mfo_write_hdr(wdsizet, wdsizef)
152  implicit none
153 
154  include 'SIZE'
155  include 'INPUT'
156  include 'RESTART'
157  include 'PARALLEL'
158  include 'TSTEP'
159  include 'TSRSD'
160 
161  ! argument list
162  integer wdsizet, wdsizef
163 
164  ! local variables
165  integer ierr ! error flag
166  real*4 test_pattern ! byte key
167  integer lglist(0:lhis) ! dummy array
168 
169  integer idum, inelp
170  integer nelo ! number of points to write
171  integer nfileoo ! number of files to create
172 
173  integer jl ! loop index
174  integer mtype ! tag
175 
176  integer ibsw_out, len
177  integer*8 ioff ! offset
178 
179  character*132 hdr ! header
180 !----------------------------------------------------------------------
181  ierr = 0
182 
183  call nekgsync()
184  idum = 1
185 
186  if(ifmpiio) then
187  nfileoo = 1 ! all data into one file
188  nelo = tsrs_nptot
189  else
190  nfileoo = nfileo
191  if(nid.eq.pid0) then ! how many elements to dump
192  nelo = tsrs_npts
193  do jl = pid0+1,pid1
194  mtype = jl
195  call csend(mtype,idum,4,jl,0) ! handshake
196  call crecv(mtype,inelp,4)
197  nelo = nelo + inelp
198  enddo
199  else
200  mtype = nid
201  call crecv(mtype,idum,4) ! hand-shake
202  call csend(mtype,tsrs_npts,4,pid0,0) ! u4 :=: u8
203  endif
204  endif
205 
206  ! write a header
207  if(nid.eq.pid0) then
208  call blank(hdr,132)
209 
210  write(hdr,1) wdsizet,wdsizef,ldim,nelo,tsrs_nptot,tsrs_ntsnap,
211  $ tsrs_nfld,time,fid0,nfileoo
212  1 format('#std',1x,i1,1x,i1,1x,i1,1x,i10,1x,i10,1x,i10,1x,i4,1x,
213  $ e20.13,1x,i6,1x,i6)
214 
215  ! write test pattern for byte swap
216  test_pattern = 6.54321
217 
218  len = wdsizo/4 * tsrs_ntsnap
219  if(ifmpiio) then
220  ! only rank0 (pid00) will write hdr + test_pattern + time list
221  call byte_write_mpi(hdr,iheadersize/4,pid00,ifh_mbyte,ierr)
222  call byte_write_mpi(test_pattern,1,pid00,ifh_mbyte,ierr)
223  call byte_write_mpi(tsrs_tmlist,len,pid00,ifh_mbyte,ierr)
224  else
225  call byte_write(hdr,iheadersize/4,ierr)
226  call byte_write(test_pattern,1,ierr)
227  call byte_write(tsrs_tmlist,len,ierr)
228  endif
229  endif
230 
231  call mntr_check_abort(tsrs_id,ierr,
232  $ 'Error writing header')
233 
234  ! write global point number
235  if(nid.eq.pid0) then
236  if(ifmpiio) then
237  ioff = iheadersize + 4 + wdsizo * tsrs_ntsnap + nelb*isize
238  call byte_set_view (ioff,ifh_mbyte)
239  call byte_write_mpi(tsrs_ipts,tsrs_npts,-1,ifh_mbyte,ierr)
240  else
241  call byte_write(tsrs_ipts,tsrs_npts,ierr)
242  endif
243 
244  do jl = pid0+1,pid1
245  mtype = jl
246  call csend(mtype,idum,isize,jl,0) ! handshake
247  len = isize*(lhis+1)
248  call crecv(mtype,lglist,len)
249  if(ierr.eq.0) then
250  if(ifmpiio) then
251  call byte_write_mpi
252  $ (lglist(1),lglist(0),-1,ifh_mbyte,ierr)
253  else
254  call byte_write(lglist(1),lglist(0),ierr)
255  endif
256  endif
257  enddo
258  else
259  mtype = nid
260  call crecv(mtype,idum,isize) ! hand-shake
261 
262  lglist(0) = tsrs_npts
263  call icopy(lglist(1),tsrs_ipts,tsrs_npts)
264 
265  len = isize*(tsrs_npts+1)
266  call csend(mtype,lglist,len,pid0,0)
267  endif
268 
269  call mntr_check_abort(tsrs_id,ierr,
270  $ 'Error writing global point numbers')
271 
272  return
273  end subroutine
274 !=======================================================================
284  subroutine tsrs_mfo_outs(ul,lpts,ierr)
285  implicit none
286 
287  include 'SIZE'
288  include 'INPUT'
289  include 'PARALLEL'
290  include 'RESTART'
291  include 'TSRSD'
292 
293  ! global memory space
294  ! dummy arrays
295  integer lw2
296  parameter(lw2=tsrs_nfld*tsrs_ltsnap*lhis)
297  real*4 u4(2+2*lw2)
298  common /scrvh/ u4
299  real*8 u8(1+lw2)
300  equivalence(u4,u8)
301 
302  ! argument list
303  real ul(lpts)
304  integer lpts, ierr
305 
306  ! local variables
307  integer len, leo, nout
308  integer idum
309  integer kl, mtype
310 !----------------------------------------------------------------------
311  ! clear outstanding message queues
312  call nekgsync()
313 
314  len = 8 + 8*lw2 ! recv buffer size
315  leo = 8 + wdsizo*lpts
316  idum = 1
317  ierr = 0
318 
319  if (nid.eq.pid0) then
320 
321  if (wdsizo.eq.4) then ! 32-bit output
322  call copyx4 (u4,ul,lpts)
323  else
324  call copy (u8,ul,lpts)
325  endif
326  nout = wdsizo/4 * lpts
327  if(ierr.eq.0) then
328  if(ifmpiio) then
329  call byte_write_mpi(u4,nout,-1,ifh_mbyte,ierr)
330  else
331  call byte_write(u4,nout,ierr) ! u4 :=: u8
332  endif
333  endif
334 
335  ! write out the data of my childs
336  idum = 1
337  do kl= pid0+1, pid1
338  mtype = kl
339  call csend(mtype,idum,4,kl,0) ! handshake
340  call crecv(mtype,u4,len)
341  nout = wdsizo/4 * u8(1)
342  if (wdsizo.eq.4.and.ierr.eq.0) then
343  if(ifmpiio) then
344  call byte_write_mpi(u4(3),nout,-1,ifh_mbyte,ierr)
345  else
346  call byte_write(u4(3),nout,ierr)
347  endif
348  elseif(ierr.eq.0) then
349  if(ifmpiio) then
350  call byte_write_mpi(u8(2),nout,-1,ifh_mbyte,ierr)
351  else
352  call byte_write(u8(2),nout,ierr)
353  endif
354  endif
355  enddo
356 
357  else
358 
359  u8(1)= lpts
360  if (wdsizo.eq.4) then ! 32-bit output
361  call copyx4 (u4(3),ul,lpts)
362  else
363  call copy (u8(2),ul,lpts)
364  endif
365 
366  mtype = nid
367  call crecv(mtype,idum,4) ! hand-shake
368  call csend(mtype,u4,leo,pid0,0) ! u4 :=: u8
369 
370  endif
371 
372  return
373  end subroutine
374 !=======================================================================
377  subroutine tsrs_mfi_points()
378  implicit none
379 
380  include 'SIZE'
381  include 'INPUT'
382  include 'RESTART'
383  include 'PARALLEL'
384  include 'FRAMELP'
385  include 'TSRSD'
386 
387  ! global data structures
388  integer mid,mp,nekcomm,nekgroup,nekreal
389  common /nekmpi/ mid,mp,nekcomm,nekgroup,nekreal
390 
391  ! local variables
392  integer il, jl ! loop index
393  integer ierr ! error flag
394  integer ldiml ! dimesion of interpolation file
395  integer nptsr ! number of points in the file
396  integer npass ! number of messages to send
397  integer itmp_ipts(lhis)
398  real rtmp_pts(ldim,lhis)
399  real*4 rbuffl(2*ldim*lhis)
400  real rtmp1, rtmp2
401  character*132 fname ! file name
402  integer hdrl
403  parameter(hdrl=32)
404  character*32 hdr ! file header
405  character*4 dummy
406  real*4 bytetest
407  integer iglob ! global point numbering
408 
409  ! functions
410  logical if_byte_swap_test
411 !-----------------------------------------------------------------------
412  ! master opens files and gets point number
413  ierr = 0
414  if (nid.eq.pid00) then
415  !open the file
416  fname='int_pos'
417  call byte_open(fname,ierr)
418 
419  ! read header
420  call blank (hdr,hdrl)
421  call byte_read (hdr,hdrl/4,ierr)
422  if (ierr.ne.0) goto 101
423 
424  ! big/little endian test
425  call byte_read (bytetest,1,ierr)
426  if(ierr.ne.0) goto 101
427  if_byte_sw = if_byte_swap_test(bytetest,ierr)
428  if(ierr.ne.0) goto 101
429 
430  ! extract header information
431  read(hdr,*,iostat=ierr) dummy, wdsizr, ldiml, nptsr
432  endif
433 
434  101 continue
435 
436  call mntr_check_abort(tsrs_id,ierr,
437  $ 'tsrs_mfi_points: Error opening point files')
438 
439  ! broadcast header data
440  call bcast(wdsizr,isize)
441  call bcast(ldiml,isize)
442  call bcast(nptsr,isize)
443  call bcast(if_byte_sw,lsize)
444 
445  ! check dimension consistency
446  if (ldim.ne.ldiml) call mntr_abort(tsrs_id,
447  $ 'tsrs_mfi_points: Inconsisten dimension.')
448 
449  ! calculate point distribution; I assume it is post-processing
450  ! done on small number of cores, so I assume nptsr >> mp
451  tsrs_nptot = nptsr
452  tsrs_npts = nptsr/mp
453  if (tsrs_npts.gt.0) then
454  tsrs_npt1 = mod(tsrs_nptot,mp)
455  else
456  tsrs_npt1 = tsrs_nptot
457  endif
458  if (nid.lt.tsrs_npt1) tsrs_npts = tsrs_npts +1
459 
460  ! stamp logs
461  call mntr_logi(tsrs_id,lp_prd,
462  $ 'Interpolation point number :', tsrs_nptot)
463 
464  ierr = 0
465  if (tsrs_npts.gt.lhis) ierr = 1
466  call mntr_check_abort(tsrs_id,ierr,
467  $ 'tsrs_mfi_points: lhis too small')
468 
469  ! read and redistribute points
470  ! this part is not optimised, but it is post-processing
471  ! done locally, so I don't care
472  if (nid.eq.pid00) then
473  if (tsrs_nptot.gt.0) then
474  ! read points for the master rank
475  ldiml = ldim*tsrs_npts*wdsizr/4
476  call byte_read (rbuffl,ldiml,ierr)
477 
478  ! get byte shift
479  if (if_byte_sw) then
480  if(wdsizr.eq.8) then
481  call byte_reverse8(rbuffl,ldiml,ierr)
482  else
483  call byte_reverse(rbuffl,ldiml,ierr)
484  endif
485  endif
486 
487  ! copy data
488  ldiml = ldim*tsrs_npts
489  if (wdsizr.eq.4) then
490  call copy4r(tsrs_pts,rbuffl,ldiml)
491  else
492  call copy(tsrs_pts,rbuffl,ldiml)
493  endif
494  ! provide global point numbers
495  iglob = 0
496  do il = 1, tsrs_npts
497  iglob = iglob + 1
498  tsrs_ipts(il) = iglob
499  enddo
500 
501  ! redistribute rest of points
502  npass = min(mp,tsrs_nptot)
503  do il = 1,npass-1
504  nptsr = tsrs_npts
505  if (tsrs_npt1.gt.0.and.il.ge.tsrs_npt1) then
506  nptsr = tsrs_npts -1
507  endif
508  ! read points for the slave rank
509  ldiml = ldim*nptsr*wdsizr/4
510  call byte_read (rbuffl,ldiml,ierr)
511 
512  ! get byte shift
513  if (if_byte_sw) then
514  if(wdsizr.eq.8) then
515  call byte_reverse8(rbuffl,ldiml,ierr)
516  else
517  call byte_reverse(rbuffl,ldiml,ierr)
518  endif
519  endif
520 
521  ! copy data
522  ldiml = ldim*nptsr
523  if (wdsizr.eq.4) then
524  call copy4r(rtmp_pts,rbuffl,ldiml)
525  else
526  call copy(rtmp_pts,rbuffl,ldiml)
527  endif
528  ! provide global point numbers
529  do jl = 1, nptsr
530  iglob = iglob + 1
531  itmp_ipts(jl) = iglob
532  enddo
533 
534  ! send data
535  ldiml = ldiml*wdsizr
536  call csend(il,rtmp_pts,ldiml,il,jl)
537  ldiml = nptsr*isize
538  call csend(il,itmp_ipts,ldiml,il,jl)
539  enddo
540  endif
541  else
542  if (tsrs_npts.gt.0) then
543  call crecv2(nid,tsrs_pts,ldim*tsrs_npts*wdsize,0)
544  call crecv2(nid,tsrs_ipts,tsrs_npts*isize,0)
545  endif
546  endif
547 
548  ! master closes files
549  if (nid.eq.pid00) then
550  call byte_close(ierr)
551  endif
552 
553  return
554  end subroutine
555 !=======================================================================
556 
557 
558 
559 
560 
#define byte_reverse8
Definition: byte.c:34
#define byte_open
Definition: byte.c:35
#define byte_reverse
Definition: byte.c:33
#define byte_write
Definition: byte.c:39
#define byte_close
Definition: byte.c:36
#define byte_read
Definition: byte.c:38
subroutine byte_write_mpi(buf, icount, iorank, mpi_fh, ierr)
Definition: byte_mpi.f:65
subroutine byte_close_mpi(mpi_fh, ierr)
Definition: byte_mpi.f:84
subroutine byte_set_view(ioff_in, mpi_fh)
Definition: byte_mpi.f:97
subroutine crecv2(mtype, buf, lenm, jnid)
Definition: comm_mpi.f:333
subroutine crecv(mtype, buf, lenm)
Definition: comm_mpi.f:313
subroutine nekgsync()
Definition: comm_mpi.f:502
subroutine csend(mtype, buf, len, jnid, jpid)
Definition: comm_mpi.f:303
subroutine bcast(buf, len)
Definition: comm_mpi.f:431
subroutine mntr_logi(mid, priority, logs, ivar)
Write log message adding single integer.
Definition: mntrlog.f:709
subroutine mntr_abort(mid, logs)
Abort simulation.
Definition: mntrlog.f:837
subroutine mntr_check_abort(mid, ierr, logs)
Abort simulation.
Definition: mntrlog.f:856
subroutine tsrs_mfi_points()
Read interpolation points positions, number and redistribute them.
Definition: tsrs_IO.f:378
subroutine tsrs_mfo_write_hdr(wdsizet, wdsizef)
Write file header.
Definition: tsrs_IO.f:152
subroutine tsrs_mfo_outfld(bff, lbff)
Write a point history file.
Definition: tsrs_IO.f:15
subroutine tsrs_mfo_outs(ul, lpts, ierr)
Write single field for a local set of points.
Definition: tsrs_IO.f:285
subroutine icopy(a, b, n)
Definition: math.f:289
subroutine copy(a, b, n)
Definition: math.f:260
subroutine blank(A, N)
Definition: math.f:19
subroutine mfo_open_files(prefix, ierr)
Definition: prepost.f:1071
subroutine copy4r(a, b, n)
Definition: prepost.f:568
subroutine copyx4(a, b, n)
Definition: prepost.f:560
subroutine io_init
Definition: prepost.f:1024