<s> """<STR_LIT>""" <EOL> from __future__ import print_function , absolute_import <EOL> import contextlib <EOL> import numpy as np <EOL> from . cudadrv import devicearray , devices , driver <EOL> try : <EOL> long <EOL> except NameError : <EOL> long = int <EOL> require_context = devices . require_context <EOL> current_context = devices . get_context <EOL> gpus = devices . gpus <EOL> @ require_context <EOL> def to_device ( obj , stream = <NUM_LIT:0> , copy = True , to = None ) : <EOL> """<STR_LIT>""" <EOL> if to is None : <EOL> to , new = devicearray . auto_device ( obj , stream = stream , copy = copy ) <EOL> return to <EOL> if copy : <EOL> to . copy_to_device ( obj , stream = stream ) <EOL> return to <EOL> @ require_context <EOL> def device_array ( shape , dtype = np . float , strides = None , order = '<STR_LIT:C>' , stream = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> shape , strides , dtype = _prepare_shape_strides_dtype ( shape , strides , dtype , <EOL> order ) <EOL> return devicearray . DeviceNDArray ( shape = shape , strides = strides , dtype = dtype , <EOL> stream = stream ) <EOL> @ require_context <EOL> def pinned_array ( shape , dtype = np . float , strides = None , order = '<STR_LIT:C>' ) : <EOL> """<STR_LIT>""" <EOL> shape , strides , dtype = _prepare_shape_strides_dtype ( shape , strides , dtype , <EOL> order ) <EOL> bytesize = driver . memory_size_from_info ( shape , strides , <EOL> dtype . itemsize ) <EOL> buffer = current_context ( ) . memhostalloc ( bytesize ) <EOL> return np . ndarray ( shape = shape , strides = strides , dtype = dtype , order = order , <EOL> buffer = buffer ) <EOL> @ require_context <EOL> def mapped_array ( shape , dtype = np . float , strides = None , order = '<STR_LIT:C>' , stream = <NUM_LIT:0> , <EOL> portable = False , wc = False ) : <EOL> """<STR_LIT>""" <EOL> shape , strides , dtype = _prepare_shape_strides_dtype ( shape , strides , dtype , <EOL> order ) <EOL> bytesize = driver . memory_size_from_info ( shape , strides , dtype . itemsize ) <EOL> buffer = current_context ( ) . memhostalloc ( bytesize , mapped = True ) <EOL> npary = np . ndarray ( shape = shape , strides = strides , dtype = dtype , order = order , <EOL> buffer = buffer ) <EOL> mappedview = np . ndarray . view ( npary , type = devicearray . MappedNDArray ) <EOL> mappedview . device_setup ( buffer , stream = stream ) <EOL> return mappedview <EOL> def synchronize ( ) : <EOL> "<STR_LIT>" <EOL> return current_context ( ) . synchronize ( ) <EOL> def _prepare_shape_strides_dtype ( shape , strides , dtype , order ) : <EOL> dtype = np . dtype ( dtype ) <EOL> if isinstance ( shape , ( int , long ) ) : <EOL> shape = ( shape , ) <EOL> if isinstance ( strides , ( int , long ) ) : <EOL> strides = ( strides , ) <EOL> else : <EOL> if shape == ( ) : <EOL> shape = ( <NUM_LIT:1> , ) <EOL> strides = strides or _fill_stride_by_order ( shape , dtype , order ) <EOL> return shape , strides , dtype <EOL> def _fill_stride_by_order ( shape , dtype , order ) : <EOL> nd = len ( shape ) <EOL> strides = [ <NUM_LIT:0> ] * nd <EOL> if order == '<STR_LIT:C>' : <EOL> strides [ - <NUM_LIT:1> ] = dtype . itemsize <EOL> for d in reversed ( range ( nd - <NUM_LIT:1> ) ) : <EOL> strides [ d ] = strides [ d + <NUM_LIT:1> ] * shape [ d + <NUM_LIT:1> ] <EOL> elif order == '<STR_LIT:F>' : <EOL> strides [ <NUM_LIT:0> ] = dtype . itemsize <EOL> for d in range ( <NUM_LIT:1> , nd ) : <EOL> strides [ d ] = strides [ d - <NUM_LIT:1> ] * shape [ d - <NUM_LIT:1> ] <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> return tuple ( strides ) <EOL> def device_array_like ( ary , stream = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> return device_array ( shape = ary . shape , dtype = ary . dtype , <EOL> strides = ary . strides , stream = stream ) <EOL> @ require_context <EOL> def stream ( ) : <EOL> """<STR_LIT>""" <EOL> return current_context ( ) . create_stream ( ) <EOL> @ require_context <EOL> @ contextlib . contextmanager <EOL> def pinned ( * arylist ) : <EOL> """<STR_LIT>""" <EOL> pmlist = [ ] <EOL> for ary in arylist : <EOL> pm = current_context ( ) . mempin ( ary , driver . host_pointer ( ary ) , <EOL> driver . host_memory_size ( ary ) , <EOL> mapped = False ) <EOL> pmlist . append ( pm ) <EOL> yield <EOL> del pmlist <EOL> @ require_context <EOL> @ contextlib . contextmanager <EOL> def mapped ( * arylist , ** kws ) : <EOL> """<STR_LIT>""" <EOL> assert not kws or '<STR_LIT>' in kws , "<STR_LIT>" <EOL> pmlist = [ ] <EOL> stream = kws . get ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> for ary in arylist : <EOL> pm = current_context ( ) . mempin ( ary , driver . host_pointer ( ary ) , <EOL> driver . host_memory_size ( ary ) , <EOL> mapped = True ) <EOL> pmlist . append ( pm ) <EOL> devarylist = [ ] <EOL> for ary , pm in zip ( arylist , pmlist ) : <EOL> devary = devicearray . from_array_like ( ary , gpu_data = pm , stream = stream ) <EOL> devarylist . append ( devary ) <EOL> if len ( devarylist ) == <NUM_LIT:1> : <EOL> yield devarylist [ <NUM_LIT:0> ] <EOL> else : <EOL> yield devarylist <EOL> def event ( timing = True ) : <EOL> """<STR_LIT>""" <EOL> evt = current_context ( ) . create_event ( timing = timing ) <EOL> return evt <EOL> event_elapsed_time = driver . event_elapsed_time <EOL> def select_device ( device_id ) : <EOL> """<STR_LIT>""" <EOL> context = devices . get_context ( device_id ) <EOL> return context . device <EOL> def get_current_device ( ) : <EOL> "<STR_LIT>" <EOL> return current_context ( ) . device <EOL> def list_devices ( ) : <EOL> "<STR_LIT>" <EOL> return devices . gpus <EOL> def close ( ) : <EOL> """<STR_LIT>""" <EOL> devices . reset ( ) <EOL> def _auto_device ( ary , stream = <NUM_LIT:0> , copy = True ) : <EOL> return devicearray . auto_device ( ary , stream = stream , copy = copy ) <EOL> def detect ( ) : <EOL> """<STR_LIT>""" <EOL> devlist = list_devices ( ) <EOL> print ( '<STR_LIT>' % len ( devlist ) ) <EOL> supported_count = <NUM_LIT:0> <EOL> for dev in devlist : <EOL> attrs = [ ] <EOL> cc = dev . compute_capability <EOL> attrs += [ ( '<STR_LIT>' , '<STR_LIT>' % cc ) ] <EOL> attrs += [ ( '<STR_LIT>' , dev . PCI_DEVICE_ID ) ] <EOL> attrs += [ ( '<STR_LIT>' , dev . PCI_BUS_ID ) ] <EOL> if cc < ( <NUM_LIT:2> , <NUM_LIT:0> ) : <EOL> support = '<STR_LIT>' <EOL> else : <EOL> support = '<STR_LIT>' <EOL> supported_count += <NUM_LIT:1> <EOL> print ( '<STR_LIT>' % ( dev . id , dev . name , support ) ) <EOL> for key , val in attrs : <EOL> print ( '<STR_LIT>' % ( key , val ) ) <EOL> print ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' % ( supported_count , len ( devlist ) ) ) <EOL> return supported_count > <NUM_LIT:0> <EOL> @ contextlib . contextmanager <EOL> def defer_cleanup ( ) : <EOL> tserv = get_current_device ( ) . trashing <EOL> with tserv . defer_cleanup : <EOL> yield <EOL> profiling = require_context ( driver . profiling ) <EOL> profile_start = require_context ( driver . profile_start ) <EOL> profile_stop = require_context ( driver . profile_stop ) </s>
