File: group_bitmap.c

package info (click to toggle)
openmpi 5.0.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 202,312 kB
  • sloc: ansic: 612,441; makefile: 42,495; sh: 11,230; javascript: 9,244; f90: 7,052; java: 6,404; perl: 5,154; python: 1,856; lex: 740; fortran: 61; cpp: 20; tcl: 12
file content (195 lines) | stat: -rw-r--r-- 6,907 bytes parent folder | download | duplicates (2)
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */
/*
 * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
 *                         University Research and Technology
 *                         Corporation.  All rights reserved.
 * Copyright (c) 2004-2005 The University of Tennessee and The University
 *                         of Tennessee Research Foundation.  All rights
 *                         reserved.
 * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
 *                         University of Stuttgart.  All rights reserved.
 * Copyright (c) 2004-2005 The Regents of the University of California.
 *                         All rights reserved.
 * Copyright (c) 2006-2007 University of Houston. All rights reserved.
 * Copyright (c) 2007      Cisco Systems, Inc. All rights reserved.
 * Copyright (c) 2013      Los Alamos National Security, LLC.  All rights
 *                         reserved.
 * Copyright (c) 2022      Triad National Security, LLC. All rights
 *                         reserved.
 * $COPYRIGHT$
 *
 * Additional copyrights may follow
 *
 * $HEADER$
 */

#include "ompi_config.h"
#include "ompi/group/group.h"
#include "ompi/constants.h"
#include "mpi.h"

static bool check_ranks (int, const int *);

int ompi_group_calc_bmap ( int n, int orig_size , const int *ranks) {
    if (check_ranks(n,ranks)) {
        return ompi_group_div_ceil(orig_size,BSIZE);
    }
    else {
        return -1;
    }
}

/* from parent group to child group*/
int ompi_group_translate_ranks_bmap ( ompi_group_t *parent_group,
                                      int n_ranks, const int *ranks1,
                                      ompi_group_t *child_group,
                                      int *ranks2)
{
    int i,count,j,k,m;
    unsigned char tmp, tmp1;
    for (j=0 ; j<n_ranks ; j++) {
        if ( MPI_PROC_NULL == ranks1[j]) {
            ranks2[j] = MPI_PROC_NULL;
        }
        else {
            ranks2[j] = MPI_UNDEFINED;
            m = ranks1[j];
            count = 0;
            tmp = ( 1 << (m % BSIZE) );
            /* check if the bit that corresponds to the parent rank is set in the bitmap */
            if ( tmp == (child_group->sparse_data.grp_bitmap.grp_bitmap_array[(int)(m/BSIZE)]
                         & (1 << (m % BSIZE)))) {
                /*
                 * add up how many bits are set, till we get to the bit of parent
                 * rank that we want. The rank in the child will be the sum of the bits
                 * that are set on the way till we get to the corresponding bit
                 */
                for (i=0 ; i<=(int)(m/BSIZE) ; i++) {
                    for (k=0 ; k<BSIZE ; k++) {
                        tmp1 = ( 1 << k);
                        if ( tmp1 == ( child_group->sparse_data.grp_bitmap.grp_bitmap_array[i]
                                       & (1 << k) ) ) {
                            count++;
                        }
                        if( i==(int)(m/BSIZE) &&  k==m % BSIZE ) {
                            ranks2[j] = count-1;
                            i = (int)(m/BSIZE) + 1;
                            break;
                        }
                    }
                }
            }
        }
    }
    return OMPI_SUCCESS;
}
/* from child group to parent group */
int ompi_group_translate_ranks_bmap_reverse ( ompi_group_t *child_group,
                                              int n_ranks, const int *ranks1,
                                              ompi_group_t *parent_group,
                                              int *ranks2)
{
    int i,j,count,m,k;
    unsigned char tmp;
    for (j=0 ; j<n_ranks ; j++) {
        if ( MPI_PROC_NULL == ranks1[j]) {
            ranks2[j] = MPI_PROC_NULL;
        }
        else {
            m = ranks1[j];
            count = 0;
            /*
             * Go through all the bits set in the bitmap up to the child rank.
             * The parent rank will be the sum of all bits passed (set and unset)
             */
            for (i=0 ; i<child_group->sparse_data.grp_bitmap.grp_bitmap_array_len ; i++) {
                for (k=0 ; k<BSIZE ; k++) {
                    tmp = ( 1 << k);
                    if ( tmp == ( child_group->sparse_data.grp_bitmap.grp_bitmap_array[i]
                                  & (1 << k) ) ) {
                        count++;
                    }
                    if( m == count-1 ) {
                        ranks2[j] = i*BSIZE + k;
                        i = child_group->sparse_data.grp_bitmap.grp_bitmap_array_len + 1;
                        break;
                    }
                }
            }
        }
    }
    return OMPI_SUCCESS;
}

int ompi_group_div_ceil (int num, int den)
{
    if (0 == num%den) {
        return num/den;
    }
    else {
        return (int)(num/den) + 1;
    }
}
/*
 * This functions is to check that all ranks in the included list of ranks
 * are monotonically increasing. If not, the bitmap format can not be used
 * since we won't be able to translate the ranks correctly since the algorithms
 * assume that the ranks are in order in the bitmap list.
 */
static bool check_ranks (int n, const int *ranks) {
    int i;
    for (i=1 ; i < n ; i++) {
        if ( ranks[i-1] > ranks [i] ) {
            return false;
        }
    }
    return true;
}

int ompi_group_incl_bmap(ompi_group_t* group, int n, const int *ranks,
                         ompi_group_t **new_group)
{
    /* local variables */
    int my_group_rank,i,bit_set;
    ompi_group_t *group_pointer, *new_group_pointer;

    group_pointer = (ompi_group_t *)group;

    if ( 0 == n ) {
        *new_group = MPI_GROUP_EMPTY;
        OBJ_RETAIN(MPI_GROUP_EMPTY);
        return OMPI_SUCCESS;
    }

    new_group_pointer = ompi_group_allocate_bmap(group, n);
    if( NULL == new_group_pointer ) {
        return MPI_ERR_GROUP;
    }
    /* Initialize the bit array to zeros */
    for (i=0 ; i<new_group_pointer->sparse_data.grp_bitmap.grp_bitmap_array_len ; i++) {
        new_group_pointer->
            sparse_data.grp_bitmap.grp_bitmap_array[i] = 0;
    }

    /* set the bits */
    for (i=0 ; i<n ; i++) {
        bit_set = ranks[i] % BSIZE;
        new_group_pointer->
            sparse_data.grp_bitmap.grp_bitmap_array[(int)(ranks[i]/BSIZE)] |= (1 << bit_set);
    }

    new_group_pointer -> grp_parent_group_ptr = group_pointer;

    OBJ_RETAIN(new_group_pointer -> grp_parent_group_ptr);
    ompi_group_increment_proc_count(new_group_pointer -> grp_parent_group_ptr);

    ompi_group_increment_proc_count(new_group_pointer);
    my_group_rank=group_pointer->grp_my_rank;

    ompi_group_translate_ranks (group_pointer,1,&my_group_rank,
                                new_group_pointer,&new_group_pointer->grp_my_rank);

    *new_group = (MPI_Group)new_group_pointer;

    return OMPI_SUCCESS;
}