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
|
//------------------------------------------------------------------------------
// gbextracttuples: extract all entries from a GraphBLAS matrix
//------------------------------------------------------------------------------
// SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2022, All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
//------------------------------------------------------------------------------
// Usage:
// [I J X] = GrB.extracttuples (A)
// [I J X] = GrB.extracttuples (A, desc)
// The desciptor is optional. If present, it must be a struct.
// desc.base = 'zero-based': I and J are returned as 0-based int64 indices
// desc.base = 'one-based int': I and J are returned as 1-based int64 indices
// desc.base = 'one-based': I and J are returned as 1-based double indices
// desc.base = 'default': 'one-based', unless max(size(A)) > flintmax,
// in which case 'one-based int' is used.
#include "gb_interface.h"
#define USAGE "usage: [I,J,X] = GrB.extracttuples (A, desc)"
void mexFunction
(
int nargout,
mxArray *pargout [ ],
int nargin,
const mxArray *pargin [ ]
)
{
//--------------------------------------------------------------------------
// check inputs
//--------------------------------------------------------------------------
gb_usage (nargin >= 1 && nargin <= 2 && nargout <= 3, USAGE) ;
//--------------------------------------------------------------------------
// get the optional descriptor
//--------------------------------------------------------------------------
base_enum_t base = BASE_DEFAULT ;
kind_enum_t kind = KIND_FULL ; // ignored
GxB_Format_Value fmt = GxB_NO_FORMAT ; // ignored
int sparsity = 0 ; // ignored
GrB_Descriptor desc = NULL ;
if (nargin > 1)
{
desc = gb_mxarray_to_descriptor (pargin [nargin-1], &kind, &fmt,
&sparsity, &base) ;
}
OK (GrB_Descriptor_free (&desc)) ;
//--------------------------------------------------------------------------
// get the matrix
//--------------------------------------------------------------------------
GrB_Matrix A = gb_get_shallow (pargin [0]) ;
GrB_Index nvals ;
OK (GrB_Matrix_nvals (&nvals, A)) ;
GrB_Type xtype ;
OK (GxB_Matrix_type (&xtype, A)) ;
//--------------------------------------------------------------------------
// determine what to extract
//--------------------------------------------------------------------------
bool extract_I = true ;
bool extract_J = (nargout > 1) ;
bool extract_X = (nargout > 2) ;
//--------------------------------------------------------------------------
// allocate I and J
//--------------------------------------------------------------------------
GrB_Index s = MAX (nvals, 1) ;
GrB_Index *I = extract_I ? mxMalloc (s * sizeof (GrB_Index)) : NULL ;
GrB_Index *J = extract_J ? mxMalloc (s * sizeof (GrB_Index)) : NULL ;
//--------------------------------------------------------------------------
// extract the tuples and export X
//--------------------------------------------------------------------------
if (xtype == GrB_BOOL)
{
bool *X = extract_X ? mxMalloc (s * sizeof (bool)) : NULL ;
OK (GrB_Matrix_extractTuples_BOOL (I, J, X, &nvals, A)) ;
if (extract_X)
{
pargout [2] = gb_export_to_mxfull ((void **) (&X), nvals, 1,
GrB_BOOL) ;
}
}
else if (xtype == GrB_INT8)
{
int8_t *X = extract_X ? mxMalloc (s * sizeof (int8_t)) : NULL ;
OK (GrB_Matrix_extractTuples_INT8 (I, J, X, &nvals, A)) ;
if (extract_X)
{
pargout [2] = gb_export_to_mxfull ((void **) (&X), nvals, 1,
GrB_INT8) ;
}
}
else if (xtype == GrB_INT16)
{
int16_t *X = extract_X ? mxMalloc (s * sizeof (int16_t)) : NULL ;
OK (GrB_Matrix_extractTuples_INT16 (I, J, X, &nvals, A)) ;
if (extract_X)
{
pargout [2] = gb_export_to_mxfull ((void **) (&X), nvals, 1,
GrB_INT16) ;
}
}
else if (xtype == GrB_INT32)
{
int32_t *X = extract_X ? mxMalloc (s * sizeof (int32_t)) : NULL ;
OK (GrB_Matrix_extractTuples_INT32 (I, J, X, &nvals, A)) ;
if (extract_X)
{
pargout [2] = gb_export_to_mxfull ((void **) (&X), nvals, 1,
GrB_INT32) ;
}
}
else if (xtype == GrB_INT64)
{
int64_t *X = extract_X ? mxMalloc (s * sizeof (int64_t)) : NULL ;
OK (GrB_Matrix_extractTuples_INT64 (I, J, X, &nvals, A)) ;
if (extract_X)
{
pargout [2] = gb_export_to_mxfull ((void **) (&X), nvals, 1,
GrB_INT64) ;
}
}
else if (xtype == GrB_UINT8)
{
uint8_t *X = extract_X ? mxMalloc (s * sizeof (uint8_t)) : NULL ;
OK (GrB_Matrix_extractTuples_UINT8 (I, J, X, &nvals, A)) ;
if (extract_X)
{
pargout [2] = gb_export_to_mxfull ((void **) (&X), nvals, 1,
GrB_UINT8) ;
}
}
else if (xtype == GrB_UINT16)
{
uint16_t *X = extract_X ? mxMalloc (s * sizeof (uint16_t)) : NULL ;
OK (GrB_Matrix_extractTuples_UINT16 (I, J, X, &nvals, A)) ;
if (extract_X)
{
pargout [2] = gb_export_to_mxfull ((void **) (&X), nvals, 1,
GrB_UINT16) ;
}
}
else if (xtype == GrB_UINT32)
{
uint32_t *X = extract_X ? mxMalloc (s * sizeof (uint32_t)) : NULL ;
OK (GrB_Matrix_extractTuples_UINT32 (I, J, X, &nvals, A)) ;
if (extract_X)
{
pargout [2] = gb_export_to_mxfull ((void **) (&X), nvals, 1,
GrB_UINT32) ;
}
}
else if (xtype == GrB_UINT64)
{
uint64_t *X = extract_X ? mxMalloc (s * sizeof (uint64_t)) : NULL ;
OK (GrB_Matrix_extractTuples_UINT64 (I, J, X, &nvals, A)) ;
if (extract_X)
{
pargout [2] = gb_export_to_mxfull ((void **) (&X), nvals, 1,
GrB_UINT64) ;
}
}
else if (xtype == GrB_FP32)
{
float *X = extract_X ? mxMalloc (s * sizeof (float)) : NULL ;
OK (GrB_Matrix_extractTuples_FP32 (I, J, X, &nvals, A)) ;
if (extract_X)
{
pargout [2] = gb_export_to_mxfull ((void **) (&X), nvals, 1,
GrB_FP32) ;
}
}
else if (xtype == GrB_FP64)
{
double *X = extract_X ? mxMalloc (s * sizeof (double)) : NULL ;
OK (GrB_Matrix_extractTuples_FP64 (I, J, X, &nvals, A)) ;
if (extract_X)
{
pargout [2] = gb_export_to_mxfull ((void **) (&X), nvals, 1,
GrB_FP64) ;
}
}
else if (xtype == GxB_FC32)
{
GxB_FC32_t *X = extract_X ? mxMalloc (s * sizeof (GxB_FC32_t)) : NULL ;
OK (GxB_Matrix_extractTuples_FC32 (I, J, X, &nvals, A)) ;
if (extract_X)
{
pargout [2] = gb_export_to_mxfull ((void **) (&X), nvals, 1,
GxB_FC32) ;
}
}
else if (xtype == GxB_FC64)
{
GxB_FC64_t *X = extract_X ? mxMalloc (s * sizeof (GxB_FC64_t)) : NULL ;
OK (GxB_Matrix_extractTuples_FC64 (I, J, X, &nvals, A)) ;
if (extract_X)
{
pargout [2] = gb_export_to_mxfull ((void **) (&X), nvals, 1,
GxB_FC64) ;
}
}
else
{
ERROR ("unsupported type") ;
}
//--------------------------------------------------------------------------
// determine if zero-based or one-based
//--------------------------------------------------------------------------
if (base == BASE_DEFAULT)
{
GrB_Index nrows, ncols ;
OK (GrB_Matrix_nrows (&nrows, A)) ;
OK (GrB_Matrix_ncols (&ncols, A)) ;
if (MAX (nrows, ncols) > FLINTMAX)
{
// the matrix is too large for I and J to be returned as double
base = BASE_1_INT64 ;
}
else
{
// this is the typical case
base = BASE_1_DOUBLE ;
}
}
//--------------------------------------------------------------------------
// free workspace
//--------------------------------------------------------------------------
OK (GrB_Matrix_free (&A)) ;
//--------------------------------------------------------------------------
// export I and J
//--------------------------------------------------------------------------
if (base == BASE_0_INT64)
{
//----------------------------------------------------------------------
// export I and J in their native zero-based integer format
//----------------------------------------------------------------------
if (extract_I)
{
pargout [0] = gb_export_to_mxfull ((void **) (&I), nvals, 1,
GrB_INT64) ;
}
if (extract_J)
{
pargout [1] = gb_export_to_mxfull ((void **) (&J), nvals, 1,
GrB_INT64) ;
}
}
else if (base == BASE_1_DOUBLE)
{
//----------------------------------------------------------------------
// export I and J as double one-based integers
//----------------------------------------------------------------------
if (extract_I)
{
double *I_double = mxMalloc (s * sizeof (double)) ;
GB_helper1 (I_double, I, (int64_t) nvals) ;
gb_mxfree ((void **) (&I)) ;
pargout [0] = gb_export_to_mxfull ((void **) (&I_double), nvals, 1,
GrB_FP64) ;
}
if (extract_J)
{
double *J_double = mxMalloc (s * sizeof (double)) ;
GB_helper1 (J_double, J, (int64_t) nvals) ;
gb_mxfree ((void **) (&J)) ;
pargout [1] = gb_export_to_mxfull ((void **) (&J_double), nvals, 1,
GrB_FP64) ;
}
}
else if (base == BASE_1_INT64)
{
//----------------------------------------------------------------------
// export I and J as int64 one-based integers
//----------------------------------------------------------------------
if (extract_I)
{
GB_helper1i ((int64_t *) I, (int64_t) nvals) ;
pargout [0] = gb_export_to_mxfull ((void **) (&I), nvals, 1,
GrB_INT64) ;
}
if (extract_J)
{
GB_helper1i ((int64_t *) J, (int64_t) nvals) ;
pargout [1] = gb_export_to_mxfull ((void **) (&J), nvals, 1,
GrB_INT64) ;
}
}
GB_WRAPUP ;
}
|