Skip to content

Instantly share code, notes, and snippets.

@jepio
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jepio/3c85dae5381d912f4125 to your computer and use it in GitHub Desktop.
Save jepio/3c85dae5381d912f4125 to your computer and use it in GitHub Desktop.
Shows how CUDA memory addressing could look for a block of (dimx, dimy) threads.
from __future__ import print_function
import sys
if len(sys.argv) == 1:
dimx, dimy = 4, 3
elif len(sys.argv) == 3:
dimx, dimy = [int(var) for var in sys.argv[1:3]]
else:
sys.exit('Wrong amount of parameters')
def fill():
def row(nr):
return [(nr, i) for i in range(dimy)]
return [row(i) for i in range(dimx)]
def pprint(array):
from math import log10, ceil
length = int(ceil(log10(dimx * dimy)))
frmt_str = "{}d".format(length)
def format_fcn(elem):
return str(elem) if type(elem) is tuple else format(elem, frmt_str)
for row in array:
print(" ".join(map(format_fcn, row)))
def convert(array):
def new_row(row):
return [x * dimy + y for x, y in row]
return [new_row(row) for row in array]
def main():
before = fill()
pprint(before)
after = convert(before)
pprint(after)
if __name__ == '__main__':
main()
@jepio
Copy link
Author

jepio commented Jun 19, 2014

Run with python cuda.py $dimx $dimy or without parameters for the default (4, 3).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment