Changeset 216a9e1 in sasmodels


Ignore:
Timestamp:
Dec 3, 2014 11:38:49 AM (9 years ago)
Author:
Paul Kienzle <pkienzle@…>
Branches:
master, core_shell_microgels, costrafo411, magnetic_model, release_v0.94, release_v0.95, ticket-1257-vesicle-product, ticket_1156, ticket_1265_superball, ticket_822_more_unit_tests
Children:
8fc26f8
Parents:
31819c5
Message:

batch generate random old/new comparisons across all models

Files:
2 added
2 edited

Legend:

Unmodified
Added
Removed
  • compare.py

    rba69383 r216a9e1  
    7575        return 200*np.random.rand() 
    7676 
     77def randomize_model(name, pars, seed=None): 
     78    if seed is None: 
     79        seed = np.random.randint(1e9) 
     80    np.random.seed(seed) 
     81    # Note: the sort guarantees order of calls to random number generator 
     82    pars = dict((p,randomize(p,v)) for p,v in sorted(pars.items())) 
     83    # The capped cylinder model has a constraint on its parameters 
     84    if name == 'capped_cylinder' and pars['cap_radius'] < pars['radius']: 
     85        pars['radius'],pars['cap_radius'] = pars['cap_radius'],pars['radius'] 
     86    return pars, seed 
     87 
    7788def parlist(pars): 
    7889    return "\n".join("%s: %s"%(p,v) for p,v in sorted(pars.items())) 
     
    8899        if p.endswith("_pd"): pars[p] = 0 
    89100 
     101def eval_sasview(name, pars, data, Nevals=1): 
     102    model = sasview_model(name, **pars) 
     103    toc = tic() 
     104    for _ in range(Nevals): 
     105        if hasattr(data, 'qx_data'): 
     106            value = model.evalDistribution([data.qx_data, data.qy_data]) 
     107        else: 
     108            value = model.evalDistribution(data.x) 
     109    average_time = toc()*1000./Nevals 
     110    return value, average_time 
     111 
     112def eval_opencl(name, pars, data, dtype='single', Nevals=1, cutoff=0): 
     113    try: 
     114        model = load_opencl(name, dtype=dtype) 
     115    except Exception,exc: 
     116        print exc 
     117        print "... trying again with single precision" 
     118        model = load_opencl(name, dtype='single') 
     119    problem = BumpsModel(data, model, cutoff=cutoff, **pars) 
     120    toc = tic() 
     121    for _ in range(Nevals): 
     122        #pars['scale'] = np.random.rand() 
     123        problem.update() 
     124        value = problem.theory() 
     125    average_time = toc()*1000./Nevals 
     126    return value, average_time 
     127 
     128def eval_ctypes(name, pars, data, dtype='double', Nevals=1, cutoff=0): 
     129    model = load_ctypes(name, dtype=dtype) 
     130    problem = BumpsModel(data, model, cutoff=cutoff, **pars) 
     131    toc = tic() 
     132    for _ in range(Nevals): 
     133        problem.update() 
     134        value = problem.theory() 
     135    average_time = toc()*1000./Nevals 
     136    return value, average_time 
     137 
     138def make_data(qmax, is2D, Nq=128): 
     139    if is2D: 
     140        from sasmodels.bumps_model import empty_data2D, set_beam_stop 
     141        data = empty_data2D(np.linspace(-qmax, qmax, Nq)) 
     142        set_beam_stop(data, 0.004) 
     143        index = ~data.mask 
     144    else: 
     145        from sasmodels.bumps_model import empty_data1D 
     146        qmax = math.log10(qmax) 
     147        data = empty_data1D(np.logspace(qmax-3, qmax, Nq)) 
     148        index = slice(None, None) 
     149    return data, index 
     150 
    90151def compare(name, pars, Ncpu, Ngpu, opts, set_pars): 
     152    opt_values = dict(split 
     153                      for s in opts for split in ((s.split('='),)) 
     154                      if len(split) == 2) 
    91155    # Sort out data 
    92156    qmax = 1.0 if '-highq' in opts else (0.2 if '-midq' in opts else 0.05) 
    93     if "-1d" in opts: 
    94         from sasmodels.bumps_model import empty_data1D 
    95         qmax = math.log10(qmax) 
    96         data = empty_data1D(np.logspace(qmax-3, qmax, 128)) 
    97         index = slice(None, None) 
    98     else: 
    99         from sasmodels.bumps_model import empty_data2D, set_beam_stop 
    100         data = empty_data2D(np.linspace(-qmax, qmax, 128)) 
    101         set_beam_stop(data, 0.004) 
    102         index = ~data.mask 
    103     is2D = hasattr(data, 'qx_data') 
     157    Nq = int(opt_values.get('-Nq', '128')) 
     158    is2D = not "-1d" in opts 
     159    data, index = make_data(qmax, is2D, Nq) 
     160 
    104161 
    105162    # modelling accuracy is determined by dtype and cutoff 
    106163    dtype = 'double' if '-double' in opts else 'single' 
    107     cutoff_opts = [s for s in opts if s.startswith('-cutoff')] 
    108     cutoff = float(cutoff_opts[0].split('=')[1]) if cutoff_opts else 1e-5 
     164    cutoff = float(opt_values.get('-cutoff','1e-5')) 
    109165 
    110166    # randomize parameters 
    111     random_opts = [s for s in opts if s.startswith('-random')] 
    112     if random_opts: 
    113         if '=' in random_opts[0]: 
    114             seed = int(random_opts[0].split('=')[1]) 
    115         else: 
    116             seed = int(np.random.rand()*10000) 
    117         np.random.seed(seed) 
     167    if '-random' in opts or '-random' in opt_values: 
     168        seed = int(opt_values['-random']) if '-random' in opt_values else None 
     169        pars, seed = randomize_model(name, pars, seed=seed) 
    118170        print "Randomize using -random=%i"%seed 
    119         # Note: the sort guarantees order of calls to random number generator 
    120         pars = dict((p,randomize(p,v)) for p,v in sorted(pars.items())) 
    121         # The capped cylinder model has a constraint on its parameters 
    122         if name == 'capped_cylinder' and pars['cap_radius'] < pars['radius']: 
    123             pars['radius'],pars['cap_radius'] = pars['cap_radius'],pars['radius'] 
    124171    pars.update(set_pars) 
    125172 
     
    132179    # OpenCl calculation 
    133180    if Ngpu > 0: 
    134         try: 
    135             model = load_opencl(name, dtype=dtype) 
    136         except Exception,exc: 
    137             print exc 
    138             print "... trying again with single precision" 
    139             model = load_opencl(name, dtype='single') 
    140         problem = BumpsModel(data, model, cutoff=cutoff, **pars) 
    141         toc = tic() 
    142         for _ in range(Ngpu): 
    143             #pars['scale'] = np.random.rand() 
    144             problem.update() 
    145             gpu = problem.theory() 
    146         gpu_time = toc()*1000./Ngpu 
     181        gpu, gpu_time = eval_opencl(name, pars, data, dtype, Ngpu) 
    147182        print "opencl t=%.1f ms, intensity=%.0f"%(gpu_time, sum(gpu[index])) 
    148183        #print max(gpu), min(gpu) 
     
    150185    # ctypes/sasview calculation 
    151186    if Ncpu > 0 and "-ctypes" in opts: 
    152         model = load_ctypes(name, dtype=dtype) 
    153         problem = BumpsModel(data, model, cutoff=cutoff, **pars) 
    154         toc = tic() 
    155         for _ in range(Ncpu): 
    156             problem.update() 
    157             cpu = problem.theory() 
    158         cpu_time = toc()*1000./Ncpu 
     187        cpu, cpu_time = eval_ctypes(name, pars, data, dtype=dtype, cutoff=cutoff, Nevals=Ncpu) 
    159188        comp = "ctypes" 
    160189        print "ctypes t=%.1f ms, intensity=%.0f"%(cpu_time, sum(cpu[index])) 
    161190    elif Ncpu > 0: 
    162         model = sasview_model(name, **pars) 
    163         toc = tic() 
    164         for _ in range(Ncpu): 
    165             if is2D: 
    166                 cpu = model.evalDistribution([data.qx_data, data.qy_data]) 
    167             else: 
    168                 cpu = model.evalDistribution(data.x) 
    169         cpu_time = toc()*1000./Ncpu 
     191        cpu, cpu_time = eval_sasview(name, pars, data, Ncpu) 
    170192        comp = "sasview" 
    171193        print "sasview t=%.1f ms, intensity=%.0f"%(cpu_time, sum(cpu[index])) 
     
    235257    -single*/-double uses double precision for comparison 
    236258    -lowq*/-midq/-highq use q values up to 0.05, 0.2 or 1.0 
    237     -1d/-2d* uses 1d or 2d random data 
     259    -Nq=128 sets the number of Q points in the data set 
     260    -1d/-2d* computes 1d or 2d data 
    238261    -preset*/-random[=seed] preset or random parameters 
    239262    -mono/-poly* force monodisperse/polydisperse 
     
    252275""" 
    253276 
    254 VALID_OPTIONS = [ 
     277NAME_OPTIONS = set([ 
    255278    'plot','noplot', 
    256279    'single','double', 
     
    263286    'rel','abs', 
    264287    'hist','nohist', 
     288    ]) 
     289VALUE_OPTIONS = [ 
     290    # Note: random is both a name option and a value option 
     291    'cutoff', 'random', 'Nq', 
    265292    ] 
    266293 
     
    277304        sys.exit(1) 
    278305 
    279     valid_opts = set(VALID_OPTIONS) 
    280306    invalid = [o[1:] for o in opts 
    281                if o[1:] not in valid_opts 
    282                   and not o.startswith('-cutoff=') 
    283                   and not o.startswith('-random=')] 
     307               if o[1:] not in NAME_OPTIONS 
     308                  and not any(o.startswith('-%s='%t) for t in VALUE_OPTIONS)] 
    284309    if invalid: 
    285310        print "Invalid options: %s"%(", ".join(invalid)) 
     
    328353        radius=260, length=290, 
    329354        theta=30, phi=0, 
    330         radius_pd=.2, radius_pd_n=1, 
    331         length_pd=.2,length_pd_n=1, 
     355        radius_pd=.2, radius_pd_n=9, 
     356        length_pd=.2,length_pd_n=10, 
    332357        theta_pd=15, theta_pd_n=45, 
    333358        phi_pd=15, phi_pd_n=1, 
     
    344369        radius_pd=.2, radius_pd_n=1, 
    345370        cap_radius_pd=.2, cap_radius_pd_n=1, 
    346         length_pd=.2, length_pd_n=1, 
     371        length_pd=.2, length_pd_n=10, 
    347372        theta_pd=15, theta_pd_n=45, 
    348373        phi_pd=15, phi_pd_n=1, 
     
    359384        theta=30, phi=15, 
    360385        radius_pd=.2, radius_pd_n=1, 
    361         length_pd=.2, length_pd_n=1, 
    362         thickness_pd=.2, thickness_pd_n=1, 
     386        length_pd=.2, length_pd_n=10, 
     387        thickness_pd=.2, thickness_pd_n=10, 
    363388        theta_pd=15, theta_pd_n=45, 
    364389        phi_pd=15, phi_pd_n=1, 
     
    374399        rpolar=50, requatorial=30, 
    375400        theta=30, phi=15, 
    376         rpolar_pd=.2, rpolar_pd_n=1, 
    377         requatorial_pd=.2, requatorial_pd_n=1, 
     401        rpolar_pd=.2, rpolar_pd_n=15, 
     402        requatorial_pd=.2, requatorial_pd_n=15, 
    378403        theta_pd=15, theta_pd_n=45, 
    379404        phi_pd=15, phi_pd_n=1, 
     
    391416        req_minor_pd=0, req_minor_pd_n=1, 
    392417        req_major_pd=0, req_major_pd_n=1, 
    393         rpolar_pd=.2, rpolar_pd_n=1, 
     418        rpolar_pd=.2, rpolar_pd_n=30, 
    394419        theta_pd=15, theta_pd_n=45, 
    395420        phi_pd=15, phi_pd_n=1, 
  • sasmodels/dll.py

    r5d4777d r216a9e1  
    1414# Compiler platform details 
    1515if sys.platform == 'darwin': 
    16     COMPILE = "gcc-mp-4.7 -shared -fPIC -std=c99 -fopenmp -O2 -Wall %s -o %s -lm -lgomp" 
     16    #COMPILE = "gcc-mp-4.7 -shared -fPIC -std=c99 -fopenmp -O2 -Wall %s -o %s -lm -lgomp" 
     17    COMPILE = "gcc -shared -fPIC -std=c99 -O2 -Wall %s -o %s -lm" 
    1718elif os.name == 'nt': 
    1819    COMPILE = "gcc -shared -fPIC -std=c99 -fopenmp -O2 -Wall %s -o %s -lm" 
Note: See TracChangeset for help on using the changeset viewer.