12 #define ISALPHA(X) (('a' <= X && X <= 'z') || ('A' <= X && X <= 'Z')) 15 read_words_from_a_file(
const struct kmr_kv_box kv0,
19 assert(kvi == 0 && kv0.klen == 0 && kv0.vlen == 0 && kvo != 0);
20 FILE *f = fopen(
"LICENSE",
"r");
22 perror(
"Cannot open a file \"LICENSE\"; fopen(LICENSE)");
23 MPI_Abort(MPI_COMM_WORLD, 1);
28 assert((
size_t)j <= (
sizeof(b) - 1));
30 if ((cc == EOF || !ISALPHA(cc) || (j == (
sizeof(b) - 1))) && j != 0) {
33 .klen = (j + 1), .k.p = b,
34 .vlen =
sizeof(
long), .v.i = 1};
54 int rank = kvi->c.mr->rank;
55 if (rank == 0 && i < 5) {
56 printf(
"#%s=%d\n", kv0.v.p, (
int)(0 - kv0.k.i));
63 sum_counts_for_a_word(
const struct kmr_kv_box kv[],
const long n,
67 for (
long i = 0; i < n; i++) {
80 main(
int argc,
char **argv)
82 int nprocs, rank, thlv;
83 MPI_Init_thread(&argc, &argv, MPI_THREAD_SERIALIZED, &thlv);
84 MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
85 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
89 MPI_Barrier(MPI_COMM_WORLD);
90 if (rank == 0) {printf(
"Ranking words...\n");}
93 kmr_map_once(kvs0, 0, kmr_noopt, 0, read_words_from_a_file);
99 kmr_reduce(kvs1, kvs2, 0, kmr_noopt, sum_counts_for_a_word);
107 kmr_map(kvs4, 0, 0, kmr_noopt, print_top_five);
Key-Value Stream (abstract).
#define kmr_reduce(KVI, KVO, ARG, OPT, R)
Reduces key-value pairs.
int kmr_add_kv(KMR_KVS *kvs, const struct kmr_kv_box kv)
Adds a key-value pair.
int kmr_map_once(KMR_KVS *kvo, void *arg, struct kmr_option opt, _Bool rank_zero_only, kmr_mapfn_t m)
Maps once.
#define kmr_create_kvs(MR, KF, VF)
Makes a new key-value stream (of type KMR_KVS) with the specified field datatypes.
int kmr_shuffle(KMR_KVS *kvi, KMR_KVS *kvo, struct kmr_option opt)
Shuffles key-value pairs to the appropriate destination ranks.
#define kmr_map(KVI, KVO, ARG, OPT, M)
Maps simply.
Handy Copy of a Key-Value Field.
int kmr_fin(void)
Clears the environment.
#define kmr_init()
Sets up the environment.
int kmr_sort(KMR_KVS *kvi, KMR_KVS *kvo, struct kmr_option opt)
Sorts a key-value stream globally.
int kmr_free_context(KMR *mr)
Releases a context created with kmr_create_context().
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.
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).