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 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
|
//------------------------------------------------------------------------------
// GB_mex_AxB_idx: C=A*B, A'*B, A*B', or A'*B' using the indexop semirings
//------------------------------------------------------------------------------
// SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2025, All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
//------------------------------------------------------------------------------
// This is for testing only. See GrB_mxm instead.
// monoid: min, max, plus, times
// mult: firsti, firsti1, firstj, firstj1, secondi, secondi1, secondj, secondj1
#include "GB_mex.h"
#include "GB_mex_errors.h"
#define USAGE "C = GB_mex_AxB_idx (A, B, atrans, btrans, axb_method," \
" C_is_csc, builtin, add, mult)"
#define FREE_ALL \
{ \
GrB_Matrix_free (&A) ; \
GrB_Matrix_free (&B) ; \
GrB_Matrix_free (&C) ; \
GrB_Scalar_free (&Theta) ; \
GrB_Descriptor_free (&desc) ; \
GrB_BinaryOp_free (&mult) ; \
GxB_IndexBinaryOp_free (&Iop) ; \
GrB_Monoid_free (&monoid) ; \
GrB_Semiring_free (&semiring) ; \
GB_mx_put_global (true) ; \
}
//------------------------------------------------------------------------------
// user-defined index binary operators
//------------------------------------------------------------------------------
void gb_firsti_theta (int64_t *z,
const void *x, uint64_t ix, uint64_t jx,
const void *y, uint64_t iy, uint64_t jy,
const int64_t *theta) ;
void gb_firsti_theta (int64_t *z,
const void *x, uint64_t ix, uint64_t jx,
const void *y, uint64_t iy, uint64_t jy,
const int64_t *theta)
{
(*z) = ix + (*theta) ;
}
#define FIRSTI_THETA_DEFN \
"void gb_firsti_theta (int64_t *z, \n" \
" const void *x, uint64_t ix, uint64_t jx, \n" \
" const void *y, uint64_t iy, uint64_t jy, \n" \
" const int64_t *theta) \n" \
"{ \n" \
" (*z) = ix + (*theta) ; \n" \
"}"
void gb_secondi_theta (int64_t *z,
const void *x, uint64_t ix, uint64_t jx,
const void *y, uint64_t iy, uint64_t jy,
const int64_t *theta) ;
void gb_secondi_theta (int64_t *z,
const void *x, uint64_t ix, uint64_t jx,
const void *y, uint64_t iy, uint64_t jy,
const int64_t *theta)
{
(*z) = iy + (*theta) ;
}
#define SECONDI_THETA_DEFN \
"void gb_secondi_theta (int64_t *z, \n" \
" const void *x, uint64_t ix, uint64_t jx, \n" \
" const void *y, uint64_t iy, uint64_t jy, \n" \
" const int64_t *theta) \n" \
"{ \n" \
" (*z) = iy + (*theta) ; \n" \
"}"
void gb_firstj_theta (int64_t *z,
const void *x, uint64_t ix, uint64_t jx,
const void *y, uint64_t iy, uint64_t jy,
const int64_t *theta) ;
void gb_firstj_theta (int64_t *z,
const void *x, uint64_t ix, uint64_t jx,
const void *y, uint64_t iy, uint64_t jy,
const int64_t *theta)
{
(*z) = jx + (*theta) ;
}
#define FIRSTJ_THETA_DEFN \
"void gb_firstj_theta (int64_t *z, \n" \
" const void *x, uint64_t ix, uint64_t jx, \n" \
" const void *y, uint64_t iy, uint64_t jy, \n" \
" const int64_t *theta) \n" \
"{ \n" \
" (*z) = jx + (*theta) ; \n" \
"}"
void gb_secondj_theta (int64_t *z,
const void *x, uint64_t ix, uint64_t jx,
const void *y, uint64_t iy, uint64_t jy,
const int64_t *theta) ;
void gb_secondj_theta (int64_t *z,
const void *x, uint64_t ix, uint64_t jx,
const void *y, uint64_t iy, uint64_t jy,
const int64_t *theta)
{
(*z) = jy + (*theta) ;
}
#define SECONDJ_THETA_DEFN \
"void gb_secondj_theta (int64_t *z, \n" \
" const void *x, uint64_t ix, uint64_t jx, \n" \
" const void *y, uint64_t iy, uint64_t jy, \n" \
" const int64_t *theta) \n" \
"{ \n" \
" (*z) = jy + (*theta) ; \n" \
"}"
//------------------------------------------------------------------------------
void mexFunction
(
int nargout,
mxArray *pargout [ ],
int nargin,
const mxArray *pargin [ ]
)
{
GrB_Info info = GrB_SUCCESS ;
bool malloc_debug = GB_mx_get_global (true) ;
GrB_Matrix A = NULL, B = NULL, C = NULL ;
GrB_Scalar Theta = NULL ;
GrB_BinaryOp mult = NULL ;
GxB_IndexBinaryOp Iop = NULL ;
GrB_Semiring semiring = NULL ;
uint64_t anrows = 0, ancols = 0, bnrows = 0, bncols = 0 ;
GrB_Descriptor desc = NULL ;
GrB_Monoid monoid = NULL ;
// check inputs
if (nargout > 1 || nargin < 2 || nargin > 9)
{
mexErrMsgTxt ("Usage: " USAGE) ;
}
#define GET_DEEP_COPY ;
#define FREE_DEEP_COPY ;
// get A and B
A = GB_mx_mxArray_to_Matrix (pargin [0], "A", false, true) ;
B = GB_mx_mxArray_to_Matrix (pargin [1], "B", false, true) ;
if (A == NULL || B == NULL)
{
FREE_ALL ;
mexErrMsgTxt ("failed") ;
}
// get the atrans option
bool GET_SCALAR (2, bool, atrans, false) ;
// get the btrans option
bool GET_SCALAR (3, bool, btrans, false) ;
// get the axb_method
int GET_SCALAR (4, int, AxB_method, GxB_DEFAULT) ;
// get the C_is_csc option
bool GET_SCALAR (5, bool, C_is_csc, true) ;
// get the builtin option
bool GET_SCALAR (6, bool, builtin, true) ;
// get the add monoid; defaults to 'min'
#define LEN 256
char addname [LEN+1] ;
strcpy (addname, "min") ;
if (nargin > 7)
{
int len = GB_mx_mxArray_to_string (addname, LEN, pargin [7]) ;
if (len == -1)
{
mexErrMsgTxt ("addname must be char") ;
}
}
// get the mult operator; defaults to 'secondi1'
char multname [LEN+1] ;
strcpy (multname, "secondi1") ;
if (nargin > 8)
{
int len = GB_mx_mxArray_to_string (multname, LEN, pargin [8]) ;
if (len == -1)
{
mexErrMsgTxt ("multname must be char") ;
}
}
// set the Descriptor
OK (GrB_Descriptor_new (&desc)) ;
OK (GrB_Descriptor_set (desc, GrB_INP0, atrans ? GrB_TRAN : GxB_DEFAULT)) ;
OK (GrB_Descriptor_set (desc, GrB_INP1, btrans ? GrB_TRAN : GxB_DEFAULT)) ;
OK (GrB_Descriptor_set (desc, GxB_AxB_METHOD, AxB_method)) ;
// determine the dimensions
OK (GrB_Matrix_nrows (&anrows, A)) ;
OK (GrB_Matrix_ncols (&ancols, A)) ;
OK (GrB_Matrix_nrows (&bnrows, B)) ;
OK (GrB_Matrix_ncols (&bncols, B)) ;
uint64_t cnrows = (atrans) ? ancols : anrows ;
uint64_t cncols = (btrans) ? bnrows : bncols ;
// create the output matrix C
OK (GrB_Matrix_new (&C, GrB_INT64, cnrows, cncols)) ;
OK (GrB_Matrix_set_INT32 (C, C_is_csc, GrB_STORAGE_ORIENTATION_HINT)) ;
// create the monoid
if (MATCH (addname, "min" )) monoid = GrB_MIN_MONOID_INT64 ;
else if (MATCH (addname, "max" )) monoid = GrB_MAX_MONOID_INT64 ;
else if (MATCH (addname, "plus" )) monoid = GrB_PLUS_MONOID_INT64 ;
else if (MATCH (addname, "times")) monoid = GrB_TIMES_MONOID_INT64 ;
else
{
mexErrMsgTxt ("add not supported") ;
}
// create the mult operator
if (builtin)
{
//----------------------------------------------------------------------
// built-in operator
//----------------------------------------------------------------------
if (MATCH (multname, "firsti" )) mult = GxB_FIRSTI_INT64 ;
else if (MATCH (multname, "firsti1" )) mult = GxB_FIRSTI1_INT64 ;
else if (MATCH (multname, "firstj" )) mult = GxB_FIRSTJ_INT64 ;
else if (MATCH (multname, "firstj1" )) mult = GxB_FIRSTJ1_INT64 ;
else if (MATCH (multname, "secondi" )) mult = GxB_SECONDI_INT64 ;
else if (MATCH (multname, "secondi1")) mult = GxB_SECONDI1_INT64 ;
else if (MATCH (multname, "secondj" )) mult = GxB_SECONDJ_INT64 ;
else if (MATCH (multname, "secondj1")) mult = GxB_SECONDJ1_INT64 ;
else
{
mexErrMsgTxt ("mult not supported") ;
}
}
else
{
//----------------------------------------------------------------------
// user-defined operator
//----------------------------------------------------------------------
// create the index binary op
int theta ;
if (MATCH (multname, "firsti" ))
{
theta = 0 ;
OK (GxB_IndexBinaryOp_new (&Iop,
(GxB_index_binary_function) gb_firsti_theta,
GrB_INT64, GrB_FP64, GrB_FP64, GrB_INT64,
"gb_firsti_theta", FIRSTI_THETA_DEFN)) ;
}
else if (MATCH (multname, "firsti1"))
{
theta = 1 ;
OK (GxB_IndexBinaryOp_new (&Iop,
(GxB_index_binary_function) gb_firsti_theta,
GrB_INT64, GrB_FP64, GrB_FP64, GrB_INT64,
"gb_firsti_theta", FIRSTI_THETA_DEFN)) ;
}
else if (MATCH (multname, "firstj" ))
{
theta = 0 ;
OK (GxB_IndexBinaryOp_new (&Iop,
(GxB_index_binary_function) gb_firstj_theta,
GrB_INT64, GrB_FP64, GrB_FP64, GrB_INT64,
"gb_firstj_theta", FIRSTJ_THETA_DEFN)) ;
}
else if (MATCH (multname, "firstj1"))
{
theta = 1 ;
OK (GxB_IndexBinaryOp_new (&Iop,
(GxB_index_binary_function) gb_firstj_theta,
GrB_INT64, GrB_FP64, GrB_FP64, GrB_INT64,
"gb_firstj_theta", FIRSTJ_THETA_DEFN)) ;
}
else if (MATCH (multname, "secondi" ))
{
theta = 0 ;
OK (GxB_IndexBinaryOp_new (&Iop,
(GxB_index_binary_function) gb_secondi_theta,
GrB_INT64, GrB_FP64, GrB_FP64, GrB_INT64,
"gb_secondi_theta", SECONDI_THETA_DEFN)) ;
}
else if (MATCH (multname, "secondi1"))
{
theta = 1 ;
OK (GxB_IndexBinaryOp_new (&Iop,
(GxB_index_binary_function) gb_secondi_theta,
GrB_INT64, GrB_FP64, GrB_FP64, GrB_INT64,
"gb_secondi_theta", SECONDI_THETA_DEFN)) ;
}
else if (MATCH (multname, "secondj" ))
{
theta = 0 ;
OK (GxB_IndexBinaryOp_new (&Iop,
(GxB_index_binary_function) gb_secondj_theta,
GrB_INT64, GrB_FP64, GrB_FP64, GrB_INT64,
"gb_secondj_theta", SECONDJ_THETA_DEFN)) ;
}
else if (MATCH (multname, "secondj1"))
{
theta = 1 ;
OK (GxB_IndexBinaryOp_new (&Iop,
(GxB_index_binary_function) gb_secondj_theta,
GrB_INT64, GrB_FP64, GrB_FP64, GrB_INT64,
"gb_secondj_theta", SECONDJ_THETA_DEFN)) ;
}
else
{
mexErrMsgTxt ("mult not supported") ;
}
// create the mult binary op
OK (GrB_Scalar_new (&Theta, GrB_INT64)) ;
OK (GrB_Scalar_setElement_INT64 (Theta, theta)) ;
OK (GxB_BinaryOp_new_IndexOp (&mult, Iop, Theta)) ;
}
// create the semiring
OK (GrB_Semiring_new (&semiring, monoid, mult)) ;
// GxB_print (semiring, 5) ;
// C = A*B, A'*B, A*B', or A'*B'
OK (GrB_mxm (C, NULL, NULL, semiring, A, B, desc)) ;
// return C
pargout [0] = GB_mx_Matrix_to_mxArray (&C, "C AxB idx result", true) ;
FREE_ALL ;
}
|