File: mult_session.c

package info (click to toggle)
mpich 4.0.2-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 423,384 kB
  • sloc: ansic: 1,088,434; cpp: 71,364; javascript: 40,763; f90: 22,829; sh: 17,463; perl: 14,773; xml: 14,418; python: 10,265; makefile: 9,246; fortran: 8,008; java: 4,355; asm: 324; ruby: 176; lisp: 19; php: 8; sed: 4
file content (160 lines) | stat: -rw-r--r-- 4,490 bytes parent folder | download | duplicates (3)
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
 * Copyright (C) by Argonne National Laboratory
 *     See COPYRIGHT in top-level directory
 */

#include "mpi.h"
#include <stdio.h>
#include "mpitest.h"

/* adapted from init/session.c */

#define NTHREADS 4

int thread_errs[NTHREADS];

MTEST_THREAD_RETURN_TYPE library_foo_test(void *p);

int main(int argc, char *argv[])
{
    int provided;
    MTest_init_thread_pkg();
    MPI_Init_thread(&argc, &argv, MPI_THREAD_MULTIPLE, &provided);

    for (int i = 1; i < NTHREADS; i++) {
        MTest_Start_thread(library_foo_test, (void *) (long) i);
    }

    int rank;
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    thread_errs[0] += MTestTestIntracomm(MPI_COMM_WORLD);

    MTest_Join_threads();
    MPI_Finalize();
    MTest_finalize_thread_pkg();

    int errs = 0;
    for (int i = 0; i < NTHREADS; i++) {
        errs += thread_errs[i];
    }

    if (rank == 0 && errs == 0) {
        printf("No Errors\n");
    }
    return MTestReturnValue(errs);
}

static bool library_foo_init(int thread_idx, MPI_Session * p_session, MPI_Comm * p_comm);
static void library_foo_finalize(int thread_idx, MPI_Session * p_session, MPI_Comm * p_comm);

MTEST_THREAD_RETURN_TYPE library_foo_test(void *p)
{
    int rank, size;
    int thread_idx = (int) (long) p;

    MPI_Session lib_shandle = MPI_SESSION_NULL;
    MPI_Comm lib_comm = MPI_COMM_NULL;
    if (library_foo_init(thread_idx, &lib_shandle, &lib_comm)) {
        MPI_Comm_size(lib_comm, &size);
        MPI_Comm_rank(lib_comm, &rank);

        thread_errs[thread_idx] += MTestTestIntracomm(lib_comm);

        library_foo_finalize(thread_idx, &lib_shandle, &lib_comm);
    }

    return MTEST_THREAD_RETVAL_IGN;
}

static bool library_foo_init(int thread_idx, MPI_Session * p_session, MPI_Comm * p_comm)
{
    int rc, flag;
    int ret = MPI_SUCCESS;
    const char *pset_name;
    char out_value[100];        /* large enough */

    /* Let's test both WORLD and SELF. e.g. with 4 threads, thread 2 will run on SELF */
    if (thread_idx % 2) {
        pset_name = "mpi://WORLD";
    } else {
        pset_name = "mpi://SELF";
    }

    MPI_Group wgroup = MPI_GROUP_NULL;
    rc = MPI_Session_init(MPI_INFO_NULL, MPI_ERRORS_RETURN, p_session);
    if (rc != MPI_SUCCESS) {
        thread_errs[thread_idx]++;
        printf("MPI_Session_init failed in thread %d\n", thread_idx);
        goto fn_exit;
    }

    /* check we got thread support level foo library needs */
    MPI_Info tinfo = MPI_INFO_NULL;
    const char mt_key[] = "thread_level";
    const char mt_value[] = "MPI_THREAD_MULTIPLE";
    rc = MPI_Session_get_info(*p_session, &tinfo);
    if (rc != MPI_SUCCESS) {
        thread_errs[thread_idx]++;
        goto fn_exit;
    }

    MPI_Info_get(tinfo, mt_key, sizeof(out_value), out_value, &flag);
    if (flag != 1) {
        thread_errs[thread_idx]++;
        printf("Could not find key %s\n", mt_key);
        goto fn_exit;
    }
    if (strcmp(out_value, mt_value)) {
        thread_errs[thread_idx]++;
        printf("Did not get thread multiple support, got %s\n", out_value);
        goto fn_exit;
    }

    /* create a group from the WORLD process set */
    rc = MPI_Group_from_session_pset(*p_session, pset_name, &wgroup);
    if (rc != MPI_SUCCESS) {
        thread_errs[thread_idx]++;
        printf("MPI_Group_from_session_pset failed in thread %d\n", thread_idx);
        goto fn_exit;
    }

    /* get a communicator */
    char string_tag[20];
    sprintf(string_tag, "thread %d", thread_idx);
    rc = MPI_Comm_create_from_group(wgroup, string_tag, MPI_INFO_NULL, MPI_ERRORS_RETURN, p_comm);
    if (rc != MPI_SUCCESS) {
        thread_errs[thread_idx]++;
        printf("MPI_Comm_create_from_group failed in thread %d\n", thread_idx);
        goto fn_exit;
    }

    /* free group, library doesn’t need it. */
  fn_exit:
    MPI_Group_free(&wgroup);
    if (tinfo != MPI_INFO_NULL) {
        MPI_Info_free(&tinfo);
    }
    if (ret != MPI_SUCCESS) {
        MPI_Session_finalize(p_session);
    }
    return (ret == MPI_SUCCESS);
}

static void library_foo_finalize(int thread_idx, MPI_Session * p_session, MPI_Comm * p_comm)
{
    int rc;

    rc = MPI_Comm_free(p_comm);
    if (rc != MPI_SUCCESS) {
        thread_errs[thread_idx]++;
        printf("MPI_Comm_free returned %d\n", rc);
        return;
    }

    rc = MPI_Session_finalize(p_session);
    if (rc != MPI_SUCCESS) {
        thread_errs[thread_idx]++;
        printf("MPI_Session_finalize returned %d\n", rc);
        return;
    }
}