Exemple #1
0
void ParallellizeBySquares2D::parallelize(){
        // we must have the same number of blocks as processors
    PLB_PRECONDITION(xTiles*yTiles == processorNumber);
    
    plint totalCost = computeCost(originalBlocks, finestBoundingBox);
    plint idealCostPerProcessor = totalCost/processorNumber;
    
    pcout << "Total cost of computations = " << totalCost << std::endl;
    pcout << "We are using " << processorNumber << " processors...\n";
    pcout << "Ideal cost per processor = " << idealCostPerProcessor << std::endl;
    
    std::vector<plint> totalCosts(processorNumber);
    
    plint total = 0;
    for (plint iProc=0; iProc<processorNumber; ++iProc){
        plint blockCost = computeCost(originalBlocks,finestDivision[iProc]);
        totalCosts[iProc] += blockCost;
        mpiDistribution[iProc] = iProc;
        total += blockCost;
    }
    
    pcout << "---- Costs Per Processor ----\n";
    
    for (pluint i=0; i<totalCosts.size(); ++i){
        pcout << i << " : " << totalCosts[i] << std::endl;
        // check if everyone is doing something
        if (totalCosts[i] == 0){
            pcout << "\t >> processor " << i << " does not have work to do. Exiting.....\n";
            std::exit(1);
        }
    }
    
    pcout << "*******************************\n";
    pcout << "Sum of all costs = " << total << std::endl;
    pcout << "*******************************\n";
    
    // convert the original blocks to the new blocks
    recomputedBlocks.resize(originalBlocks.size());
    finalMpiDistribution.resize(originalBlocks.size());
    
    plint finestLevel= (plint)originalBlocks.size()-1;
    for (plint iLevel=finestLevel; iLevel>=0; --iLevel) {
        parallelizeLevel(iLevel, originalBlocks,finestDivision, mpiDistribution);
        // Adapt the regions to the next-coarser level.
        for (pluint iRegion=0; iRegion<finestDivision.size(); ++iRegion) {
            finestDivision[iRegion] = finestDivision[iRegion].divideAndFitSmaller(2);
        }
    }
}
void ParallellizeByCubes3D::parallelize(){
    // we must have the same number of blocks as processors
    PLB_PRECONDITION(xTiles*yTiles*zTiles == processorNumber);
    
    plint totalCost = computeCost(originalBlocks, finestBoundingBox);
    plint idealCostPerProcessor = totalCost/processorNumber;
    
    pcout << "Total cost of computations = " << totalCost << std::endl;
    pcout << "We are using " << processorNumber << " processors...\n";
    pcout << "Ideal cost per processor = " << idealCostPerProcessor << std::endl;
    
    std::vector<plint> totalCosts(processorNumber);
    
    // greedy load balancing part
//     plint currentProcessor = 0;
//     bool allAssigned = false;
//     plint iBlock = 0;
//     plint maxBlockCost = 0;
//     while ( (currentProcessor<processorNumber) && !allAssigned) {
//         plint blockCost = computeCost(originalBlocks, finestDivision[iBlock]);
//         if (blockCost > maxBlockCost) maxBlockCost = blockCost;
//         if ( totalCosts[currentProcessor] < idealCostPerProcessor ){
//             totalCosts[currentProcessor] += blockCost;
//             mpiDistribution[iBlock] = currentProcessor;
//             ++iBlock;
//         }
//         else {
//             currentProcessor++;
//         }
//         if (iBlock>=(plint)finestDivision.size()){
//             allAssigned = true;
//         }
//     } 
//     
//     if (maxBlockCost > idealCostPerProcessor){
//         pcout << "There is a problem : maxBlockCost=" << maxBlockCost << " and ideal cost=" << idealCostPerProcessor
//               << std::endl;
//     }
    
    plint total = 0;
    for (plint iProc=0; iProc<processorNumber; ++iProc){
        plint blockCost = computeCost(originalBlocks,finestDivision[iProc]);
        totalCosts[iProc] += blockCost;
        mpiDistribution[iProc] = iProc;
        total += blockCost;
    }
    
    pcout << "---- Costs Per Processor ----\n";
    
    for (pluint i=0; i<totalCosts.size(); ++i){
        pcout << i << " : " << totalCosts[i] << std::endl;
        // check if everyone is doing something
        if (totalCosts[i] == 0){
            pcout << "\t >> processor " << i << " does not have work to do. Exiting.....\n";
            std::exit(1);
        }
    }
    
    pcout << "*******************************\n";
    pcout << "Sum of all costs = " << total << std::endl;
    pcout << "*******************************\n";
    
    // convert the original blocks to the new blocks
    recomputedBlocks.resize(originalBlocks.size());
    finalMpiDistribution.resize(originalBlocks.size());
    
    plint finestLevel= (plint)originalBlocks.size()-1;
    for (plint iLevel=finestLevel; iLevel>=0; --iLevel) {
        parallelizeLevel(iLevel, originalBlocks,finestDivision, mpiDistribution);
        // Adapt the regions to the next-coarser level.
        for (pluint iRegion=0; iRegion<finestDivision.size(); ++iRegion) {
            finestDivision[iRegion] = finestDivision[iRegion].divideAndFitSmaller(2);
        }
    }
}