N0dev

Ladislav Dobrovský

Fighting CUDA paralelism for printing vectors

Aug 062019

Parallel programming has its challanges. For my case I did not find very usefull to use a debbuger and also for the quick fix I did not want to extract the G matrix to print it on the CPU as I use only one row for each thread and store it in the "__shared__" memory (see CUDA Memory model).

Naive printing as I would do on the CPU did not do the tick at all... :-)

undefined

I did not find an sprintf implementation quickly (cuda does not provide one) and decided to tackle the problem on my own. As I know how many digits I need to print (here in the example just 2 at most).
It helped me find the bug that eluded me for some time and I might write it as a function to use for my further debugging.

undefined

(pow must get double arguments, or it prints error like "cannot call host function from __global__ device function")

My actual debug printing looks like this. Semicolon separated values can be easily analyzed with python scripts or Calc/Excel.

undefined

 

numpy - I miss sortrows from Matlab!

Feb 272019

There are things where NumPy is great! Not having sortrows is not one of them.
This is my straightforward implementation for 2D numpy arrays. Negative indicies cannot be used for descending order, therefore I use auxiliary Descending-object.

import numpy as np

class Descending:
    """ for np_sortrows: sort column in descending order """
    def __init__(self, column_index):
        self.column_index = column_index

    def __int__(self):  # when cast to integer
        return self.column_index


def np_sortrows(M, columns=None):
    """  sorting 2D matrix by rows
    :param M: 2D numpy array to be sorted by rows
    :param columns: None for all columns to be used,
                    iterable of indexes or Descending objects
    :return: returns sorted M
    """
    if len(M.shape) != 2:
        raise ValueError('M must be 2d numpy.array')
    if columns is None:  # no columns specified, use all (reversed)
        M_columns = tuple(M[:, c] for c in range(M.shape[1]-1, -1, -1))
    else:
        M_columns = []
        for c in columns:
            M_c = M[:, int(c)]
            if isinstance(c, Descending):
                M_columns.append(M_c[::-1])
            else:
                M_columns.append(M_c)
        M_columns.reverse()

    return M[np.lexsort(M_columns), :]


if __name__ == '__main__':

    data = np.array([[3, 0, 0, .24],
                     [4, 1, 1, .41],
                     [2, 1, 3, .25],
                     [2, 1, 1, .63],
                     [1, 1, 3, .38]])

    # to satisfy original question:
    # third column is index 2, fourth column in reversed order
    print(np_sortrows(data, [2, Descending(3)]))

 

Original question that proded me to make the post:

https://stackoverflow.com/questions/18920010/sortrows-with-multiple-sorting-keys-in-numpy/

Atom

Powered by Nibbleblog