Changeset a4280bd in sasmodels for sasmodels/generate.py


Ignore:
Timestamp:
Jul 25, 2016 11:54:30 PM (8 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:
2f5c6d4
Parents:
2c74c11
Message:

restructure magnetic models to use less code

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sasmodels/generate.py

    r2c74c11 ra4280bd  
    487487    source.append(code) 
    488488 
    489  
    490489def make_source(model_info): 
    491     # type: (ModelInfo) -> str 
     490    # type: (ModelInfo) -> Dict[str, str] 
    492491    """ 
    493492    Generate the OpenCL/ctypes kernel from the module info. 
     
    575574    # Fill in definitions for numbers of parameters 
    576575    source.append("#define MAX_PD %s"%partable.max_pd) 
    577     source.append("#define NPARS %d"%partable.npars) 
     576    source.append("#define NUM_PARS %d"%partable.npars) 
     577    source.append("#define NUM_VALUES %d" % partable.nvalues) 
    578578    source.append("#define NUM_MAGNETIC %d" % partable.nmagnetic) 
    579     source.append("#define NUM_VALUES %d" % partable.nvalues) 
    580579    source.append("#define MAGNETIC_PARS %s"%",".join(str(k) for k in magpars)) 
     580    for k,v in enumerate(magpars[:3]): 
     581        source.append("#define MAGNETIC_PAR%d %d"%(k+1, v)) 
    581582 
    582583    # TODO: allow mixed python/opencl kernels? 
    583584 
    584     source.append("#if defined(USE_OPENCL)") 
    585     source.extend(_add_kernels(ocl_code, call_iq, call_iqxy, model_info.name)) 
    586     source.append("#else /* !USE_OPENCL */") 
    587     source.extend(_add_kernels(dll_code, call_iq, call_iqxy, model_info.name)) 
    588     source.append("#endif /* !USE_OPENCL */") 
    589     return '\n'.join(source) 
    590  
    591  
    592 def _add_kernels(kernel, call_iq, call_iqxy, name): 
     585    ocl = kernels(ocl_code, call_iq, call_iqxy, model_info.name) 
     586    dll = kernels(dll_code, call_iq, call_iqxy, model_info.name) 
     587    result = { 
     588        'dll': '\n'.join(source+dll[0]+dll[1]+dll[2]), 
     589        'opencl': '\n'.join(source+ocl[0]+ocl[1]+ocl[2]), 
     590    } 
     591 
     592    return result 
     593 
     594 
     595def kernels(kernel, call_iq, call_iqxy, name): 
    593596    # type: ([str,str], str, str, str) -> List[str] 
    594597    code = kernel[0] 
     
    598601        "#define KERNEL_NAME %s_Iq" % name, 
    599602        call_iq, 
    600         '#line 1 "%s-Iq"' % path, 
     603        '#line 1 "%s Iq"' % path, 
    601604        code, 
    602605        "#undef CALL_IQ", 
     
    608611        "#define KERNEL_NAME %s_Iqxy" % name, 
    609612        call_iqxy, 
    610         '#line 1 "%s-Iqxy"' % path, 
     613        '#line 1 "%s Iqxy"' % path, 
    611614        code, 
    612615        "#undef CALL_IQ", 
     
    619622        "#define MAGNETIC 1", 
    620623        call_iqxy, 
    621         '#line 1 "%s-Imagnetic"' % path, 
     624        '#line 1 "%s Imagnetic"' % path, 
    622625        code, 
    623626        "#undef MAGNETIC", 
     
    625628        "#undef KERNEL_NAME", 
    626629    ] 
    627     return iq+iqxy+imagnetic 
     630 
     631    return iq, iqxy, imagnetic 
    628632 
    629633 
     
    739743        model_info = make_model_info(kernel_module) 
    740744        source = make_source(model_info) 
    741         print(source) 
     745        print(source['dll']) 
    742746 
    743747 
Note: See TracChangeset for help on using the changeset viewer.