
Concurrent Bubble Sort on CUDA
- Tutorial
Hello, Habr. I thought someone would need parallel sorting with a relatively simple implementation and high performance on the CUDA platform. This is bubble sorting. Under the cat is an explanation and code that may come in handy (or maybe not ...). I must say right away that the presented program is a benchmark in comparison of performance on the GPU and CPU. If you do not mind the reader, then compile it, please, and put the calculation results in the comments of this article. This is not for science. Just interesting =)
The essence of the bubble sorting method is pairwise comparing the elements and replacing them in the event that a certain condition is met, depending on the direction of sorting. The iterations of the algorithm in single-threaded mode are shown below, when it sorts an increasing array of integers in descending order (each line is the state of the array at the moment) - this is table 1.
Table 2 shows the iterations of sorting by a bubble in multi-threaded mode, which is implemented very simply: while one element moves to the side, then after 2 movements the next element can also be sorted by the bubble method and begins to move, and after another 2 - another, etc. Thus, the entire array begins to “pop up”, which significantly reduces the calculation time.
Already by the number of actions taken, you can see that parallel sorting is faster. But this is not always the case: with small sizes of arrays, single-threaded mode is faster due to the absence of problems with data transfer. I did some test calculations here, and here is their result:

I give an implementation code in the CUDA C language, which contains the code that performs SECURITY SECURITY sorting on CUDA and CPU (in single-threaded mode). In fact, this is a benchmark for your computer about how smartly sorting works on the video card and on the process. Part of this program is the sorting function on a video card on the CUDA platform, which you can use on health in your masterpieces with elementary copy'n'past. The code is presented below the tables.
Well, for this you need to download the drivers for CUDA from the Nvidia website, install them, download the Nvidia Toolkit, install and the Nvidia SDK, also install. Fortunately, now everything is already in one package. I will write how to install CUDA if there is a demand, but I just wish good luck if you are a beginner.
When everything is ready and nvidia CUDA works on your computer, then just compile the main.cu file (or whatever it was called there) and run the program, get the result and put it here.
Wat and that's it for now, thanks for reading. See you later.
PS I would be grateful if you tell me how to speed up the algorithm =)
Let me remind you a bit
The essence of the bubble sorting method is pairwise comparing the elements and replacing them in the event that a certain condition is met, depending on the direction of sorting. The iterations of the algorithm in single-threaded mode are shown below, when it sorts an increasing array of integers in descending order (each line is the state of the array at the moment) - this is table 1.
Table 2 shows the iterations of sorting by a bubble in multi-threaded mode, which is implemented very simply: while one element moves to the side, then after 2 movements the next element can also be sorted by the bubble method and begins to move, and after another 2 - another, etc. Thus, the entire array begins to “pop up”, which significantly reduces the calculation time.
Profit
Already by the number of actions taken, you can see that parallel sorting is faster. But this is not always the case: with small sizes of arrays, single-threaded mode is faster due to the absence of problems with data transfer. I did some test calculations here, and here is their result:

