5/28/2023 0 Comments Easy vector code![]() This would involve getting the differences between every point in A and every point in B in parallel. The previous method can be further extended. ![]() sad is row vector, dist_mat needs column vector shape of bvec_tiled is (feat_len, alen) int feat_len = a.dims(0) // Same as b.dims(0) Īrray dist_mat = constant(0, alen, blen) static array dist_tile1(array a, array b) ![]() You can now find distance of a single point in A from all points in B. You can get a single point from set A and replicate it to create a matrix that is the same size as B. We can get rid of another FOR loop to have 2-D vectorized code. Static array dist_gfor2(array a, array b) static array dist_gfor1(array a, array b) This can be done in the following manners. Since GFOR's cannot be nested, we can replace one of the for loops using GFOR. GFOR is a parallel version of the for loop in ArrayFire. static array dist_vec(array a, array b)ĭist_mat(ii, jj) = sum(abs(avec - bvec)) A reduction operation after getting the absolute differences will give us the distance without the need of a third loop. You can perform the difference between these vectors in parallel. Keeping the outer two loops the same, you can improve the performance by treating the set of three coordinates as a single vector. Static array dist_naive(array a, array b)Īrray dist_mat = constant(0, a.dims(1), b.dims(1)) ĭist_mat(ii, jj) += abs(a(kk, ii) - b(kk, jj)) This is extremely slow on GPUs // CODE SHOWN FOR REFERENCE. This can be implemented in the following manner. You can find difference in coordinates between the two points and find the distance between the two points. The inner most loop iterating through the three dimensions. The second loop iterating through points in B. The outer most loop iterating through points in A. The naive method would be to have three loops. We can generate the necessary data in the following manner. Each point has coordinates in 3 dimensional space. For example, if A has 200 points and B has 300 points, the distance matrix will be of size 200 x 300. The problem we will be looking at is the calculation of distances from every point in a set A to every point in set B. The same methods can be easily used in numpy, octave, julia or other scientific computing software. ArrayFire is chosen because of my familiarity with the software. In this post, I present various ways to vectorize your code using ArrayFire. Hence, writing vectorized code involves analyzing the pros and cons of the available methods and choosing the right one to solve your problem. Each method has its own benefits and drawbacks. There are many ways you can vectorize a given code segment. However, writing vectorized code may not be intuitive immediately. Writing vectorized code is becoming a necessity to get the best performance out of the current generation parallel hardware and scientific computing software. Programmers and Data Scientists want to take advantage of fast and parallel computational devices.
0 Comments
Leave a Reply. |