pyjac.core.shared_memory module

Handles shared memory usage to accelerate memory accesses for CUDA

class pyjac.core.shared_memory.variable(base, index, lang='cuda')[source]

Bases: object

Class that represents an array/index pair. Used for in the internal dicitonary of the shared_memory_manager for identification and tracking of variable usage for eviction.

Creates a variable with given base and index

Parameters:
  • base (str) – The name of the array
  • index (int) – The index in the array
reset()[source]

Reset the usage count of this variable

update()[source]

Increment the usage count of this variable

to_string()[source]

Converts this variable to a string representation

class pyjac.core.shared_memory.shared_memory_manager(blocks_per_sm=8, num_threads=64, L1_PREFERRED=True)[source]

Bases: object

Manager for GPU shared memory.

Creates a shared memory manager

Parameters:
  • blocks_per_sm (int, optional) – The number of blocks / streaming multiprocessor to target
  • num_threads (int, optional) – The number of threads / block expected in kernel launches
  • L1_PREFERRED (bool, optional) – Whether or not to prefer a larger L1 cache over more shared memory (recommended).

Notes

For ease, a single SMM is used in the entire program. Thus this class has methods for setting the state/behaviour (e.g., reset, and set_on_eviction).

force_eviction()[source]

Forces eviction of the manager’s internal dictionary.

Notes

The internal dictionary will be reset, and (if supplied) the on_eviction function will be called on each evicted entry.

evict_longest_gap()[source]

Evicts entry in the internal dictionary the longest without use.

evict(shared_index)[source]

Removes the entry at shared_index from the internal dictionary

Parameters:shared_index (int) – The key to remove from the internal dictionary

Notes

If set, on_eviction will be called.

add_to_dictionary(val)[source]

Adds the value to the next available dictionary location

Parameters:val (variable) – The value to add to the dictionary
set_on_eviction(func)[source]
Sets a callback function that is called upon eviction of a variable from the
internal dictionary
Parameters:func (function) – Function that takes one arguement (the evicted variable)
Returns:
Return type:None
reset()[source]

Resets the SMM for use by other methods/callers

Returns:
Return type:None
write_init(file, indent=4)[source]

Convenience method to define shared memory for CUDA

Parameters:
  • file (File) – Open File object to write to
  • indent (int, optional) – The number of spaces to use in the indent
Returns:

Return type:

None

load_into_shared(file, variables, estimated_usage=None, indent=2, load=True)[source]

The main SMM method, loads/evicts variables based upon estimated usage and stagnancy.

Parameters:
  • file (File) – Open File object to write to
  • variables (list of variable) – List of variables to consider loading
  • estimated_usage (list of float, optional) – If specified, these will be used to prioritize variable additon
  • indent (int, optional) – The number of spaces to use in the indentation
  • load (bool, optional) – If True (default), a load into the internal dictionary will be written to the file. If False, this will must be handled by the calling routine.
Returns:

Return type:

List of bool to indicate if variables are loaded in shared memory.

mark_for_eviction(variables)[source]

Marks variables for possible eviction upon next load_into_shared call

Parameters:variables (list of variable) – List of variables to consider for eviction
get_index(var)[source]

Checks to see if a variable is in the internal dictionary. If so returns internal index and variable

Parameters:var (variable) – The variable to check
Returns:
  • our_ind (int) – Index of variable in internal dictionary
  • our_var (variable) – Variable found in internal dictionary
get_array(lang, thevar, index, twod=None)[source]

A substitute for utils.get_array.

If the variable is in our internal dictionary returns shared memory address, otherwise calls utils.get_array.

Parameters:
  • lang ({'c', 'cuda'}) – Programming language.
  • thevar (variable) – Variable of interest.
  • index (int) – Index in the array.
  • twod (int) – Not used in this function.
Returns:

name – String with indexed array.

Return type:

str