utestMpiQs.cpp 9.7 KB
Newer Older
1
// See LICENCE file at project root
BRAMAS Berenger's avatar
BRAMAS Berenger committed
2 3 4
#ifndef UTESTMPIQS_HPP
#define UTESTMPIQS_HPP

BRAMAS Berenger's avatar
BRAMAS Berenger committed
5
#include "Utils/FGlobal.hpp"
BRAMAS Berenger's avatar
BRAMAS Berenger committed
6 7
#include "FUTester.hpp"

BRAMAS Berenger's avatar
BRAMAS Berenger committed
8 9
#include "Utils/FMpi.hpp"
#include "Utils/FQuickSortMpi.hpp"
BRAMAS Berenger's avatar
BRAMAS Berenger committed
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

#include <memory>
#include <limits>
#include <cstdlib>

// ==== CMAKE =====
// @FUSE_MPI
// ================

/** this class test the mpi quick sort */
class TestMpiQs : public FUTesterMpi<TestMpiQs> {
    ////////////////////////////////////////////////////////////
    /// Check function
    ////////////////////////////////////////////////////////////

    /** To test if an array is sorted */
    template <class ValueType, class IndexType>
    void CheckIfSorted(const ValueType array[], const IndexType size){
        for(int idx = 1 ; idx < size ; ++idx){
            uassert(array[idx-1] <= array[idx]);
        }
    }
    /** To test if the global sorting is correct */
    template <class ValueType, class IndexType>
    void CheckBorder(const ValueType array[], const IndexType size){
        const int myRank = app.global().processId();
        const int nbProcess = app.global().processCount();

        ValueType atMyLeft;     // The value on my left [myRank-1][size-1]
        ValueType atMyRight;    // The value on my right[myRank+1][0]
        ValueType myLeft;       // My left value [myRank][0]
        ValueType myRight;      // My right value [myRank][size-1]

        // If there is someone on my left
        if(myRank != 0){
            // If I do not have value I shoud replace it
            if(size == 0){
                // If there is no-one on my right I take the max
                if(myRank == nbProcess-1){
                    myLeft = std::numeric_limits<ValueType>::max();
                }
                // [A] Else I receive the value from the right and say it is my left
                else{
                    FMpi::Assert(MPI_Recv(&atMyRight, sizeof(atMyRight), MPI_BYTE, myRank+1, 0, app.global().getComm(), MPI_STATUS_IGNORE) , __LINE__);
                    myLeft = atMyRight;
                }
            }
            else{
                // Take my left value
                myLeft = array[0];
            }
            // Send it to my left neighbors
            FMpi::Assert(MPI_Send(&myLeft, sizeof(myLeft), MPI_BYTE , myRank-1, 0, app.global().getComm()) , __LINE__);
        }
        // If there is someone on my right
        if(myRank != nbProcess-1){
            // I should receive the value (if not already done in [A])
            if(size != 0){
                FMpi::Assert(MPI_Recv(&atMyRight, sizeof(atMyRight), MPI_BYTE, myRank+1, 0, app.global().getComm(), MPI_STATUS_IGNORE) , __LINE__);
            }
            // If I do not have value I shoud replace it
            if(size == 0){
                // If there is no-one on my left I take the min
                if(myRank == 0){
                    myRight = std::numeric_limits<ValueType>::min();
                }
                // [B] Else I receive the value from the left and say it is my right
                else{
                    FMpi::Assert(MPI_Recv(&atMyLeft, sizeof(atMyLeft), MPI_BYTE, myRank-1, 0, app.global().getComm(), MPI_STATUS_IGNORE) , __LINE__);
                    myRight = atMyLeft;
                }
            }
            else{
                // Take my right value
                myRight = array[size-1];
            }
            // Send it to my right neighbors
            FMpi::Assert(MPI_Send(&myRight, sizeof(myRight), MPI_BYTE , myRank+1, 0, app.global().getComm()) , __LINE__);
        }
        // If there is someone on my left
        if(myRank != 0){
            // If not already receive in [B]
            if(size != 0){
                FMpi::Assert(MPI_Recv(&atMyLeft, sizeof(atMyLeft), MPI_BYTE, myRank-1, 0, app.global().getComm(), MPI_STATUS_IGNORE) , __LINE__);
            }
        }
        // Test only if I hold data and if someone on my left
        if(myRank != 0 && size != 0){
            uassert(atMyLeft <= myLeft);
        }
        // Test only if I hold data and if someone on my right
        if(myRank != nbProcess-1 && size != 0){
            uassert(myRight <= atMyRight);
        }
    }

    ////////////////////////////////////////////////////////////
    /// The tests
    ////////////////////////////////////////////////////////////

    void TestSmallSort(){
111
        //const int myRank = app.global().processId();
BRAMAS Berenger's avatar
BRAMAS Berenger committed
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
        const int nbProcess = app.global().processCount();

        const int nbElements = nbProcess;
        std::unique_ptr<long[]> elements(new long[nbElements]);

        for(int idx = 0 ; idx < nbElements ; ++idx){
            elements[idx] = idx;
        }

        const int nbElementsInTest = app.global().reduceSum(nbElements);

        int outSize;
        long* outElements = nullptr;
        FQuickSortMpi<long, long, int>::QsMpi(elements.get(), nbElements, &outElements, &outSize, app.global());

        CheckIfSorted(outElements, outSize);
        CheckBorder(outElements, outSize);

        uassert(nbElementsInTest == app.global().reduceSum(outSize));
        delete[] outElements;
    }

