Changeset 74e9b5f in sasmodels for sasmodels/kernelcuda.py


Ignore:
Timestamp:
Oct 12, 2018 10:52:48 PM (6 years ago)
Author:
pkienzle
Branches:
master, core_shell_microgels, magnetic_model, ticket-1257-vesicle-product, ticket_1156, ticket_1265_superball, ticket_822_more_unit_tests
Children:
4de14584
Parents:
b0de252
Message:

autotag functions as device functions for cuda. Refs #1076.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sasmodels/kernelcuda.py

    rb0de252 r74e9b5f  
    6262import logging 
    6363import time 
     64import re 
    6465 
    6566import numpy as np  # type: ignore 
     
    146147    return dtype in (generate.F32, generate.F64) 
    147148 
     149 
     150FUNCTION_PATTERN = re.compile(r"""^ 
     151  (?P<space>\s*)                   # initial space 
     152  (?P<qualifiers>^(?:\s*\b\w+\b\s*)+) # one or more qualifiers before function 
     153  (?P<function>\s*\b\w+\b\s*[(])      # function name plus open parens 
     154  """, re.VERBOSE|re.MULTILINE) 
     155 
     156MARKED_PATTERN = re.compile(r""" 
     157  \b(return|else|kernel|device|__device__)\b 
     158  """, re.VERBOSE|re.MULTILINE) 
     159 
     160def _add_device_tag(match): 
     161    # type: (None) -> str 
     162    # Note: should be re.Match, but that isn't a simple type 
     163    """ 
     164    replace qualifiers with __device__ qualifiers if needed 
     165    """ 
     166    qualifiers = match.group("qualifiers") 
     167    if MARKED_PATTERN.search(qualifiers): 
     168        start, end = match.span() 
     169        return match.string[start:end] 
     170    else: 
     171        function = match.group("function") 
     172        space = match.group("space") 
     173        return "".join((space, "__device__ ", qualifiers, function)) 
     174 
     175def mark_device_functions(source): 
     176    # type: (str) -> str 
     177    """ 
     178    Mark all function declarations as __device__ functions (except kernel). 
     179    """ 
     180    return FUNCTION_PATTERN.sub(_add_device_tag, source) 
     181 
     182def show_device_functions(source): 
     183    # type: (str) -> str 
     184    """ 
     185    Show all discovered function declarations, but don't change any. 
     186    """ 
     187    for match in FUNCTION_PATTERN.finditer(source): 
     188        print(match.group('qualifiers').replace('\n',r'\n'), match.group('function'), '(') 
     189    return source 
     190 
    148191def compile_model(source, dtype, fast=False): 
    149192    # type: (str, np.dtype, bool) -> SourceModule 
     
    163206    source_list.insert(0, "#define USE_SINCOS\n") 
    164207    source = "\n".join(source_list) 
    165     options = '-use_fast_math' if fast else None 
     208    #source = show_device_functions(source) 
     209    source = mark_device_functions(source) 
     210    #with open('/tmp/kernel.cu', 'w') as fd: fd.write(source) 
     211    #print(source) 
     212    #options = ['--verbose', '-E'] 
     213    options = ['--use_fast_math'] if fast else None 
    166214    program = SourceModule(source, no_extern_c=True, options=options) # include_dirs=[...] 
     215 
    167216    #print("done with "+program) 
    168217    return program 
Note: See TracChangeset for help on using the changeset viewer.