disordered-photonics / celes

CELES: CUDA-accelerated electromagnetic scattering by large ensembles of spheres
Other
48 stars 18 forks source link

Non-CUDA alternatives #16

Open AmosEgel opened 6 years ago

AmosEgel commented 6 years ago

Right now, the user needs to have a setup with Matlab, CUDA and C++ compiler available to even test our software.

It would be great to have an alternative Matrix-Vector product that runs on the CPU only. This function will probably be slower than the current, but at least allow the user to test the software.

This could be achieved either by a Matlab script coupling_matrix_multiply_CPU.m or by a c++ file that does what coupling_matrix_multiply_CUDA.cu does (and which is compiled by mex).

arunoruto commented 2 years ago

I am currently on it to implement a pure C++ variant of the translationMatrixProduct function in coupling_matrix_multiply_CUDA.cu. I wanted to precompute the associated legendre and hankel function values and pass them to the C++ function. But the assocLegendreFunction C++ function defined in coupling_matrix_multiply_CUDA.cu gives different results than the built in function of matlab and python. It looks similar to this expression, but the results still differ to just simply calling legendre(n, cos(theta)) (matlab) or lpmv(m, l, cos(theta)) (python). I could use your provided function, but I thought maybe it would be simpler to use the already provided matlab and python functions since they are also highly optimized.

AmosEgel commented 2 years ago

Hey Mirza, that's good to hear! With regard to the associated Legendre functions: there exist different normalizations. You could try different input to the normalization argument of Matlab's legendre function. If I recall correctly, one of the normalization options is equivalent to what we use with CELES. The strategy that we follow in CELES is based on the fact that P_lm is simply a polynomial expression in sin and cos of the polar angle of the relative position vector between two particles. So we precompute the coefficients of these polynoms and then in the fast CUDA kernel we evaluate sin and cos of the angle and get P_lm my evaluating the polynomial expression with the given (precomputed) polynom coefficients.

arunoruto commented 2 years ago

Hello Amos, I have been tinkering with a non-CUDA variant of the .cu file and came up with this function here. I did not have time to put it in a mex function, but under python `cffi it works nicely and the result matches with the one obtained using Matlab.

Regarding my question about the normalization, what I was missing, was the c_ml coefficient here. But that has also been resolved nicely.

I am currently trying to implement the code in python just so I can make it easier to deploy on any machine, since I am currently using a weird setup utilizing a docker container with a specific CUDA version and a mounted instance of Matlab and X11 forwarding.

To be also transparent about the project, I have also mentioned you in the credits and your paper to be cited ;) If someone is motivated to contribute to this implementation, you will be very welcomed. I am planning only to convert the parts I currently need for my own needs, which would be the incidence and scattering coefficients. Everything beyond it isn't in my interest sadly. But I will make my extensions regarding the computation of cross-sections and similar values available here and possible create pull request to your Matlab implementation down the road.

AmosEgel commented 2 years ago

Hi Mirza, wow that is great news. I know that @lpattelli was also thinking about a Python version of CELES. Great that you have done that now.

I am planning only to convert the parts I currently need for my own needs, which would be the incidence and scattering coefficients. Everything beyond it isn't in my interest sadly.

Fair enough. If people need other functionality, they can still fall back to the Matlab version or port the functions to Python themselves.