Commit 92c841ad authored by kolusask's avatar kolusask
Browse files

Enabled random tests in all cases

parent 6420f6b1
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -16,7 +16,7 @@ else
pre-build:
	@echo 'CUDA not used'
CPP := g++ -std=c++17
CPPFLAGS := -Wall -pedantic -Wextra -g $(SANITIZER_FLAGS) $(TNLFLAGS)
CPPFLAGS := -Wall -pedantic -Wextra -Wpessimizing-move -Wredundant-move -g $(TNLFLAGS) $(SANITIZER_FLAGS) 
endif

HEADERS := CuckooHash/CuckooHashMap.h\
+6 −0
Original line number Diff line number Diff line
@@ -3,6 +3,8 @@
#include <TNL/Containers/Array.h>

#include <memory>


template<typename K, typename V> struct Pair {
    __cuda_callable__ Pair(K key=K(), V value=V()) : key(key), value(value) {}

@@ -12,6 +14,10 @@ template<typename K, typename V> struct Pair {
        return *this;
    }

    __cuda_callable__ bool operator==(const Pair& other) {
        return this->key == other.key && this->value == other.value;
    }

    K key;
    V value;
};
+42 −13
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@
#include <TNL/Algorithms/ParallelFor.h>
#include <TNL/Containers/StaticArray.h>

#include <algorithm>
#include <vector>


@@ -25,7 +26,7 @@ using CpuPairs = std::vector<Pair<K, V>>;
/**
 * Wrapper class for testing std::map
 */
