|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <string.h> |
| 4 | +#include "mpi.h" |
| 5 | + |
| 6 | +void my_session_errhandler (MPI_Session *foo, int *bar, ...) |
| 7 | +{ |
| 8 | + fprintf(stderr, "errhandler called with error %d\n", *bar); |
| 9 | +} |
| 10 | + |
| 11 | +int main (int argc, char *argv[]) |
| 12 | +{ |
| 13 | + MPI_Session session, session1; |
| 14 | + MPI_Errhandler errhandler; |
| 15 | + MPI_Group group; |
| 16 | + MPI_Comm comm_world, comm_self; |
| 17 | + MPI_Info info; |
| 18 | + int rc, npsets, one = 1, sum, i; |
| 19 | + |
| 20 | + rc = MPI_Session_create_errhandler (my_session_errhandler, &errhandler); |
| 21 | + if (MPI_SUCCESS != rc) { |
| 22 | + fprintf (stderr, "Error handler creation failed with rc = %d\n", rc); |
| 23 | + abort (); |
| 24 | + } |
| 25 | + |
| 26 | + rc = MPI_Info_create (&info); |
| 27 | + if (MPI_SUCCESS != rc) { |
| 28 | + fprintf (stderr, "Info creation failed with rc = %d\n", rc); |
| 29 | + abort (); |
| 30 | + } |
| 31 | + |
| 32 | + rc = MPI_Info_set(info, "mpi_thread_support_level", "MPI_THREAD_MULTIPLE"); |
| 33 | + if (MPI_SUCCESS != rc) { |
| 34 | + fprintf (stderr, "Info key/val set failed with rc = %d\n", rc); |
| 35 | + abort (); |
| 36 | + } |
| 37 | + |
| 38 | + rc = MPI_Session_init (info, errhandler, &session); |
| 39 | + if (MPI_SUCCESS != rc) { |
| 40 | + fprintf (stderr, "Session initialization failed with rc = %d\n", rc); |
| 41 | + abort (); |
| 42 | + } |
| 43 | + |
| 44 | + rc = MPI_Session_get_num_psets (session, MPI_INFO_NULL, &npsets); |
| 45 | + for (i = 0 ; i < npsets ; ++i) { |
| 46 | + int psetlen = 0; |
| 47 | + char name[256]; |
| 48 | + MPI_Session_get_nth_pset (session, MPI_INFO_NULL, i, &psetlen, NULL); |
| 49 | + MPI_Session_get_nth_pset (session, MPI_INFO_NULL, i, &psetlen, name); |
| 50 | + fprintf (stderr, " PSET %d: %s (len: %d)\n", i, name, psetlen); |
| 51 | + } |
| 52 | + |
| 53 | + rc = MPI_Group_from_session_pset (session, "mpi://WORLD", &group); |
| 54 | + if (MPI_SUCCESS != rc) { |
| 55 | + fprintf (stderr, "Could not get a group for mpi://WORLD. rc = %d\n", rc); |
| 56 | + abort (); |
| 57 | + } |
| 58 | + |
| 59 | + MPI_Comm_create_from_group (group, "my_world", MPI_INFO_NULL, MPI_ERRORS_RETURN, &comm_world); |
| 60 | + MPI_Group_free (&group); |
| 61 | + |
| 62 | + MPI_Allreduce (&one, &sum, 1, MPI_INT, MPI_SUM, comm_world); |
| 63 | + |
| 64 | + fprintf (stderr, "World Comm Sum (1): %d\n", sum); |
| 65 | + |
| 66 | + rc = MPI_Group_from_session_pset (session, "mpi://SELF", &group); |
| 67 | + if (MPI_SUCCESS != rc) { |
| 68 | + fprintf (stderr, "Could not get a group for mpi://SELF. rc = %d\n", rc); |
| 69 | + abort (); |
| 70 | + } |
| 71 | + |
| 72 | + MPI_Comm_create_from_group (group, "myself", MPI_INFO_NULL, MPI_ERRORS_RETURN, &comm_self); |
| 73 | + MPI_Group_free (&group); |
| 74 | + MPI_Allreduce (&one, &sum, 1, MPI_INT, MPI_SUM, comm_self); |
| 75 | + |
| 76 | + fprintf (stderr, "Self Comm Sum (1): %d\n", sum); |
| 77 | + |
| 78 | + MPI_Errhandler_free (&errhandler); |
| 79 | + MPI_Info_free (&info); |
| 80 | + |
| 81 | + |
| 82 | + MPI_Comm_free (&comm_world); |
| 83 | + MPI_Comm_free (&comm_self); |
| 84 | + MPI_Session_finalize (&session); |
| 85 | + printf("Finished first finalize\n"); |
| 86 | + |
| 87 | + rc = MPI_Session_create_errhandler (my_session_errhandler, &errhandler); |
| 88 | + if (MPI_SUCCESS != rc) { |
| 89 | + fprintf (stderr, "Error handler creation failed with rc = %d\n", rc); |
| 90 | + abort (); |
| 91 | + } |
| 92 | + |
| 93 | + rc = MPI_Info_create (&info); |
| 94 | + if (MPI_SUCCESS != rc) { |
| 95 | + fprintf (stderr, "Info creation failed with rc = %d\n", rc); |
| 96 | + abort (); |
| 97 | + } |
| 98 | + |
| 99 | + rc = MPI_Info_set(info, "mpi_thread_support_level", "MPI_THREAD_MULTIPLE"); |
| 100 | + if (MPI_SUCCESS != rc) { |
| 101 | + fprintf (stderr, "Info key/val set failed with rc = %d\n", rc); |
| 102 | + abort (); |
| 103 | + } |
| 104 | + |
| 105 | + printf("Starting second init\n"); |
| 106 | + rc = MPI_Session_init (info, errhandler, &session); |
| 107 | + printf("Finished second init\n"); |
| 108 | + if (MPI_SUCCESS != rc) { |
| 109 | + fprintf (stderr, "Session initialization failed with rc = %d\n", rc); |
| 110 | + abort (); |
| 111 | + } |
| 112 | + |
| 113 | + rc = MPI_Session_get_num_psets (session, MPI_INFO_NULL, &npsets); |
| 114 | + for (i = 0 ; i < npsets ; ++i) { |
| 115 | + int psetlen = 0; |
| 116 | + char name[256]; |
| 117 | + MPI_Session_get_nth_pset (session, MPI_INFO_NULL, i, &psetlen, NULL); |
| 118 | + MPI_Session_get_nth_pset (session, MPI_INFO_NULL, i, &psetlen, name); |
| 119 | + fprintf (stderr, " PSET %d: %s (len: %d)\n", i, name, psetlen); |
| 120 | + } |
| 121 | + |
| 122 | + rc = MPI_Group_from_session_pset (session, "mpi://WORLD", &group); |
| 123 | + if (MPI_SUCCESS != rc) { |
| 124 | + fprintf (stderr, "Could not get a group for mpi://WORLD. rc = %d\n", rc); |
| 125 | + abort (); |
| 126 | + } |
| 127 | + |
| 128 | + MPI_Comm_create_from_group (group, "my_world", MPI_INFO_NULL, MPI_ERRORS_RETURN, &comm_world); |
| 129 | + MPI_Group_free (&group); |
| 130 | + |
| 131 | + MPI_Allreduce (&one, &sum, 1, MPI_INT, MPI_SUM, comm_world); |
| 132 | + |
| 133 | + fprintf (stderr, "World Comm Sum (1): %d\n", sum); |
| 134 | + |
| 135 | + |
| 136 | + rc = MPI_Group_from_session_pset (session, "mpi://SELF", &group); |
| 137 | + if (MPI_SUCCESS != rc) { |
| 138 | + fprintf (stderr, "Could not get a group for mpi://SELF. rc = %d\n", rc); |
| 139 | + abort (); |
| 140 | + } |
| 141 | + |
| 142 | + MPI_Comm_create_from_group (group, "myself", MPI_INFO_NULL, MPI_ERRORS_RETURN, &comm_self); |
| 143 | + MPI_Group_free (&group); |
| 144 | + MPI_Allreduce (&one, &sum, 1, MPI_INT, MPI_SUM, comm_self); |
| 145 | + |
| 146 | + fprintf (stderr, "Self Comm Sum (1): %d\n", sum); |
| 147 | + |
| 148 | + MPI_Errhandler_free (&errhandler); |
| 149 | + MPI_Info_free (&info); |
| 150 | + |
| 151 | + |
| 152 | + MPI_Comm_free (&comm_world); |
| 153 | + MPI_Comm_free (&comm_self); |
| 154 | + MPI_Session_finalize (&session); |
| 155 | + |
| 156 | + return 0; |
| 157 | +} |
0 commit comments