Giter Club home page Giter Club logo

Comments (3)

takagi avatar takagi commented on May 22, 2024

cupy.partiotion as well as numpy.partition belong to a problem domain called unordered partial sort, which is a very similar problem with selection of kth element.

https://en.wikipedia.org/wiki/Selection_algorithm#Unordered_partial_sorting

There are the following algorithms to solve such kind of problems:

  • Quickselect
  • Median of median
  • Heap select
  • Introselect
  • Radixselect

numpy.partition uses introselect, which is a hybrid algorithm of quickselect and median of medians and does not paralellize well. For GPU environment with CuPy, radixselect would be the most likely option.

The following papers mention on radixselect:

The first paper has its public source code and Tensorflow guys have some discussion on it recently.

from cupy.

takagi avatar takagi commented on May 22, 2024

I compared the performance of:

  • radixselect on CPU
  • std::sort & choose on CPU
  • thrust::sort & choose on GPU

(Celeron G3900 @ 2.80GHz and GeForce GTX 750 Ti)

Just thrust::sort & choose may be enough at first because its performance fit well to O(n) in time complexity.

https://gist.github.com/sonots/7e11231af2f2cc6b1f519ad832632566

$ nvcc radixselect.cu -O2 -std=c++11 -Wno-deprecated-gpu-targets && ./a.out

----- n = 1000, k = 100
radixselect : 59233 Time elapsed 0.000006 sec
std::sort   : 59233 Time elapsed 0.000043 sec
thrust::sort: 59233 Time elapsed 0.000583 sec

----- n = 10000, k = 1000
radixselect : 59017 Time elapsed 0.000032 sec
std::sort   : 59017 Time elapsed 0.000597 sec
thrust::sort: 59017 Time elapsed 0.000586 sec

----- n = 100000, k = 10000
radixselect : 58992 Time elapsed 0.000347 sec
std::sort   : 58992 Time elapsed 0.006836 sec
thrust::sort: 58992 Time elapsed 0.000436 sec

----- n = 1000000, k = 100000
radixselect : 58953 Time elapsed 0.004255 sec
std::sort   : 58953 Time elapsed 0.074907 sec
thrust::sort: 58953 Time elapsed 0.002840 sec

----- n = 10000000, k = 1000000
radixselect : 58986 Time elapsed 0.039464 sec
std::sort   : 58986 Time elapsed 0.745077 sec
thrust::sort: 58986 Time elapsed 0.023981 sec

----- n = 100000000, k = 10000000
radixselect : 58983 Time elapsed 0.489075 sec
std::sort   : 58983 Time elapsed 7.569387 sec
thrust::sort: 58983 Time elapsed 0.235514 sec

from cupy.

takagi avatar takagi commented on May 22, 2024

According to Fig. 7 in the paper,

sort & choose has O(N) complexity in time for vector length N as theoretically shown, and is at least just four times slower than radixselect in case vector length is 2^27.

It seems reasonable to first implement cupy.partition with naive sort & choose.

from cupy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.