Last active
August 29, 2015 14:01
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Run with
python cuda.py $dimx $dimy
or without parameters for the default (4, 3).