LucioLau / simulation

Python simulation
0 stars 1 forks source link

Changing Codes from Numba to Numba CUDA #1

Open LucioLau opened 1 year ago

LucioLau commented 1 year ago

Hi everyone,

I am recently trying to work on simulating the payments between banks. I have codes written in Numba Python which is here https://github.com/LucioLau/simulation/blob/LucioLau-patch-1/Sim_CUDA.ipynb. These are codes which read the Excel file that contains the simulated payments between banks and carries out the simulation. The simulated data are generated in the other file here https://github.com/LucioLau/simulation/blob/65d3d456076e25537b6933a43034c2a7e4c2e739/Sample_generator_homogeneous_banks.ipynb. The simulation of one sample is fast, however, the iteration is very large. I would like to ask if I can submit it to CUDA. As each iteration looks heavy in space complexity, I don't know if it is doable for a display card.

Any comments on improving the code are welcomed!

Many thanks,

Lucio

P.S. I am new to github, I did want to attach my codes within this space, but I have no clue how to do so.

zafar-hussain commented 1 year ago

Hi @LucioLau ,

If you can, please give a test case for sample_generator(k, N, T, _alpha), with values of k, N, T, _alpha.

Add border cases, like a small data set that finishes quickly, and a medium one, which gives you reasonable execution times, plus a large one, that takes very long, even doesn't finish on your machine

Allowable data types for k, N, T, _alpha, would be very helpful, plus a sample of the Excel file, you mentioned above.

cheers zafar

LucioLau commented 1 year ago

Hi @zafar-hussain ,

The sample generator has no issue itself. It is kind of slow just because S = 10_000.

My problem comes from the simulation function in the first link, where 1 iteration is fast, however, I have to loop it for 51 x 6 x 701 x 71 x 10,000 = 1.52 x 10^11 times. So I am thinking of submitting it to the GPU, but I have no clue if the GPU is capable of handling this. Comments on optimising the code are also welcomed.

Cheers,

Lucio

P.S. The simulation idea comes from the paper Payment prioritisation and liquidity risk in collateralised interbank payment systems by De Caux et al.

zafar-hussain commented 1 year ago

Hi @LucioLau,

Cheers zafar

LucioLau commented 1 year ago

Hi @zafar-hussain,

Thank you for your advice, I have applied your suggestions and uploaded them as https://github.com/LucioLau/simulation/blob/main/Sim_CUDA2.ipynb.

I don't completely understand what you mean in your second last and last point (sorry about that, I don't have a computer science background but a maths one).

Other than the two points above, I guess I have applied what you have suggested and hope they are optimised. Many thanks.

Cheers,

Lucio

zafar-hussain commented 1 year ago

Nicely done @LucioLau,

Hence the more we can reuse data, the less gpu will have to access slow memory (bound by memory bandwidth)

I am free from Tuesday, and will profile your code and help you takecare of above two points.

Please if you can add some tests for the code, so that I don't break anything in your code

cheers

zafar

LucioLau commented 1 year ago

Hi @zafar-hussain,

Thank you for the further explain, I think I understand what you mean now.

For tests, what I usually do is to run simulation on the sample generated by the sample generator with any arrays of liquidity and buffer array with size N, e.g. np.ones(N) for both and compare the result with the previous version.

Many thanks,

Lucio