Exemplo n.º 1
0
def dot(a, b, out=None):
    """
    This function computes the dot product of two arrays.
    input parameter:
        a, b: input matrix of float32/float64 type. These can only have a
              maximum of 2 dimensions.
        out: optional input parameter, must be conforming to the shape of a.b
    output parameter: output: Returns the dot product of a and b.
         If a and b are both scalars or both 1-D arrays then a
         scalar is returned; otherwise an array is returned.
    """
    # when both a and b are scalar
    if np.isscalar(a) and np.isscalar(b):
        return a * b
    # when either a or b is scalar
    elif np.isscalar(a) and not np.isscalar(b):
        b, cv, isM, _ = get_computation_matrix(b, copy_bcm=False, \
                                        check_col_vector=True, \
		                        return_ndim=True)
        PBLAS.scal(b, a)
        return handle_scal_output(b, cv, out, isM)
    elif not np.isscalar(a) and np.isscalar(b):
        a, cv, isM, _ = get_computation_matrix(a, copy_bcm=False, \
                                        check_col_vector=True,\
		                        return_ndim=True)
        PBLAS.scal(a, b)
        return handle_scal_output(a, cv, out, isM)
    # when neither a nor b is scalar
    else:
        a, cv1, isM1, a_ndim = get_computation_matrix(a, copy_bcm=False, \
                                                check_col_vector=True, \
                                                return_ndim=True)
        b, cv2, isM2, b_ndim = get_computation_matrix(b, copy_bcm=False, \
                                                check_col_vector=True, \
                                                return_ndim=True)
        if a.get_dtype() != b.get_dtype():
            raise TypeError("dot: dtype of a and b are not compatible!")

        if a_ndim == b_ndim == 1: # both a and b are 1D vector
                                    #    (column vector)
            return PBLAS.dot(a, b)
        elif a_ndim == 2 and b_ndim == 1:
            if a.numCols() != b.numRows():
                raise ValueError("dot: input dimensions does not comply \
                    with matrix-vector multiplication rule!")
            c = PBLAS.gemv(a, b)
            return handle_dot_output(a, b, c, cv1, cv2, out, isM1 or isM2)
        elif a_ndim == 2 and b_ndim == 2:
            if a.numCols() != b.numRows():
                raise ValueError("dot: input dimensions does not comply \
                    with matrix-matrix multiplication rule!")
            c = PBLAS.gemm(a, b)
            return handle_dot_output(a, b, c, cv1, cv2, out, isM1 or isM2)
        else:
            raise ValueError("dot: vector-matrix multiplication is not \
                supported!")
Exemplo n.º 2
0
bcy = FrovedisBlockcyclicMatrix(y) # blockcyclic vector (y)
bcm = FrovedisBlockcyclicMatrix(m) # blockcyclic matrix (m)
bcn = FrovedisBlockcyclicMatrix(n) # blockcyclic matrix (n)

# --- print original data
print ("x:")
print (x)
print ("y:")
print (y)
print ("m:")
print (m)
print ("m:")
print (n)

# --- swap ---
PBLAS.swap(bcx,bcy)
print ("after swap (x <=> y):")
print ("x = ")
bcx.debug_print()
print (bcx.to_numpy_matrix())
print ("y = ")
print (bcy.to_numpy_matrix())

# --- scal ---
PBLAS.scal(bcx,2)
print ("after scal (x = 2x), x = ")
print (bcx.to_numpy_matrix())

# --- axpy ---
PBLAS.axpy(bcx,bcy,2)
print ("after axpy (y = 2x + y), y = ")
Exemplo n.º 3
0
# sample numpy matrices creation
m1 = np.matrix([[1],[2],[3],[4]], dtype=np.float64) # 4x1
m2 = np.matrix([[5],[6],[7],[8]], dtype=np.float64) # 4x1
m3 = np.matrix([[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]],
               dtype=np.float64) # 4x4: eye(I)
m4 = np.matrix([[1,2,3,4],[5,6,7,8],[8,7,6,5],[4,3,2,1]],
               dtype=np.float64) # 4x4

# Creating Frovedis server side blockcyclic matrics from numpy matrices
bcm1 = FrovedisBlockcyclicMatrix(m1) # blockcyclic vector (x)
bcm2 = FrovedisBlockcyclicMatrix(m2) # blockcyclic vector (y)
bcm3 = FrovedisBlockcyclicMatrix(m3) # blockcyclic matrix (m)
bcm4 = FrovedisBlockcyclicMatrix(m4) # blockcyclic matrix (n)

# --- swap demo ---
PBLAS.swap(bcm1,bcm2)
print ("after swap (x <=> y):")
bcm1.get_rowmajor_view()
bcm2.get_rowmajor_view()

# --- scal demo ---
PBLAS.scal(bcm1,2)
print ("after scal (x = 2x):")
bcm1.get_rowmajor_view()

# --- axpy demo ---
PBLAS.axpy(bcm1,bcm2,2)
print ("after axpy (y = 2x + y):")
bcm1.get_rowmajor_view()
bcm2.get_rowmajor_view()
Exemplo n.º 4
0
# sample numpy matrices creation
m1 = np.matrix([[1], [2], [3], [4]], dtype=np.float64)  # 4x1
m2 = np.matrix([[5], [6], [7], [8]], dtype=np.float64)  # 4x1
m3 = np.matrix([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]],
               dtype=np.float64)  # 4x4: eye(I)