The abscissa axis is the number of elements, the ordinates are the execution time in seconds.
Implementation
I give an implementation code in the CUDA C language, which contains the code that performs SECURITY SECURITY sorting on CUDA and CPU (in single-threaded mode). In fact, this is a benchmark for your computer about how smartly sorting works on the video card and on the process. Part of this program is the sorting function on a video card on the CUDA platform, which you can use on health in your masterpieces with elementary copy'n'past. The code is presented below the tables.
How to compile it ???
Well, for this you need to download the drivers for CUDA from the Nvidia website, install them, download the Nvidia Toolkit, install and the Nvidia SDK, also install. Fortunately, now everything is already in one package. I will write how to install CUDA if there is a demand, but I just wish good luck if you are a beginner.
When everything is ready and nvidia CUDA works on your computer, then just compile the main.cu file (or whatever it was called there) and run the program, get the result and put it here.
Wat and that's it for now, thanks for reading. See you later.
PS I would be grateful if you tell me how to speed up the algorithm =)
Table 1: Single Sort Bubble Sort
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | eleven | 12 | thirteen | 14 |
1 | 0 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | eleven | 12 | thirteen | 14 |
1 | 2 | 0 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | eleven | 12 | thirteen | 14 |
1 | 2 | 3 | 0 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | eleven | 12 | thirteen | 14 |
1 | 2 | 3 | 4 | 0 | 5 | 6 | 7 | 8 | 9 | 10 | eleven | 12 | thirteen | 14 |
1 | 2 | 3 | 4 | 5 | 0 | 6 | 7 | 8 | 9 | 10 | eleven | 12 | thirteen | 14 |
1 | 2 | 3 | 4 | 5 | 6 | 0 | 7 | 8 | 9 | 10 | eleven | 12 | thirteen | 14 |
1 | 2 | 3 | 4 | 5 | 6 | 7 | 0 | 8 | 9 | 10 | eleven | 12 | thirteen | 14 |
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 0 | 9 | 10 | eleven | 12 | thirteen | 14 |
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 0 | 10 | eleven | 12 | thirteen | 14 |
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 0 | eleven | 12 | thirteen | 14 |
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | eleven | 0 | 12 | thirteen | 14 |
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | eleven | 12 | 0 | thirteen | 14 |
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | eleven | 12 | thirteen | 0 | |
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | eleven | 12 | thirteen | 14 | 0 |
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | eleven | 12 | thirteen | 14 | |
2 | 1 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | eleven | 12 | thirteen | 14 | 0 |
2 | 3 | 1 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | eleven | 12 | thirteen | 14 | 0 |
2 | 3 | 4 | 1 | 5 | 6 | 7 | 8 | 9 | 10 | eleven | 12 | thirteen | 14 | 0 |
2 | 3 | 4 | 5 | 1 | 6 | 7 | 8 | 9 | 10 | eleven | 12 | thirteen | 14 | 0 |
2 | 3 | 4 | 5 | 6 | 1 | 7 | 8 | 9 | 10 | eleven | 12 | thirteen | 14 | 0 |
2 | 3 | 4 | 5 | 6 | 7 | 1 | 8 | 9 | 10 | eleven | 12 | thirteen | 14 | 0 |
2 | 3 | 4 | 5 | 6 | 7 | 8 | 1 | 9 | 10 | eleven | 12 | thirteen | 14 | 0 |
2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 1 | 10 | eleven | 12 | thirteen | 14 | 0 |
2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 1 | eleven | 12 | thirteen | 14 | 0 |
2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | eleven | 1 | 12 | thirteen | 14 | 0 |
2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | eleven | 12 | 1 | thirteen | 14 | 0 |
2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | eleven | 12 | thirteen | 1 | 14 | 0 |
2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | eleven | 12 | thirteen | 14 | 1 | |
2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | eleven | 12 | thirteen | 14 | ||
3 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | eleven | 12 | thirteen | 14 | 1 | 0 |
3 | 4 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | eleven | 12 | thirteen | 14 | 1 | 0 |
3 | 4 | 5 | 2 | 6 | 7 | 8 | 9 | 10 | eleven | 12 | thirteen | 14 | 1 | 0 |
3 | 4 | 5 | 6 | 2 | 7 | 8 | 9 | 10 | eleven | 12 | thirteen | 14 | 1 | 0 |
3 | 4 | 5 | 6 | 7 | 2 | 8 | 9 | 10 | eleven | 12 | thirteen | 14 | 1 | 0 |
3 | 4 | 5 | 6 | 7 | 8 | 2 | 9 | 10 | eleven | 12 | thirteen | 14 | 1 | 0 |
3 | 4 | 5 | 6 | 7 | 8 | 9 | 2 | 10 | eleven | 12 | thirteen | 14 | 1 | 0 |
3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 2 | eleven | 12 | thirteen | 14 | 1 | 0 |
3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | eleven | 2 | 12 | thirteen | 14 | 1 | 0 |
3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | eleven | 12 | 2 | thirteen | 14 | 1 | 0 |
3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | eleven | 12 | thirteen | 2 | 14 | 1 | 0 |
3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | eleven | 12 | thirteen | 14 | 2 | 1 | 0 |
3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | eleven | 12 | thirteen | 14 | |||
4 | 3 | 5 | 6 | 7 | 8 | 9 | 10 | eleven | 12 | thirteen | 14 | 2 | 1 | 0 |
4 | 5 | 3 | 6 | 7 | 8 | 9 | 10 | eleven | 12 | thirteen | 14 | 2 | 1 | 0 |
4 | 5 | 6 | 3 | 7 | 8 | 9 | 10 | eleven | 12 | thirteen | 14 | 2 | 1 | 0 |
4 | 5 | 6 | 7 | 3 | 8 | 9 | 10 | eleven | 12 | thirteen | 14 | 2 | 1 | 0 |
4 | 5 | 6 | 7 | 8 | 3 | 9 | 10 | eleven | 12 | thirteen | 14 | 2 | 1 | 0 |
4 | 5 | 6 | 7 | 8 | 9 | 3 | 10 | eleven | 12 | thirteen | 14 | 2 | 1 | 0 |
4 | 5 | 6 | 7 | 8 | 9 | 10 | 3 | eleven | 12 | thirteen | 14 | 2 | 1 | 0 |
4 | 5 | 6 | 7 | 8 | 9 | 10 | eleven | 3 | 12 | thirteen | 14 | 2 | 1 | 0 |
4 | 5 | 6 | 7 | 8 | 9 | 10 | eleven | 12 | 3 | thirteen | 14 | 2 | 1 | 0 |
4 | 5 | 6 | 7 | 8 | 9 | 10 | eleven | 12 | thirteen | 3 | 14 | 2 | 1 | 0 |
4 | 5 | 6 | 7 | 8 | 9 | 10 | eleven | 12 | thirteen | 14 | 3 | 2 | 1 | 0 |
4 | 5 | 6 | 7 | 8 | 9 | 10 | eleven | 12 | thirteen | 14 | 1 | 0 | ||
5 | 4 | 6 | 7 | 8 | 9 | 10 | eleven | 12 | thirteen | 14 | 3 | 2 | 1 | 0 |
5 | 6 | 4 | 7 | 8 | 9 | 10 | eleven | 12 | thirteen | 14 | 3 | 2 | 1 | 0 |
5 | 6 | 7 | 4 | 8 | 9 | 10 | eleven | 12 | thirteen | 14 | 3 | 2 | 1 | 0 |
5 | 6 | 7 | 8 | 4 | 9 | 10 | eleven | 12 | thirteen | 14 | 3 | 2 | 1 | 0 |
5 | 6 | 7 | 8 | 9 | 4 | 10 | eleven | 12 | thirteen | 14 | 3 | 2 | 1 | 0 |
5 | 6 | 7 | 8 | 9 | 10 | 4 | eleven | 12 | thirteen | 14 | 3 | 2 | 1 | 0 |
5 | 6 | 7 | 8 | 9 | 10 | eleven | 4 | 12 | thirteen | 14 | 3 | 2 | 1 | 0 |
5 | 6 | 7 | 8 | 9 | 10 | eleven | 12 | 4 | thirteen | 14 | 3 | 2 | 1 | 0 |
5 | 6 | 7 | 8 | 9 | 10 | eleven | 12 | thirteen | 4 | 14 | 3 | 2 | 1 | 0 |
5 | 6 | 7 | 8 | 9 | 10 | eleven | 12 | thirteen | 14 | 4 | 3 | 2 | 1 | 0 |
5 | 6 | 7 | 8 | 9 | 10 | eleven | 12 | thirteen | 14 | 2 | 1 | 0 | ||
6 | 5 | 7 | 8 | 9 | 10 | eleven | 12 | thirteen | 14 | 4 | 3 | 2 | 1 | 0 |
6 | 7 | 5 | 8 | 9 | 10 | eleven | 12 | thirteen | 14 | 4 | 3 | 2 | 1 | 0 |
6 | 7 | 8 | 5 | 9 | 10 | eleven | 12 | thirteen | 14 | 4 | 3 | 2 | 1 | 0 |
6 | 7 | 8 | 9 | 5 | 10 | eleven | 12 | thirteen | 14 | 4 | 3 | 2 | 1 | 0 |
6 | 7 | 8 | 9 | 10 | 5 | eleven | 12 | thirteen | 14 | 4 | 3 | 2 | 1 | 0 |
6 | 7 | 8 | 9 | 10 | eleven | 5 | 12 | thirteen | 14 | 4 | 3 | 2 | 1 | 0 |
6 | 7 | 8 | 9 | 10 | eleven | 12 | 5 | thirteen | 14 | 4 | 3 | 2 | 1 | 0 |
6 | 7 | 8 | 9 | 10 | eleven | 12 | thirteen | 5 | 14 | 4 | 3 | 2 | 1 | 0 |
6 | 7 | 8 | 9 | 10 | eleven | 12 | thirteen | 14 | 5 | 4 | 3 | 2 | 1 | 0 |
6 | 7 | 8 | 9 | 10 | eleven | 12 | thirteen | 14 | 3 | 2 | 1 | 0 | ||
7 | 6 | 8 | 9 | 10 | eleven | 12 | thirteen | 14 | 5 | 4 | 3 | 2 | 1 | 0 |
7 | 8 | 6 | 9 | 10 | eleven | 12 | thirteen | 14 | 5 | 4 | 3 | 2 | 1 | 0 |
7 | 8 | 9 | 6 | 10 | eleven | 12 | thirteen | 14 | 5 | 4 | 3 | 2 | 1 | 0 |
7 | 8 | 9 | 10 | 6 | eleven | 12 | thirteen | 14 | 5 | 4 | 3 | 2 | 1 | 0 |
7 | 8 | 9 | 10 | eleven | 6 | 12 | thirteen | 14 | 5 | 4 | 3 | 2 | 1 | 0 |
7 | 8 | 9 | 10 | eleven | 12 | 6 | thirteen | 14 | 5 | 4 | 3 | 2 | 1 | 0 |
7 | 8 | 9 | 10 | eleven | 12 | thirteen | 6 | 14 | 5 | 4 | 3 | 2 | 1 | 0 |
7 | 8 | 9 | 10 | eleven | 12 | thirteen | 14 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
7 | 8 | 9 | 10 | eleven | 12 | thirteen | 14 | 4 | 3 | 2 | 1 | 0 | ||
8 | 7 | 9 | 10 | eleven | 12 | thirteen | 14 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
8 | 9 | 7 | 10 | eleven | 12 | thirteen | 14 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
8 | 9 | 10 | 7 | eleven | 12 | thirteen | 14 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
8 | 9 | 10 | eleven | 7 | 12 | thirteen | 14 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
8 | 9 | 10 | eleven | 12 | 7 | thirteen | 14 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
8 | 9 | 10 | eleven | 12 | thirteen | 7 | 14 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
8 | 9 | 10 | eleven | 12 | thirteen | 14 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
8 | 9 | 10 | eleven | 12 | thirteen | 14 | 5 | 4 | 3 | 2 | 1 | 0 | ||
9 | 8 | 10 | eleven | 12 | thirteen | 14 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
9 | 10 | 8 | eleven | 12 | thirteen | 14 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
9 | 10 | eleven | 8 | 12 | thirteen | 14 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
9 | 10 | eleven | 12 | 8 | thirteen | 14 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
9 | 10 | eleven | 12 | thirteen | 8 | 14 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
9 | 10 | eleven | 12 | thirteen | 14 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
9 | 10 | eleven | 12 | thirteen | 14 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | ||
10 | 9 | eleven | 12 | thirteen | 14 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
10 | eleven | 9 | 12 | thirteen | 14 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
10 | eleven | 12 | 9 | thirteen | 14 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
10 | eleven | 12 | thirteen | 9 | 14 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
10 | eleven | 12 | thirteen | 14 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
10 | eleven | 12 | thirteen | 14 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | ||
eleven | 10 | 12 | thirteen | 14 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
eleven | 12 | 10 | thirteen | 14 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
eleven | 12 | thirteen | 10 | 14 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
eleven | 12 | thirteen | 14 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
eleven | 12 | thirteen | 14 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | ||
12 | eleven | thirteen | 14 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
12 | thirteen | eleven | 14 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
12 | thirteen | 14 | eleven | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
12 | thirteen | 14 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | ||
thirteen | 12 | 14 | eleven | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
thirteen | 14 | 12 | eleven | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
thirteen | 14 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | ||
14 | thirteen | 12 | eleven | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
Table 2: Bubble Sort in Multithreaded Mode
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | eleven | 12 | thirteen | 14 |
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | eleven | 12 | thirteen | 14 |
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | eleven | 12 | thirteen | 14 |
1 | 0 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | eleven | 12 | thirteen | 14 |
1 | 2 | 0 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | eleven | 12 | thirteen | 14 |
2 | 1 | 3 | 0 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | eleven | 12 | thirteen | 14 |
2 | 3 | 1 | 4 | 0 | 5 | 6 | 7 | 8 | 9 | 10 | eleven | 12 | thirteen | 14 |
3 | 2 | 4 | 1 | 5 | 0 | 6 | 7 | 8 | 9 | 10 | eleven | 12 | thirteen | 14 |
3 | 4 | 2 | 5 | 1 | 6 | 0 | 7 | 8 | 9 | 10 | eleven | 12 | thirteen | 14 |
4 | 3 | 5 | 2 | 6 | 1 | 7 | 0 | 8 | 9 | 10 | eleven | 12 | thirteen | 14 |
4 | 5 | 3 | 6 | 2 | 7 | 1 | 8 | 0 | 9 | 10 | eleven | 12 | thirteen | 14 |
5 | 4 | 6 | 3 | 7 | 2 | 8 | 1 | 9 | 0 | 10 | eleven | 12 | thirteen | 14 |
5 | 6 | 4 | 7 | 3 | 8 | 2 | 9 | 1 | 10 | 0 | eleven | 12 | thirteen | 14 |
6 | 5 | 7 | 4 | 8 | 3 | 9 | 2 | 10 | 1 | eleven | 0 | 12 | thirteen | 14 |
6 | 7 | 5 | 8 | 4 | 9 | 3 | 10 | 2 | eleven | 1 | 12 | 0 | thirteen | 14 |
7 | 6 | 8 | 5 | 9 | 4 | 10 | 3 | eleven | 2 | 12 | 1 | thirteen | 0 | 14 |
7 | 8 | 6 | 9 | 5 | 10 | 4 | eleven | 3 | 12 | 2 | thirteen | 1 | 14 | 0 |
8 | 7 | 9 | 6 | 10 | 5 | eleven | 4 | 12 | 3 | thirteen | 2 | 14 | 1 | 0 |
8 | 9 | 7 | 10 | 6 | eleven | 5 | 12 | 4 | thirteen | 3 | 14 | 2 | 1 | 0 |
9 | 8 | 10 | 7 | eleven | 6 | 12 | 5 | thirteen | 4 | 14 | 3 | 2 | 1 | 0 |
9 | 10 | 8 | eleven | 7 | 12 | 6 | thirteen | 5 | 14 | 4 | 3 | 2 | 1 | 0 |
10 | 9 | eleven | 8 | 12 | 7 | thirteen | 6 | 14 | 5 | 4 | 3 | 2 | 1 | 0 |
10 | eleven | 9 | 12 | 8 | thirteen | 7 | 14 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
eleven | 10 | 12 | 9 | thirteen | 8 | 14 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
eleven | 12 | 10 | thirteen | 9 | 14 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
12 | eleven | thirteen | 10 | 14 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
12 | thirteen | eleven | 14 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
thirteen | 12 | 14 | eleven | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
thirteen | 14 | 12 | eleven | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
14 | thirteen | 12 | eleven | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
14 | thirteen | 12 | eleven | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
The code:
#include
#include
__global__ void bubbleMove(int *array_device, int N, int step){
int idx = blockDim.x * blockIdx.x + threadIdx.x;
if (idx<(N-1)) {
if (step-2>=idx){
if (array_device[idx]>>(array_device,N,step);
cudaThreadSynchronize();
}
cudaMemcpy(array_host,array_device,N*sizeof(int),cudaMemcpyDeviceToHost);
cudaFree(array_device);
}
void bubleSortCPU(int *array_host, int N){
for (int i = 0; i < N; i++){
for (int j = 0; j < N-i-1; j++) {
if (array_host[j]