template<typename K, typename V, typename D>
template<typename K, typename V, typename Device>
class StdMapWrapper {
    struct Comparator {
        bool operator()(const K& v1, const K& v2) const {
@@ -34,9 +35,9 @@ class StdMapWrapper {
    };

  public:
    StdMapWrapper(const PairArray<K, V>& values, const Pair<K, V>) {
        for (int i = 0; i < values.getSize(); i++)
            m_map[values.key] = values[i].value;        
    StdMapWrapper(const std::vector<Pair<K, V>>& values) {
        for (int i = 0; i < values.size(); i++)
            m_map[values[i].key] = values[i].value;        
    }

    bool find(const K& key, V& value) const {
@@ -77,6 +78,16 @@ M<K, V, Device> test_building(const PairArray<K, V>& pairs) {
}


template<template<typename, typename, class> class M, typename K, typename V>
M<K, V, Device> test_building(const std::vector<Pair<K, V>>& pairs) {
    BEGIN_TEST(BUILDING)
    std::cerr << "\t\tInserting " << pairs.size() << " entries" << std::endl;
    auto table = M<K, V, Device>(pairs);
    END_TEST
    return table;
}


template<template<typename, typename, class> class M, typename K, typename V>
void test_correct_query(const M<K, V, Device>& table, 
                        const CpuPairs<K, V>& cpuPairs,
@@ -123,13 +134,19 @@ void test_wrong_query(const M<K, V, Device>& table,
template<template<typename, typename, class> class M, typename K, typename V>
void test_random(const typename Array<Pair<K, V>, Device>::ConstViewType pairs, const CpuPairs<K, V>& cpuPairs, int nTests, double addedPart) {
    int nAdded = int(pairs.getSize() * addedPart);
    M<K, V, Device> table([&] {
        if constexpr (std::is_same<M<K, V, Device>, StdMapWrapper<K, V, Device>>::value)
            return M<K, V, Device>(std::vector<Pair<K, V>>(cpuPairs.begin(), cpuPairs.begin() + nAdded));
        else {
            auto arr = Array<Pair<K, V>, Device>(nAdded);
            auto aView = arr.getView();
    auto copy_part = [aView, pairs] __cuda_callable__ (int i) mutable {
        aView[i] = pairs[i];
            auto copy_part = [aView, cpuPairs] __cuda_callable__ (int i) mutable {
                aView[i] = cpuPairs[i];
            };
            TNL::Algorithms::ParallelFor<Device>::exec(0, nAdded, copy_part);
    auto table = M<K, V, Device>(arr);
            return M<K, V, Device>(arr);
        }
    }());
    int correct = 0, wrong = 0;
    BEGIN_TEST(RANDOM)
    std::cerr << "\t\tFor " << pairs.getSize() << " entries, "
@@ -175,8 +192,20 @@ template<template<typename, typename, class> class M, typename K, typename V>
void test(const char* fileName) {
    std::vector<Pair<K, V>> cpuPairs; 
    auto pairs = read_data<K, V>(fileName, cpuPairs);
    auto table = test_building<M, K, V>(pairs);
    M<K, V, Device> table([&] {
        if constexpr (std::is_same<M<K, V, Device>, StdMapWrapper<K, V, Device>>::value)
            return test_building<M, K, V>(cpuPairs);
        else
            return test_building<M, K, V>(pairs);
    }());
    //table.debug_print();
    std::sort(cpuPairs.begin(), cpuPairs.end(), [](const Pair<K, V>& p1, const Pair<K, V>& p2) -> bool {
        for (int i = 0; i < 3; i++)
            if (p1.key[i] != p2.key[i])
                return p1.key[i] < p2.key[i];
        return false;
    });
    cpuPairs.erase(std::unique(cpuPairs.begin(), cpuPairs.end()), cpuPairs.end());
    test_correct_query<M, K, V>(table, cpuPairs, pairs.getSize());
    test_wrong_query<M, K, V>(table, cpuPairs, pairs.getSize());
    test_random<M, K, V>(pairs.getConstView(), cpuPairs, 100000, 0.5);
@@ -272,7 +301,7 @@ void print_results_table() {


void run() {
    //test_class<StdMapWrapper>("std::map");
    test_class<StdMapWrapper>("std::map");
    test_class<CuckooHashMap>("CuckooHashMap");
    test_class<HashGraphV1Map>("HashGraphV1Map");

+15 −7
Original line number Diff line number Diff line
@@ -230,7 +230,7 @@ Array<Cell, Device> read_data(std::string fileName, std::vector<Cell>& cpuLines)


template<template<class, class> class S, typename T>
void test(std::string fileName, bool testRandom = true) {
void test(std::string fileName) {
    std::vector<T> cpuValues;
    auto values = get_data<T>(fileName, cpuValues);
    S<T, Device> table([&] {
@@ -239,12 +239,20 @@ void test(std::string fileName, bool testRandom = true) {
        else
            return test_building<S, T>(values);
    }());
    std::sort(cpuValues.begin(), cpuValues.end(),
    [](const T& v1, const T& v2) -> bool {
        if constexpr (std::is_same<T, Cell>::value) {
            for (int i = 0; i < 4; i++)
                if (v1[i] != v2[i])
                    return v1[i] < v2[i];
            return false;
        } else
            return v1 < v2;
    });
    cpuValues.erase(std::unique(cpuValues.begin(), cpuValues.end()), cpuValues.end());
    test_correct_query<S, T>(table, cpuValues, cpuValues.size());
    test_wrong_query<S, T>(table, cpuValues, cpuValues.size());
    if (testRandom)
        test_random<S, T>(values.getConstView(), cpuValues, 100000, 0.5);
    else
        g_results[g_row][g_col] = nan("");
    test_random<S, T>(Array<T, Device>(cpuValues).getConstView(), cpuValues, 100000, 0.5);
    g_row++;
    g_col = 0;
}
@@ -270,10 +278,10 @@ Triangle ruin<Triangle>(const Triangle& value) {
template<template<class, class> class S>
void test_class(const std::string className, const std::string dataFile) {
    std::cerr << "Testing " << className << "<TNL::Containers::StaticArray<4, int>> with " << dataFile << ':' << std::endl;
    test<S, Cell>(dataFile, false);
    test<S, Cell>(dataFile);

    std::cerr <<  "Testing " << className << "<Triangle> with " << dataFile << ':' << std::endl;
    test<S, Triangle>(dataFile, false);
    test<S, Triangle>(dataFile);
}