GrB_Info GB_emult // C = A.*B ( GrB_Matrix *Chandle, // output matrix (unallocated on input) const GrB_Type ctype, // type of output matrix C const bool C_is_csc, // format of output matrix C const GrB_Matrix A, // input A matrix const GrB_Matrix B, // input B matrix const GrB_BinaryOp op, // op to perform C = op (A,B) GB_Context Context ) { //-------------------------------------------------------------------------- // check inputs //-------------------------------------------------------------------------- ASSERT (Chandle != NULL) ; ASSERT_OK (GB_check (A, "A for C=A.*B", GB0)) ; ASSERT_OK (GB_check (B, "B for C=A.*B", GB0)) ; ASSERT (!GB_PENDING (A)) ; ASSERT (!GB_ZOMBIES (A)) ; ASSERT (!GB_PENDING (B)) ; ASSERT (!GB_ZOMBIES (B)) ; ASSERT_OK (GB_check (op, "op for C=A.*B", GB0)) ; ASSERT (A->vdim == A->vdim && B->vlen == A->vlen) ; ASSERT (GB_Type_compatible (ctype, op->ztype)) ; ASSERT (GB_Type_compatible (A->type, op->xtype)) ; ASSERT (GB_Type_compatible (B->type, op->ytype)) ; (*Chandle) = NULL ; //-------------------------------------------------------------------------- // allocate the output matrix C //-------------------------------------------------------------------------- // C is hypersparse if A or B are hypersparse (contrast with GB_add) bool C_is_hyper = (A->is_hyper || B->is_hyper) && (A->vdim > 1) ; // [ allocate the result C; C->p is malloc'd // worst case nnz (C) is min (nnz (A), nnz (B)) GrB_Info info ; GrB_Matrix C = NULL ; // allocate a new header for C GB_CREATE (&C, ctype, A->vlen, A->vdim, GB_Ap_malloc, C_is_csc, GB_SAME_HYPER_AS (C_is_hyper), B->hyper_ratio, GB_IMIN (A->nvec_nonempty, B->nvec_nonempty), GB_IMIN (GB_NNZ (A), GB_NNZ (B)), true) ; if (info != GrB_SUCCESS) { return (info) ; } //-------------------------------------------------------------------------- // get functions and type sizes //-------------------------------------------------------------------------- GB_cast_function cast_A_to_X, cast_B_to_Y, cast_Z_to_C ; cast_A_to_X = GB_cast_factory (op->xtype->code, A->type->code) ; cast_B_to_Y = GB_cast_factory (op->ytype->code, B->type->code) ; cast_Z_to_C = GB_cast_factory (C->type->code, op->ztype->code) ; // If types are user-defined, the cast* function is just // GB_copy_user_user, which requires the size of the type. No typecast is // done. GxB_binary_function fmult = op->function ; size_t xsize = op->xtype->size ; size_t ysize = op->ytype->size ; size_t zsize = op->ztype->size ; size_t asize = A->type->size ; size_t bsize = B->type->size ; size_t csize = C->type->size ; // no typecasting needed if all the types match the operator bool nocasting = (A->type->code == op->xtype->code) && (B->type->code == op->ytype->code) && (C->type->code == op->ztype->code) ; // scalar workspace char xwork [nocasting ? 1 : xsize] ; char ywork [nocasting ? 1 : ysize] ; char zwork [nocasting ? 1 : zsize] ; //-------------------------------------------------------------------------- // C = A .* B, where .*+ is defined by z=fmult(x,y) //-------------------------------------------------------------------------- int64_t *Ci = C->i ; GB_void *Cx = C->x ; int64_t jlast, cnz, cnz_last ; GB_jstartup (C, &jlast, &cnz, &cnz_last) ; const int64_t *Ai = A->i, *Bi = B->i ; const GB_void *Ax = A->x, *Bx = B->x ; GB_for_each_vector2 (A, B) { //---------------------------------------------------------------------- // get the next column, A (:,j) and B (:j) //---------------------------------------------------------------------- int64_t GBI2_initj (Iter, j, pa, pa_end, pb, pb_end) ; int64_t ajnz = pa_end - pa ; int64_t bjnz = pb_end - pb ; //---------------------------------------------------------------------- // compute C (:,j): pattern is the set intersection //---------------------------------------------------------------------- if (ajnz == 0 || bjnz == 0) { // one or both columns are empty; set intersection is empty ; } else if (Ai [pa_end-1] < Bi [pb]) { // all entries in A are in lower row indices than all the // entries in B; set intersection is empty ; } else if (Bi [pb_end-1] < Ai [pa]) { // all entries in B are in lower row indices than all the // entries in A; set intersection is empty ; } else if (ajnz > 256 * bjnz) { //------------------------------------------------------------------ // A (:,j) has many more nonzeros than B (:,j) //------------------------------------------------------------------ for ( ; pa < pa_end && pb < pb_end ; ) { int64_t ia = Ai [pa] ; int64_t ib = Bi [pb] ; if (ia < ib) { // A (ia,j) appears before B (ib,j) // discard all entries A (ia:ib-1,j) int64_t pleft = pa + 1 ; int64_t pright = pa_end ; GB_BINARY_TRIM_SEARCH (ib, Ai, pleft, pright) ; ASSERT (pleft > pa) ; pa = pleft ; } else if (ia > ib) { // B (ib,j) appears before A (ia,j) pb++ ; } else // ia == ib { // A (i,j) and B (i,j) match GB_EMULT ; } } } else if (bjnz > 256 * ajnz) { //------------------------------------------------------------------ // B (:,j) has many more nonzeros than A (:,j) //------------------------------------------------------------------ for ( ; pa < pa_end && pb < pb_end ; ) { int64_t ia = Ai [pa] ; int64_t ib = Bi [pb] ; if (ia < ib) { // A (ia,j) appears before B (ib,j) pa++ ; } else if (ia > ib) { // B (ib,j) appears before A (ia,j) // discard all entries B (ib:ia-1,j) int64_t pleft = pb + 1 ; int64_t pright = pb_end ; GB_BINARY_TRIM_SEARCH (ia, Bi, pleft, pright) ; ASSERT (pleft > pb) ; pb = pleft ; } else // ia == ib { // A (i,j) and B (i,j) match GB_EMULT ; } } } else { //------------------------------------------------------------------ // A (:,j) and B (:,j) have about the same number of entries //------------------------------------------------------------------ for ( ; pa < pa_end && pb < pb_end ; ) { int64_t ia = Ai [pa] ; int64_t ib = Bi [pb] ; if (ia < ib) { // A (ia,j) appears before B (ib,j) pa++ ; } else if (ia > ib) { // B (ib,j) appears before A (ia,j) pb++ ; } else // ia == ib { // A (i,j) and B (i,j) match GB_EMULT ; } } } //---------------------------------------------------------------------- // finalize C(:,j) //---------------------------------------------------------------------- // this cannot fail since C->plen is the upper bound: min of the // non-empty vectors of A and B info = GB_jappend (C, j, &jlast, cnz, &cnz_last, Context) ; ASSERT (info == GrB_SUCCESS) ; #if 0 // if it could fail, do this: if (info != GrB_SUCCESS) { GB_MATRIX_FREE (&C) ; return (info) ; } #endif }
GrB_Info GB_to_hyper // convert a matrix to hypersparse ( GrB_Matrix A, // matrix to convert to hypersparse GB_Context Context ) { //-------------------------------------------------------------------------- // check inputs //-------------------------------------------------------------------------- // GB_subref_numeric can return a matrix with jumbled columns, since it // soon be transposed (and sorted) in GB_accum_mask. However, it passes // the jumbled matrix to GB_to_hyper_conform. This function does not // access the row indices at all, so it works fine if the columns have // jumbled row indices. ASSERT_OK_OR_JUMBLED (GB_check (A, "A converting to hypersparse", GB0)) ; #ifndef NDEBUG GrB_Info info ; #endif //-------------------------------------------------------------------------- // convert A to hypersparse form //-------------------------------------------------------------------------- if (!A->is_hyper) { ASSERT (A->h == NULL) ; ASSERT (A->nvec == A->plen && A->plen == A->vdim) ; //---------------------------------------------------------------------- // count the number of non-empty vectors in A //---------------------------------------------------------------------- int64_t *restrict Ap_old = A->p ; bool Ap_old_shallow = A->p_shallow ; int64_t n = A->vdim ; int64_t nvec_new = A->nvec_nonempty ; //---------------------------------------------------------------------- // allocate the new A->p and A->h //---------------------------------------------------------------------- int64_t *restrict Ap_new ; int64_t *restrict Ah_new ; GB_MALLOC_MEMORY (Ap_new, nvec_new+1, sizeof (int64_t)) ; GB_MALLOC_MEMORY (Ah_new, nvec_new, sizeof (int64_t)) ; if (Ap_new == NULL || Ah_new == NULL) { // out of memory A->is_hyper = true ; // A is hypersparse, but otherwise invalid GB_FREE_MEMORY (Ap_new, nvec_new+1, sizeof (int64_t)) ; GB_FREE_MEMORY (Ah_new, nvec_new, sizeof (int64_t)) ; GB_CONTENT_FREE (A) ; return (GB_OUT_OF_MEMORY (GBYTES (2*nvec_new+1, sizeof (int64_t)))); } //---------------------------------------------------------------------- // transplant the new A->p and A->h into the matrix //---------------------------------------------------------------------- // this must be done here so that GB_jappend, just below, can be used. A->is_hyper = true ; A->plen = nvec_new ; A->nvec = 0 ; A->p = Ap_new ; A->h = Ah_new ; A->p_shallow = false ; A->h_shallow = false ; //---------------------------------------------------------------------- // construct the new hyperlist in the new A->p and A->h //---------------------------------------------------------------------- int64_t jlast, anz, anz_last ; GB_jstartup (A, &jlast, &anz, &anz_last) ; for (int64_t j = 0 ; j < n ; j++) { anz = Ap_old [j+1] ; ASSERT (A->nvec <= A->plen) ; #ifndef NDEBUG info = #endif GB_jappend (A, j, &jlast, anz, &anz_last, Context) ; ASSERT (info == GrB_SUCCESS) ; ASSERT (A->nvec <= A->plen) ; } GB_jwrapup (A, jlast, anz) ; ASSERT (A->nvec == nvec_new) ; ASSERT (A->nvec_nonempty == nvec_new) ; //---------------------------------------------------------------------- // free the old A->p unless it's shallow //---------------------------------------------------------------------- // this cannot use GB_ph_free because the new A->p content has already // been placed into A, as required by GB_jappend just above. if (!Ap_old_shallow) { GB_FREE_MEMORY (Ap_old, n+1, sizeof (int64_t)) ; } } //-------------------------------------------------------------------------- // A is now in hypersparse form //-------------------------------------------------------------------------- ASSERT_OK_OR_JUMBLED (GB_check (A, "A converted to hypersparse", GB0)) ; ASSERT (A->is_hyper) ; return (GrB_SUCCESS) ; }