utestMpiBitonic.cpp 7.62 KB
Newer Older
1
// See LICENCE file at project root
2 3 4
#ifndef UTESTMPIBITONIC_CPP
#define UTESTMPIBITONIC_CPP

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

BRAMAS Berenger's avatar
BRAMAS Berenger committed
8 9
#include "Utils/FMpi.hpp"
#include "Utils/FBitonicSort.hpp"
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 TestMpiBitonic : public FUTesterMpi<TestMpiBitonic> {
    ////////////////////////////////////////////////////////////
    /// 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();
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
        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);

        FBitonicSort<long, long, int>::Sort(elements.get(), nbElements,app.global());

        CheckIfSorted(elements.get(), nbElements);
        CheckBorder(elements.get(), nbElements);

        uassert(nbElementsInTest == app.global().reduceSum(nbElements));
    }

    void TestSameSort(){
132
        //const int myRank = app.global().processId();
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
        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);

        FBitonicSort<long, long, int>::Sort(elements.get(), nbElements,app.global());

        CheckIfSorted(elements.get(), nbElements);
        CheckBorder(elements.get(), nbElements);

        uassert(nbElementsInTest == app.global().reduceSum(nbElements));
    }


    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);

        FBitonicSort<long, long, int>::Sort(elements.get(), nbElements,app.global());

        CheckIfSorted(elements.get(), nbElements);
        CheckBorder(elements.get(), nbElements);

        uassert(nbElementsInTest == app.global().reduceSum(nbElements));
    }

    void TestBigSort(){
175 176
        //const int myRank = app.global().processId();
        //const int nbProcess = app.global().processCount();
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

        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);

        FBitonicSort<long, long, int>::Sort(elements.get(), nbElements,app.global());

        CheckIfSorted(elements.get(), nbElements);
        CheckBorder(elements.get(), nbElements);

        uassert(nbElementsInTest == app.global().reduceSum(nbElements));
    }


    ////////////////////////////////////////////////////////////
    /// Starter functions
    ////////////////////////////////////////////////////////////

    // set test
    void SetTests(){
        AddTest(&TestMpiBitonic::TestSmallSort,"Test small sort");
        AddTest(&TestMpiBitonic::TestSameSort,"Test with all the same value");
        AddTest(&TestMpiBitonic::TestUniqueSort,"Test with unique value");
        AddTest(&TestMpiBitonic::TestBigSort,"Test with random values");
    }
public:
    TestMpiBitonic(int argc,char ** argv) : FUTesterMpi(argc,argv){
    }
};

// You must do this
TestClassMpi(TestMpiBitonic)

#endif // UTESTMPIBITONIC_CPP