1 // Class to manage both stringify functions from semiring, ops and monoids to char buffers
2 // Also provides a iostream callback to deliver the buffer to jitify as if read from a file
3 
4 // (c) Nvidia Corp. 2020 All rights reserved
5 // SPDX-License-Identifier: Apache-2.0
6 
7 // Implementations of string callbacks
8 #pragma once
9 #include <iostream>
10 #include "GB.h"
11 #include "GB_binop.h"
12 #include "GB_stringify.h"
13 #include <cstdint>
14 
15 // Define function pointer we will use later
16 //std::istream* (*file_callback)(std::string, std::iostream&);
17 
18 // Define a factory class for building any buffer of text
19 class GB_cuda_stringifier {
20   char callback_buffer[4096];
21   char *callback_string;
22   const char *include_filename;
23 
24   public:
25 
26 //------------------------------------------------------------------------------
27 // load string: set string and file name to mimic
28 //------------------------------------------------------------------------------
load_string(const char * fname,char * input)29     void load_string(const char *fname, char *input)
30     {
31         callback_string = input;
32         include_filename =  fname;
33     }
34 
35 //------------------------------------------------------------------------------
36 // callback: return string as if it was read from a file
37 // this is a pointed to by a global function pointer
38 //------------------------------------------------------------------------------
39 
callback(std::string filename,std::iostream & tmp_stream)40     std::istream* callback( std::string filename, std::iostream& tmp_stream)
41     {
42         if ( filename == std::string(this->include_filename) )
43         {
44            tmp_stream << this->callback_string;
45            return &tmp_stream;
46         }
47         else
48         {
49            return nullptr;
50         }
51     }
52 
53 //------------------------------------------------------------------------------
54 // Enumify takes a set of inputs describing and operation (semiring, mask,
55 // datatypes, sparsity formats) and produces a numerical unique value for those
56 // This allows rapid lookups to see if we have handled this case before,
57 // and avoids the need to generate and manage strings at this stage.
58 //------------------------------------------------------------------------------
59 
enumify_semiring(uint64_t * scode,GrB_Semiring semiring,bool flipxy,GrB_Type ctype,GrB_Type mtype,GrB_Type atype,GrB_Type btype,bool Mask_struct,bool Mask_comp,int C_sparsity,int M_sparsity,int A_sparsity,int B_sparsity)60     void enumify_semiring
61     (
62         // output:
63         uint64_t *scode,        // unique encoding of the entire semiring
64         // input:
65         GrB_Semiring semiring,  // the semiring to enumify
66         bool flipxy,            // multiplier is: mult(a,b) or mult(b,a)
67         GrB_Type ctype,         // the type of C
68         GrB_Type mtype,         // the type of M, or NULL if no mask
69         GrB_Type atype,         // the type of A
70         GrB_Type btype,         // the type of B
71         bool Mask_struct,       // mask is structural
72         bool Mask_comp,         // mask is complemented
73         int C_sparsity,         // sparsity structure of C
74         int M_sparsity,         // sparsity structure of M
75         int A_sparsity,         // sparsity structure of A
76         int B_sparsity          // sparsity structure of B
77     )
78     {
79        GB_enumify_semiring (
80 	    // output:
81 	    scode,        // unique encoding of the entire semiring
82 	    // input:
83 	    semiring,  // the semiring to enumify
84 	    flipxy,            // multiplier is: mult(a,b) or mult(b,a)
85 	    ctype,         // the type of C
86 	    mtype,         // the type of M, or NULL if no mask
87 	    atype,         // the type of A
88 	    btype,         // the type of B
89 	    Mask_struct,       // mask is structural
90 	    Mask_comp,         // mask is complemented
91 	    C_sparsity,         // sparsity structure of C
92 	    M_sparsity,         // sparsity structure of M
93 	    A_sparsity,         // sparsity structure of A
94 	    B_sparsity          // sparsity structure of B
95        ) ;
96 
97     }
98 
macrofy_semiring(uint64_t scode)99     void macrofy_semiring
100     (
101         // input:
102         uint64_t scode          // unique encoding of the entire semiring
103     )
104     {
105        GB_macrofy_semiring (
106 	    // output:
107 	    callback_buffer,      // all macros that define the semiring
108 	    // input:
109 	    scode
110        ) ;
111 
112     }
113 
114 
115 }; // GB_cuda_stringifier
116 
117