File: reducer.c

package info (click to toggle)
openmpi 4.1.0-10
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 126,560 kB
  • sloc: ansic: 685,465; makefile: 42,952; f90: 19,220; sh: 7,002; java: 6,360; perl: 3,524; cpp: 2,227; python: 1,350; lex: 989; fortran: 61; tcl: 12
file content (82 lines) | stat: -rw-r--r-- 2,135 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/* -*- C -*-
 */

#include <time.h>
#include <stdio.h>

#include "orte/constants.h"

#include "opal/class/opal_list.h"
#include "opal/util/argv.h"

#include "orte/runtime/runtime.h"

typedef struct {
    opal_list_item_t super;
    char *word;
    int count;
} word_count_t;
OBJ_CLASS_INSTANCE(word_count_t,
                   opal_list_item_t,
                   NULL, NULL);

int main(int argc, char* argv[])
{
    char text[100];
    opal_list_t words;
    word_count_t *cnt;
    char **incnt;
    bool found;
    opal_list_item_t *item;

    if (ORTE_SUCCESS != orte_init(&argc, &argv, ORTE_PROC_NON_MPI)) {
        fprintf(stderr, "Failed orte_init\n");
        exit(1);
    }

    OBJ_CONSTRUCT(&words, opal_list_t);
    while (fgets(text, sizeof(text), stdin)) {
        /* remove trailing newline */
        if ('\n' == text[strlen(text)-1]) {
            text[strlen(text)-1] = '\0';
        }
        incnt = opal_argv_split(text, '\t');
        found = false;
        if (opal_argv_count(incnt) < 2) {
            opal_output(0, "INCORRECT WORD SPLIT: %s", text);
            opal_argv_free(incnt);
            continue;
        }
        for (item = opal_list_get_first(&words);
             item != opal_list_get_end(&words);
             item = opal_list_get_next(item)) {
            cnt = (word_count_t*)item;
            if (0 == strcmp(cnt->word, incnt[0])) {
                cnt->count += atoi(incnt[1]);
                found = true;
                break;
            }
        }
        if (!found) {
            cnt = OBJ_NEW(word_count_t);
            cnt->word = strdup(incnt[0]);
            cnt->count = atoi(incnt[1]);
            opal_list_append(&words, &cnt->super);
        }
        opal_argv_free(incnt);
    }

    fprintf(stdout, "FINAL COUNT:\n");
    while (NULL != (item = opal_list_remove_first(&words))) {
        cnt = (word_count_t*)item;
        fprintf(stdout, "%s: %d\n", cnt->word, cnt->count);
        OBJ_RELEASE(item);
    }
    OBJ_DESTRUCT(&words);

    if (ORTE_SUCCESS != orte_finalize()) {
        fprintf(stderr, "Failed orte_finalize\n");
        exit(1);
    }
    return 0;
}