m4 = np.matrix([[1, 2, 3, 4], [5, 6, 7, 8], [8, 7, 6, 5], [4, 3, 2, 1]],
               dtype=np.float64)  # 4x4

# Creating Frovedis server side blockcyclic matrics from numpy matrices
bcm1 = FrovedisBlockcyclicMatrix(m1)  # blockcyclic vector (x)
bcm2 = FrovedisBlockcyclicMatrix(m2)  # blockcyclic vector (y)
bcm3 = FrovedisBlockcyclicMatrix(m3)  # blockcyclic matrix (m)
bcm4 = FrovedisBlockcyclicMatrix(m4)  # blockcyclic matrix (n)

# --- swap demo ---
PBLAS.swap(bcm1, bcm2)
print("after swap (x <=> y):")
bcm1.get_rowmajor_view()
bcm2.get_rowmajor_view()

# --- scal demo ---
PBLAS.scal(bcm1, 2)
print("after scal (x = 2x):")
bcm1.get_rowmajor_view()

# --- axpy demo ---
PBLAS.axpy(bcm1, bcm2, 2)
print("after axpy (y = 2x + y):")
bcm1.get_rowmajor_view()
bcm2.get_rowmajor_view()
Exemplo n.º 5
0
def dot(a, b, out=None):
    """
    This function computes the dot product of two arrays.
    input parameter:
        a, b: input matrix of float32/float64 type. These can only have a
              maximum of 2 dimensions.
        out: optional input parameter, must be conforming to the shape of a.b
    output parameter: output: Returns the dot product of a and b.
         If a and b are both scalars or both 1-D arrays then a
         scalar is returned; otherwise an array is returned.
    """
    #if out is specified, check its type with input mat
    if (not isinstance(a, FrovedisBlockcyclicMatrix)) and\
       out is not None:
        if np.asarray(a).dtype != np.asarray(out).dtype:
            raise TypeError("dot: dtype of out is not compatible with input")
    # when both a and b are scalar
    if np.isscalar(a) and np.isscalar(b):
        return a * b
    # when either a or b is scalar
    elif np.isscalar(a) and not np.isscalar(b):
        b_isRowvec, b_isNmat = check_if_vec(b)
        b, cv, isM, _ = get_computation_matrix(b, copy_bcm=False, \
                                        check_col_vector=True, \
                          return_ndim=True)
        PBLAS.scal(b, a)
        if b_isRowvec:
            return handle_scal_output(b, cv, out, isM, toFlatten=True)
        else:
            return handle_scal_output(b, cv, out, isM)
    elif not np.isscalar(a) and np.isscalar(b):
        a_isRowvec, a_isNmat = check_if_vec(a)
        a, cv, isM, _ = get_computation_matrix(a, copy_bcm=False, \
                                        check_col_vector=True,\
                          return_ndim=True)
        PBLAS.scal(a, b)
        if a_isRowvec:
            return handle_scal_output(a, cv, out, isM, toFlatten=True)
        else:
            return handle_scal_output(a, cv, out, isM)
    # when neither a nor b is scalar
    else:
        a_isRowvec, a_isNmat = check_if_vec(a)
        b_isRowvec, b_isNmat = check_if_vec(b)
        if a_isRowvec and b_isNmat:
            a, cv1, isM1, a_ndim = get_computation_matrix(a, copy_bcm=False, \
                                                      check_col_vector=True, \
                                                      return_ndim=True,      \
                                                      isVector=True)
            b, cv2, isM2, b_ndim = get_computation_matrix(b, copy_bcm=False, \
                                                      check_col_vector=True, \
                                                      return_ndim=True)
        else:
            a, cv1, isM1, a_ndim = get_computation_matrix(a, copy_bcm=False, \
                                                      check_col_vector=True, \
                                                      return_ndim=True)
            b, cv2, isM2, b_ndim = get_computation_matrix(b, copy_bcm=False, \
                                                      check_col_vector=True, \
                                                      return_ndim=True)
        if a.get_dtype() != b.get_dtype():
            raise TypeError("dot: dtype of a and b are not compatible!")

        if a_ndim == b_ndim == 1:  # both a and b are 1D vector
            #    (column vector)
            return PBLAS.dot(a, b)
        elif a_ndim == 2 and b_ndim == 1:
            if a.numCols() != b.numRows():
                raise ValueError("dot: input dimensions does not comply \
                    with matrix-vector multiplication rule!")
            c = PBLAS.gemv(a, b)
            if a_isNmat and b_isRowvec:
                return handle_dot_output(a, b, c, cv1, cv2, out, isM1 or isM2,\
                                         toFlatten=True)
            else:
                return handle_dot_output(a, b, c, cv1, cv2, out, isM1 or isM2)
        elif a_ndim == 2 and b_ndim == 2:
            if a.numCols() != b.numRows():
                raise ValueError("dot: input dimensions does not comply \
                    with matrix-matrix multiplication rule!")
            c = PBLAS.gemm(a, b)
            return handle_dot_output(a, b, c, cv1, cv2, out, isM1 or isM2)
        else:
            if a_isRowvec and not b_isNmat:
                print(b_isNmat)
                c = PBLAS.gemv(a, b)
                return handle_dot_output(a, b, c, cv1, cv2, out, isM1 or isM2,\
                                         toFlatten=True)
            else:
                c = PBLAS.gemm(a, b)
                return handle_dot_output(a, b, c, cv1, cv2, out, isM1 or isM2,\
                                         toFlatten=True)