KMR
test4.c
1 /* test4.c (2014-02-04) */
2 
3 /* Check master-slave mappers. */
4 
5 #include <mpi.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include <fcntl.h>
10 #include <limits.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <sys/time.h>
14 #include <assert.h>
15 #ifdef _OPENMP
16 #include <omp.h>
17 #endif
18 
19 #include "kmr.h"
20 #include "kmrimpl.h"
21 
22 static int
23 copykvfn(struct kmr_kv_box kv, const KMR_KVS *kvi,
24  KMR_KVS *kvo, void *p, const long ii)
25 {
26  printf("mapfn(k=%s, v=%s)\n", kv.k.p, kv.v.p);
27  fflush(0);
28  kmr_add_kv(kvo, kv);
29  return MPI_SUCCESS;
30 }
31 
32 static void
33 simple0(int nprocs, int rank)
34 {
35  int cc;
36 
37  KMR *mr = kmr_create_context(MPI_COMM_WORLD, MPI_INFO_NULL, 0);
38  mr->trace_map_ms = 1;
39 
40  if (1) {
41  MPI_Barrier(MPI_COMM_WORLD);
42  usleep(50 * 1000);
43  if (rank == 0) {printf("Checking kmr_map_ms...\n");}
44  fflush(0);
45  usleep(50 * 1000);
46 
47  KMR_KVS *kvs00 = kmr_create_kvs(mr, KMR_KV_OPAQUE, KMR_KV_OPAQUE);
48  if (mr->rank == 0) {
49  char k[256];
50  char v[256];
51  for (int i = 0; i < 20; i++) {
52  snprintf(k, sizeof(k), "key%d", i);
53  snprintf(v, sizeof(v), "value%d", i);
54  kmr_add_string(kvs00, k, v);
55  }
56  }
57  kmr_add_kv_done(kvs00);
58  KMR_KVS *kvs01 = kmr_create_kvs(mr, KMR_KV_OPAQUE, KMR_KV_OPAQUE);
59  do {
60  struct kmr_option opt = kmr_noopt;
61  opt.nothreading = 1;
62  cc = kmr_map_ms(kvs00, kvs01, 0, opt, copykvfn);
63  } while (cc == MPI_ERR_ROOT);
64  /*kmr_dump_kvs(kvs01, 0);*/
65  if (mr->rank == 0) {
66  assert(kvs01->c.element_count == 20);
67  } else {
68  assert(kvs01->c.element_count == 0);
69  }
70  kmr_free_kvs(kvs01);
71  }
72 
73  kmr_free_context(mr);
74 }
75 
76 static void
77 simple1(int nprocs, int rank)
78 {
79  int cc;
80 
81  KMR *mr = kmr_create_context(MPI_COMM_WORLD, MPI_INFO_NULL, 0);
82  mr->trace_map_ms = 1;
83 
84  if (1) {
85  MPI_Barrier(MPI_COMM_WORLD);
86  usleep(50 * 1000);
87  if (rank == 0) {printf("Checking kmr_map_ms_commands...\n");}
88  fflush(0);
89  usleep(50 * 1000);
90 
91  KMR_KVS *kvs00 = kmr_create_kvs(mr, KMR_KV_OPAQUE, KMR_KV_OPAQUE);
92  if (mr->rank == 0) {
93  char k[256];
94  char v[256];
95  snprintf(k, sizeof(k), "key");
96  snprintf(v, sizeof(v), "./a.out a0 a1 a2");
97  for (int i = 0; i < 20; i++) {
98  kmr_add_string(kvs00, k, v);
99  }
100  }
101  kmr_add_kv_done(kvs00);
102  KMR_KVS *kvs01 = kmr_create_kvs(mr, KMR_KV_OPAQUE, KMR_KV_OPAQUE);
103  do {
104  struct kmr_option mopt = kmr_noopt;
105  mopt.nothreading = 1;
106  struct kmr_spawn_option sopt = {.separator_space = 1};
107  cc = kmr_map_ms_commands(kvs00, kvs01, 0,
108  mopt, sopt, copykvfn);
109  } while (cc == MPI_ERR_ROOT);
110  /*kmr_dump_kvs(kvs01, 0);*/
111  if (mr->rank == 0) {
112  assert(kvs01->c.element_count == 20);
113  } else {
114  assert(kvs01->c.element_count == 0);
115  }
116  kmr_free_kvs(kvs01);
117  }
118 
119  kmr_free_context(mr);
120 }
121 
122 int
123 main(int argc, char *argv[])
124 {
125  if (argc == 1 || argc == 2) {
126  int nprocs, rank, lev;
127  MPI_Init_thread(&argc, &argv, MPI_THREAD_SERIALIZED, &lev);
128  MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
129  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
130 
131  kmr_init();
132 
133  if (rank == 0) {printf("CHECK MS MAPPER\n");}
134  fflush(0);
135 
136  simple0(nprocs, rank);
137  simple1(nprocs, rank);
138 
139  MPI_Barrier(MPI_COMM_WORLD);
140  usleep(50 * 1000);
141  if (rank == 0) {printf("OK\n");}
142  fflush(0);
143 
144  kmr_fin();
145 
146  MPI_Finalize();
147  } else {
148  printf("a process runs: %s %s %s %s\n",
149  argv[0], argv[1], argv[2], argv[3]);
150  fflush(0);
151  sleep(3);
152  printf("a process done.\n");
153  fflush(0);
154  }
155  return 0;
156 }
Key-Value Stream (abstract).
Definition: kmr.h:587
Utilities Private Part (do not include from applications).
Options to Mapping, Shuffling, and Reduction.
Definition: kmr.h:613
int kmr_add_kv(KMR_KVS *kvs, const struct kmr_kv_box kv)
Adds a key-value pair.
Definition: kmrbase.c:751
#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
int kmr_add_kv_done(KMR_KVS *kvs)
Marks finished adding key-value pairs.
Definition: kmrbase.c:881
KMR Context.
Definition: kmr.h:222
int kmr_free_kvs(KMR_KVS *kvs)
Releases a key-value stream (type KMR_KVS).
Definition: kmrbase.c:621
Handy Copy of a Key-Value Field.
Definition: kmr.h:358
Options to Mapping by Spawns.
Definition: kmr.h:662
int kmr_fin(void)
Clears the environment.
Definition: kmrbase.c:124
#define kmr_init()
Sets up the environment.
Definition: kmr.h:747
int kmr_map_ms_commands(KMR_KVS *kvi, KMR_KVS *kvo, void *arg, struct kmr_option opt, struct kmr_spawn_option sopt, kmr_mapfn_t m)
Maps in master-slave mode, specialized to run serial commands.
Definition: kmrmapms.c:2198
int kmr_free_context(KMR *mr)
Releases a context created with kmr_create_context().
Definition: kmrbase.c:326
KMR Interface.
int kmr_map_ms(KMR_KVS *kvi, KMR_KVS *kvo, void *arg, struct kmr_option opt, kmr_mapfn_t m)
Maps in master-slave mode.
Definition: kmrmapms.c:310
int kmr_add_string(KMR_KVS *kvs, const char *k, const char *v)
Adds a key-value pair of strings.
Definition: kmrbase.c:913
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:147