-
Notifications
You must be signed in to change notification settings - Fork 73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cython functions for detsim #756
Conversation
I have rebased on accepted #757 PR since they share some common functions and test files |
Are there any benchmarks that justify the use of Cython? |
The problem is that there are no reasonable alternatives that solve the exact problem : having a matrix of
|
Is there somewhere I can read a high-level description of the computational task? |
Put another way: can you describe
|
I am afraid not, but I tried to prepare a document that explains the process. If there are unclear parts please ask. |
My current understanding is something like this: Input
Output
(You also mention applying a Poisson distribution, but it seems that it is not CalculationVery roughly, for SiPMs
For PMTs it's almost the same, just that the z-distribution is collapsed to a DiscussionIn your earlier comment you allude to memory being a problem because there are I assume (trust?, hope!) that the detector spits out results in chronological You also mentioned arbitrary binning ✕ number of sensors in the context of My original question was about benchmarks which justify the use of Cython. That Fundamentally, you have to show that your Cython solution is significantly more [*] Or some other compelling advantage. For example, it might be reasonable to Cython offers very little advantage over Python, beyond speed of execution, but Questions
|
Ah, ok, I think I was not clear enough. Looping over numpy is guaranteed to be slower than cython loop over numpy array but using fast (c-implemented) numpy operations on vectors is usually not. Hence the main goal would be to solve the problem using numpy operations on arrays directly, as was our first approach. This required putting number_of_electrons x number_of_partitions x number_of_sensors in memory and doing multiplications and histogrammings, and this is memory consuming (20+ Gb). Second approach was to do a loop over electrons and use numpy vector manipulations inside the first loop. This was order of 10 minutes . Doing all three loops explicitely is slower than that. Then we tried to use cython. I tried several other approaches, the most promising one was to first group electrons in voxels determined by the psf file (ie distance and EL time partitions), and then loop over sensors and do the calculations using numpy fast bincount. This was around 1.2 minutes per event. The conclusion: we didnt manage to think of any pure python approach that can remotely come close to cython speed without the excessive memory usage. |
My take: While premature optimisation may be the root of evil, this is a case where a number of other alternatives have been tried and discarded, cython being the most effective solution and an accepted tool for IC. So I think it is OK, for now.
Alternatives to write fast code based on rust could be attractive, and worth to explore when/if relevant.
… On 15 Dec 2020, at 20:47, mmkekic ***@***.***> wrote:
Ah, ok, I think I was not clear enough.
Looping over numpy is guaranteed to be slower than cython loop over numpy array but using fast (c-implemented) numpy operations on vectors is usually not. Hence the main goal would be to solve the problem using numpy operations on arrays directly, as was our first approach. This required putting number_of_electrons x number_of_partitions x number_of_sensors in memory and doing multiplications and histogrammings, and this is memory consuming (20+ Gb).
The actual input/output array sizes are not a problem for now (I agree that having all this array in memory is not optimal and could be a problem in the future, but this would need to be fixed in other parts of IC as well, for example irene/diomira where the full array is read and manipulated).
Second approach was to do a loop over electrons and use numpy vector manipulations inside the first loop. This was order of 10 minutes . Doing all three loops explicitely is slower than that.
Then we tried to use cython.
I tried several other approaches, the most promising one was to first group electrons in voxels determined by the psf file (ie distance and EL time partitions), and then loop over sensors and do the calculations using numpy fast bincount. This was around 1.2 minutes per event.
The conclusion: we didnt manage to think of any pure python approach that can remotely come close to cython speed without the excessive memory usage.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub <#756 (comment)>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/AB5SID23AXX2DUAKP7FMJILSU64M5ANCNFSM4T2KXU2Q>.
|
By all means accept this PR, and get on with your lives. This is the best thing However, as you know, I take the long-term view: Investing in some careful In general, one shouldn't use Cython unless it solves a problem that cannot Some technical details:
(As for Rust, it offers many, many advantages which Cython does not, but it |
Are there data somewhere in the IC repo that can be fed in to these functions in order to make a benchmark? |
No, the data are too large. In abovementioned repository https://github.com/mmkekic/detsim_checks, in README there is a dropbox link to download the data that should be run through the full city, as well as a full light tables. The branch that has a working version of a full city is https://github.com/mmkekic/IC/tree/detsim_unification, hence to make it work you would need to checkout the branch, compile cython and run standard The other option is to give random x, y, time, number_of_photons arrays (of a size ~110k) but this might not describe the reality well if you plan to try to use some grouping or voxelization. |
Why don't you add
to the IC The first can be placed in an arbitrary convenient subdirectory of IC, the As it stands, with everything scattered all over the place (across different (Hard-wired absolute paths to one developer's local filesystem, don't help |
I added the files to the testfiles folder and detsim_benchmark folder has the corresponding config and run_detsim.py script, hence you need to checkout the branch ( I don't think it is a good practice to add unnecesarry files to our LFS server but I believe in this case is justified :) ) |
Thanks. This should make it much easier to contribute. I have some comments up-front:
It is incorrect to classify these files as unnecessary. It makes about as much Consequently, the commit title
is wrong. This commit must be merged into master (if the code that it helps to |
I agree partially, but the problem (as usual) is that we are doing development without a clear idea of what future flexibility we need. For example, the light tables format and dependencies can change, hence the light tables reading should be flexible (actually the format of the table did change during the development so we would need to commit them 2x if we uploaded the files to the server from the start). The event type can change, there could be events with more hits (ie electrons) so whatever implementation should be at most linear with the number of electrons (both in time and memory consumption). |
I'm confused: Should I be able to find the function called |
OK, so it's approximately |
What is the relationship between the It's really annoying to be faced with two versions of the code which are similar-but-different. To review this PR it must be possible to run benchmarks for it. Having to switch to another branch and run a different version of the code in order to run the benchmarks, is not really an reasonable way of working! |
I agree! @gonzaponte was against the idea of putting a full city in one PR so we divided it in a bunch of small ones. However they are not independent and were developing simultaniously so we ended up with a spaghetti code with merge conflicts. If it will make things easier I (or @gondiaz since he is supposed to open a final city body PR once all the rests are approved) can do (yet another!) branch that should resemble more a final version... |
Originally the work was divided into different tasks that were (in principle) independent of each other. I am not really sure how we got here, but I guess there were not so independent after all. My involvement has been discontinuous and perhaps counterproductive since I couldn't really follow its evolution. |
@jacg , I have rebased |
I think this is in much better shape now, but I'd suggest some commit squashing before merging. |
Commits are squashed and the branch is rebased on #757 |
I approve this PR. |
#757 [author: gondiaz] This PR contains the detsim functions that simulate the S1 signal from the total number of photons using the S1 LightTable. It is a single module `simulate_S1.py` and its corresponding test module. Also, the S1 LightTable test file is added. This PR, along with PR #756, overrides PRs #698, #699 and #734. [reviewer: mmkekic] This PR adds functionalities for simulation of the S1 scintillation light needed for detsim city. The code is clear and tested, good job!
All light tables classes should inherit from this one
Cryptic lines have explanatory comments
The position of z inside EL gap is hardcoded to the middle of EL. Cryptic lines have explanatory comments Private __extend_lt_bounds method extends light tables to active_radius size
Tests added: test for LT_SiPM check el_gap and active_r set properly test for LT_SIPM get values test for LT_PMT get_values test for LT_PMT values with extended radius test get_values return 0s for non existing sensors
The function loops over ionization electrons and creates waveforms of expected values (the output should be poissonised for real waveforms) Cryptic parts of the code have explanatory comments
The spreading is to be used in case of PMTs light simulation since the photon distribution of one electron is assumed to be uniform across EL gap.
Tests are: test for correct output shape test for tmin parameter tests for waveforms integrated signal test for time distribution signal on pmts test for time distribution signal on sipms
This slows down a code around 10-20%
#769 [author: gondiaz] This is the last of the series of PRs #757 #756 #695 #694. It contains detsim city flow module that performs a fast detector simulation. From MC hits, it simulates inonization electron creation, drift and diffusion. Then it simulates the photon propagation based on previously computed light-tables, which depend on detector geometry. The final output are the signal waveforms at sensors (without electronic simulation). [reviewer: mmkekic] This PR is the last piece of the long lasting effort to implement fast detector light simulation in IC. The PR introduces the final city flow that is using already approved functionalities. Code is readable and appropriate tests are added. Good job!
This PR adds cython classes for light tables on sipms and pmts for S2 signal, and a loop over ionization electrons producing a waveform-shaped matrix of expected photon values.