KMR
kmrf.F90
Go to the documentation of this file.
1 ! kmrf.F90 (2014-02-04) -*-Mode: F90;-*-
2 ! Copyright (C) 2012-2018 RIKEN R-CCS
3 
4 !> \file kmrf.F90 KMR Fortran Binding. KMR mixes-up integers,
5 !> doubles, and pointers in passing key-value pairs as all occupy 8
6 !> bytes, and KMR defines bad-mannered converters. See the definition
7 !> kmr_kv_box in C, where it uses union, but just long in the Fortran
8 !> binding. Options to mapper/shuffler/reducer are integers (c_int)
9 !> in Fortran, instead of structures in C. So, zero means no options.
10 !> Integer options are converted to long-integers (c_int to c_long)
11 !> before passing to C routines. This conversion is needed because
12 !> structure options are compatible to long-integers. Conversion is
13 !> done by kmr_fixopt() defined here.
14 
15 ! NOTE: Excessive "bind(c)" are added to procedure dummy arguments
16 ! (i.e., parameters of procedure types), although their associated
17 ! type definitions are already with bind(c). It is illegally required
18 ! by some versions of gcc-4.4.x (OK for gcc-4.7, 4.8, and 4.9).
19 
20 ! MEMO: It does not assume MPI Fortran-binding is by modules. So,
21 ! some paramters are just c_int instread of specific kinds.
22 
23 ! MEMO: c_sizeof is only defined in F2008, not F2003.
24 
25 module kmrf
26  !use, intrinsic :: iso_c_binding
27  use iso_c_binding
28  implicit none
29 
30  public
31 
32  !> Data presentation of keys and values (see ::kmr_kv_field in C).
33 
34  integer(c_int), parameter :: &
35  kmr_kv_opaque = 1, &
36  kmr_kv_cstring = 2, &
37  kmr_kv_integer = 3, &
38  kmr_kv_float8 = 4, &
39  kmr_kv_pointer_owned = 5, &
40  kmr_kv_pointer_unmanaged = 6
41 
42  !> Option bits (OR'ed) to mapping and reduction (see ::kmr_option).
43 
44  integer(c_int), parameter :: &
45  kmr_nothreading = ishft(1,0), &
46  kmr_inspect = ishft(1,1), &
47  kmr_keep_open = ishft(1,2), &
48  kmr_key_as_rank = ishft(1,3), &
49  kmr_rank_zero = ishft(1,4)
50 
51  !> Option bits (OR'ed) to mapping on files (see ::kmr_file_option).
52 
53  integer(c_int), parameter :: &
54  kmr_each_rank = ishft(1,0), &
55  kmr_subdirectories = ishft(1,1), &
56  kmr_list_file = ishft(1,2), &
57  kmr_shuffle_names = ishft(1,3)
58 
59  !> Option bits (OR'ed) to mapping by spawning (see ::kmr_spawn_option).
60 
61  integer(c_int), parameter :: &
62  kmr_separator_space = ishft(1,0), &
63  kmr_reply_each = ishft(1,1), &
64  kmr_reply_root = ishft(1,2), &
65  kmr_one_by_one = ishft(1,3)
66 
67  !> struct kmr_kv_box {int klen; int vlen; unit_sized k; unit_sized v;};
68 
69  type, bind(c) :: kmr_kv_box
70  !sequence
71  integer(c_int) :: klen, vlen
72  integer(c_long) :: k, v
73  end type kmr_kv_box
74 
75  type, bind(c) :: kmr_spawn_info
76  type(c_ptr) :: maparg
77  integer(c_long) :: icomm_cc
78  integer(c_int) :: icomm
79  integer(c_int) :: reply_root
80  end type kmr_spawn_info
81 
82  !> Type of map-function.
83 
84  abstract interface
85  integer(c_int) function kmr_mapfn(kv, kvi, kvo, p, i) bind(c)
86  use iso_c_binding
87  import kmr_kv_box
88  implicit none
89  type(kmr_kv_box), value, intent(in) :: kv
90  type(c_ptr), value, intent(in) :: kvi, kvo
91  type(c_ptr), value, intent(in) :: p
92  integer(c_long), value, intent(in) :: i
93  end function kmr_mapfn
94  end interface
95 
96  !> Type of reduce-function.
97 
98  abstract interface
99  integer(c_int) function kmr_redfn(kv, n, kvi, kvo, p) bind(c)
100  use iso_c_binding
101  import kmr_kv_box
102  implicit none
103  type(kmr_kv_box), intent(in) :: kv(*)
104  integer(c_long), value, intent(in) :: n
105  type(c_ptr), value, intent(in) :: kvi, kvo
106  type(c_ptr), value, intent(in) :: p
107  end function kmr_redfn
108  end interface
109 
110  !> Converts a string to a C pointer (ill-mannered).
111 
112  interface kmr_strptr
113  type(c_ptr) function kmr_strptr(s) bind(c, name='kmr_strptr_ff')
114  use iso_c_binding
115  implicit none
116  character(kind=c_char,len=1) :: s
117  end function kmr_strptr
118  end interface kmr_strptr
119 
120  !> Converts in reverse of kmr_strptr
121 
122  interface kmr_ptrstr
123  character(kind=c_char,len=1) function kmr_ptrstr(s) &
124  bind(c, name='kmr_ptrstr_ff')
125  use iso_c_binding
126  implicit none
127  type(c_ptr), value :: s
128  end function kmr_ptrstr
129  end interface kmr_ptrstr
130 
131  !> Converts a pointer to a long for key/value (ill-mannered).
132 
133  interface kmr_ptrint
134  integer(c_long) function kmr_ptrint(p) bind(c, name='kmr_ptrint_ff')
135  use iso_c_binding
136  implicit none
137  type(c_ptr), value, intent(in) :: p
138  end function kmr_ptrint
139  end interface kmr_ptrint
140 
141  !> Converts in reverse of kmr_ptrint.
142 
143  interface kmr_intptr
144  type(c_ptr) function kmr_intptr(p) bind(c, name='kmr_intptr_ff')
145  use iso_c_binding
146  implicit none
147  integer(c_long), value, intent(in) :: p
148  end function kmr_intptr
149  end interface kmr_intptr
150 
151  !> Converts a double to a long for key/value (ill-mannered).
152 
153  interface kmr_dblint
154  integer(c_long) function kmr_dblint(v) bind(c, name='kmr_dblint_ff')
155  use iso_c_binding
156  implicit none
157  real(c_double), value, intent(in) :: v
158  end function kmr_dblint
159  end interface kmr_dblint
160 
161  !> Converts in reverse of kmr_dblint.
162 
163  interface kmr_intdbl
164  real(c_double) function kmr_intdbl(v) bind(c, name='kmr_intdbl_ff')
165  use iso_c_binding
166  implicit none
167  integer(c_long), value, intent(in) :: v
168  end function kmr_intdbl
169  end interface kmr_intdbl
170 
171  !> Converts a character array to a (pointer value) integer for
172  !> key/value (it is casting in C).
173 
174  interface kmr_strint
175  integer(c_long) function kmr_strint(s) bind(c, name='kmr_strint_ff')
176  use iso_c_binding
177  implicit none
178  character(kind=c_char,len=1) :: s
179  end function kmr_strint
180  end interface kmr_strint
181 
182  !> Converts in reverse of kmr_strint(). It fills the character
183  !> array (s) by the contents at the pointer value integer (p) by the
184  !> length (n).
185 
186  interface kmr_intstr
187  integer(c_int) function kmr_intstr(p, s, n) &
188  bind(c, name='kmr_intstr_ff')
189  use iso_c_binding
190  implicit none
191  integer(c_long), value :: p
192  character(kind=c_char,len=1) :: s
193  integer(c_int), value :: n
194  end function kmr_intstr
195  end interface kmr_intstr
196 
197  !> Fixes little-endian bits used in Fortran to host-endian.
198 
200  integer(c_long) function kmr_fix_bits_endian_ff(b) &
201  bind(c, name='kmr_fix_bits_endian_ff')
202  use iso_c_binding
203  implicit none
204  integer(c_long), value, intent(in) :: b
205  end function kmr_fix_bits_endian_ff
206  end interface kmr_fix_bits_endian_ff
207 
208  !> Gets MPI rank from a key-value stream.
209 
210  interface kmr_get_rank
211  integer(c_int) function kmr_get_rank(kvs) &
212  bind(c, name='kmr_get_rank_ff')
213  use iso_c_binding
214  implicit none
215  type(c_ptr), value, intent(in) :: kvs
216  end function kmr_get_rank
217  end interface kmr_get_rank
218 
219  !> Gets MPI nprocs from a key-value stream.
220 
221  interface kmr_get_nprocs
222  integer(c_int) function kmr_get_nprocs(kvs) &
223  bind(c, name='kmr_get_nprocs_ff')
224  use iso_c_binding
225  implicit none
226  type(c_ptr), value, intent(in) :: kvs
227  end function kmr_get_nprocs
228  end interface kmr_get_nprocs
229 
231  integer(c_int) function kmr_get_key_type(kvs) &
232  bind(c, name='kmr_get_key_type_ff')
233  use iso_c_binding
234  implicit none
235  type(c_ptr), value, intent(in) :: kvs
236  end function kmr_get_key_type
237  end interface kmr_get_key_type
238 
240  integer(c_int) function kmr_get_value_type(kvs) &
241  bind(c, name='kmr_get_value_type_ff')
242  use iso_c_binding
243  implicit none
244  type(c_ptr), value, intent(in) :: kvs
245  end function kmr_get_value_type
246  end interface kmr_get_value_type
247 
248  !> Returns the element count local on each node.
249 
251  integer(c_int) function kmr_local_element_count(kvs, v) &
252  bind(c, name='kmr_local_element_count')
253  use iso_c_binding
254  implicit none
255  type(c_ptr), value, intent(in) :: kvs
256  integer(c_long), intent(out) :: v
257  end function kmr_local_element_count
258  end interface kmr_local_element_count
259 
260  interface kmr_init_ff
261  integer(c_int) function kmr_init_ff(kf, opt, fopt) &
262  bind(c, name='kmr_init_ff')
263  use iso_c_binding
264  implicit none
265  integer(c_int), value, intent(in) :: kf
266  integer(c_long), value, intent(in) :: opt
267  integer(c_long), value, intent(in) :: fopt
268  end function kmr_init_ff
269  end interface kmr_init_ff
270 
271  !> (See ::kmr_fin() in C).
272 
273  interface kmr_fin
274  integer(c_int) function kmr_fin() bind(c, name='kmr_fin')
275  use iso_c_binding
276  implicit none
277  end function kmr_fin
278  end interface kmr_fin
279 
280  interface kmr_create_context_ff
281  type(c_ptr) function kmr_create_context_ff(comm, info, name) &
282  bind(c, name='kmr_create_context_ff')
283  use iso_c_binding
284  implicit none
285  integer(c_int), value, intent(in) :: comm
286  integer(c_int), value, intent(in) :: info
287  type(c_ptr), intent(in) :: name
288  end function kmr_create_context_ff
289  end interface kmr_create_context_ff
290 
291  !> (See ::kmr_free_context() in C).
292 
294  integer(c_int) function kmr_free_context(mr) &
295  bind(c, name='kmr_free_context')
296  use iso_c_binding
297  implicit none
298  type(c_ptr), value, intent(in) :: mr
299  end function kmr_free_context
300  end interface kmr_free_context
301 
302  !> (See ::kmr_get_context_of_kvs() in C).
303 
305  type(c_ptr) function kmr_get_context_of_kvs(kvs) &
306  bind(c, name='kmr_get_context_of_kvs')
307  use iso_c_binding
308  implicit none
309  type(c_ptr), value, intent(in) :: kvs
310  end function kmr_get_context_of_kvs
311  end interface kmr_get_context_of_kvs
312 
314  type(c_ptr) function kmr_create_kvs7_ff(mr, kf, vf, opt, f, l, n) &
315  bind(c, name='kmr_create_kvs7')
316  use iso_c_binding
317  implicit none
318  type(c_ptr), value, intent(in) :: mr
319  integer(c_int), value, intent(in) :: kf, vf
320  integer(c_long), value, intent(in) :: opt
321  type(c_ptr), value, intent(in) :: f
322  integer(c_int), value, intent(in) :: l
323  type(c_ptr), value, intent(in) :: n
324  end function kmr_create_kvs7_ff
325  end interface kmr_create_kvs7_ff
326 
327  !> (See ::kmr_free_kvs() in C).
328 
329  interface kmr_free_kvs
330  integer(c_int) function kmr_free_kvs(kvs) bind(c, name='kmr_free_kvs')
331  use iso_c_binding
332  implicit none
333  type(c_ptr), value, intent(in) :: kvs
334  end function kmr_free_kvs
335  end interface kmr_free_kvs
336 
337  !> (See ::kmr_add_kv() in C).
338 
339  interface kmr_add_kv
340  integer(c_int) function kmr_add_kv(kvs, kv) bind(c, name='kmr_add_kv')
341  use iso_c_binding
342  import kmr_kv_box
343  implicit none
344  type(c_ptr), value, intent(in) :: kvs
345  type(kmr_kv_box), value, intent(in) :: kv;
346  end function kmr_add_kv
347  end interface kmr_add_kv
348 
349  !> (See ::kmr_add_kv_done() in C).
350 
351  interface kmr_add_kv_done
352  integer(c_int) function kmr_add_kv_done(kvs) &
353  bind(c, name='kmr_add_kv_done')
354  use iso_c_binding
355  implicit none
356  type(c_ptr), value, intent(in) :: kvs
357  end function kmr_add_kv_done
358  end interface kmr_add_kv_done
359 
360  interface kmr_map_ff
361  integer(c_int) function kmr_map_ff(s, kvi, kvo, p, opt, m, g, l, f) &
362  bind(c, name='kmr_map9')
363  use iso_c_binding
364  implicit none
365  integer(c_int), value, intent(in) :: s
366  type(c_ptr), value, intent(in) :: kvi, kvo
367  type(c_ptr), value, intent(in) :: p
368  integer(c_long), value, intent(in) :: opt
369  type(c_funptr), value, intent(in) :: m
370  type(c_ptr), value, intent(in) :: g
371  integer(c_int), value, intent(in) :: l
372  type(c_ptr), value, intent(in) :: f
373  end function kmr_map_ff
374  end interface kmr_map_ff
375 
376  interface kmr_map_on_rank_zero_ff
377  integer(c_int) function kmr_map_on_rank_zero_ff(kvo, p, opt, m) &
378  bind(c, name='kmr_map_on_rank_zero')
379  use iso_c_binding
380  implicit none
381  type(c_ptr), value, intent(in) :: kvo
382  type(c_ptr), value, intent(in) :: p
383  integer(c_long), value, intent(in) :: opt
384  type(c_funptr), value, intent(in) :: m
385  end function kmr_map_on_rank_zero_ff
386  end interface kmr_map_on_rank_zero_ff
387 
388  interface kmr_map_once_ff
389  integer(c_int) function kmr_map_once_ff(kvo, p, opt, rankzeroonly, m) &
390  bind(c, name='kmr_map_once')
391  use iso_c_binding
392  implicit none
393  type(c_ptr), value, intent(in) :: kvo
394  type(c_ptr), value, intent(in) :: p
395  integer(c_long), value, intent(in) :: opt
396  logical(c_bool), value, intent(in) :: rankzeroonly
397  type(c_funptr), value, intent(in) :: m
398  end function kmr_map_once_ff
399  end interface kmr_map_once_ff
400 
401  interface kmr_map_ms_ff
402  integer(c_int) function kmr_map_ms_ff(kvi, kvo, p, opt, m) &
403  bind(c, name='kmr_map_ms')
404  use iso_c_binding
405  implicit none
406  type(c_ptr), value, intent(in) :: kvi, kvo
407  type(c_ptr), value, intent(in) :: p
408  integer(c_long), value, intent(in) :: opt
409  type(c_funptr), value, intent(in) :: m
410  end function kmr_map_ms_ff
411  end interface kmr_map_ms_ff
412 
414  integer(c_int) function kmr_map_via_spawn_ff(kvi, kvo, p, &
415  info, opt, m) &
416  bind(c, name='kmr_map_via_spawn_ff')
417  use iso_c_binding
418  implicit none
419  type(c_ptr), value, intent(in) :: kvi, kvo
420  type(c_ptr), value, intent(in) :: p
421  integer(c_int), value, intent(in) :: info
422  integer(c_long), value, intent(in) :: opt
423  type(c_funptr), value, intent(in) :: m
424  end function kmr_map_via_spawn_ff
425  end interface kmr_map_via_spawn_ff
426 
428  integer(c_int) function kmr_get_spawner_communicator_ff(mr, &
429  ii, comm) bind(c, name='kmr_get_spawner_communicator_ff')
430  use iso_c_binding
431  implicit none
432  type(c_ptr), value, intent(in) :: mr
433  integer(c_long), value, intent(in) :: ii
434  integer(c_int), intent(out) :: comm
436  end interface kmr_get_spawner_communicator_ff
437 
439  integer(c_int) function kmr_sort_locally_ff(kvi, kvo, shuffling, opt) &
440  bind(c, name='kmr_sort_locally')
441  use iso_c_binding
442  implicit none
443  type(c_ptr), value, intent(in) :: kvi, kvo
444  logical(c_bool), value, intent(in) :: shuffling
445  integer(c_long), value, intent(in) :: opt
446  end function kmr_sort_locally_ff
447  end interface kmr_sort_locally_ff
448 
449  interface kmr_shuffle_ff
450  integer(c_int) function kmr_shuffle_ff(kvi, kvo, opt) &
451  bind(c, name='kmr_shuffle')
452  use iso_c_binding
453  implicit none
454  type(c_ptr), value, intent(in) :: kvi, kvo
455  integer(c_long), value, intent(in) :: opt
456  end function kmr_shuffle_ff
457  end interface kmr_shuffle_ff
458 
460  integer(c_int) function kmr_replicate_ff(kvi, kvo, opt) &
461  bind(c, name='kmr_replicate')
462  use iso_c_binding
463  implicit none
464  type(c_ptr), value, intent(in) :: kvi, kvo
465  integer(c_long), value, intent(in) :: opt
466  end function kmr_replicate_ff
467  end interface kmr_replicate_ff
468 
469  interface kmr_reduce_ff
470  integer(c_int) function kmr_reduce_ff(s, kvi, kvo, p, opt, r, g, l, f) &
471  bind(c, name='kmr_reduce9')
472  use iso_c_binding
473  implicit none
474  integer(c_int), value, intent(in) :: s
475  type(c_ptr), value, intent(in) :: kvi, kvo
476  type(c_ptr), value, intent(in) :: p
477  integer(c_long), value, intent(in) :: opt
478  type(c_funptr), value, intent(in) :: r
479  type(c_ptr), value, intent(in) :: g
480  integer(c_int), value, intent(in) :: l
481  type(c_ptr), value, intent(in) :: f
482  end function kmr_reduce_ff
483  end interface kmr_reduce_ff
484 
485  !> (See ::kmr_dump_kvs() in C).
486 
487  interface kmr_dump_kvs
488  integer(c_int) function kmr_dump_kvs(kvi, f) &
489  bind(c, name='kmr_dump_kvs')
490  use iso_c_binding
491  implicit none
492  type(c_ptr), value, intent(in) :: kvi
493  integer(c_int), value, intent(in) :: f
494  end function kmr_dump_kvs
495  end interface kmr_dump_kvs
496 
497  !> (See ::kmr_get_element_count() in C).
498 
500  integer(c_int) function kmr_get_element_count(kvs, v) &
501  bind(c, name='kmr_get_element_count')
502  use iso_c_binding
503  implicit none
504  type(c_ptr), value, intent(in) :: kvs
505  integer(c_long), intent(out) :: v
506  end function kmr_get_element_count
507  end interface kmr_get_element_count
508 
509  interface kmr_sort_ff
510  integer(c_int) function kmr_sort_ff(kvi, kvo, opt) &
511  bind(c, name='kmr_sort')
512  use iso_c_binding
513  implicit none
514  type(c_ptr), value, intent(in) :: kvi, kvo
515  integer(c_long), value, intent(in) :: opt
516  end function kmr_sort_ff
517  end interface kmr_sort_ff
518 
519  interface kmr_reverse_ff
520  integer(c_int) function kmr_reverse_ff(kvi, kvo, opt) &
521  bind(c, name='kmr_reverse')
522  use iso_c_binding
523  implicit none
524  type(c_ptr), value, intent(in) :: kvi, kvo
525  integer(c_long), value, intent(in) :: opt
526  end function kmr_reverse_ff
527  end interface kmr_reverse_ff
528 
529 contains
530 
531  !> Asserts the expression to be true.
532 
533  subroutine kmr_assert(v, expr)
534  include "mpif.h"
535  logical, intent(in) :: v
536  character(len=*), intent(in) :: expr
537  if (v) return
538  print *, "Assertion failed: ", trim(expr)
539  call mpi_abort(mpi_comm_world, 1)
540  !call exit_with_status(1)
541  end subroutine kmr_assert
542 
543  !> Places null function. Use kmr_nullmapfn for a C null function.
544  !> It is a placeholder and never called.
545 
546  integer(c_int) function kmr_nullmapfn(kv, kvi, kvo, p, i) bind(c) result(zz)
547  use iso_c_binding
548  implicit none
549  type(kmr_kv_box), value, intent(in) :: kv
550  type(c_ptr), value, intent(in) :: kvi, kvo
551  type(c_ptr), value, intent(in) :: p
552  integer(c_long), value, intent(in) :: i
553  integer(c_long) :: z
554  ! JUST SUPPRESS WARNINGS
555  call kmr_assert(c_associated(kvi) .and. c_associated(kvo) &
556  .and. c_associated(p), &
557  .and..and."c_associated(kvi) c_associated(kvo) c_associated(p)")
558  z = (kv%vlen + i)
559  zz = 0
560  end function kmr_nullmapfn
561 
562  ! Fixes null functions to zero for C.
563 
564  !type(c_funptr) function kmr_fixfun(m) result(zz)
565  ! type(c_funptr), value, intent(in) :: m
566  ! if (c_associated(m, c_funloc(kmr_nullmapfn))) then
567  ! zz = c_null_funptr
568  ! else
569  ! zz = m
570  ! end if
571  !end function kmr_fixfun
572 
573  !> Fixes bits-endian of option bits.
574 
575  integer(c_long) function kmr_fixopt(b) result(zz)
576  integer(c_int), value, intent(in) :: b
577  zz = kmr_fix_bits_endian_ff(int(b, c_long))
578  end function kmr_fixopt
579 
580  !> (See ::kmr_init() in C).
581 
582  integer(c_int) function kmr_init() result(zz)
583  call kmr_assert(c_int > 0, 'c_int > 0')
584  call kmr_assert(c_long > 0, 'c_long > 0')
585  !call kmr_assert(c_ptr > 0, 'c_ptr > 0')
586  !call kmr_assert(c_sizeof(v) == 8, 'c_sizeof(c_long) == 8')
587  zz = kmr_init_ff(kmr_kv_pointer_unmanaged, &
588  kmr_fixopt(kmr_rank_zero), kmr_fixopt(kmr_shuffle_names))
589  end function kmr_init
590 
591  !> (See ::kmr_create_context() in C).
592 
593  type(c_ptr) function kmr_create_context(comm, info) result(zz)
594  integer, value, intent(in) :: comm
595  integer, value, intent(in) :: info
596  zz = kmr_create_context_ff(int(comm, c_int), &
597  int(info, c_int), c_null_ptr)
598  end function kmr_create_context
599 
600  !> (See ::kmr_create_kvs() in C).
601 
602  type(c_ptr) function kmr_create_kvs(mr, kf, vf) result(zz)
603  type(c_ptr), value, intent(in) :: mr
604  integer(c_int), value, intent(in) :: kf, vf
605  zz = kmr_create_kvs7_ff(mr, kf, vf, int(0, c_long), &
606  c_null_ptr, 0, c_null_ptr)
607  end function kmr_create_kvs
608 
609  !> (See ::kmr_map() in C).
610 
611  integer(c_int) function kmr_map(kvi, kvo, p, opt, m) result(zz)
612  type(c_ptr), value, intent(in) :: kvi, kvo
613  type(c_ptr), value, intent(in) :: p
614  integer(c_int), value, intent(in) :: opt
615  procedure(kmr_mapfn), bind(c) :: m
616  zz = kmr_map_ff(0, kvi, kvo, p, kmr_fixopt(opt), c_funloc(m), &
617  c_null_ptr, 0, c_null_ptr)
618  end function kmr_map
619 
620  !> (See ::kmr_map_on_rank_zero() in C).
621 
622  integer(c_int) function kmr_map_on_rank_zero(kvo, p, opt, m) result(zz)
623  type(c_ptr), value, intent(in) :: kvo
624  type(c_ptr), value, intent(in) :: p
625  integer(c_int), value, intent(in) :: opt
626  procedure(kmr_mapfn), bind(c) :: m
627  !print *, "kmr_map_on_rank_zero:kvo=", kmr_ptrint(kvo)
628  !print *, "kmr_map_on_rank_zero:m=", kmr_ptrint(c_funloc(m))
629  call kmr_assert(c_associated(kvo), 'c_associated(kvo)')
630  zz = kmr_map_on_rank_zero_ff(kvo, p, kmr_fixopt(opt), c_funloc(m))
631  end function kmr_map_on_rank_zero
632 
633  !> (See ::kmr_map_once() in C).
634 
635  integer(c_int) function kmr_map_once(kvo, p, opt, rankzeroonly, m) result(zz)
636  type(c_ptr), value, intent(in) :: kvo
637  type(c_ptr), value, intent(in) :: p
638  integer(c_int), value, intent(in) :: opt
639  logical, value, intent(in) :: rankzeroonly
640  procedure(kmr_mapfn), bind(c) :: m
641  logical(c_bool) :: bb
642  call kmr_assert(c_associated(kvo), 'c_associated(kvo)')
643  if (rankzeroonly) then
644  bb = .true.
645  else
646  bb = .false.
647  end if
648  zz = kmr_map_once_ff(kvo, p, kmr_fixopt(opt), bb, c_funloc(m))
649  end function kmr_map_once
650 
651  !> (See ::kmr_map_ms() in C).
652 
653  integer(c_int) function kmr_map_ms(kvi, kvo, p, opt, m) result(zz)
654  type(c_ptr), value, intent(in) :: kvi, kvo
655  type(c_ptr), value, intent(in) :: p
656  integer(c_int), value, intent(in) :: opt
657  procedure(kmr_mapfn), bind(c) :: m
658  call kmr_assert(c_associated(kvi), 'c_associated(kvi)')
659  zz = kmr_map_ms_ff(kvi, kvo, p, kmr_fixopt(opt), c_funloc(m))
660  end function kmr_map_ms
661 
662  !> (See ::kmr_map_via_spawn() in C).
663 
664  integer(c_int) function kmr_map_via_spawn(kvi, kvo, p, info, opt, m) &
665  result(zz)
666  type(c_ptr), value, intent(in) :: kvi, kvo
667  type(c_ptr), value, intent(in) :: p
668  integer, value, intent(in) :: info
669  integer(c_int), value, intent(in) :: opt
670  procedure(kmr_mapfn), bind(c) :: m
671  type(c_funptr) :: fp
672  call kmr_assert(c_associated(kvi), 'c_associated(kvi)')
673  !fp = kmr_fixfun(c_funloc(m))
674  if (c_associated(c_funloc(m), c_funloc(kmr_nullmapfn))) then
675  fp = c_null_funptr
676  else
677  fp = c_funloc(m)
678  end if
679  zz = kmr_map_via_spawn_ff(kvi, kvo, p, int(info, c_int), &
680  kmr_fixopt(opt), fp)
681  end function kmr_map_via_spawn
682 
683  !> (See ::kmr_get_spawner_communicator() in C).
684  !> MPI_Comm_free() cannot be used on the returned communicator in
685  !> the Fortran binding.
686 
687  integer(c_int) function kmr_get_spawner_communicator(mr, ii, comm) &
688  result(zz)
689  type(c_ptr), value, intent(in) :: mr
690  integer(c_long), value, intent(in) :: ii
691  integer, intent(out) :: comm
692  zz = kmr_get_spawner_communicator_ff(mr, ii, comm)
693  end function kmr_get_spawner_communicator
694 
695  !> (See ::kmr_sort_locally() in C).
696 
697  integer(c_int) function kmr_sort_locally(kvi, kvo, shuffling, opt) result(zz)
698  type(c_ptr), value, intent(in) :: kvi, kvo
699  logical(c_bool), value, intent(in) :: shuffling
700  integer(c_int), value, intent(in) :: opt
701  call kmr_assert(c_associated(kvi), 'c_associated(kvi)')
702  call kmr_assert(c_associated(kvo), 'c_associated(kvo)')
703  zz = kmr_sort_locally_ff(kvi, kvo, shuffling, kmr_fixopt(opt))
704  end function kmr_sort_locally
705 
706  !> (See ::kmr_shuffle() in C).
707 
708  integer(c_int) function kmr_shuffle(kvi, kvo, opt) result(zz)
709  type(c_ptr), value, intent(in) :: kvi, kvo
710  integer(c_int), value, intent(in) :: opt
711  call kmr_assert(c_associated(kvi), 'c_associated(kvi)')
712  call kmr_assert(c_associated(kvo), 'c_associated(kvo)')
713  zz = kmr_shuffle_ff(kvi, kvo, kmr_fixopt(opt))
714  end function kmr_shuffle
715 
716  !> (See ::kmr_replicate() in C).
717 
718  integer(c_int) function kmr_replicate(kvi, kvo, opt) result(zz)
719  type(c_ptr), value, intent(in) :: kvi, kvo
720  integer(c_int), value, intent(in) :: opt
721  call kmr_assert(c_associated(kvi), 'c_associated(kvi)')
722  call kmr_assert(c_associated(kvo), 'c_associated(kvo)')
723  zz = kmr_replicate_ff(kvi, kvo, kmr_fixopt(opt))
724  end function kmr_replicate
725 
726  !> (See ::kmr_reduce() in C).
727 
728  integer(c_int) function kmr_reduce(kvi, kvo, p, opt, r) result(zz)
729  type(c_ptr), value, intent(in) :: kvi, kvo
730  type(c_ptr), value, intent(in) :: p
731  integer(c_int), value, intent(in) :: opt
732  procedure(kmr_redfn), bind(c) :: r
733  call kmr_assert(c_associated(kvi), 'c_associated(kvi)')
734  zz = kmr_reduce_ff(0, kvi, kvo, p, kmr_fixopt(opt), c_funloc(r), &
735  c_null_ptr, 0, c_null_ptr)
736  end function kmr_reduce
737 
738  !> (See ::kmr_sort() in C).
739 
740  integer(c_int) function kmr_sort(kvi, kvo, opt) result(zz)
741  type(c_ptr), value, intent(in) :: kvi, kvo
742  integer(c_int), value, intent(in) :: opt
743  call kmr_assert(c_associated(kvi), 'c_associated(kvi)')
744  call kmr_assert(c_associated(kvo), 'c_associated(kvo)')
745  zz = kmr_sort_ff(kvi, kvo, kmr_fixopt(opt))
746  end function kmr_sort
747 
748  !> (See ::kmr_reverse() in C).
749 
750  integer(c_int) function kmr_reverse(kvi, kvo, opt) result(zz)
751  type(c_ptr), value, intent(in) :: kvi, kvo
752  integer(c_int), value, intent(in) :: opt
753  call kmr_assert(c_associated(kvi), 'c_associated(kvi)')
754  call kmr_assert(c_associated(kvo), 'c_associated(kvo)')
755  zz = kmr_reverse_ff(kvi, kvo, kmr_fixopt(opt))
756  end function kmr_reverse
757 
758  !! BELOWS ARE TO BE ADDED SOON
759 
760  !!kmr_add_string
761 
762  !!kmr_concatenate_kvs
763  !!kmr_local_element_count
764  !!kmr_dump_kvs_stats
765  !!kmr_save_kvs
766  !!kmr_restore_kvs
767  !!kmr_retrieve_kvs_entries
768 
769  !!kmr_map_rank_by_rank
770  !!kmr_map_for_some
771  !!kmr_map_ms_commands
772 
773  !!kmr_reply_to_spawner
774  !!kmr_map_processes
775  !!kmr_send_kvs_to_spawner
776  !!kmr_receive_kvs_from_spawned_fn
777 
778  !!kmr_reduce_as_one
779  !!kmr_reduce_for_some
780 
781  !!kmr_distribute
782 
783  !!kmr_read_files_reassemble
784  !!kmr_read_file_by_segments
785 
786  !> Reinterprets a pointer (as an integer) as a character array. It
787  !> is the same as kmr_ptrstr(). The return value is used like
788  !> v=kmr_str(kv%k);v(1:kv%keln). Note kmr_intstr() copies the
789  !> string.
790 
791  character(kind=c_char,len=1) function kmr_str(p) result(zz)
792  use iso_c_binding
793  integer(c_long), intent(in) :: p
794  zz = transfer(p, zz)
795  end function kmr_str
796 
797 end module kmrf
798 
799 ! Copyright (C) 2012-2018 RIKEN R-CCS
800 ! This library is distributed WITHOUT ANY WARRANTY. This library can be
801 ! redistributed and/or modified under the terms of the BSD 2-Clause License.
#define kmr_reduce(KVI, KVO, ARG, OPT, R)
Reduces key-value pairs.
Definition: kmr.h:88
Converts in reverse of kmr_strint().
Definition: kmrf.F90:186
Spawning Info.
Definition: kmr.h:759
Converts in reverse of kmr_ptrint.
Definition: kmrf.F90:143
int kmr_map_once(KMR_KVS *kvo, void *arg, struct kmr_option opt, _Bool rank_zero_only, kmr_mapfn_t m)
Maps once.
Definition: kmrbase.c:1460
Type of reduce-function.
Definition: kmrf.F90:99
#define kmr_create_kvs(MR, KF, VF)
Makes a new key-value stream (of type KMR_KVS) with the specified field datatypes.
Definition: kmr.h:71
(See kmr_get_context_of_kvs() in C).
Definition: kmrf.F90:304
MPI_Comm * kmr_get_spawner_communicator(KMR *mr, long index)
Obtains (a reference to) a parent inter-communicator of a spawned process.
Definition: kmrmapms.c:1916
Gets MPI nprocs from a key-value stream.
Definition: kmrf.F90:221
int kmr_shuffle(KMR_KVS *kvi, KMR_KVS *kvo, struct kmr_option opt)
Shuffles key-value pairs to the appropriate destination ranks.
Definition: kmrbase.c:2094
(See kmr_free_context() in C).
Definition: kmrf.F90:293
(See kmr_add_kv() in C).
Definition: kmrf.F90:339
(See kmr_fin() in C).
Definition: kmrf.F90:273
Returns the element count local on each node.
Definition: kmrf.F90:250
Gets MPI rank from a key-value stream.
Definition: kmrf.F90:210
#define kmr_map(KVI, KVO, ARG, OPT, M)
Maps simply.
Definition: kmr.h:82
Handy Copy of a Key-Value Field.
Definition: kmr.h:401
(See kmr_get_element_count() in C).
Definition: kmrf.F90:499
#define kmr_init()
Sets up the environment.
Definition: kmr.h:794
(See kmr_add_kv_done() in C).
Definition: kmrf.F90:351
int kmr_sort(KMR_KVS *kvi, KMR_KVS *kvo, struct kmr_option opt)
Sorts a key-value stream globally.
Definition: kmrmoreops.c:575
Converts a double to a long for key/value (ill-mannered).
Definition: kmrf.F90:153
Converts in reverse of kmr_dblint.
Definition: kmrf.F90:163
(See kmr_free_kvs() in C).
Definition: kmrf.F90:329
int kmr_map_via_spawn(KMR_KVS *kvi, KMR_KVS *kvo, void *arg, MPI_Info info, struct kmr_spawn_option opt, kmr_mapfn_t mapfn)
Maps on processes started by MPI_Comm_spawn().
Definition: kmrmapms.c:1992
Type of map-function.
Definition: kmrf.F90:85
Fixes little-endian bits used in Fortran to host-endian.
Definition: kmrf.F90:199
Converts a string to a C pointer (ill-mannered).
Definition: kmrf.F90:112
int kmr_map_ms(KMR_KVS *kvi, KMR_KVS *kvo, void *arg, struct kmr_option opt, kmr_mapfn_t m)
Maps in master-worker mode.
Definition: kmrmapms.c:344
Converts in reverse of kmr_strptr.
Definition: kmrf.F90:122
int kmr_replicate(KMR_KVS *kvi, KMR_KVS *kvo, struct kmr_option opt)
Replicates key-value pairs to be visible on all ranks, that is, it has the effect of bcast or all-gat...
Definition: kmrbase.c:2240
Converts a character array to a (pointer value) integer for key/value (it is casting in C)...
Definition: kmrf.F90:174
int kmr_sort_locally(KMR_KVS *kvi, KMR_KVS *kvo, _Bool shuffling, struct kmr_option opt)
Reorders key-value pairs in a single rank.
Definition: kmrbase.c:2051
Converts a pointer to a long for key/value (ill-mannered).
Definition: kmrf.F90:133
int kmr_reverse(KMR_KVS *kvi, KMR_KVS *kvo, struct kmr_option opt)
Makes a new pair by swapping the key and the value in each pair.
Definition: kmrmoreops.c:159
(See kmr_dump_kvs() in C).
Definition: kmrf.F90:487
int kmr_map_on_rank_zero(KMR_KVS *kvo, void *arg, struct kmr_option opt, kmr_mapfn_t m)
Maps on rank0 only.
Definition: kmrbase.c:1514
KMR * kmr_create_context(const MPI_Comm comm, const MPI_Info conf, const char *name)
Makes a new KMR context (a context has type KMR).
Definition: kmrbase.c:168