1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
|
//------------------------------------------------------------------------------
// GB_mex_AdotB: compute C=spones(Mask).*(A'*B)
//------------------------------------------------------------------------------
// SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2022, All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
//------------------------------------------------------------------------------
// Returns a plain built-in sparse matrix, not a struct. Only works in double
// and complex. Input matrices must be built-in sparse matrices, or GraphBLAS
// structs in CSC format.
#include "GB_mex.h"
#define USAGE "C = GB_mex_AdotB (A,B,Mask,flipxy)"
#define FREE_ALL \
{ \
GrB_Matrix_free_(&A) ; \
GrB_Matrix_free_(&Aconj) ; \
GrB_Matrix_free_(&B) ; \
GrB_Matrix_free_(&C) ; \
GrB_Matrix_free_(&Mask) ; \
GrB_Monoid_free_(&add) ; \
GrB_Semiring_free_(&semiring) ; \
GB_mx_put_global (true) ; \
}
GrB_Matrix A = NULL, B = NULL, C = NULL, Aconj = NULL, Mask = NULL ;
GrB_Monoid add = NULL ;
GrB_Semiring semiring = NULL ;
GrB_Info adotb_complex (GB_Context Context) ;
GrB_Info adotb (GB_Context Context) ;
GrB_Index anrows, ancols, bnrows, bncols, mnrows, mncols ;
bool flipxy = false ;
struct GB_Matrix_opaque C_header ;
//------------------------------------------------------------------------------
GrB_Info adotb_complex (GB_Context Context)
{
GrB_Info info = GrB_Matrix_new (&Aconj, Complex, anrows, ancols) ;
if (info != GrB_SUCCESS) return (info) ;
info = GxB_Matrix_Option_set (Aconj, GxB_FORMAT, GxB_BY_COL) ;
if (info != GrB_SUCCESS)
{
GrB_Matrix_free_(&Aconj) ;
return (info) ;
}
info = GrB_Matrix_apply_(Aconj, NULL, NULL, Complex_conj, A, NULL) ;
if (info != GrB_SUCCESS)
{
GrB_Matrix_free_(&Aconj) ;
return (info) ;
}
// force completion
info = GrB_Matrix_wait_(Aconj, GrB_MATERIALIZE) ;
if (info != GrB_SUCCESS)
{
GrB_Matrix_free_(&Aconj) ;
return (info) ;
}
bool mask_applied = false ;
GrB_Semiring semiring = Complex_plus_times ;
if (Mask != NULL)
{
// C<M> = A'*B using dot product method
info = GB_AxB_dot3 (C, false, NULL, Mask, false, Aconj, B, semiring,
flipxy, Context) ;
mask_applied = true ;
}
else
{
// C = A'*B using dot product method
mask_applied = false ; // no mask to apply
info = GB_AxB_dot2 (C, false, NULL, NULL, false, false,
false, Aconj, B, semiring, flipxy, Context) ;
}
GrB_Matrix_free_(&Aconj) ;
return (info) ;
}
//------------------------------------------------------------------------------
GrB_Info adotb (GB_Context Context)
{
// create the Semiring for regular z += x*y
GrB_Info info = GrB_Monoid_new_FP64_(&add, GrB_PLUS_FP64, (double) 0) ;
if (info != GrB_SUCCESS) return (info) ;
info = GrB_Semiring_new (&semiring, add, GrB_TIMES_FP64) ;
if (info != GrB_SUCCESS)
{
GrB_Monoid_free_(&add) ;
return (info) ;
}
// C = A'*B
bool mask_applied = false ;
if (Mask != NULL)
{
// C<M> = A'*B using dot product method
info = GB_AxB_dot3 (C, false, NULL, Mask, false, A, B,
semiring /* GxB_PLUS_TIMES_FP64 */,
flipxy, Context) ;
mask_applied = true ;
}
else
{
mask_applied = false ; // no mask to apply
info = GB_AxB_dot2 (C, false, NULL, NULL, false, false,
false, A, B, semiring /* GxB_PLUS_TIMES_FP64 */, flipxy, Context) ;
}
GrB_Monoid_free_(&add) ;
GrB_Semiring_free_(&semiring) ;
return (info) ;
}
//------------------------------------------------------------------------------
void mexFunction
(
int nargout,
mxArray *pargout [ ],
int nargin,
const mxArray *pargin [ ]
)
{
bool malloc_debug = GB_mx_get_global (true) ;
GB_CONTEXT (USAGE) ;
// check inputs
if (nargout > 1 || nargin < 2 || nargin > 4)
{
mexErrMsgTxt ("Usage: " USAGE) ;
}
#define GET_DEEP_COPY ;
#define FREE_DEEP_COPY ;
GET_DEEP_COPY ;
// get A and B (shallow copies)
A = GB_mx_mxArray_to_Matrix (pargin [0], "A input", false, true) ;
B = GB_mx_mxArray_to_Matrix (pargin [1], "B input", false, true) ;
if (A == NULL)
{
FREE_ALL ;
mexErrMsgTxt ("A failed") ;
}
if (B == NULL)
{
FREE_ALL ;
mexErrMsgTxt ("B failed") ;
}
GrB_Matrix_nrows (&anrows, A) ;
GrB_Matrix_ncols (&ancols, A) ;
GrB_Matrix_nrows (&bnrows, B) ;
GrB_Matrix_ncols (&bncols, B) ;
if (!A->is_csc || !B->is_csc)
{
FREE_ALL ;
mexErrMsgTxt ("matrices must be CSC only") ;
}
if (A->iso || B->iso)
{
FREE_ALL ;
mexErrMsgTxt ("matrices must be non-iso only") ;
}
// get Mask (shallow copy)
if (nargin > 2)
{
Mask = GB_mx_mxArray_to_Matrix (pargin [2], "Mask input", false, false);
GrB_Matrix_nrows (&mnrows, Mask) ;
GrB_Matrix_ncols (&mncols, Mask) ;
if (!Mask->is_csc)
{
FREE_ALL ;
mexErrMsgTxt ("matrices must be CSC only") ;
}
if (mnrows != ancols || mncols != bncols)
{
FREE_ALL ;
mexErrMsgTxt ("mask wrong dimension") ;
}
}
if (anrows != bnrows)
{
FREE_ALL ;
mexErrMsgTxt ("inner dimensions of A'*B do not match") ;
}
if (anrows == 0)
{
FREE_ALL ;
mexErrMsgTxt ("inner dimensions of A'*B must be > 0") ;
}
// get flipxy
GET_SCALAR (3, bool, flipxy, false) ;
struct GB_Matrix_opaque C_header ;
C = GB_clear_static_header (&C_header) ;
if (A->type == Complex)
{
// C = A'*B, complex case
METHOD (adotb_complex (Context)) ;
}
else
{
METHOD (adotb (Context)) ;
}
// return C
pargout [0] = GB_mx_Matrix_to_mxArray (&C, "C AdotB result", false) ;
FREE_ALL ;
}
|