Commit 1e1505dd authored by Tomáš Oberhuber's avatar Tomáš Oberhuber
Browse files

Fixing bubble sort.

parent b17d31fa
Loading
Loading
Loading
Loading
+3 −4
Original line number Diff line number Diff line
@@ -49,10 +49,9 @@ int main( int argc, char* argv[] )
   /***
    * Firstly, test the sorting on CPU.
    */
   // Currently this does not work on CPU.
   //std::cout << "Sorting on CPU ... " << std::endl;
   //Array< int, Devices::Host > host_array;
   //sort( host_array );
   std::cout << "Sorting on CPU ... " << std::endl;
   Array< int, Devices::Host > host_array;
   sort( host_array );

#ifdef HAVE_CUDA
   /***
+23 −16
Original line number Diff line number Diff line
@@ -13,6 +13,7 @@
#pragma once

#include <algorithm>
#include <TNL/Assert.h>

namespace TNL {
   namespace Algorithms {
@@ -21,28 +22,34 @@ namespace TNL {
struct BubbleSort
{
   template< typename Device, typename Index, typename Compare, typename Swap >
   void static inplaceSort( const Index begin, const Index end, const Compare& compare, const Swap& swap )
   void static inplaceSort( const Index begin, const Index end, Compare& compare, Swap& swap )
   {
      if( std::is_same< Device, Devices::Host >::value )
      {
         Index left( begin ), right( end );
         Index left( begin ), right( end -1 );
         while( left < right )
         {
            int lastChange;
            //int lastChange( end -1 );
            for( int j = left; j < right - 1; j++ )
            {
               TNL_ASSERT_LT( j+1, end, "" );
               if( ! compare( j, j+1 ) )
               {
                  swap( j, j+1 );
                     lastChange = j;
                  //lastChange = j;
               }
            }
            right = lastChange;
            for( int j = right - 1; j >= left; j-- )
            right--; //lastChange;
            for( int j = right; j >= left; j-- )
            {
               TNL_ASSERT_LT( j+1, end, "" );
               if( ! compare( j, j+1 ) )
               {
                  swap( j, j+1 );
                     lastChange = j;
                  //lastChange = j;
               }
            }
            left = lastChange + 1;
            left++; //lastChange;
         }
      }
      else
+1 −1
Original line number Diff line number Diff line
@@ -138,7 +138,7 @@ template< typename Device,
          typename Compare,
          typename Swap,
          typename Sorter = typename Sorting::DefaultInplaceSorter< Device >::SorterType >
void sort( const Index begin, const Index end, const Compare& compare, const Swap& swap )
void sort( const Index begin, const Index end, Compare&& compare, Swap&& swap )
{
   Sorter::template inplaceSort< Device, Index >( begin, end, compare, swap );
}
+6 −4
Original line number Diff line number Diff line
@@ -9,7 +9,7 @@
#include <TNL/Algorithms/Sorting/BubbleSort.h>
#include <TNL/Algorithms/sort.h>

#if defined HAVE_GTEST && defined HAVE_CUDA
#if defined HAVE_GTEST
#include <gtest/gtest.h>


@@ -22,7 +22,7 @@ void fetchAndSwapSorter( TNL::Containers::ArrayView< TYPE, TNL::Devices::Host >
{
    auto Cmp = [=]__cuda_callable__(const int i, const int j ){return view[ i ]  < view[ j ];};
    auto Swap = [=] __cuda_callable__ (int i, int j) mutable {TNL::swap(view[i], view[j]);};
    bubbleSort(0, view.getSize(), Cmp, Swap);
    BubbleSort::inplaceSort< TNL::Devices::Host >(0, view.getSize(), Cmp, Swap);
}

TEST(fetchAndSwap, oneBlockSort)
@@ -68,7 +68,7 @@ void fetchAndSwap_sortMiddle(TNL::Containers::ArrayView<int, TNL::Devices::Host>
    //auto Fetch = [=]__cuda_callable__(int i){return view[i];};
    auto Cmp = [=]__cuda_callable__(const int i, const int j ){ return view[ i ] < view[ j ]; };
    auto Swap = [=] __cuda_callable__ (int i, int j) mutable { TNL::swap(view[i], view[j]); };
    bubbleSort(from, to, Cmp, Swap);
    BubbleSort::inplaceSort< TNL::Devices::Host >(from, to, Cmp, Swap);
}

TEST(fetchAndSwap, sortMiddle)
@@ -84,9 +84,11 @@ TEST(fetchAndSwap, sortMiddle)
    for(size_t i = 0; i < orig.size(); i++)
    {
        if( i < from || i >= to )
        {
            EXPECT_TRUE(view.getElement(i) == orig[i]);
        }
    }
}

#endif