Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace bubble sort with a faster algorithm #60

Open
DoubleCouponDay opened this issue Sep 18, 2022 · 9 comments
Open

replace bubble sort with a faster algorithm #60

DoubleCouponDay opened this issue Sep 18, 2022 · 9 comments
Assignees
Labels
3.0 enhancement New feature or request

Comments

@DoubleCouponDay
Copy link
Owner

DoubleCouponDay commented Sep 18, 2022

The algorithm must be able to handle arrays which have no measure of inequality.

@DoubleCouponDay DoubleCouponDay added enhancement New feature or request 3.0 labels Sep 18, 2022
@DoubleCouponDay
Copy link
Owner Author

Struggles particularly with this image: link

It's evident the sorting scales badly with pixel count.

@DoubleCouponDay
Copy link
Owner Author

not just bubble sort. sort.c is the bottleneck, based on performance of logs.

@DoubleCouponDay
Copy link
Owner Author

DoubleCouponDay commented Oct 13, 2022

plan:

  • Extend algorithm threads into sorting.
  • Append the shape lists after sorting has completed.
  • Iterate from the first chunk of every border list. If the chunk is adjacent and was not used before and is in the same shape and is a boundary chunk, set that as the next chunk.
  • set current chunk equal to the next boundary chunk and repeat until done.

@DoubleCouponDay
Copy link
Owner Author

DoubleCouponDay commented Oct 13, 2022

as of 03625c5
sort.c line 53: current_list is set to null and causes null reference behaviour.

@DoubleCouponDay
Copy link
Owner Author

infinite loop at sort.c line 84. only occurs in thread 3.

my guess is that in some cases, next chunk never circles back around to the first chunk.

@DoubleCouponDay
Copy link
Owner Author

currently working on sorting in place during the algorithm, instead of sorting after all shapes created.

@DoubleCouponDay
Copy link
Owner Author

DoubleCouponDay commented Dec 30, 2022

Sorting in place requires too many assumptions with very few indicators on the best course of action. I propose a new plan:

  • Clone opencl and link with it.
  • Create an opencl kernel written in opencl C.
  • The kernel must perform boundary comparison matrices of each pixel with each of their adjacent pixels to create 8 2D arrays of boolean values.
  • Put each of the 8 matrix computations in a separate thread.
  • Put each layer in a separate thread.
  • The comparisons can be done using bitwise OR operations, 32 or 64 bit long depending on the platform architecture. This will aggregate the boundary statuses into the output 2D array.
  • Iterate through all boundary chunks and path find each one, checking that its Neighbours are not already in a shape.
  • Do not path find a chunk if it was already used.
  • Zip each boundary chunk in the direction of the angle between its two adjacent boundary chunks.

@DoubleCouponDay DoubleCouponDay modified the milestones: CPU based, GPU option Dec 30, 2022
@DoubleCouponDay DoubleCouponDay modified the milestones: CPU architecture implemented first, Splitting stage, Aggregating stage, Pathfinding stage, Zipping stage Feb 1, 2023
@DoubleCouponDay DoubleCouponDay added this to the Splitting stage milestone Feb 1, 2023
@DoubleCouponDay DoubleCouponDay modified the milestones: Splitting stage, Aggregating stage, CPU architecture implemented Oct 21, 2023
@DoubleCouponDay
Copy link
Owner Author

Instead of Layers, Adaptive Thresholding should be used so that the right threshold is used for each area. the chunksize variable could be repurposed for this. Chunksize will be the width of a square in which the mean magnitude in 3D is calculated.

To calculate the mean, use the following formula:
mean = Sigma(total of all magnitudes) / number of magnitudes

@DoubleCouponDay
Copy link
Owner Author

Getting a faster algorithm using Adaptive Thresholding requires some concessions to be made in terms of complexity. For the time being I propose that OpenCL research be discontinued until Version 3 is finalized.

  • Read chunksize as int

  • Divide the image into chunks

  • Foreach chunk calculate the mean magnitude of the RGB pixels in the chunk
    each value is a dimension

    store each mean in a reduced 2d array of chunks where xy coords can work

  • Use the mean as the threshold value for that chunk

  • Split vectorizing into 8 directions, one for each direction, one thread per split

  • Foreach split, perform boundary comparison between two pixels along a the split direction

  • Aggregate all splits together using bitwise OR operations on rows of pixels

  • Pathfind the aggregate
    do not pathfind a pixel that has already been used

  • Write paths to svg file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.0 enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants