Q:

Accelerating the construction of a ParallelTable

I wish to calculate a rather dense antisymmetric matrix quite fast based on a random distribution of points (and their radii) in the three dimensional space as follows:
ClearAll["Global`*"]; 
n = 12; size = n*n*n;
Print["The size of the matrix is = ", size];
grid = RandomReal[{-10, 10}, {size, 3}];
epsilon = 5.2; cl = 6.;
xgrid2 = Map[First, grid];
ygrid2 = Map[(#[[2]]) &, grid];
zgrid2 = Map[Last, grid];
rad[k_, l_] := EuclideanDistance[grid[[k]], grid[[l]]]
mat1 = ParallelTable[With[{radial = rad[i, j]},
     If[radial <= epsilon, -((
       56 Max[cl - radial, 
         0]^5 (cl + 5 radial) (xgrid2[[i]] - xgrid2[[j]]))/cl^8), 0]]
    , {i, 1, size}, {j, 1, size}]; // AbsoluteTiming
AntisymmetricMatrixQ[mat1]

Unfortunately, although I used ParallelTable, it seems it requires a considerable time when the size of the matrix is high, e.g., when $n=25$ and the size would be $15625$!
So, I would be thankful if someone could provide a couple of comments in order to accelerate such a process.

A:

We can speed this up a lot by using that radial <= epsilon means only a small fraction of all those matrix elements have to be considered. Here's how I would do that:
computeMatrix[grid_, xgrid2_, size_] := Module[{dist, diff, eliminate, pos, values, gr = ConstantArray[xgrid2, size]},
  dist = DistanceMatrix[grid, DistanceFunction -> EuclideanDistance];
  diff = gr - Transpose[gr];
  eliminate = UnitStep[ConstantArray[epsilon, {size, size}] - dist];
  pos = SparseArray[eliminate]["NonzeroPositions"];
  dist = Extract[dist, pos];
  diff = Extract[diff, pos];
  values = ((56 Clip[cl - dist, {0, \[Infinity]}]^5 (cl + 5 dist) diff)/cl^8);
  SparseArray[pos -> values]
]

mat1 == computeMatrix[grid, xgrid2, size]
(* True *)

This evaluates in approximately 0.1 seconds on my computer, more than 100 times faster than the original solution.
Marius provided the vectorized function in the second to last line in a comment below. (If there is interest, one can see this solution without with a non-vectorized function in the edit history of this post.)
Note: This is a very memory hungry function for large $n$. A typical symptom for lack of memory is that the operating system is sluggish or non-responsive while the computation is ongoing. I might update this later with an improvement directed at memory efficiency, but if memory is a problem is might also be a good idea to ask a question about how this can be optimized with regards to memory. It also appears that the speed up with the vectorized approach as compared to my previous approach is lost when the computation is memory constrained.