    void TestTinySort(){
        const int myRank = app.global().processId();
        const int nbProcess = app.global().processCount();

        const int nbElements = (myRank == 0 ? nbProcess : 0);
        std::unique_ptr<long[]> elements(new long[nbElements]);

        for(int idx = 0 ; idx < nbElements ; ++idx){
            elements[idx] = idx;
        }

        const int nbElementsInTest = app.global().reduceSum(nbElements);

        int outSize;
        long* outElements = nullptr;
        FQuickSortMpi<long, long, int>::QsMpi(elements.get(), nbElements, &outElements, &outSize, app.global());

        CheckIfSorted(outElements, outSize);
        CheckBorder(outElements, outSize);

        uassert(nbElementsInTest == app.global().reduceSum(outSize));
        delete[] outElements;
    }

    void TestSameSort(){
159
        //const int myRank = app.global().processId();
BRAMAS Berenger's avatar
BRAMAS Berenger committed
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
        const int nbProcess = app.global().processCount();

        const int nbElements = nbProcess * 100;
        std::unique_ptr<long[]> elements(new long[nbElements]);

        for(int idx = 0 ; idx < nbElements ; ++idx){
            elements[idx] = nbProcess;
        }

        const int nbElementsInTest = app.global().reduceSum(nbElements);

        int outSize;
        long* outElements = nullptr;
        FQuickSortMpi<long, long, int>::QsMpi(elements.get(), nbElements, &outElements, &outSize, app.global());

        CheckIfSorted(outElements, outSize);
        CheckBorder(outElements, outSize);

        uassert(nbElementsInTest == app.global().reduceSum(outSize));
        delete[] outElements;
    }


    void TestUniqueSort(){
        const int myRank = app.global().processId();
        const int nbProcess = app.global().processCount();

        const int nbElements = nbProcess * 100;
        std::unique_ptr<long[]> elements(new long[nbElements]);

        for(int idx = 0 ; idx < nbElements ; ++idx){
            elements[idx] = myRank;
        }

        const int nbElementsInTest = app.global().reduceSum(nbElements);

        int outSize;
        long* outElements = nullptr;
        FQuickSortMpi<long, long, int>::QsMpi(elements.get(), nbElements, &outElements, &outSize, app.global());

        CheckIfSorted(outElements, outSize);
        CheckBorder(outElements, outSize);

        uassert(nbElementsInTest == app.global().reduceSum(outSize));
        delete[] outElements;
    }

    void TestBigSort(){
208 209
        //const int myRank = app.global().processId();
        //const int nbProcess = app.global().processCount();
BRAMAS Berenger's avatar
BRAMAS Berenger committed
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231

        const int nbElements = 500;
        std::unique_ptr<long[]> elements(new long[nbElements]);

        for(int idx = 0 ; idx < nbElements ; ++idx){
            elements[idx] = long(drand48() * 10000);
        }

        const int nbElementsInTest = app.global().reduceSum(nbElements);

        int outSize;
        long* outElements = nullptr;
        FQuickSortMpi<long, long, int>::QsMpi(elements.get(), nbElements, &outElements, &outSize, app.global());

        CheckIfSorted(outElements, outSize);
        CheckBorder(outElements, outSize);

        uassert(nbElementsInTest == app.global().reduceSum(outSize));
        delete[] outElements;
    }


232 233

    void TestPivotSort(){
234 235
        //const int myRank = app.global().processId();
        //const int nbProcess = app.global().processCount();
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257

        const int nbElements = 500;
        std::unique_ptr<long[]> elements(new long[nbElements]);

        for(int idx = 0 ; idx < nbElements ; ++idx){
            elements[idx] = 10000;
        }
        elements[0] = 9999;

        const int nbElementsInTest = app.global().reduceSum(nbElements);

        int outSize;
        long* outElements = nullptr;
        FQuickSortMpi<long, long, int>::QsMpi(elements.get(), nbElements, &outElements, &outSize, app.global());

        CheckIfSorted(outElements, outSize);
        CheckBorder(outElements, outSize);

        uassert(nbElementsInTest == app.global().reduceSum(outSize));
        delete[] outElements;
    }

BRAMAS Berenger's avatar
BRAMAS Berenger committed
258 259 260 261 262 263 264 265 266 267 268
    ////////////////////////////////////////////////////////////
    /// Starter functions
    ////////////////////////////////////////////////////////////

    // set test
    void SetTests(){
        AddTest(&TestMpiQs::TestTinySort,"Test tiny sort with values only on p0 at starting");
        AddTest(&TestMpiQs::TestSmallSort,"Test small sort");
        AddTest(&TestMpiQs::TestSameSort,"Test with all the same value");
        AddTest(&TestMpiQs::TestUniqueSort,"Test with unique value");
        AddTest(&TestMpiQs::TestBigSort,"Test with random values");
269
        AddTest(&TestMpiQs::TestPivotSort,"Test with big pivot");
BRAMAS Berenger's avatar
BRAMAS Berenger committed
270 271 272 273 274 275 276 277 278 279
    }
public:
    TestMpiQs(int argc,char ** argv) : FUTesterMpi(argc,argv){
    }
};

// You must do this
TestClassMpi(TestMpiQs)

#endif // UTESTMPIQS_HPP