Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:48:49

0001 #!/usr/bin/env python
0002 #
0003 # Copyright (c) 2019 Opticks Team. All Rights Reserved.
0004 #
0005 # This file is part of Opticks
0006 # (see https://bitbucket.org/simoncblyth/opticks).
0007 #
0008 # Licensed under the Apache License, Version 2.0 (the "License"); 
0009 # you may not use this file except in compliance with the License.  
0010 # You may obtain a copy of the License at
0011 #
0012 #   http://www.apache.org/licenses/LICENSE-2.0
0013 #
0014 # Unless required by applicable law or agreed to in writing, software 
0015 # distributed under the License is distributed on an "AS IS" BASIS, 
0016 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
0017 # See the License for the specific language governing permissions and 
0018 # limitations under the License.
0019 #
0020 
0021 """
0022 profilesmryplot.py
0023 ====================
0024 
0025 ::
0026 
0027     an ; ip profilesmryplot.py
0028 
0029     an ; ip profilesmryplot.py --pfx "scan-ph" --gpu "TITAN V" --cvd 1 
0030     an ; ip profilesmryplot.py --pfx "scan-ph-0" --gpu Quadro_RTX_8000 --cvd 0 
0031     an ; ip profilesmryplot.py --pfx "scan-ph-1" --gpu Quadro_RTX_8000 --cvd 0 
0032     an ; ip profilesmryplot.py --pfx "scan-ph-2" --gpu Quadro_RTX_8000 --cvd 0 
0033     an ; ip profilesmryplot.py --pfx "scan-ph-3" --gpu Quadro_RTX_8000 --cvd 0 
0034     an ; ip profilesmryplot.py --pfx "scan-ph-4" --gpu TITAN_RTX --cvd 1 
0035     an ; ip profilesmryplot.py --pfx "scan-ph-5" --gpu TITAN_RTX --cvd 1 
0036 
0037     an ; ip profilesmryplot.py --pfx "scan-ph-7" --gpu TITAN_RTX --cvd 1 
0038     an ; ip profilesmryplot.py --pfx "scan-ph-8" --gpu TITAN_RTX --cvd 1 
0039     an ; ip profilesmryplot.py --pfx "scan-ph-9" --gpu TITAN_RTX --cvd 1 
0040     an ; ip profilesmryplot.py --pfx "scan-ph-10" --gpu TITAN_RTX --cvd 1 
0041 
0042 """
0043 
0044 from __future__ import print_function
0045 import os, sys, logging, numpy as np, argparse
0046 from collections import OrderedDict as odict
0047 log = logging.getLogger(__name__)
0048 
0049 from opticks.ana.num import Num
0050 from opticks.ana.base import findfile
0051 from opticks.ana.profile_ import Profile
0052 from opticks.ana.profilesmry import ProfileSmry, ProfileMain
0053 from opticks.ana.profilesmrytab import ProfileSmryTab
0054 
0055 
0056 class O(object):
0057     """
0058 
0059     """
0060     ID = "scan-ph (profilesmryplot.py)"
0061     TT = odict()
0062     SLI = slice(0,None)
0063 
0064     @classmethod
0065     def Get(cls, key):
0066         try:
0067             ikey = int(key)
0068             o = None if ikey < 0 else cls.TT.values()[ikey]
0069         except ValueError:
0070             o = cls.TT.get(key)
0071         pass
0072         return o 
0073 
0074     def __repr__(self):
0075         return " key %s ratio %s cfg4 %s " % (self.key, self.ratio, self.cfg4 ) 
0076 
0077     @classmethod
0078     def XLabel(cls, sli):
0079         return "Number of Photons (Millions)"
0080 
0081     def _get_xlabel(self):
0082         return self.XLabel(self.sli)
0083     xlabel = property(_get_xlabel)
0084 
0085 
0086     def __init__(self, key, desc):
0087         self.TT[key] = self
0088         self.key = key
0089         self.desc = desc
0090         self.title = "%s : %s " % ( key, desc )
0091         self.ratio = desc.find("Ratio") > -1  
0092         self.cfg4 = desc.find("G4") > -1 
0093         self.sli = self.SLI
0094 
0095         #self.fontsize = 20
0096         #self.figsize = 18,10.2  # png 1800,1019 pixels fontsize 20 looks good 
0097         self.fontsize = 15  
0098         self.figsize = 12.8,7.20   # png of 1280,720   fontsize 20 too big 
0099 
0100         self.ylabel = "Times (s)" if not self.ratio else "Ratio"
0101 
0102         self.ylim = None
0103         self.xlog = False
0104         self.ylog = False
0105         self.loc = "upper left"
0106 
0107         if self.ratio:
0108             if self.cfg4:
0109                 if self.key == "Opticks_Speedup":
0110                     self.ylim = [0, 2800]  
0111                     self.rr = "19l 19i 09l 09i"
0112                     self.ylog = False
0113                     #self.loc = [0.4, 0.45 ] 
0114                     self.loc = "upper right" 
0115                 else:
0116                     assert 0, self.key 
0117                 pass
0118             else:  
0119                 if self.key == "RTX_Speedup":
0120                     self.rr = "10" 
0121                     self.ylim = [0,10]          
0122                 elif self.key == "Interval_over_Launch":
0123                     self.ylim = [0, 1.5]  
0124                     self.rr = "00 11" 
0125                     self.loc = "upper right"   
0126                 else:
0127                     assert 0, self.key 
0128                 pass
0129             pass
0130         else:
0131             if self.cfg4:
0132                self.ii = [9,0,1]
0133             else:   
0134                self.ii = [0,1]
0135             pass
0136 
0137             if self.key == "Overheads":
0138                 self.loc = "upper left"   
0139             elif self.key == "NHit":
0140                 self.ii = [1]
0141                 self.sli = slice(None)
0142                 self.loc = "upper left"   
0143                 self.ylabel = "Number of Hits " 
0144             elif self.key == "Opticks_vs_Geant4":
0145                 self.ylog = True
0146                 #self.loc = [0.6, 0.5 ] 
0147                 self.loc = "lower right"
0148             else:
0149                 pass
0150             pass
0151 
0152         pass
0153 
0154 
0155 def make_fig( plt, o, ps, rs, idlabel=None ):
0156 
0157     sli = o.sli
0158     plt.rcParams['figure.figsize'] = o.figsize    
0159     plt.rcParams['font.size'] = o.fontsize
0160 
0161     plt.rcParams['ytick.right'] = plt.rcParams['ytick.labelright'] = True
0162     plt.rcParams['ytick.left'] = plt.rcParams['ytick.labelleft'] = True
0163 
0164 
0165     fig = plt.figure()
0166     ax = fig.add_subplot(111)
0167     plt.title(o.title, fontsize=o.fontsize)
0168 
0169     pscale = 1e6 
0170 
0171     if not o.ratio:
0172         for i in o.ii:
0173             p = ps[i]
0174             g4 = p.label[:2] == "G4"
0175             if not g4:
0176                 if i == 1 and o.key == "NHit":
0177                     plt.plot( p.npho[sli]/pscale, p.nhit[sli],   p.fmt, c="r", label="%s NHit" % p.label )
0178                 else:
0179                     plt.plot( p.npho[sli]/pscale, p.ainterval[sli], p.fmt, c="b", label="%s (interval)" % p.label )
0180                     plt.plot( p.npho[sli]/pscale, p.alaunch[sli],   p.fmt, c="r", label="%s (launch)" % p.label )
0181                 pass
0182             else:
0183                 plt.plot( p.npho[sli]/pscale, p.ainterval[sli], p.fmt, c="g", label="%s" % p.label )
0184             pass
0185         pass
0186     pass
0187 
0188     if o.ratio: 
0189         for j in o.rr.split():
0190             r = rs[j]
0191             print(j)
0192             plt.plot( r.npho[sli]/pscale,  r.ratio[sli],  r.fmt, label=r.label  ) 
0193         pass
0194     pass
0195 
0196     if not o.ylabel is None:
0197         plt.ylabel(o.ylabel, fontsize=o.fontsize)
0198     pass
0199     if not o.xlabel is None:
0200         plt.xlabel(o.xlabel, fontsize=o.fontsize)
0201     pass
0202     if o.ylog:
0203         ax.set_yscale('log')
0204     pass
0205     if o.xlog:
0206         ax.set_xscale('log')
0207     pass
0208     if not o.ylim is None:
0209         ax.set_ylim(o.ylim)
0210     pass
0211     ax.legend(loc=o.loc, fontsize=o.fontsize, shadow=True)
0212 
0213 
0214     #ax.yaxis.tick_right()
0215     #ax.yaxis.set_ticks_position('both')
0216 
0217     if idlabel is None:
0218         pass
0219     else:
0220         plt.text( 1.0, -0.07, idlabel , transform=ax.transAxes, verticalalignment='top', horizontalalignment='right', fontsize=7 )
0221     pass
0222     return fig 
0223 
0224 def figpath(key, scanid):
0225     elem = filter(None,["$TMP/ana", scanid, "%s.png" % key ])
0226     path = os.path.expandvars(os.path.join(*elem)) 
0227     dir_ = os.path.dirname(path)
0228     if not os.path.exists(dir_):
0229         log.info("creating dir %s " % dir_)
0230         os.makedirs(dir_)   
0231     pass
0232     return path
0233 
0234 
0235 
0236 
0237 if __name__ == '__main__':
0238     logging.basicConfig(level=logging.INFO)
0239     np.set_printoptions(precision=4, linewidth=200)
0240 
0241     pm = ProfileMain.ParseArgs(__doc__) 
0242 
0243     # plot specifications 
0244 
0245     O("Opticks_vs_Geant4",  "Extrapolated G4 times compared to Opticks launch+interval times with RTX mode ON and OFF")  
0246     O("Opticks_Speedup",    "Ratio of extrapolated G4 times to Opticks launch(interval) times")  
0247     O("Overheads",   "Comparison of Opticks GPU launch times and intervals with RTX mode ON and OFF")  
0248     O("RTX_Speedup", "Ratio of launch times with RTX mode OFF to ON ")  
0249     O("Interval_over_Launch",   "Ratio of Opticks GPU interval/launch times with RTX mode ON and OFF").comment = "RTX ON overheads are worse because launch times reduced"  
0250     O("NHit",        "Number of Hits vs Number of Photons")
0251 
0252     o = O.Get(-1)  # None for -1, corresponding to all plots OR a single plot specification selected by index
0253 
0254     pfx = pm.pfx0
0255     cvd = pm.get_cvd(pfx) 
0256 
0257     cat0 = "cvd_%s_rtx_0" % cvd 
0258     cat1 = "cvd_%s_rtx_1" % cvd
0259 
0260     pfxs = [pfx,]
0261     cats = [cat0, cat1]
0262     pftab = ProfileSmryTab.FromCrossList(pfxs, cats)
0263 
0264     g4_seconds_1M = pm.g4_seconds_1M 
0265     pftab.addG4Extrapolation(g4_seconds_1M=g4_seconds_1M)
0266 
0267     print(repr(pftab)) 
0268 
0269     ps = pftab.ps
0270 
0271     # RTX OFF
0272     ps[0].fmt = "o:"  
0273     ps[0].label = ps[0].autolabel
0274 
0275     # RTX ON 
0276     ps[1].fmt = "o-"
0277     ps[1].label = ps[1].autolabel
0278 
0279     assert ps[0].gpu == ps[1].gpu, (ps[0].gpu, ps[1].gpu) 
0280     gpu = ps[0].gpu 
0281 
0282     ps[9].fmt = "o--" 
0283     ps[9].label = "G4 Extrapolated (from %4.1f s for 1M) " % g4_seconds_1M
0284 
0285 
0286     import matplotlib.pyplot as plt
0287     plt.ion()
0288 
0289     oo = O.TT.values() if o is None else [o] 
0290     for o in oo:
0291 
0292         print(o)
0293   
0294         rs = {}
0295         if o.ratio:
0296             if o.cfg4: 
0297                 rs["09l"] = ProfileSmry.FromAB( ps[0], ps[9], att="alaunch" )
0298                 rs["09l"].fmt = "ro--"
0299                 rs["09l"].label = "G4 Extrapolated / %s (launch)" % ps[0].label
0300 
0301                 rs["09i"] = ProfileSmry.FromAB( ps[0], ps[9], att="ainterval" )
0302                 rs["09i"].fmt = "bo--"
0303                 rs["09i"].label = "G4 Extrapolated / %s (interval)" % ps[0].label
0304 
0305                 rs["19l"] = ProfileSmry.FromAB( ps[1], ps[9], att="alaunch")
0306                 rs["19l"].fmt = "ro-"
0307                 rs["19l"].label = "G4 Extrapolated / %s (launch)" % ps[1].label
0308 
0309                 rs["19i"] = ProfileSmry.FromAB( ps[1], ps[9], att="ainterval")
0310                 rs["19i"].fmt = "bo-"
0311                 rs["19i"].label = "G4 Extrapolated / %s (interval)" % ps[1].label 
0312 
0313             else:
0314 
0315                 rs["10"] = ProfileSmry.FromAB( ps[1], ps[0], att="alaunch" )
0316                 rs["10"].fmt = "ro--"
0317                 rs["10"].label = "%s, RTX OFF/ON (launch)" % gpu
0318 
0319                 rs["00"] = ProfileSmry.FromAtt( ps[0], num_att="ainterval", den_att="alaunch" )
0320                 rs["00"].fmt = "bo--"
0321                 rs["00"].label = "%s  interval / launch " % ps[0].label
0322 
0323                 rs["11"] = ProfileSmry.FromAtt( ps[1], num_att="ainterval", den_att="alaunch" )
0324                 rs["11"].fmt = "ro--"
0325                 rs["11"].label = "%s interval / launch " % ps[1].label 
0326             pass 
0327         pass
0328 
0329 
0330 
0331         fig = make_fig( plt, o, ps, rs, idlabel=pftab.scanid )
0332         fig.show()
0333 
0334         path = figpath(o.key, pftab.scanid)
0335         log.info("savefig %s " % path ) 
0336         plt.savefig(path)
0337 
0338     pass
0339 
0340