Skip to content
Snippets Groups Projects
SplitMatrixUpperBound.c 15.7 KiB
Newer Older
  • Learn to ignore specific revisions
  • 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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
    #include "SplitMatrixUpperBound.h"
    
    struct PartWeight {
        long part;
        long weight;
    };
    
    /**
     * Comparison functions.
     */
    int comparePartWeightsASC (const void *a, const void *b) {
        long diff = ((struct PartWeight*)a)->weight - ((struct PartWeight*)b)->weight;
        if(diff == 0)
            return 0;
        return (diff < 0) ? -1 : 1;
    } /* end comparePartWeightsASC */
    
    int comparePartWeightsDESC (const void *a, const void *b) {
        long diff = ((struct PartWeight*)a)->weight - ((struct PartWeight*)b)->weight;
        if(diff == 0)
            return 0;
        return (diff > 0) ? -1 : 1;
    } /* end comparePartWeightsDESC */
    
    
    int SplitMatrixUpperBound(struct sparsematrix *pT, int P, const struct opts *pOptions) {
        /* This function splits the sparse matrix T into P (nearly-)equal parts.
           The split is performed by distributing entire columns (or rows) over the parts,
           possibly cutting some columns (or rows) to maintain feasibility. Nonzeros may be moved.
           This function does not minimize communication volume, but does keep it below the
           upper bound (min(m,n)+1)(P-1).
           
           Input: T sparse matrix,
                  P number of parts to split into,
                  options the options.
           
           Output: T sparse matrix
                   The nonzeros of part i are in positions pT->Pstart[i], pT->Pstart[i+1]-1,
                   where 0<=i<P.
           
           NB: This function works only if the weights are based on the number of nonzeros.
           It does not work when:
            - the matrix is stored as a symmetric matrix,
            - dummy nonzeros with weight 0 are present,
            - or weights are based on matrix columns.
        */
           
        long s, t,          /* Nonzero iterators */
             j, n,          /* Column (row) index and number of columns (rows) */
             *nzInd,        /* Pointer to matrix column (row) indices */
             *w,            /* w[j] = number of nonzeros in column (row) j */
             *lastCol,      /* Last column added to a part */
             *partWeights,  /* Weight of a part */
             *partPointer,  /* Nonzero iterator per part */
             delta, end, targetWeight;
        
        int *p,      /* p[j] = part that column (row) j is assigned to */
            k,       /* General iterator over all parts */
            l,       /* First dimension iterator in ColWghtDist[] */
            q,       /* General processor number; also second dimension iterator in ColWghtDist[] */
            numMinHeap, numMaxHeap;
        
        struct PartWeight maxWeight, minWeight, *MaxHeapItems, *MinHeapItems;
        struct heap MinHeap, MaxHeap;
    
        if (!pT || !pOptions) {
            fprintf(stderr, "SplitMatrixUpperBound(): Null arguments!\n");
            return FALSE;
        }
        
        if ((pT->MMTypeCode[3]=='S' || pT->MMTypeCode[3]=='K' || pT->MMTypeCode[3]=='H') &&
                 pOptions->SymmetricMatrix_UseSingleEntry == SingleEntYes) {
            fprintf(stderr, "SplitMatrixUpperBound(): Cannot be used with symmetric matrices!\n");
            return FALSE;
        }
        if (pT->NrDummies > 0) {
            fprintf(stderr, "SplitMatrixUpperBound(): Cannot be used with dummy nonzeros!\n");
            return FALSE;
        }
        if (pT->MMTypeCode[0] == 'W' && pT->NrColWeights > 0) {
            fprintf(stderr, "SplitMatrixUpperBound(): Cannot be used with column-weighted matrix!\n");
            return FALSE;
        }
        
        if(pT->NrProcs < P) {
            pT->NrProcs = P;
            pT->Pstart = (long *)realloc(pT->Pstart, (pT->NrProcs+1)*sizeof(long));
            if(pT->Pstart == NULL) {
                fprintf( stderr, "SplitMatrixUpperBound(): Not enough memory.\n" );
                return FALSE;
            }
        }
        
        /****
         * Determine direction
         ****/
        if(pT->m < pT->n) {
            n = pT->m;
            nzInd = pT->i;
        }
        else {
            n = pT->n;
            nzInd = pT->j;
        }
        
        /* Compute number of nonzeros in a column (row) */
        w = (long *)calloc(n, sizeof(long));
        if(w == NULL) {
            fprintf( stderr, "SplitMatrixUpperBound(): Not enough memory.\n" );
            return FALSE;
        }
        for(t=0; t<pT->NrNzElts; ++t) {
            ++w[nzInd[t]];
        }
        
        /****
         * Create min heap of part weights
         ****/
        MinHeapItems = (struct PartWeight*)malloc(P*sizeof(struct PartWeight));
        if(MinHeapItems == NULL) {
            fprintf( stderr, "SplitMatrixUpperBound(): Not enough memory.\n" );
            return FALSE;
        }
        
        HeapInit(&MinHeap, sizeof(struct PartWeight), comparePartWeightsDESC);
        
        for(k=0; k<P; ++k) {
            MinHeapItems[k].part = k;
            MinHeapItems[k].weight = 0;
        }
        Heapify(&MinHeap, MinHeapItems, P);
        
        /****
         * Assign columns (rows) to parts
         ****/
        p = (int *)malloc(n * sizeof(int));
        lastCol = (long *)malloc(P * sizeof(long));
        if(p == NULL || lastCol == NULL) {
            fprintf( stderr, "SplitMatrixUpperBound(): Not enough memory.\n" );
            return FALSE;
        }
        
        for(k=0; k<P; ++k) {
            lastCol[k] = -1;
        }
        
        for(j=0; j<n; ++j) {
            /* Assign column (row) j to part minWeight.part with currently least weight. */
            HeapPeek(&MinHeap, &minWeight);
            minWeight.weight += w[j];
            p[j] = minWeight.part;
            lastCol[minWeight.part] = j;
            HeapReplace(&MinHeap, &minWeight, NULL);
        }
        
        /****
         * Compute surpluses and shortages per part and store them in heaps, to be able to determine where we should cut columns (rows).
         * Also register weights of the columns (rows) that will be cut, to be able to keep track of how we want to cut them.
         * We reuse the variable MinHeap, which now will contain shortages, while MaxHeap will contain surpluses.
         ****/
        
        HeapInit(&MaxHeap, sizeof(struct PartWeight), comparePartWeightsASC);
        MaxHeapItems = (struct PartWeight*)malloc(P*sizeof(struct PartWeight));
        
        long **ColWghtDist = (long **)malloc(P * sizeof(long*));
        long *_ColWghtDist = (long *)calloc(P * P, sizeof(long));
        if(MaxHeapItems == NULL || ColWghtDist == NULL || _ColWghtDist == NULL) {
            fprintf( stderr, "SplitMatrixUpperBound(): Not enough memory.\n" );
            return FALSE;
        }
        
        for(l=0; l<P; ++l) {
            ColWghtDist[l] = &(_ColWghtDist[l*P]);
        }
        
        targetWeight = ceil(pT->NrNzElts / (double)P);
        numMinHeap = 0;
        numMaxHeap = 0;
        l = 0;
        for(k=0; k<P; ++k) {
            /* Here we use the fact that the Heap data structure
             * just permutes all entries, hence we can just iterate over
             * these entries as if it were an array (which it 
             * (MinHeapItems) actually still is). We could also iteratively
             * call HeapPop(), but this would be O(P log(P)) while just
             * iterating over the array is O(P).
             */
            if(MinHeapItems[k].weight > targetWeight) {
                /* This part has a surplus */
                MaxHeapItems[numMaxHeap].part = MinHeapItems[k].part;
                MaxHeapItems[numMaxHeap].weight = MinHeapItems[k].weight - targetWeight;
                ++numMaxHeap;
                
                /* Mark last column as cut and link it to ColWghtDist[l] */
                q = MinHeapItems[k].part;
                j = lastCol[q];
                p[j] = -l-1;
                ColWghtDist[l][q] = w[j];
                
                ++l;
            }
            else if(MinHeapItems[k].weight < targetWeight) {
                /* This part has a shortage */
                MinHeapItems[numMinHeap].part = MinHeapItems[k].part;
                MinHeapItems[numMinHeap].weight = MinHeapItems[k].weight - targetWeight;
                ++numMinHeap;
            }
        }
        MinHeapItems = (struct PartWeight*)realloc(MinHeapItems, numMinHeap*sizeof(struct PartWeight));
        MaxHeapItems = (struct PartWeight*)realloc(MaxHeapItems, numMaxHeap*sizeof(struct PartWeight));
        Heapify(&MinHeap, MinHeapItems, numMinHeap);
        Heapify(&MaxHeap, MaxHeapItems, numMaxHeap);
        
        free(w); w = NULL;
        
        /****
         * Cut columns (rows) where necessary, until perfect balance is achieved.
         * By cutting a column, we can move weight from a surplus part to a shortage part.
         ****/
        while(MaxHeap.numItems > 0) {
            if(MinHeap.numItems < 1) {
                fprintf( stderr, "SplitMatrixUpperBound(): Corrupt data.\n" );
                return FALSE;
            }
            HeapPeek(&MinHeap, &minWeight); /* minWeight.weight < 0 */
            HeapPeek(&MaxHeap, &maxWeight); /* maxWeight.weight > 0 */
            
            /* Determine the weight (delta) to move */
            if(maxWeight.weight < -minWeight.weight) {
                delta = maxWeight.weight;
                
                /* Remove maxWeight, reduce minWeight */
                HeapPop(&MaxHeap, NULL);
                minWeight.weight += maxWeight.weight;
                HeapReplace(&MinHeap, &minWeight, NULL);
            }
            else if(maxWeight.weight > -minWeight.weight) {
                delta = -minWeight.weight;
                
                /* Remove minWeight, reduce maxWeight */
                HeapPop(&MinHeap, NULL);
                maxWeight.weight += minWeight.weight;
                HeapReplace(&MaxHeap, &maxWeight, NULL);
            }
            else { /* max == min */
                delta = maxWeight.weight;
                
                HeapPop(&MaxHeap, NULL);
                HeapPop(&MinHeap, NULL);
            }
            
            /* Keep track of the cut */
            l = -p[lastCol[maxWeight.part]]-1;
            ColWghtDist[l][maxWeight.part] -= delta;
            ColWghtDist[l][minWeight.part] = delta;
        }
        
        free(lastCol); lastCol = NULL;
        HeapDestroy(&MaxHeap); MaxHeapItems = NULL; /* Also free()s MaxHeapItems */
        
        /* cutColPrtPtr[l] := the first (lowest) index q for which ColWghtDist[l][q] is nonzero. */
        int *cutColPrtPtr = (int *)malloc(numMaxHeap * sizeof(int));
        if(cutColPrtPtr == NULL) {
            fprintf( stderr, "SplitMatrixUpperBound(): Not enough memory.\n" );
            return FALSE;
        }
        for(l=0; l<numMaxHeap; ++l) {
            cutColPrtPtr[l] = 0;
            
            for(q=0; q<P; ++q) {
                if(ColWghtDist[l][q] > 0) {
                    break;
                }
                ++cutColPrtPtr[l];
            }
        }
        
        /* At this point:
         * p[j] =  q>0    if column j is completely assigned to part q
         *         q<0    if column j cut. The distribution data is available in ColWghtDist[-q-1]
         */
        
        /****
         * Compute total part weights and build Pstart variable
         ****/
        partWeights = (long *)malloc(P * sizeof(long));
        partPointer = (long *)malloc(P * sizeof(long));
        if(partWeights == NULL || partPointer == NULL) {
            fprintf( stderr, "SplitMatrixUpperBound(): Not enough memory.\n" );
            return FALSE;
        }
        
        for(k=0; k<P; ++k) {
            partWeights[k] = targetWeight;
        }
        for(k=0; k<MinHeap.numItems; ++k) {
            /* Here again we use the fact that MinHeapItems is just
             * a valid (permuted) array to iterate over
             */
            partWeights[MinHeapItems[k].part] += MinHeapItems[k].weight;
        }
        
        HeapDestroy(&MinHeap); MinHeapItems = NULL; /* Also free()s MinHeapItems */
        
        /* Build Pstart */
        pT->Pstart[k] = partPointer[0] = 0;
        for(k=1; k<P; ++k) {
            pT->Pstart[k] = partPointer[k] = partPointer[k-1]+partWeights[k-1];
        }
        pT->Pstart[P] = partPointer[P-1]+partWeights[P-1];
        
        /* Sanity check */
        if(pT->Pstart[P] != pT->NrNzElts) {
            fprintf( stderr, "SplitMatrixUpperBound(): Corrupt sum.\n" );
            return FALSE;
        }
        
        free(partWeights); partWeights = NULL;
        
        
        /****
         * Sort nonzeros by part number in order to apply the computed partitioning
         ****/
        for(k=0; k<P; ++k) {
            end = pT->Pstart[k+1];
            
            while(partPointer[k]<end) {
                /* We move the nonzero at position s to part q */
                s = partPointer[k];
                q = p[nzInd[s]];
                
                if(q < 0) {
                    /* We have a cut column (row), so determine q from the cut data */
                    l = -q-1;
                    
                    if(ColWghtDist[l][k] > 0) {
                        /* We may still add this nonzero to the current part.
                         * Doing this, we prevent 1 swap.
                         */
                        q = k;
                    }
                    else {
                        /* We should add this nonzero to an other part. */
                        q = cutColPrtPtr[l];
                        
                        if(ColWghtDist[l][q] == 0) {
                            fprintf( stderr, "SplitMatrixUpperBound(): Unexpected empty column part. %d %d\n", l, q );
                            return FALSE;
                        }
                    }
                    
                    --ColWghtDist[l][q];
                    
                    /* Update cutColPrtPtr to point to a part we may still assign nonzeros to */
                    if(q == cutColPrtPtr[l]) {
                        while(cutColPrtPtr[l] < P && ColWghtDist[l][cutColPrtPtr[l]] == 0) {
                            ++cutColPrtPtr[l];
                        }
                    }
                    
                }
                /* else, column (row) nzInd[s] is completely assigned to part q */
                
                /* If nonzero is already in the right part, we do not need to swap */
                if(k == q) {
                    ++partPointer[k];
                    continue;
                }
                
                /* ('Else',) The nonzero needs to be swapped to the target part */
                t = partPointer[q];
                
    #ifdef INFO2
                if(t > pT->Pstart[q+1]) {
                    fprintf( stderr, "SplitMatrixUpperBound(): Corrupt target pointer.\n" );
                    return FALSE;
                }
    #endif
                
                SwapLong(pT->i, s, t);
                SwapLong(pT->j, s, t);
                if (pT->MMTypeCode[2] != 'P')
                    SwapDouble(pT->ReValue, s, t);
                if (pT->MMTypeCode[2] == 'C')
                    SwapDouble(pT->ImValue, s, t);
                
                /* Update partPointer to point to the next nonzero not belonging to q */
                do {
                    ++partPointer[q];
                }
                while(partPointer[q] < pT->Pstart[q+1] && p[nzInd[partPointer[q]]] == q);
            }
        }
        
    #ifdef INFO2
        /* Check part integrity */
        for(k=0; k<P; ++k) {
            if(partPointer[k] != pT->Pstart[k+1]) {
                fprintf( stderr, "SplitMatrixUpperBound(): Corrupt result.\n" );
                return FALSE;
            }
        }
    
    #endif
        
        free(p);
        free(cutColPrtPtr);
        free(ColWghtDist);
        free(_ColWghtDist);
        free(partPointer);
    
    #ifdef INFO2
        if(!CheckUpperBoundSolution(pT)) {
            return FALSE;
        }
    #endif
        
    
        /* Compute new lambdas */
        InitNprocs(pT, COL, pT->RowLambda);
        InitNprocs(pT, ROW, pT->ColLambda);
        
    
        return TRUE;
    } /* end SplitMatrixUpperBound */
    
    
    int CheckUpperBoundSolution(struct sparsematrix *pT) {
        /* This function checks whether the distributed matrix computed by
           SplitMatrixUpperBound() is valid. I.e., it checks whether
           the computed solution has a fairly distributed work load and
           communication volume not higher than the upper bound.
           
           Input: T sparse matrix
           Output: TRUE if the solution is valid, FALSE otherwise
        */
        
        int P = pT->NrProcs;
    
        
        /* Calculate weights */
        long Wmax = 0, weight;
        for(int q=0; q<P; ++q) {
            weight = pT->Pstart[q+1] - pT->Pstart[q];
            if(weight > Wmax) {
                Wmax = weight;
            }
        }
        
        if(Wmax > ceil(pT->NrNzElts/(double)P)) {
    
    #ifdef INFO2
            fprintf( stderr, "CheckUpperBoundSolution(): Invalid imbalance result.\n" );
    #endif
    
            return FALSE;
        }
        
        /* Calculate communication volume */
        long ComVol1, ComVol2, tmp;
        CalcCom(pT, NULL, (pT->m < pT->n)?ROW:COL, &ComVol1, &tmp, &tmp, &tmp, &tmp);
        CalcCom(pT, NULL, (pT->m < pT->n)?COL:ROW, &ComVol2, &tmp, &tmp, &tmp, &tmp);
        
    
        long n = (pT->m < pT->n)?pT->m:pT->n;
    
        if(ComVol1 > n*(P-1) || ComVol2 > P-1) {
    
    #ifdef INFO2
            fprintf( stderr, "CheckUpperBoundSolution(): Invalid communication result.\n" );
    #endif
    
            return FALSE;
        }
        
        return TRUE;
    
    } /* end CheckUpperBoundSolution */