Changeset 5d316e9 in sasmodels for sasmodels/generate.py


Ignore:
Timestamp:
Dec 8, 2015 6:08:51 AM (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:
cf404cb
Parents:
eaca9eb
Message:

support fast and loose single precision and half precision

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sasmodels/generate.py

    r9404dd3 r5d316e9  
    197197# TODO: identify model files which have changed since loading and reload them. 
    198198 
    199 __all__ = ["make", "doc", "sources", "use_single", "use_long_double"] 
     199__all__ = ["make", "doc", "sources", "convert_type"] 
    200200 
    201201import sys 
     
    208208C_KERNEL_TEMPLATE_PATH = joinpath(dirname(__file__), 'kernel_template.c') 
    209209 
     210F16 = np.dtype('float16') 
    210211F32 = np.dtype('float32') 
    211212F64 = np.dtype('float64') 
     
    316317    return [_search(search_path, f) for f in info['source']] 
    317318 
    318 def use_single(source): 
    319     """ 
    320     Convert code from double precision to single precision. 
    321     """ 
    322     # Convert double keyword to float.  Accept an 'n' parameter for vector 
    323     # values, where n is 2, 4, 8 or 16. Assume complex numbers are represented 
    324     # as cdouble which is typedef'd to double2. 
     319# Pragmas for enable OpenCL features.  Be sure to protect them so that they 
     320# still compile even if OpenCL is not present. 
     321_F16_PRAGMA = """\ 
     322#ifdef cl_khr_fp16 
     323#  pragma OPENCL EXTENSION cl_khr_fp16: enable 
     324#endif 
     325""" 
     326 
     327_F64_PRAGMA = """\ 
     328#ifdef cl_khr_fp64 
     329#  pragma OPENCL EXTENSION cl_khr_fp64: enable 
     330#endif 
     331""" 
     332 
     333def convert_type(source, dtype): 
     334    """ 
     335    Convert code from double precision to the desired type. 
     336    """ 
     337    if dtype == F16: 
     338        source = _F16_PRAGMA + _convert_type(source, "half", "f") 
     339    elif dtype == F32: 
     340        source = _convert_type(source, "float", "f") 
     341    elif dtype == F64: 
     342        source = _F64_PRAGMA + source  # Source is already double 
     343    elif dtype == F128: 
     344        source = _convert_type(source, "long double", "L") 
     345    else: 
     346        raise ValueError("Unexpected dtype in source conversion: %s"%dtype) 
     347    return source 
     348 
     349 
     350def _convert_type(source, type_name, constant_flag): 
     351    # Convert double keyword to float/long double/half. 
     352    # Accept an 'n' # parameter for vector # values, where n is 2, 4, 8 or 16. 
     353    # Assume complex numbers are represented as cdouble which is typedef'd 
     354    # to double2. 
    325355    source = re.sub(r'(^|[^a-zA-Z0-9_]c?)double(([248]|16)?($|[^a-zA-Z0-9_]))', 
    326                     r'\1float\2', source) 
    327     # Convert floating point constants to single by adding 'f' to the end. 
    328     # OS/X driver complains if you don't do this. 
     356                    r'\1%s\2'%type_name, source) 
     357    # Convert floating point constants to single by adding 'f' to the end, 
     358    # or long double with an 'L' suffix.  OS/X complains if you don't do this. 
    329359    source = re.sub(r'[^a-zA-Z_](\d*[.]\d+|\d+[.]\d*)([eE][+-]?\d+)?', 
    330                     r'\g<0>f', source) 
    331     return source 
    332  
    333 def use_long_double(source): 
    334     """ 
    335     Convert code from double precision to long double precision. 
    336     """ 
    337     # Convert double keyword to float.  Accept an 'n' parameter for vector 
    338     # values, where n is 2, 4, 8 or 16. Assume complex numbers are represented 
    339     # as cdouble which is typedef'd to double2. 
    340     source = re.sub(r'(^|[^a-zA-Z0-9_]c?)double(([248]|16)?($|[^a-zA-Z0-9_]))', 
    341                     r'\1long double\2', source) 
    342     # Convert floating point constants to single by adding 'f' to the end. 
    343     # OS/X driver complains if you don't do this. 
    344     source = re.sub(r'[^a-zA-Z_](\d*[.]\d+|\d+[.]\d*)([eE][+-]?\d+)?', 
    345                     r'\g<0>L', source) 
     360                    r'\g<0>%s'%constant_flag, source) 
    346361    return source 
    347362 
Note: See TracChangeset for help on using the changeset viewer.