# Parallelization way

**URL:** <https://discourse.julialang.org/t/parallelization-way/87536>\
**Category:** General Usage\
**Tags:** parallel\
**Created:** [September 20, 2022, 10:17pm UTC](https://discourse.julialang.org/t/parallelization-way/87536 "2022-09-20T22:17:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![andrey2185](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andrey2185/32/9889_2.png) [@andrey2185](https://discourse.julialang.org/u/andrey2185)\
**Post date:** [September 20, 2022, 10:17pm UTC](https://discourse.julialang.org/t/parallelization-way/87536/1 "2022-09-20T22:17:45Z")

</div>

Hello, please help to parallelize the task, maybe with CUDA?

There are two point clouds A and B, the clouds have a different number of points.

In cloud A there can be several Y along the X axis,  
In cloud B, there is only one Y for every X.

Data is ordered along the x-axis.

For each point of cloud A on the X-axis within [a\_x; a\_x + 10]  
find the nearest point from cloud B whose sum of y-values is more than zero: (a\_y + b\_y) \>= 0

I do it but it’s so slow:

```julia
## 
using Random

limit = 10

# points number
A_n = 100_000_000
B_n = 30_000_000

#
func(a_y, b_y) = (a_y + b_y) >= 0

# cloud A
A_x = Int32.(cumsum(rand(0:3, A_n)))
A_y = Int32.(rand(-2:2, A_n))

# cloud B
B_x = Int32.(cumsum(rand(1:9, B_n)))
B_y = Int32.(rand(-2:2, B_n))

# for results
R = zeros(Int32, A_n)

## calculations
b = 1
bb = 1
for a = 1 : A_n
    while B_x[b] < A_x[a]
        b += 1
        b == B_n && break
    end
    bb = b
    while B_x[bb] <= (A_x[a] + limit)
        if func(A_y[a], B_y[bb])
            R[a] = B_x[bb]
            break
        end
        bb == B_n && break
        bb += 1
    end
    a % 1_000_000 == 0 && println(a) # progress
    b == B_n && break
end

```

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [September 20, 2022, 10:27pm UTC](https://discourse.julialang.org/t/parallelization-way/87536/2 "2022-09-20T22:27:43Z")

</div>

Before that, check the performance tips of the manual.

First of all, put all the critical code in a function and do not use global variables.

[https://docs.julialang.org/en/v1/manual/performance-tips/](https://docs.julialang.org/en/v1/manual/performance-tips/)

---

<div class="post-metadata">

**Author:** ![jmair](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmair/32/35117_2.png) [@jmair](https://discourse.julialang.org/u/jmair)\
**Post date:** [September 20, 2022, 10:32pm UTC](https://discourse.julialang.org/t/parallelization-way/87536/3 "2022-09-20T22:32:34Z")

</div>

Since the sizes are quite large, this is perfect for parallelisation. I would recommend taking a look at multithreading first - using `Threads.@threads` on the start of a for loop. This kind of problem is known as “embarrasingly” parallel and is a good chance to get to grips with parallel programming.

But I agree with @lmiq, putting the code in a function first is your best bet.
