|
| 1 | + |
| 2 | + |
| 3 | +""" |
| 4 | +returns area and centroids for all elements in sites_set. |
| 5 | +""" |
| 6 | +function grid_features(A::AbstractMatrix{<:Integer}, sites_set) |
| 7 | + device = get_backend(A) |
| 8 | + # Define the number of bins for the histogram |
| 9 | + num_bins = length(sites_set) |
| 10 | + |
| 11 | + # Create arrays to store the histogram counts |
| 12 | + hist_counts = similar(A, Int32, num_bins) |
| 13 | + hist_counts .= 0 |
| 14 | + |
| 15 | + # Create arrays to store the sum of x-coordinates for each bin |
| 16 | + hist_x_sum = similar(A, Int32, num_bins) |
| 17 | + hist_x_sum .= 0 |
| 18 | + # Create arrays to store the sum of y-coordinates for each bin |
| 19 | + hist_y_sum = similar(A, Int32, num_bins) |
| 20 | + hist_y_sum .= 0 |
| 21 | + |
| 22 | + sites_array = similar(A, Int32, num_bins) |
| 23 | + sites_array = typeof(sites_array)(Int32.(collect(sites_set))) |
| 24 | + # Define the kernel |
| 25 | + @kernel function histogram_kernel!(A, hist_counts, hist_x_sum, hist_y_sum, sites_set) |
| 26 | + i = @index(Global, Linear) |
| 27 | + #x = mod(i - 1, size(A, 2)) + 1 |
| 28 | + #y = cld(i, size(A, 2)) |
| 29 | + sz_grid = size(A) |
| 30 | + coords = CartesianIndices(sz_grid) |
| 31 | + |
| 32 | + x = coords[i][1] |
| 33 | + y = coords[i][2] |
| 34 | + |
| 35 | + bin_idx = findfirst(==(A[x, y]), sites_set) |
| 36 | + if !isnothing(bin_idx) |
| 37 | + Atomix.@atomic hist_counts[bin_idx] += 1 |
| 38 | + KernelAbstractions.@atomic hist_x_sum[bin_idx] += Int32(x) |
| 39 | + KernelAbstractions.@atomic hist_y_sum[bin_idx] += Int32(y) |
| 40 | + #= |
| 41 | + =# |
| 42 | + end |
| 43 | + end |
| 44 | + # @show "launch hist" |
| 45 | + # Launch the kernel |
| 46 | + event = histogram_kernel!(device, 128)( |
| 47 | + A, |
| 48 | + hist_counts, |
| 49 | + hist_x_sum, |
| 50 | + hist_y_sum, |
| 51 | + sites_array; |
| 52 | + ndrange = size(A), |
| 53 | + ) |
| 54 | + KernelAbstractions.synchronize(device) |
| 55 | + |
| 56 | + |
| 57 | + # Calculate the centroids |
| 58 | + centroids_x = similar(A, Float32, num_bins) |
| 59 | + centroids_y = similar(A, Float32, num_bins) |
| 60 | + |
| 61 | + @kernel function centroid_kernel!( |
| 62 | + hist_counts, |
| 63 | + hist_x_sum, |
| 64 | + hist_y_sum, |
| 65 | + centroids_x, |
| 66 | + centroids_y, |
| 67 | + ) |
| 68 | + i = @index(Global, Linear) |
| 69 | + if hist_counts[i] > 0 |
| 70 | + centroids_x[i] = hist_x_sum[i] / hist_counts[i] |
| 71 | + centroids_y[i] = hist_y_sum[i] / hist_counts[i] |
| 72 | + end |
| 73 | + end |
| 74 | + # @show "launch centroid" |
| 75 | + # Launch the centroid kernel |
| 76 | + event = centroid_kernel!(device, 128)( |
| 77 | + hist_counts, |
| 78 | + hist_x_sum, |
| 79 | + hist_y_sum, |
| 80 | + centroids_x, |
| 81 | + centroids_y; |
| 82 | + ndrange = (num_bins,), |
| 83 | + ) |
| 84 | + KernelAbstractions.synchronize(device) |
| 85 | + |
| 86 | + return Vector(hist_counts) ./ *(size(A)...), |
| 87 | + vec2tuple.(Vector(centroids_x), Vector(centroids_y)) |
| 88 | +end |
| 89 | +vec2tuple(x, y) = (x, y) |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | +#= |
| 94 | +naive CPU version |
| 95 | +
|
| 96 | +function grid_features(grid,id::Int) |
| 97 | + ix = findall(x->x==id,grid) |
| 98 | + centroid = Tuple(sum(ix))./length(ix) |
| 99 | + area = length(ix) |
| 100 | +return centroid,area |
| 101 | +end |
| 102 | +=# |
0 commit comments