├── .gitignore ├── .travis.yml ├── Algo ├── .gitignore ├── Include │ ├── algo_sssp_dist.h │ ├── algo_sssp_path.h │ ├── algo_util.h │ ├── sr_dist.h │ └── sr_path.h ├── Matrix │ └── house ├── Program │ ├── algo_sssp_dist_main.c │ ├── algo_sssp_path_main.c │ └── playgenerics_main.c ├── README.txt └── Source │ ├── algo_sssp_dist.c │ ├── algo_sssp_path.c │ ├── algo_util.c │ ├── sr_dist.c │ └── sr_path.c ├── CMakeLists.txt ├── LICENSE ├── Makefile ├── NOTICE ├── README.md ├── build └── .gitignore ├── ext └── gtest │ └── CMakeLists.txt ├── include └── GraphBLAS.h ├── src ├── Exception.cc ├── Exception.hh ├── GrB_BinaryOp_t.cc ├── GrB_BinaryOp_t.hh ├── GrB_Descriptor_t.cc ├── GrB_Descriptor_t.hh ├── GrB_Mask_t.cc ├── GrB_Mask_t.hh ├── GrB_Matrix_t.cc ├── GrB_Matrix_t.hh ├── GrB_Monoid_t.cc ├── GrB_Monoid_t.hh ├── GrB_Semiring_t.cc ├── GrB_Semiring_t.hh ├── GrB_Type_t.cc ├── GrB_Type_t.hh ├── GrB_UnaryOp_t.cc ├── GrB_UnaryOp_t.hh ├── GrB_Vector_t.cc ├── GrB_Vector_t.hh ├── IBM_GraphBLAS.cc ├── IBM_GraphBLAS.hh ├── Makefile ├── Scalar.cc ├── Scalar.hh └── template │ ├── GrB_Matrix_assign.cc │ ├── GrB_Matrix_reduce.cc │ ├── GrB_Matrix_t_AxB.cc │ ├── GrB_Vector_assign.cc │ ├── GrB_Vector_reduce.cc │ └── GrB_mxm.cc └── test └── src └── test_operations.cc /.gitignore: -------------------------------------------------------------------------------- 1 | # Object files 2 | *.o 3 | *.ko 4 | *.obj 5 | *.elf 6 | 7 | # Precompiled Headers 8 | *.gch 9 | *.pch 10 | 11 | # Libraries 12 | *.lib 13 | *.a 14 | *.la 15 | *.lo 16 | 17 | # Shared objects (inc. Windows DLLs) 18 | *.dll 19 | *.so 20 | *.so.* 21 | *.dylib 22 | 23 | # Executables 24 | *.exe 25 | *.out 26 | *.app 27 | *.i*86 28 | *.x86_64 29 | *.hex 30 | 31 | # Debug files 32 | *.dSYM/ 33 | *.su 34 | 35 | # for common backups 36 | *~ 37 | *.bak 38 | *.old 39 | 40 | # for mac 41 | .DS_Store 42 | 43 | # for eclipse 44 | .project -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | #https://stackoverflow.com/questions/41916656/how-to-use-travis-ci-to-build-modern-c-using-modern-cmake 2 | #x 3 | dist: xenial 4 | #xsudo: required 5 | language: 6 | - cpp 7 | #xcompiler: 8 | #x - gcc 9 | #xaddons: 10 | #x apt: 11 | #x sources: 12 | #x - ubuntu-toolchain-r-test 13 | #x packages: 14 | #x# - gcc-6 15 | #x# - g++-6 16 | #x - cmake 17 | script: 18 | #x# # Link gcc-6 and g++-6 to their standard commands 19 | #x# - ln -s /usr/bin/gcc-6 /usr/local/bin/gcc 20 | #x# - ln -s /usr/bin/g++-6 /usr/local/bin/g++ 21 | #x# # Export CC and CXX to tell cmake which compiler to use 22 | #x# - export CC=/usr/bin/gcc-6 23 | #x# - export CXX=/usr/bin/g++-6 24 | #x # Check versions of gcc, g++ and cmake 25 | - gcc -v && g++ -v && cmake --version 26 | #x # Run your build commands next 27 | - make 28 | #x 29 | - cd build 30 | - CTEST_OUTPUT_ON_FAILURE=1 ctest -VV 31 | -------------------------------------------------------------------------------- /Algo/.gitignore: -------------------------------------------------------------------------------- 1 | /playgenerics_main 2 | /algo_sssp_dist_main 3 | /algo_sssp_path_main 4 | -------------------------------------------------------------------------------- /Algo/Include/algo_sssp_dist.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | #ifndef ALGO_ALGO_SSSP_DIST_H 21 | #define ALGO_ALGO_SSSP_DIST_H 22 | #include "GraphBLAS.h" 23 | 24 | GrB_Info algo_sssp_dist // compute SSSP for source 25 | ( 26 | const GrB_Matrix A, // adjacency matrix (IN/OUT) 27 | GrB_Index source, // index of source vertex (IN) 28 | GrB_Vector* p_d, // vector loaded w/ result (IN/OUT) 29 | bool* p_no_neg_cycle // existence of negative cycle? 30 | ); 31 | 32 | #endif 33 | 34 | -------------------------------------------------------------------------------- /Algo/Include/algo_sssp_path.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | #ifndef ALGO_ALGO_SSSP_PATH_H 21 | #define ALGO_ALGO_SSSP_PATH_H 22 | #include "GraphBLAS.h" 23 | 24 | GrB_Info algo_sssp_path // compute SSSP for source 25 | ( 26 | const GrB_Matrix A, // adjacency matrix (IN/OUT) 27 | GrB_Index source, // index of source vertex (IN) 28 | GrB_Vector* p_d, // vector loaded w/ result (IN/OUT) 29 | bool* p_no_neg_cycle // existence of negative cycle? 30 | ); 31 | 32 | #endif 33 | 34 | -------------------------------------------------------------------------------- /Algo/Include/algo_util.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | #ifndef ALGO_ALGO_UTIL_H 21 | #define ALGO_ALGO_UTIL_H 22 | #include "GraphBLAS.h" 23 | 24 | // INPUT 25 | GrB_Info algo_util_get_matrix // get a matrix from stdin 26 | ( 27 | GrB_Matrix *A_output, // matrix to create 28 | char *f // file to read the tuples from 29 | ); 30 | GrB_Info algo_util_read_matrix // read a float matrix 31 | ( 32 | GrB_Matrix *A, // handle of matrix to create 33 | FILE *f // file to read the tuples from 34 | ); 35 | // OUTPUT 36 | void algo_util_print_matrix (GrB_Matrix A, char *name); 37 | void algo_util_print_vector (GrB_Vector v, char *name); 38 | 39 | #endif 40 | 41 | -------------------------------------------------------------------------------- /Algo/Include/sr_dist.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | #ifndef ALGO_SR_DIST_H 21 | #define ALGO_SR_DIST_H 22 | #include "GraphBLAS.h" 23 | #include // for INFINITY 24 | #include // for malloc/free 25 | #include // for print stuff 26 | 27 | // OUTPUT 28 | void sr_dist_print_matrix (GrB_Matrix A, char *name); 29 | void sr_dist_print_vector (GrB_Vector v, char *name); 30 | 31 | GrB_Info Dist_init ( ); 32 | GrB_Info Dist_finalize ( ); 33 | 34 | extern GrB_Monoid Dist_min_monoid; 35 | extern GrB_Semiring Dist_min_plus; 36 | extern GrB_Monoid dist_LAND_BOOL; 37 | 38 | #endif 39 | 40 | -------------------------------------------------------------------------------- /Algo/Include/sr_path.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | #ifndef ALGO_SR_PATH_H 21 | #define ALGO_SR_PATH_H 22 | #include "GraphBLAS.h" 23 | #include // for INFINITY 24 | #include // for malloc/free 25 | #include // for print stuff 26 | 27 | void sr_path_print_matrix (GrB_Matrix A, char *name); 28 | void sr_path_print_vector (GrB_Vector v, char *name); 29 | 30 | GrB_Info Path_init ( ); 31 | GrB_Info Path_finalize ( ); 32 | 33 | typedef struct path { 34 | float distance; 35 | GrB_Index hops; 36 | GrB_Index penultimate; 37 | } path; 38 | 39 | extern GrB_Type Path; 40 | extern GrB_BinaryOp Path_min, Path_plus; 41 | extern GrB_Monoid Path_min_monoid; 42 | extern GrB_Monoid Path_plus_monoid; 43 | extern GrB_Semiring Path_min_plus; 44 | extern GrB_BinaryOp Path_eq; 45 | 46 | extern const path PathMinPlusSr_ONE; 47 | extern const path PathMinPlusSr_ZERO; 48 | extern const GrB_Index PathNodeNil; 49 | #endif 50 | 51 | -------------------------------------------------------------------------------- /Algo/Matrix/house: -------------------------------------------------------------------------------- 1 | 0 1 20.0 2 | 0 2 10.0 3 | 1 3 15.0 4 | 2 3 30.0 5 | 2 4 50.0 6 | 3 4 5.0 -------------------------------------------------------------------------------- /Algo/Program/algo_sssp_dist_main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | // Read a graph from a file and find SSSP. Usage: 21 | // 22 | // sr_dist_main < infile 23 | // 24 | // Where infile has one line per edge in the graph; these have the form 25 | // 26 | // i j x 27 | // 28 | // where A(i,j)=x is performed by GrB_Matrix_build, to construct the matrix. 29 | // The dimensions of A are assumed to be the max of (largest row index, largest 30 | // column index)and column indices plus one (in algo_util_read_matrix.c). 31 | 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include "GraphBLAS.h" 38 | #include "algo_util.h" 39 | #include "algo_sssp_dist.h" 40 | #include "sr_dist.h" 41 | 42 | #define OK(method) \ 43 | { \ 44 | info = method; \ 45 | if (! (info == GrB_SUCCESS || info == GrB_NO_VALUE)) \ 46 | { \ 47 | printf ("file %s line %d\n", __FILE__, __LINE__); \ 48 | printf ("%s\n", GrB_error ( )); \ 49 | FREE_ALL; \ 50 | return (info); \ 51 | } \ 52 | } 53 | 54 | #define FREE_ALL \ 55 | GrB_free (&A); \ 56 | GrB_free (&p_d); 57 | 58 | int main (int argc, char **argv) 59 | { 60 | // process command line input 61 | int aflag = 0; 62 | int bflag = 0; 63 | char *cvalue = NULL; 64 | int index; 65 | int c; 66 | 67 | opterr = 0; 68 | 69 | while ((c = getopt (argc, argv, "abc:")) != -1) 70 | switch (c) 71 | { 72 | case 'a': 73 | aflag = 1; 74 | break; 75 | case 'b': 76 | bflag = 1; 77 | break; 78 | case 'c': 79 | cvalue = optarg; 80 | break; 81 | case '?': 82 | if (optopt == 'c') 83 | fprintf (stderr, "Option -%c requires an argument.\n", optopt); 84 | else if (isprint (optopt)) 85 | fprintf (stderr, "Unknown option `-%c'.\n", optopt); 86 | else 87 | fprintf (stderr, 88 | "Unknown option character `\\x%x'.\n", 89 | optopt); 90 | return 1; 91 | default: 92 | abort (); 93 | } 94 | 95 | printf ("aflag = %d, bflag = %d, cvalue = %s\n", 96 | aflag, bflag, cvalue); 97 | 98 | if (optind < 1) 99 | printf ("please specify file"); 100 | 101 | char * fin = argv[optind]; 102 | printf("Processing matrix from: >%s<\n", fin); 103 | 104 | GrB_Matrix A = NULL; 105 | GrB_Vector p_d = NULL; 106 | GrB_Info info; 107 | OK (GrB_init (GrB_NONBLOCKING)); 108 | 109 | // get adjacency matrix for DiGraph 110 | OK (algo_util_get_matrix (&A, fin)); 111 | 112 | // print input 113 | sr_dist_print_matrix (A, "A(input)"); 114 | 115 | // initialize Dist Semiring 116 | OK (Dist_init()); 117 | 118 | // compute SSSP 119 | GrB_Index source = 0; 120 | bool no_neg_cycle; 121 | OK (algo_sssp_dist (A, source, &p_d, &no_neg_cycle)); 122 | 123 | // print results 124 | sr_dist_print_vector (p_d, "d(result)"); 125 | if (no_neg_cycle) printf ("no neg cycle\n"); 126 | else printf("neg cycle\n"); 127 | 128 | // FREE_ALL; 129 | OK (Dist_finalize()); 130 | GrB_finalize ( ); 131 | } 132 | -------------------------------------------------------------------------------- /Algo/Program/algo_sssp_path_main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | // Read a graph from a file and find SSSP. Usage: 21 | // 22 | // sr_path_main < infile 23 | // 24 | // Where infile has one line per edge in the graph; these have the form 25 | // 26 | // i j x 27 | // 28 | // where A(i,j)=x is performed by GrB_Matrix_build, to construct the matrix. 29 | // The dimensions of A are assumed to be the max of (largest row index, largest 30 | // column index)and column indices plus one (in sr_path_read_matrix.c). 31 | 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include "algo_util.h" 38 | #include "algo_sssp_path.h" 39 | #include "sr_path.h" 40 | 41 | #define OK(method) \ 42 | { \ 43 | info = method; \ 44 | if (! (info == GrB_SUCCESS || info == GrB_NO_VALUE)) \ 45 | { \ 46 | printf ("file %s line %d\n", __FILE__, __LINE__); \ 47 | printf ("%s\n", GrB_error ( )); \ 48 | FREE_ALL; \ 49 | return (info); \ 50 | } \ 51 | } 52 | 53 | #define FREE_ALL \ 54 | GrB_free (&A); \ 55 | GrB_free (&p_d); 56 | 57 | int main (int argc, char **argv) 58 | { 59 | // process command line input 60 | int aflag = 0; 61 | int bflag = 0; 62 | char *cvalue = NULL; 63 | int index; 64 | int c; 65 | 66 | opterr = 0; 67 | 68 | while ((c = getopt (argc, argv, "abc:")) != -1) 69 | switch (c) 70 | { 71 | case 'a': 72 | aflag = 1; 73 | break; 74 | case 'b': 75 | bflag = 1; 76 | break; 77 | case 'c': 78 | cvalue = optarg; 79 | break; 80 | case '?': 81 | if (optopt == 'c') 82 | fprintf (stderr, "Option -%c requires an argument.\n", optopt); 83 | else if (isprint (optopt)) 84 | fprintf (stderr, "Unknown option `-%c'.\n", optopt); 85 | else 86 | fprintf (stderr, 87 | "Unknown option character `\\x%x'.\n", 88 | optopt); 89 | return 1; 90 | default: 91 | abort (); 92 | } 93 | 94 | printf ("aflag = %d, bflag = %d, cvalue = %s\n", 95 | aflag, bflag, cvalue); 96 | 97 | if (optind < 1) 98 | printf ("please specify file"); 99 | 100 | char * fin = argv[optind]; 101 | printf("Processing matrix from: >%s<\n", fin); 102 | GrB_Matrix A = NULL; 103 | GrB_Vector p_d = NULL; 104 | GrB_Info info; 105 | OK (GrB_init (GrB_NONBLOCKING)); 106 | 107 | // get adjacency matrix for DiGraph 108 | OK (algo_util_get_matrix (&A, fin)); 109 | 110 | // print input 111 | algo_util_print_matrix (A, "A(input)"); 112 | 113 | GrB_Index n; 114 | OK (GrB_Matrix_nrows (&n, A)); 115 | 116 | // initialize Path Semiring 117 | OK (Path_init()); 118 | 119 | // compute SSSP 120 | GrB_Index source = 0; 121 | bool no_neg_cycle; 122 | OK (algo_sssp_path(A, source, &p_d, &no_neg_cycle)); 123 | 124 | // print results 125 | sr_path_print_vector (p_d, "d(result)"); 126 | if (no_neg_cycle) printf ("no neg cycle\n"); 127 | else printf("neg cycle\n"); 128 | 129 | OK (Path_finalize()); 130 | GrB_finalize ( ); 131 | } 132 | -------------------------------------------------------------------------------- /Algo/Program/playgenerics_main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | 24 | #define typename(x) _Generic((x), /* Get the name of a type */ \ 25 | \ 26 | _Bool: "_Bool", \ 27 | unsigned char: "unsigned char", \ 28 | char: "char", \ 29 | signed char: "signed char", \ 30 | short int: "short int", \ 31 | unsigned short int: "unsigned short int", \ 32 | int: "int", \ 33 | unsigned int: "unsigned int", \ 34 | long int: "long int", \ 35 | unsigned long int: "unsigned long int", \ 36 | long long int: "long long int", \ 37 | unsigned long long int: "unsigned long long int", \ 38 | float: "float", \ 39 | double: "double", \ 40 | long double: "long double", \ 41 | char *: "pointer to char", \ 42 | void *: "pointer to void", \ 43 | int *: "pointer to int", \ 44 | default: "other") 45 | 46 | #define fmt "%20s is '%s'\n" 47 | int main() { 48 | 49 | size_t s; 50 | ptrdiff_t p; 51 | intmax_t i; 52 | int ai[3] = {0}; 53 | 54 | printf( fmt fmt fmt fmt fmt fmt fmt fmt, 55 | "size_t", typename(s), "ptrdiff_t", typename(p), 56 | "intmax_t", typename(i), "character constant", typename('0'), 57 | "0x7FFFFFFF", typename(0x7FFFFFFF), "0xFFFFFFFF", typename(0xFFFFFFFF), 58 | "0x7FFFFFFFU", typename(0x7FFFFFFFU), "array of int", typename(ai)); 59 | } 60 | -------------------------------------------------------------------------------- /Algo/README.txt: -------------------------------------------------------------------------------- 1 | SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017, All Rights Reserved. 2 | http://suitesparse.com See GraphBLAS/Doc/License.txt for license. 3 | 4 | This is the GraphBLAS/Demo folder. It contains a set of simple demo programs 5 | that illustrate the use of GraphBLAS. To compile and run the demos, see 6 | ../README.txt. 7 | 8 | -------------------------------------------------------------------------------- 9 | Files in this folder: 10 | 11 | README.txt this file 12 | demo run all demos 13 | tri_run run tricount on a set of matrices 14 | go run tricount on GraphChallenge matrices 15 | go2 run tricount on two large matrices 16 | 17 | -------------------------------------------------------------------------------- 18 | in Demo/Source: 19 | -------------------------------------------------------------------------------- 20 | 21 | bfs5m.c breadth-first search using assign-scalar 22 | bfs5m_check.c as above, but with error checking 23 | bfs6.c breadth-first serach using apply 24 | bfs6_check.c as above, but with error checking 25 | bfs_level.c assign level for bfs6 26 | get_matrix.c get a matrix (file, Wathen, or random) 27 | mis.c maximal independent set 28 | mis_check.c as above, but with error checking 29 | mis_score.c random score for mis 30 | random_matrix.c create a random matrix 31 | read_matrix.c read a matrix from a file (Matrix/*) 32 | simple_rand.c a very simple random number generator 33 | simple_timer.c a simple yet portable timer 34 | tricount.c six triangle counting methods using GraphBLAS 35 | triu.c constructs U=triu(A,1) 36 | usercomplex.c user-defined double complex type 37 | wathen.c GraphBLAS version of the MATLAB wathen.m 38 | 39 | -------------------------------------------------------------------------------- 40 | in Demo/Program: 41 | -------------------------------------------------------------------------------- 42 | 43 | bfs_demo.c demo program to test bfs 44 | complex_demo.c demo program to test complex type 45 | mis_demo.c demo program to test mis 46 | tri_demo.c demo program to test tricount 47 | simple_demo.c demo program to test simple_rand and simple_timer 48 | wildtype_demo.c demo program with arbitrary struct as user-defined type 49 | 50 | -------------------------------------------------------------------------------- 51 | in Demo/Output: 52 | -------------------------------------------------------------------------------- 53 | 54 | bfs_demo.out_ok output of bfs_demo 55 | complex_demo_out_ok.m output of complex_demo, run in MATLAB to check results 56 | mis_demo.out_ok output of mis_demo 57 | simple_test.out_ok output of simple_demo 58 | tri_demo.out_ok output of tri_demo 59 | wildtype_demo.out_ok output of wildtype_demo 60 | 61 | -------------------------------------------------------------------------------- 62 | in Demo/Include: 63 | -------------------------------------------------------------------------------- 64 | 65 | demos.h include file for all demos 66 | simple_rand.h include file for simple_rand.c 67 | simple_timer.h include file for simple_timer 68 | usercomplex.h include file for usercomplex.h 69 | 70 | -------------------------------------------------------------------------------- 71 | in Demo/MATLAB: 72 | -------------------------------------------------------------------------------- 73 | 74 | tricount.m five triangle counting methods using MATLAB 75 | adj_to_edges.m convert adjacency matrix to incidence matrix 76 | edges_to_adj.m convert incidence matrix to adjacency matrix 77 | check_adj.m check an adjaceny matrix 78 | 79 | -------------------------------------------------------------------------------- 80 | in Demo/Matrix: 81 | -------------------------------------------------------------------------------- 82 | 83 | folder with test matrices, with 0-based indices. Many of these are derived from 84 | the Harwell/Boeing matrix collection. Contains: 85 | 86 | 2blocks 87 | ash219 88 | bcsstk01 89 | bcsstk16 90 | eye3 91 | fs_183_1 92 | ibm32a 93 | ibm32b 94 | lp_afiro 95 | mbeacxc 96 | t1 97 | t2 98 | west0067 99 | 100 | -------------------------------------------------------------------------------- /Algo/Source/algo_sssp_dist.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | //------------------------------------------------------------------------------ 21 | // GraphBLAS/Demos/sr_dist.c: bellman-ford for floats 22 | //------------------------------------------------------------------------------ 23 | 24 | // Adaptation of Fineman, Jeremy T., and Eric Robinson. 25 | // "Fundamental graph algorithms." 26 | // Graph Algorithms in the Language of Linear Algebra 22 (2011): 45. 27 | 28 | #include "algo_sssp_dist.h" 29 | #include "sr_dist.h" 30 | 31 | #undef OK 32 | #define OK(method) \ 33 | { \ 34 | info = method; \ 35 | if (! (info == GrB_SUCCESS || info == GrB_NO_VALUE)) \ 36 | { \ 37 | printf ("file %s line %d\n", __FILE__, __LINE__); \ 38 | printf ("%s\n", GrB_error ( )); \ 39 | FREE_ALL; \ 40 | FREE_INNER; \ 41 | return (info); \ 42 | } \ 43 | } 44 | 45 | #undef FREE_ALL 46 | #define FREE_ALL \ 47 | GrB_free (&A); \ 48 | GrB_free (&d); 49 | 50 | 51 | #undef FREE_INNER 52 | #define FREE_INNER 53 | 54 | //------------------------------------------------------------------------------ 55 | // sr_dist: Bellman-Ford for floats 56 | //------------------------------------------------------------------------------ 57 | 58 | GrB_Info algo_sssp_dist // compute SSSP for source 59 | ( 60 | const GrB_Matrix A_in, // adjacency matrix (IN/OUT) 61 | GrB_Index source, // index of source vertex (IN) 62 | GrB_Vector* p_d, // vector loaded w/ result (OUT) 63 | bool* p_no_neg_cycle // negative cycle (OUT) 64 | ) 65 | { 66 | GrB_Info info; 67 | bool DEBUG = false; 68 | 69 | // Dist_init(); 70 | // TODO How to ensure Path has been initialized? 71 | 72 | GrB_Matrix A = GrB_NULL; 73 | GrB_Vector d = GrB_NULL; 74 | 75 | // n = # of nodes in graph 76 | GrB_Index n; 77 | GrB_Matrix_nrows (&n, A_in); 78 | 79 | // Some semiring constants ... 80 | // Does graphBLAS provide these? It should. 81 | // since it depends on C11 Spec should mention INFINITY? TODO 82 | float FloatMinPlusSr_ONE = 0.0; 83 | float FloatMinPlusSr_ZERO = INFINITY; 84 | 85 | // construct a new adjacency matrix from input adjacency matrix 86 | // note implicit change of sparsity from 0.0 to INFINITY 87 | // also, set self loop distance to 0.0 88 | OK (GrB_Matrix_new (&A, GrB_FP32, n, n)); 89 | 90 | { 91 | #undef FREE_INNER 92 | #define FREE_INNER \ 93 | GrB_free (&Diag); \ 94 | GrB_free (&desc); 95 | GrB_Matrix Diag = GrB_NULL; 96 | GrB_Descriptor desc = GrB_NULL; 97 | 98 | OK( GrB_Matrix_new (&Diag, GrB_FP32, n, n) ); 99 | for (int64_t i = 0; i < n; i++) 100 | GrB_Matrix_setElement (Diag, FloatMinPlusSr_ONE, i, i); 101 | 102 | OK( GrB_Descriptor_new (&desc) ); 103 | OK( GrB_Descriptor_set (desc, GrB_INP1, GrB_TRAN) ); 104 | OK( GrB_eWiseAdd (A, GrB_NULL, GrB_NULL, GrB_FIRST_FP32, Diag, A_in, desc)); 105 | 106 | OK(GrB_free (&Diag)); 107 | OK(GrB_free (&desc)); 108 | #undef FREE_INNER 109 | } 110 | if (DEBUG) sr_dist_print_matrix (A, "A+diag"); 111 | 112 | // Now that Adjacency matrix is all set, 113 | // use designated source (input), to initialize vector 114 | { 115 | GrB_Vector_new (&d, GrB_FP32, n); 116 | GrB_Vector_setElement (d, FloatMinPlusSr_ONE, source); 117 | } 118 | if (DEBUG) sr_dist_print_vector(d, "d_init"); 119 | 120 | // iterate 121 | { 122 | for( GrB_Index i = 0; i < n; i += 1 ) { 123 | GrB_mxv (d, GrB_NULL, GrB_NULL, Dist_min_plus, A, d, GrB_NULL); 124 | if (DEBUG) sr_dist_print_vector(d, "d_next"); 125 | } 126 | } 127 | 128 | 129 | // check for negative weight cycle 130 | bool no_neg_cycle = true; 131 | { 132 | #undef FREE_INNER 133 | #define FREE_INNER \ 134 | GrB_free (&d_check); \ 135 | GrB_free (&d_eq); 136 | GrB_Vector d_check = GrB_NULL; 137 | GrB_Vector d_eq = GrB_NULL; 138 | 139 | OK(GrB_Vector_new (&d_check, GrB_FP32, n)); 140 | // one more iteration 141 | OK(GrB_mxv (d_check, GrB_NULL, GrB_NULL, Dist_min_plus, A, d, GrB_NULL)); 142 | if (DEBUG) sr_dist_print_vector(d_check, "d_check"); 143 | 144 | // compare the two results 145 | // shouldn't spec contain an easy more efficient way to test 146 | // equivalence of two vectors (or matrices)? TODO 147 | OK(GrB_Vector_new (&d_eq, GrB_BOOL, n)); 148 | OK(GrB_eWiseAdd (d_eq, GrB_NULL, GrB_NULL, GrB_EQ_FP32, d, d_check, GrB_NULL)); 149 | 150 | OK(GrB_reduce (&no_neg_cycle, GrB_NULL, dist_LAND_BOOL, d_eq, GrB_NULL)); 151 | 152 | if (DEBUG) { 153 | if ( no_neg_cycle) printf ("no neg cycle\n"); 154 | else printf("neg cycle\n"); 155 | } 156 | 157 | OK(GrB_free (&d_check)); 158 | OK(GrB_free (&d_eq)); 159 | #undef FREE_INNER 160 | } 161 | *p_no_neg_cycle = no_neg_cycle; 162 | 163 | // return result 164 | *p_d = d; 165 | 166 | return (GrB_SUCCESS); 167 | } 168 | 169 | -------------------------------------------------------------------------------- /Algo/Source/algo_sssp_path.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | //------------------------------------------------------------------------------ 21 | // GraphBLAS/Demos/sr_path.c: bellman-ford for floats 22 | //------------------------------------------------------------------------------ 23 | 24 | // Adaptation of Fineman, Jeremy T., and Eric Robinson. 25 | // "Fundamental graph algorithms." 26 | // Graph Algorithms in the Language of Linear Algebra 22 (2011): 45. 27 | 28 | #include "algo_sssp_path.h" 29 | #include "sr_path.h" 30 | 31 | #undef MIN 32 | #undef MAX 33 | #define MIN(a,b) (((a) < (b)) ? (a) : (b)) 34 | #define MAX(a,b) (((a) > (b)) ? (a) : (b)) 35 | 36 | #undef OK 37 | #define OK(method) \ 38 | { \ 39 | info = method; \ 40 | if (! (info == GrB_SUCCESS || info == GrB_NO_VALUE)) \ 41 | { \ 42 | printf ("file %s line %d\n", __FILE__, __LINE__); \ 43 | printf ("%s\n", GrB_error ( )); \ 44 | FREE_ALL; \ 45 | FREE_INNER; \ 46 | return (info); \ 47 | } \ 48 | } 49 | 50 | #undef FREE_ALL 51 | #define FREE_ALL \ 52 | GrB_free (&A); \ 53 | GrB_free (&d); 54 | 55 | 56 | #undef FREE_INNER 57 | #define FREE_INNER 58 | 59 | GrB_Info algo_sssp_path // compute SSSP for source 60 | ( 61 | const GrB_Matrix A_in, // adjacency matrix (IN/OUT) 62 | GrB_Index source, // index of source vertex (IN) 63 | GrB_Vector* p_d, // vector loaded w/ result (OUT) 64 | bool* p_no_neg_cycle // negative cycle (OUT) 65 | ) 66 | { 67 | GrB_Info info; 68 | GrB_Index n; 69 | 70 | GrB_Matrix A = GrB_NULL; 71 | GrB_Vector d = GrB_NULL; 72 | 73 | bool DEBUG = true; 74 | 75 | // Path_init(); 76 | // TODO How to ensure Path has been initialized? 77 | 78 | // n = # of nodes in graph 79 | OK(GrB_Matrix_nrows (&n, A_in)); 80 | 81 | OK(GrB_Matrix_new (&A, Path, n, n)); 82 | 83 | // transform matrix to correct type, UGLY! TODO 84 | { 85 | #undef FREE_INNER 86 | #define FREE_INNER \ 87 | if (I != NULL) free (I); \ 88 | if (J != NULL) free (J); \ 89 | if (X_in != NULL) free (X_in); 90 | 91 | // USE OF NULL OK TODO? 92 | GrB_Index nrows, ncols, nentries; 93 | GrB_Index *I = NULL, *J = NULL; 94 | float *X_in = NULL; 95 | 96 | OK (GrB_Matrix_nvals (&nentries, A_in)); 97 | OK (GrB_Matrix_nrows (&nrows, A_in)); 98 | OK (GrB_Matrix_ncols (&ncols, A_in)); 99 | I = malloc (MAX (nentries,1) * sizeof (GrB_Index)); 100 | J = malloc (MAX (nentries,1) * sizeof (GrB_Index)); 101 | X_in = malloc (MAX (nentries,1) * sizeof (float)); 102 | OK (GrB_Matrix_extractTuples(I, J, X_in, &nentries, A_in)); 103 | 104 | // // Use GrB_Matrix_build 105 | // // target 106 | // path *X = malloc (nentries * sizeof (path)); 107 | // for (int64_t k = 0; k < nentries; k++) 108 | // X[k]= (path) {X_in[k], 1L, I[k]}; 109 | // free (X_in); 110 | // // NULL for duplicates? TODO 111 | // GrB_Matrix_build (A, I, J, (void*) X, nentries, sr_path_first); 112 | // free (X); 113 | 114 | for (int64_t k = 0; k < nentries; k++) { 115 | // printf("i: >%" PRIu64 "<, j: >%" PRIu64 "<, x: >%f<\n", I[k], J[k], X_in[k]); 116 | const path p = (path) { 117 | X_in[k], 1L, I[k] 118 | }; 119 | OK (GrB_Matrix_setElement (A, (void *)&p, J[k], I[k])); // note transpose 120 | } 121 | free (I); 122 | I = NULL; 123 | free (J); 124 | J = NULL; 125 | free (X_in); 126 | X_in = NULL; 127 | 128 | // distance to self 129 | for (int64_t k = 0; k < n; k++) 130 | OK( GrB_Matrix_setElement (A, (void *)&PathMinPlusSr_ZERO, k, k)); 131 | 132 | #undef FREE_INNER 133 | } 134 | if (DEBUG) sr_path_print_matrix (A, "Ainit"); 135 | 136 | // Now that Adjacency matrix is all set, 137 | // use designated source (input), to initialize vector 138 | { 139 | #define FREE_INNER 140 | OK(GrB_Vector_new (&d, Path, n)); 141 | OK(GrB_Vector_setElement (d, (void *)&PathMinPlusSr_ZERO, source)); 142 | #undef FREE_INNER 143 | } 144 | if (DEBUG) sr_path_print_vector(d, "d_init"); 145 | 146 | // iterate 147 | { 148 | #define FREE_INNER 149 | for( GrB_Index i = 0; i < n; i += 1 ) { 150 | OK(GrB_mxv (d, GrB_NULL, GrB_NULL, Path_min_plus, A, d, GrB_NULL)); 151 | if (DEBUG) sr_path_print_vector(d, "d_next"); 152 | } 153 | #undef FREE_INNER 154 | } 155 | 156 | 157 | // check for negative weight cycle 158 | bool no_neg_cycle; 159 | { 160 | #undef FREE_INNER 161 | #define FREE_INNER \ 162 | GrB_free (&d_check); \ 163 | GrB_free (&d_eq); 164 | GrB_Vector d_check = GrB_NULL; 165 | GrB_Vector d_eq = GrB_NULL; 166 | 167 | OK(GrB_Vector_new (&d_check, Path, n)); 168 | // one more iteration 169 | OK(GrB_mxv (d_check, GrB_NULL, GrB_NULL, Path_min_plus, A, d, GrB_NULL)); 170 | if (DEBUG) sr_path_print_vector(d_check, "d_check"); 171 | // compare the two results 172 | // shouldn't spec contain an easy more efficient way to test 173 | // equivalence of two vectors (or matrices)? TODO 174 | OK(GrB_Vector_new (&d_eq, Path, n)); 175 | OK(GrB_eWiseAdd (d_eq, GrB_NULL, GrB_NULL, Path_eq, d, d_check, GrB_NULL)); 176 | if (DEBUG) sr_path_print_vector(d_eq, "d_eq"); 177 | 178 | path sum = PathMinPlusSr_ZERO; 179 | OK(GrB_reduce (&sum, GrB_NULL, Path_plus_monoid, d_eq, GrB_NULL)); 180 | no_neg_cycle = (sum.hops == 0L); 181 | 182 | if (DEBUG) { 183 | if ( no_neg_cycle) printf ("no neg cycle\n"); 184 | else printf("neg cycle\n"); 185 | } 186 | 187 | OK(GrB_free (&d_check)); 188 | OK(GrB_free (&d_eq)); 189 | } 190 | *p_no_neg_cycle = no_neg_cycle; 191 | 192 | // return result 193 | *p_d = d; 194 | 195 | return (GrB_SUCCESS); 196 | } 197 | -------------------------------------------------------------------------------- /Algo/Source/algo_util.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | #include 21 | #include "sr_dist.h" 22 | 23 | #undef MIN 24 | #undef MAX 25 | #define MIN(a,b) (((a) < (b)) ? (a) : (b)) 26 | #define MAX(a,b) (((a) > (b)) ? (a) : (b)) 27 | 28 | #define OK(method) \ 29 | { \ 30 | info = method; \ 31 | if (! (info == GrB_SUCCESS || info == GrB_NO_VALUE)) \ 32 | { \ 33 | printf ("file %s line %d\n", __FILE__, __LINE__); \ 34 | printf ("%s\n", GrB_error ( )); \ 35 | FREE_ALL; \ 36 | return (info); \ 37 | } \ 38 | } 39 | 40 | 41 | #undef FREE_ALL 42 | #define FREE_ALL \ 43 | if (I != NULL) free (I); \ 44 | if (J != NULL) free (J); \ 45 | if (X != NULL) free (X); \ 46 | if (I2 != NULL) free (I2); \ 47 | if (J2 != NULL) free (J2); \ 48 | if (X2 != NULL) free (X2); \ 49 | GrB_free (&C); 50 | 51 | //------------------------------------------------------------------------------ 52 | // read a matrix from a file 53 | //------------------------------------------------------------------------------ 54 | 55 | GrB_Info algo_util_read_matrix // read a float matrix 56 | ( 57 | GrB_Matrix *A_output, // handle of matrix to create 58 | FILE *f 59 | ) 60 | { 61 | 62 | int64_t len = 256; 63 | int64_t ntuples = 0; 64 | int64_t i, j; 65 | float x; 66 | 67 | //-------------------------------------------------------------------------- 68 | // set all pointers to NULL so that FREE_ALL can free everything safely 69 | //-------------------------------------------------------------------------- 70 | 71 | GrB_Matrix C = NULL, A = NULL, B = NULL; 72 | 73 | //-------------------------------------------------------------------------- 74 | // allocate initial space for tuples 75 | //-------------------------------------------------------------------------- 76 | 77 | GrB_Index *I = malloc (len * sizeof (int64_t)), *I2 = NULL; 78 | GrB_Index *J = malloc (len * sizeof (int64_t)), *J2 = NULL; 79 | float *X = malloc (len * sizeof (float )), *X2 = NULL; 80 | if (I == NULL || J == NULL || X == NULL) 81 | { 82 | // out of memory 83 | printf ("out of memory for initial tuples\n"); 84 | FREE_ALL; 85 | return (GrB_OUT_OF_MEMORY); 86 | } 87 | 88 | //-------------------------------------------------------------------------- 89 | // read in the tuples from file, one per line 90 | //-------------------------------------------------------------------------- 91 | 92 | while (fscanf (f, "%" PRIu64 " %" PRIu64 " %g\n", &i, &j, &x) != EOF) 93 | { 94 | if (ntuples >= len) 95 | { 96 | I2 = realloc (I, 2 * len * sizeof (int64_t)); 97 | J2 = realloc (J, 2 * len * sizeof (int64_t)); 98 | X2 = realloc (X, 2 * len * sizeof (float )); 99 | if (I2 == NULL || J2 == NULL || X2 == NULL) 100 | { 101 | printf ("out of memory for tuples\n"); 102 | FREE_ALL; 103 | return (GrB_OUT_OF_MEMORY); 104 | } 105 | I = I2; 106 | I2 = NULL; 107 | J = J2; 108 | J2 = NULL; 109 | X = X2; 110 | X2 = NULL; 111 | len = len * 2; 112 | } 113 | I [ntuples] = i; 114 | J [ntuples] = j; 115 | X [ntuples] = x; 116 | ntuples++; 117 | } 118 | 119 | //-------------------------------------------------------------------------- 120 | // find the dimensions 121 | //-------------------------------------------------------------------------- 122 | 123 | printf ("ntuples: %" PRIu64 "\n", ntuples); 124 | int64_t nrows = 0; 125 | int64_t ncols = 0; 126 | for (int64_t k = 0; k < ntuples; k++) 127 | { 128 | nrows = MAX (nrows, I [k]); 129 | ncols = MAX (ncols, J [k]); 130 | } 131 | nrows++; 132 | ncols++; 133 | 134 | int64_t n = MAX (nrows, ncols); 135 | printf ("n %" PRIu64 "\n", n); 136 | 137 | double tic [2], t1, t2; 138 | 139 | //-------------------------------------------------------------------------- 140 | // build the matrix, summing up duplicates, and then free the tuples 141 | //-------------------------------------------------------------------------- 142 | 143 | GrB_Info info; 144 | OK (GrB_Matrix_new (&C, GrB_FP32, n, n)); 145 | OK (GrB_Matrix_build (C, I, J, X, ntuples, GrB_PLUS_FP32)); 146 | 147 | free (I); 148 | I = NULL; 149 | free (J); 150 | J = NULL; 151 | free (X); 152 | X = NULL; 153 | 154 | //-------------------------------------------------------------------------- 155 | // create the output matrix 156 | //-------------------------------------------------------------------------- 157 | 158 | 159 | //---------------------------------------------------------------------- 160 | // return the matrix as-is 161 | //---------------------------------------------------------------------- 162 | 163 | printf ("leave A as-is\n"); 164 | *A_output = C; 165 | // set C to NULL so the FREE_ALL macro does not free *A_output 166 | C = NULL; 167 | 168 | //-------------------------------------------------------------------------- 169 | // success: free everything except the result, and return it to the caller 170 | //-------------------------------------------------------------------------- 171 | 172 | FREE_ALL; 173 | return (GrB_SUCCESS); 174 | } 175 | // INPUT 176 | #undef FREE_ALL 177 | #define FREE_ALL \ 178 | GrB_free (&A); 179 | GrB_Info algo_util_get_matrix // get a matrix from f 180 | ( 181 | GrB_Matrix *A_output, // matrix to create 182 | char *fin // input file 183 | ) 184 | { 185 | 186 | GrB_Info info; 187 | GrB_Index nrows = 1, ncols = 1, nvals; 188 | GrB_Matrix A = NULL; 189 | //---------------------------------------------------------------------- 190 | // read a matrix from fin 191 | //---------------------------------------------------------------------- 192 | 193 | // usage: ./main fspec 194 | FILE *finp; 195 | 196 | finp = fopen(fin, "r"); 197 | 198 | OK (algo_util_read_matrix (&A, finp)); 199 | fclose(finp); 200 | 201 | OK (GrB_Matrix_nrows (&nrows, A)); 202 | OK (GrB_Matrix_ncols (&ncols, A)); 203 | OK (GrB_Matrix_nvals (&nvals, A)); 204 | 205 | printf ("matrix %" PRIu64 " by %" PRIu64 ", %" PRIu64 " entries, from >%s<\n", 206 | nrows, ncols, nvals, fin); 207 | 208 | *A_output = A; 209 | A = NULL; 210 | return (GrB_SUCCESS); 211 | } 212 | 213 | // OUTPUT 214 | #undef FREE_ALL 215 | //------------------------------------------------------------------------------ 216 | // print a vector 217 | //------------------------------------------------------------------------------ 218 | 219 | void algo_util_print_vector (GrB_Vector v, char *name) 220 | { 221 | GrB_Index nrows, ncols, nentries; 222 | 223 | GrB_Vector_nvals (&nentries, v); 224 | GrB_Vector_size (&nrows, v); 225 | 226 | printf ("\n%% GraphBLAS vector %s: nrows: %" PRIu64 " entries: %" PRIu64 "\n", 227 | name, nrows, nentries); 228 | 229 | GrB_Index *I = malloc (MAX (nentries,1) * sizeof (GrB_Index)); 230 | float *X = malloc (MAX (nentries,1) * sizeof (float)); 231 | GrB_Vector_extractTuples_FP32 (I, X, &nentries, v); 232 | 233 | printf ("%s = sparse (%" PRIu64 ");\n", name, nrows); 234 | for (int64_t k = 0; k < nentries; k++) 235 | { 236 | printf (" %s (%" PRIu64 ") = (%6.2f);\n", 237 | name, I [k], X [k]); 238 | } 239 | printf ("%s\n", name); 240 | 241 | free (I); 242 | free (X); 243 | } 244 | 245 | //------------------------------------------------------------------------------ 246 | // print a matrix 247 | //------------------------------------------------------------------------------ 248 | 249 | void algo_util_print_matrix (GrB_Matrix A, char *name) 250 | { 251 | GrB_Index nrows, ncols, nentries; 252 | 253 | GrB_Matrix_nvals (&nentries, A); 254 | GrB_Matrix_nrows (&nrows, A); 255 | GrB_Matrix_ncols (&ncols, A); 256 | 257 | printf ("\n%% GraphBLAS matrix %s: nrows: %" PRIu64 " ncols %" PRIu64 " entries: %" PRIu64 "\n", 258 | name, nrows, ncols, nentries); 259 | 260 | GrB_Index *I = malloc (MAX (nentries,1) * sizeof (GrB_Index)); 261 | GrB_Index *J = malloc (MAX (nentries,1) * sizeof (GrB_Index)); 262 | float *X = malloc (MAX (nentries,1) * sizeof (float)); 263 | 264 | // GrB_Info GrB_Matrix_extractTuples_FP32 // [I,J,X] = find (A) 265 | // ( 266 | // GrB_Index *I, // array for returning row indices of tuples 267 | // GrB_Index *J, // array for returning col indices of tuples 268 | // float *X, // array for returning values of tuples 269 | // GrB_Index *nvals, // I,J,X size on input; # tuples on output 270 | // const GrB_Matrix A // matrix to extract tuples from 271 | // ); 272 | // GrB_Matrix_extractTuples_UDT (I, J, X, &nentries, A); 273 | GrB_Matrix_extractTuples_FP32 (I, J, X, &nentries, A); 274 | 275 | printf ("%s = sparse (%" PRIu64 ",%" PRIu64 ");\n", name, nrows, ncols); 276 | for (int64_t k = 0; k < nentries; k++) 277 | { 278 | // printf (" %s (%" PRIu64 ",%" PRIu64 ") = (%20.16g) + (%20.16g)*1i;\n", 279 | printf (" %s (%" PRIu64 ",%" PRIu64 ") = (%6.2f);\n", 280 | name, I [k], J [k], X [k]); 281 | // name, 1 + I [k], 1 + J [k], creal (X [k]), cimag (X [k])); 282 | } 283 | printf ("%s\n", name); 284 | 285 | free (I); 286 | free (J); 287 | free (X); 288 | } 289 | 290 | -------------------------------------------------------------------------------- /Algo/Source/sr_dist.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | #include "sr_dist.h" 21 | #include "algo_util.h" 22 | 23 | #undef MIN 24 | #undef MAX 25 | #define MIN(a,b) (((a) < (b)) ? (a) : (b)) 26 | #define MAX(a,b) (((a) > (b)) ? (a) : (b)) 27 | 28 | // OUTPUT 29 | //------------------------------------------------------------------------------ 30 | // print a vector 31 | //------------------------------------------------------------------------------ 32 | 33 | void sr_dist_print_vector (GrB_Vector v, char *name) 34 | { 35 | algo_util_print_vector(v, name); 36 | } 37 | 38 | //------------------------------------------------------------------------------ 39 | // print a matrix 40 | //------------------------------------------------------------------------------ 41 | 42 | void sr_dist_print_matrix (GrB_Matrix A, char *name) 43 | { 44 | algo_util_print_matrix(A, name); 45 | } 46 | 47 | // Semirings 48 | GrB_Monoid Dist_min_monoid = NULL; // Min monoid 49 | GrB_Semiring Dist_min_plus ; // min.plus semiring 50 | 51 | // Monoid for compare 52 | GrB_Monoid dist_LAND_BOOL = NULL; 53 | 54 | #undef OK 55 | #define OK(method) \ 56 | info = method; \ 57 | if (info != GrB_SUCCESS) \ 58 | { \ 59 | Dist_finalize ( ); \ 60 | return (info); \ 61 | } 62 | GrB_Info Dist_init ( ) 63 | { 64 | GrB_Info info; 65 | OK (GrB_Monoid_new(&Dist_min_monoid ,GrB_MIN_FP32, INFINITY )); 66 | OK (GrB_Semiring_new(&Dist_min_plus , Dist_min_monoid ,GrB_PLUS_FP32)); 67 | 68 | OK (GrB_Monoid_new_BOOL(&dist_LAND_BOOL ,GrB_LAND, true)); 69 | return (GrB_SUCCESS); 70 | } 71 | GrB_Info Dist_finalize ( ) 72 | { 73 | //-------------------------------------------------------------------------- 74 | // free the Dist min-plus semiring 75 | //-------------------------------------------------------------------------- 76 | 77 | // TODO what happens if you free monoids before semiring 78 | GrB_free (&Dist_min_plus); 79 | 80 | //-------------------------------------------------------------------------- 81 | // free the Dist monoid 82 | //-------------------------------------------------------------------------- 83 | 84 | GrB_free (&Dist_min_monoid ); 85 | 86 | //-------------------------------------------------------------------------- 87 | // free the compare binaryOp 88 | //-------------------------------------------------------------------------- 89 | 90 | GrB_free (&dist_LAND_BOOL ); 91 | 92 | return (GrB_SUCCESS); 93 | } 94 | -------------------------------------------------------------------------------- /Algo/Source/sr_path.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | //------------------------------------------------------------------------------ 21 | // GraphBLAS/Demos/sr_path.c: bellman-ford for floats 22 | //------------------------------------------------------------------------------ 23 | 24 | // Adaptation of Fineman, Jeremy T., and Eric Robinson. 25 | // "Fundamental graph algorithms." 26 | // Graph Algorithms in the Language of Linear Algebra 22 (2011): 45. 27 | 28 | #include 29 | #include 30 | #include 31 | #include "sr_path.h" 32 | 33 | #undef MIN 34 | #undef MAX 35 | #define MIN(a,b) (((a) < (b)) ? (a) : (b)) 36 | #define MAX(a,b) (((a) > (b)) ? (a) : (b)) 37 | 38 | // INPUT 39 | // NONE 40 | 41 | // OUTPUT 42 | void sr_path_print_vector (GrB_Vector v, char *name) 43 | { 44 | GrB_Index nrows, ncols, nentries; 45 | 46 | GrB_Vector_nvals (&nentries, v); 47 | GrB_Vector_size (&nrows, v); 48 | 49 | printf ("\n%% GraphBLAS vector %s: nrows: %" PRIu64 " entries: %" PRIu64 "\n", 50 | name, nrows, nentries); 51 | 52 | GrB_Index *I = malloc (MAX (nentries,1) * sizeof (GrB_Index)); 53 | path *X = malloc (MAX (nentries,1) * sizeof (path)); 54 | GrB_Vector_extractTuples_UDT (I, X, &nentries, v); 55 | 56 | printf ("%s = sparse (%" PRIu64 ");\n", name, nrows); 57 | for (int64_t k = 0; k < nentries; k++) 58 | { 59 | char str[40]; 60 | if (X[k].penultimate == PathNodeNil) 61 | strcpy(str,"Nil"); 62 | else 63 | sprintf(str, "%" PRIu64 "", X[k].penultimate); 64 | printf (" %s (%" PRIu64 ") = (%6.2f, %" PRIu64 ", %1s);\n", 65 | name, I[k], X[k].distance, X[k].hops, str); 66 | } 67 | printf ("%s\n", name); 68 | 69 | free (I); 70 | free (X); 71 | } 72 | 73 | void sr_path_print_matrix (GrB_Matrix A, char *name) 74 | { 75 | GrB_Index nrows, ncols, nentries; 76 | 77 | GrB_Matrix_nvals (&nentries, A); 78 | GrB_Matrix_nrows (&nrows, A); 79 | GrB_Matrix_ncols (&ncols, A); 80 | 81 | printf ("\n%% GraphBLAS matrix %s: nrows: %" PRIu64 " ncols %" PRIu64 " entries: %" PRIu64 "\n", 82 | name, nrows, ncols, nentries); 83 | 84 | GrB_Index *I = malloc (MAX (nentries,1) * sizeof (GrB_Index)); 85 | GrB_Index *J = malloc (MAX (nentries,1) * sizeof (GrB_Index)); 86 | path *X = malloc (MAX (nentries,1) * sizeof (path)); 87 | 88 | // GrB_Info GrB_Matrix_extractTuples_FP32 // [I,J,X] = find (A) 89 | // ( 90 | // GrB_Index *I, // array for returning row indices of tuples 91 | // GrB_Index *J, // array for returning col indices of tuples 92 | // float *X, // array for returning values of tuples 93 | // GrB_Index *nvals, // I,J,X size on input; # tuples on output 94 | // const GrB_Matrix A // matrix to extract tuples from 95 | // ); 96 | GrB_Matrix_extractTuples_UDT (I, J, X, &nentries, A); 97 | // GrB_Matrix_extractTuples_FP32 (I, J, X, &nentries, A); 98 | 99 | printf ("%s = sparse (%" PRIu64 ",%" PRIu64 ");\n", name, nrows, ncols); 100 | for (int64_t k = 0; k < nentries; k++) 101 | { 102 | // printf (" %s (%" PRIu64 ",%" PRIu64 ") = (%20.16g) + (%20.16g)*1i;\n", 103 | char str[40]; 104 | if (X[k].penultimate == PathNodeNil) 105 | strcpy(str,"Nil"); 106 | else 107 | sprintf(str, "%" PRIu64 "", X[k].penultimate); 108 | printf (" %s (%" PRIu64 ",%" PRIu64 ") = (%6.2f, %" PRIu64 ", %1s);\n", 109 | name, I[k], J[k], X[k].distance, X[k].hops, str); 110 | // name, 1 + I [k], 1 + J [k], creal (X [k]), cimag (X [k])); 111 | } 112 | printf ("%s\n", name); 113 | 114 | free (I); 115 | free (J); 116 | free (X); 117 | } 118 | 119 | 120 | // Semirings 121 | GrB_Type Path = NULL; 122 | GrB_BinaryOp Path_min = NULL, Path_plus = NULL; 123 | GrB_Monoid Path_min_monoid = NULL, Path_plus_monoid = NULL; 124 | GrB_Semiring Path_min_plus = NULL; 125 | const GrB_Index PathNodeNil = ULONG_MAX - 1L; 126 | const path PathMinPlusSr_ONE = {INFINITY, ULONG_MAX, ULONG_MAX}; 127 | const path PathMinPlusSr_ZERO = {0.0, 0L, ULONG_MAX - 1L}; 128 | 129 | GrB_BinaryOp Path_eq = NULL; 130 | 131 | // for convenience 132 | #define P struct path 133 | #define X *x 134 | #define Y *y 135 | #define Z *z 136 | 137 | //------------------------------------------------------------------------------ 138 | // sr_path: Bellman-Ford with path 139 | //------------------------------------------------------------------------------ 140 | 141 | void sr_path_min (P Z, const P X, const P Y) 142 | { 143 | // printf("sr_path_min: X.distance: >%f<, X.hops: >%" PRIu64 "<, X.penultimate: >%" PRIu64 "<\n", x->distance, x->hops, x->penultimate); 144 | // printf("sr_path_min: Y.distance: >%f<, Y.hops: >%" PRIu64 "<, Y.penultimate: >%" PRIu64 "<\n", y->distance, y->hops, y->penultimate); 145 | if (x->distance < y->distance) 146 | Z = X; 147 | else if ((x->distance == y->distance) && (x->hops < y->hops)) 148 | Z = X; 149 | else if ((x->distance == y->distance) && (x->hops == y->hops) && ((y->penultimate != PathNodeNil) && (x->penultimate < y->penultimate))) 150 | Z = X; 151 | else 152 | Z = Y; 153 | // printf("sr_path_min: Z.distance: >%f<, Z.hops: >%" PRIu64 "<, Z.penultimate: >%" PRIu64 "<\n", z->distance, z->hops, z->penultimate); 154 | } 155 | 156 | void sr_path_eq (P Z, const P X, const P Y) 157 | { 158 | if (x->distance != y->distance) 159 | Z = PathMinPlusSr_ONE; 160 | else if (x->hops != y->hops) 161 | Z = PathMinPlusSr_ONE; 162 | else if (x->penultimate < y->penultimate) 163 | Z = PathMinPlusSr_ONE; 164 | else 165 | Z = PathMinPlusSr_ZERO; 166 | } 167 | 168 | static GrB_Index sr_path_g(GrB_Index x, GrB_Index y) 169 | { 170 | if (x == ULONG_MAX || y == ULONG_MAX) 171 | return ULONG_MAX; 172 | else 173 | return x + y; 174 | } 175 | 176 | void sr_path_plus (P Z, const P Y, const P X) 177 | { 178 | float w1 = x->distance; 179 | GrB_Index h1 = x->hops; 180 | GrB_Index p1 = x->penultimate; 181 | float w2 = y->distance; 182 | GrB_Index h2 = y->hops; 183 | GrB_Index p2 = y->penultimate; 184 | // printf("sr_path_plus: w1: >%f<, h1: >%" PRIu64 "<, p1: >%" PRIu64 "<\n", w1, h1, p1); 185 | // printf("sr_path_plus: w2: >%f<, h2: >%" PRIu64 "<, p2: >%" PRIu64 "<\n", w2, h2, p2); 186 | if (p2 != PathMinPlusSr_ZERO.penultimate) 187 | if (p1 == PathMinPlusSr_ZERO.penultimate) { 188 | z->distance = w1 + w2; 189 | z->hops = sr_path_g(h1, h2); 190 | z->penultimate = p2; 191 | } // Z = (path){w1 + w2, sr_path_g(h1, h2), p2}; 192 | else if (p2 != PathNodeNil) { 193 | z->distance = w1 + w2; 194 | z->hops = sr_path_g(h1, h2); 195 | z->penultimate = p2; 196 | } // Z = (path){w1 + w2, sr_path_g(h1, h2), p2}; 197 | else { 198 | z->distance = w1 + w2; 199 | z->hops = sr_path_g(h1, h2); 200 | z->penultimate = p1; 201 | } // Z = (path){w1 + w2, sr_path_g(h1, h2), p1}; // original 202 | else { 203 | z->distance = w1 + w2; 204 | z->hops = sr_path_g(h1, h2); 205 | z->penultimate = p1; 206 | } // Z = (path){w1 + w2, sr_path_g(h1, h2), p1}; 207 | // printf("sr_path_plus: Z.distance: >%f<, Z.hops: >%" PRIu64 "<, Z.penultimate: >%" PRIu64 "<\n", z->distance, z->hops, z->penultimate); 208 | } 209 | 210 | #undef OK 211 | #define OK(method) \ 212 | info = method; \ 213 | if (info != GrB_SUCCESS) \ 214 | { \ 215 | Path_finalize ( ); \ 216 | return (info); \ 217 | } 218 | GrB_Info Path_init ( ) 219 | { 220 | GrB_Info info; 221 | OK (GrB_Type_new (&Path, sizeof(path))); 222 | 223 | OK (GrB_BinaryOp_new (&Path_min, (void (*)(void*,const void*,const void*))sr_path_min, Path, Path, Path)); 224 | OK (GrB_Monoid_new_UDT (&Path_min_monoid, Path_min, &PathMinPlusSr_ONE)); 225 | OK (GrB_BinaryOp_new (&Path_plus, (void (*)(void*,const void*,const void*))sr_path_plus, Path, Path, Path)); 226 | OK (GrB_Monoid_new_UDT (&Path_plus_monoid, Path_min, &PathMinPlusSr_ONE)); 227 | 228 | OK (GrB_BinaryOp_new (&Path_eq, (void (*)(void*,const void*,const void*))sr_path_eq, Path, Path, Path)); 229 | 230 | // the semiring 231 | OK (GrB_Semiring_new 232 | (&Path_min_plus, Path_min_monoid, Path_plus)); 233 | 234 | return (GrB_SUCCESS); 235 | } 236 | GrB_Info Path_finalize ( ) 237 | { 238 | //-------------------------------------------------------------------------- 239 | // free the Path Type 240 | //-------------------------------------------------------------------------- 241 | 242 | GrB_free (&Path); 243 | 244 | //-------------------------------------------------------------------------- 245 | // free the Path min-plus semiring 246 | //-------------------------------------------------------------------------- 247 | 248 | // TODO what happens if you free monoids before semiring 249 | GrB_free (&Path_min_plus); 250 | 251 | //-------------------------------------------------------------------------- 252 | // free the Path monoids 253 | //-------------------------------------------------------------------------- 254 | 255 | GrB_free (&Path_min_monoid ); 256 | 257 | //-------------------------------------------------------------------------- 258 | // free the Path binary operators, CxC->C 259 | //-------------------------------------------------------------------------- 260 | 261 | GrB_free (&Path_min ); 262 | GrB_free (&Path_plus); 263 | GrB_free (&Path_eq); 264 | 265 | return (GrB_SUCCESS); 266 | } 267 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # IBMGraphBLAS, IBM, (c) 2018, All Rights Reserved. 2 | # https://github.ibm.com/scalable-systems/ibm-graphblas See LICENSE for license. 3 | # 4 | # CMakeLists.txt: instructions for cmake to build IBMGraphBLAS. 5 | # 6 | # If necessary, to change your compiler, for example: 7 | # 8 | # CC=icc cmake .. 9 | # CC=xlc cmake .. 10 | # CC=gcc cmake .. 11 | # 12 | # To remove all compiled files and libraries (except installed ones): 13 | # 14 | # cd build 15 | # rm -rf * 16 | 17 | # cmake 3.0 is required. 18 | cmake_minimum_required ( VERSION 3.0.0 ) 19 | 20 | set(PROJECT_NAME_STR ibmgraphblas) 21 | 22 | project ( ${PROJECT_NAME_STR} C CXX) 23 | 24 | if ( CMAKE_VERSION VERSION_GREATER "3.0" ) 25 | cmake_policy ( SET CMP0042 NEW ) 26 | endif ( ) 27 | 28 | if (NOT CMAKE_BUILD_TYPE ) 29 | set ( CMAKE_BUILD_TYPE Release ) 30 | endif ( ) 31 | 32 | # For development only: 33 | set ( CMAKE_BUILD_TYPE Debug ) 34 | 35 | set ( CMAKE_INCLUDE_CURRENT_DIR ON ) 36 | 37 | # include directories for both ibmgraphblas, ibmgraphblasdemo, and ibmgraphblasalgo libraries 38 | include_directories ( include Algo/Include) 39 | 40 | # check which compiler is being used. If you need to make 41 | # compiler-specific modifications, here is the place to do it. 42 | if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") 43 | # set (CMAKE_C_FLAGS "-lm -pedantic-errors") 44 | set (CMAKE_CXX_FLAGS "-fno-elide-constructors -fdump-tree-original -Wall -pedantic-errors -std=c++11") 45 | set (CMAKE_CXX_FLAGS_DEBUG "-g -O2") 46 | set (CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG") 47 | if (CMAKE_C_COMPILER_VERSION VERSION_LESS 4.9) 48 | message (FATAL_ERROR "gcc version must be at least 4.9") 49 | endif ( ) 50 | #elseif ("${CMAKE_C_COMPILER_ID}" STREQUAL "Intel") 51 | # # options for icc: also needs -std=c11 52 | # set (CMAKE_C_FLAGS "-std=c11") 53 | # if (CMAKE_C_COMPILER_VERSION VERSION_LESS 18.0) 54 | # message (FATAL_ERROR "icc version must be at least 18.0") 55 | # endif ( ) 56 | #elseif ("${CMAKE_C_COMPILER_ID}" STREQUAL "Clang") 57 | # # options for clang 58 | # if (CMAKE_C_COMPILER_VERSION VERSION_LESS 3.3) 59 | # message (FATAL_ERROR "clang version must be at least 3.3") 60 | # endif ( ) 61 | #elseif ("${CMAKE_C_COMPILER_ID}" STREQUAL "MSVC") 62 | # # options for MicroSoft Visual Studio 63 | endif ( ) 64 | 65 | # create the ibmgraphblas library. Requires ANSI C11++ 66 | file ( GLOB PLUGGRAPHBLAS_SOURCES "src/*.cc" ) 67 | add_library ( ibmgraphblas SHARED ${PLUGGRAPHBLAS_SOURCES} ) 68 | SET_TARGET_PROPERTIES ( ibmgraphblas PROPERTIES VERSION 0.0.1 69 | SOVERSION 1 70 | CXX_STANDARD_REQUIRED ON 71 | PUBLIC_HEADER "include/GraphBLAS.h" ) 72 | set_property ( TARGET ibmgraphblas PROPERTY CXX_STANDARD 11 ) 73 | 74 | # ibmgraphblas installation location 75 | install ( TARGETS ibmgraphblas 76 | LIBRARY DESTINATION /usr/local/lib 77 | ARCHIVE DESTINATION /usr/local/lib 78 | PUBLIC_HEADER DESTINATION /usr/local/include ) 79 | 80 | # Demo library 81 | #file ( GLOB DEMO_SOURCES "Demo/Source/*.c" ) 82 | #add_library ( ibmgraphblasdemo SHARED ${DEMO_SOURCES} ) 83 | #SET_TARGET_PROPERTIES ( ibmgraphblasdemo PROPERTIES 84 | # C_STANDARD_REQUIRED 11 ) 85 | #set_property ( TARGET graphblasdemo PROPERTY C_STANDARD 11 ) 86 | 87 | #target_link_libraries ( ibmgraphblasdemo ibmgraphblas ) 88 | 89 | # Demo programs 90 | #add_executable ( bfs_demo "Demo/Program/bfs_demo.c" ) 91 | #add_executable ( tri_demo "Demo/Program/tri_demo.c" ) 92 | #add_executable ( mis_demo "Demo/Program/mis_demo.c" ) 93 | #add_executable ( complex_demo "Demo/Program/complex_demo.c" ) 94 | #add_executable ( simple_demo "Demo/Program/simple_demo.c" ) 95 | #add_executable ( wildtype_demo "Demo/Program/wildtype_demo.c" ) 96 | 97 | # Libraries required for Demo programs 98 | #target_link_libraries ( bfs_demo ibmgraphblas ibmgraphblasdemo ) 99 | #target_link_libraries ( tri_demo ibmgraphblas ibmgraphblasdemo ) 100 | #target_link_libraries ( mis_demo ibmgraphblas ibmgraphblasdemo ) 101 | #target_link_libraries ( complex_demo ibmgraphblas ibmgraphblasdemo ) 102 | #target_link_libraries ( simple_demo ibmgraphblasdemo ) 103 | #target_link_libraries ( wildtype_demo ibmgraphblas ) 104 | 105 | # :bh: 106 | # Algo library 107 | file ( GLOB ALGO_SOURCES "Algo/Source/*.c" ) 108 | add_library ( ibmgraphblasalgo SHARED ${ALGO_SOURCES} ) 109 | SET_TARGET_PROPERTIES ( ibmgraphblasalgo PROPERTIES 110 | C_STANDARD_REQUIRED ON ) 111 | set_property ( TARGET ibmgraphblasalgo PROPERTY C_STANDARD 11 ) 112 | 113 | target_link_libraries ( ibmgraphblasalgo ibmgraphblas ) 114 | 115 | # Algo programs 116 | add_executable ( algo_sssp_dist_main "Algo/Program/algo_sssp_dist_main.c" ) 117 | add_executable ( algo_sssp_path_main "Algo/Program/algo_sssp_path_main.c" ) 118 | 119 | # Libraries required for Algo programs 120 | target_link_libraries ( algo_sssp_dist_main ibmgraphblas ibmgraphblasalgo ) 121 | target_link_libraries ( algo_sssp_path_main ibmgraphblas ibmgraphblasalgo ) 122 | # :bh: 123 | 124 | #------------------- 125 | # Test 126 | #------------------- 127 | 128 | find_package(Threads REQUIRED) 129 | add_subdirectory(${PROJECT_SOURCE_DIR}/ext/gtest) 130 | set(EXT_PROJECTS_DIR ${PROJECT_SOURCE_DIR}/ext) 131 | enable_testing() 132 | set(PROJECT_TEST_NAME ${PROJECT_NAME_STR}_test) 133 | include_directories(${GTEST_INCLUDE_DIRS} ${COMMON_INCLUDES}) 134 | 135 | file(GLOB TEST_SRC_FILES ${PROJECT_SOURCE_DIR}/test/src/*.cc) 136 | add_executable(${PROJECT_TEST_NAME} ${TEST_SRC_FILES}) 137 | add_dependencies(${PROJECT_TEST_NAME} googletest) 138 | 139 | target_link_libraries(${PROJECT_TEST_NAME} 140 | ${GTEST_LIBS_DIR}/libgtest.a 141 | ${GTEST_LIBS_DIR}/libgtest_main.a 142 | ibmgraphblas 143 | ${CMAKE_THREAD_LIBS_INIT} 144 | ) 145 | 146 | add_test(test1 ${PROJECT_TEST_NAME}) 147 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # simple Makefile for IBMGraphBLAS, relies on cmake 2 | 3 | default: debug 4 | 5 | release: 6 | ( cd build ; cmake -DCMAKE_BUILD_TYPE=Release .. ; make ) 7 | 8 | debug: 9 | ( cd build ; cmake -DCMAKE_BUILD_TYPE=Debug .. ; make ) 10 | 11 | library: 12 | ( cd build ; cmake .. ; make ) 13 | 14 | #install: 15 | # ( cd build ; cmake .. ; make ; make install ) 16 | 17 | clean: distclean 18 | 19 | purge: distclean 20 | 21 | distclean: 22 | # rm -rf build/* Demo/*_demo.out Demo/complex_demo_out.m 23 | rm -rf build/* 24 | # ( cd Test ; make distclean ) 25 | # ( cd Tcov ; make distclean ) 26 | # ( cd Doc ; make distclean ) 27 | -------------------------------------------------------------------------------- /NOTICE: -------------------------------------------------------------------------------- 1 | IBMGraphBLAS 2 | Copyright [2018] IBM -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ibmgraphblas 2 | A plugable [GraphBLAS](http://graphblas.org)implementation. 3 | 4 | How to use: 5 | 6 | 1. git clone https://github.com/IBM/ibmgraphblas.git 7 | 2. cd ibmgraphblas 8 | 4. cd build 9 | 5. cmake .. 10 | 6. cmake --build . 11 | 7. ctest -VV 12 | 8. ./algo_sssp_dist_main ../Algo/Matrix/house 13 | 14 | Goal is to use ibmgraphblas as native component for [https://github.com/IBM/lagraph](https://github.com/IBM/lagraph "LAGraph") 15 | 16 | -------------------------------------------------------------------------------- /build/.gitignore: -------------------------------------------------------------------------------- 1 | # Ignore all files in GraphBLAS/build except this file. 2 | * 3 | */ 4 | !.gitignore 5 | -------------------------------------------------------------------------------- /ext/gtest/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8.8) 2 | project(gtest_builder C CXX) 3 | include(ExternalProject) 4 | 5 | set(GTEST_FORCE_SHARED_CRT ON) 6 | set(GTEST_DISABLE_PTHREADS OFF) 7 | 8 | if(MINGW) 9 | set(GTEST_DISABLE_PTHREADS ON) 10 | endif() 11 | 12 | ExternalProject_Add(googletest 13 | GIT_REPOSITORY https://github.com/google/googletest.git 14 | CMAKE_ARGS -DCMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG:PATH=DebugLibs 15 | -DCMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE:PATH=ReleaseLibs 16 | -DCMAKE_CXX_FLAGS=${MSVC_COMPILER_DEFS} 17 | -Dgtest_force_shared_crt=${GTEST_FORCE_SHARED_CRT} 18 | -Dgtest_disable_pthreads=${GTEST_DISABLE_PTHREADS} 19 | -DBUILD_GTEST=ON 20 | PREFIX "${CMAKE_CURRENT_BINARY_DIR}" 21 | # Disable install step 22 | INSTALL_COMMAND "" 23 | ) 24 | 25 | # Specify include dir 26 | ExternalProject_Get_Property(googletest source_dir) 27 | set(GTEST_INCLUDE_DIRS ${source_dir}/googletest/include PARENT_SCOPE) 28 | 29 | # Specify MainTest's link libraries 30 | ExternalProject_Get_Property(googletest binary_dir) 31 | set(GTEST_LIBS_DIR ${binary_dir}/googlemock/gtest PARENT_SCOPE) 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /src/Exception.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | #include "IBM_GraphBLAS.hh" 21 | #include 22 | #include 23 | 24 | GrB_Info Exception::info 25 | ( 26 | ) const 27 | { 28 | return _info; 29 | } 30 | -------------------------------------------------------------------------------- /src/Exception.hh: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | #ifndef EXCEPTION_H 21 | #define EXCEPTION_H 22 | 23 | class Exception 24 | { 25 | private: 26 | 27 | GrB_Info _info; 28 | 29 | public: 30 | 31 | Exception(); 32 | Exception(const Exception&); 33 | Exception& operator=(const Exception&); 34 | ~Exception(); 35 | 36 | Exception(GrB_Info); 37 | 38 | GrB_Info info() const; 39 | }; 40 | 41 | #endif // EXCEPTION_H 42 | -------------------------------------------------------------------------------- /src/GrB_BinaryOp_t.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | #include "IBM_GraphBLAS.hh" 21 | #include 22 | 23 | GrB_BinaryOp_t::GrB_BinaryOp_t 24 | ( 25 | GrB_Type D_out, 26 | GrB_Type D_in_1, 27 | GrB_Type D_in_2, 28 | void (*F)(void*, const void*, const void*) 29 | ) 30 | { 31 | _D_out = D_out; 32 | _D_in_1 = D_in_1; 33 | _D_in_2 = D_in_2; 34 | _F = F; 35 | _valid = true; 36 | } 37 | 38 | GrB_BinaryOp_t::~GrB_BinaryOp_t 39 | ( 40 | ) 41 | { 42 | } 43 | 44 | bool GrB_BinaryOp_t::valid 45 | ( 46 | ) const 47 | { 48 | return _valid; 49 | } 50 | 51 | GrB_Type GrB_BinaryOp_t::D_in_1 52 | ( 53 | ) const 54 | { 55 | return _D_in_1; 56 | } 57 | 58 | GrB_Type GrB_BinaryOp_t::D_in_2 59 | ( 60 | ) const 61 | { 62 | return _D_in_2; 63 | } 64 | 65 | GrB_Type GrB_BinaryOp_t::D_out 66 | ( 67 | ) const 68 | { 69 | return _D_out; 70 | } 71 | 72 | void (*GrB_BinaryOp_t::F() const)(void*, const void*, const void*) 73 | { 74 | return _F; 75 | } 76 | 77 | void GrB_BinaryOp_t::f 78 | ( 79 | void *out, 80 | const void *in1, 81 | const void *in2 82 | ) const 83 | { 84 | return _F(out,in1,in2); 85 | } 86 | -------------------------------------------------------------------------------- /src/GrB_BinaryOp_t.hh: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | #ifndef GRB_BINARYOP_T_H 21 | #define GRB_BINARYOP_T_H 22 | 23 | struct GrB_BinaryOp_t 24 | { 25 | private: 26 | 27 | GrB_Type _D_in_1; 28 | GrB_Type _D_in_2; 29 | GrB_Type _D_out; 30 | void (*_F)(void*, const void*, const void*); 31 | bool _valid; 32 | 33 | public: 34 | 35 | GrB_BinaryOp_t(GrB_Type,GrB_Type,GrB_Type,void (*)(void*, const void*, const void*)); 36 | ~GrB_BinaryOp_t(); 37 | 38 | bool valid() const; 39 | GrB_Type D_in_1() const; 40 | GrB_Type D_in_2() const; 41 | GrB_Type D_out () const; 42 | void (*F() const)(void*, const void*, const void*); 43 | void f(void*, const void*, const void*) const; 44 | }; 45 | 46 | #endif // IBM_GRB_BINARYOP_T_H 47 | -------------------------------------------------------------------------------- /src/GrB_Descriptor_t.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | #include "IBM_GraphBLAS.hh" 21 | #include 22 | 23 | GrB_Descriptor_t::GrB_Descriptor_t 24 | ( 25 | ) 26 | { 27 | _valid = true; 28 | _replace = false; 29 | _scmp = false; 30 | _tran0 = false; 31 | _tran1 = false; 32 | } 33 | 34 | GrB_Descriptor_t::~GrB_Descriptor_t 35 | ( 36 | ) 37 | { 38 | } 39 | 40 | bool GrB_Descriptor_t::valid 41 | ( 42 | ) const 43 | { 44 | return _valid; 45 | } 46 | 47 | bool GrB_Descriptor_t::outp_replace 48 | ( 49 | ) const 50 | { 51 | return _replace; 52 | } 53 | 54 | bool GrB_Descriptor_t::mask_scmp 55 | ( 56 | ) const 57 | { 58 | return _scmp; 59 | } 60 | 61 | bool GrB_Descriptor_t::inp0_tran 62 | ( 63 | ) const 64 | { 65 | return _tran0; 66 | } 67 | 68 | bool GrB_Descriptor_t::inp1_tran 69 | ( 70 | ) const 71 | { 72 | return _tran1; 73 | } 74 | 75 | void GrB_Descriptor_t::setReplace 76 | ( 77 | ) 78 | { 79 | _replace = true; 80 | } 81 | 82 | void GrB_Descriptor_t::setSCMP 83 | ( 84 | ) 85 | { 86 | _scmp = true; 87 | } 88 | 89 | void GrB_Descriptor_t::setTran0 90 | ( 91 | ) 92 | { 93 | _tran0 = true; 94 | } 95 | 96 | void GrB_Descriptor_t::setTran1 97 | ( 98 | ) 99 | { 100 | _tran1 = true; 101 | } 102 | -------------------------------------------------------------------------------- /src/GrB_Descriptor_t.hh: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | #ifndef GRB_DESCRIPTOR_T_H 21 | #define GRB_DESCRIPTOR_T_H 22 | 23 | struct GrB_Descriptor_t 24 | { 25 | private: 26 | 27 | bool _valid; 28 | bool _replace; 29 | bool _scmp; 30 | bool _tran0; 31 | bool _tran1; 32 | 33 | public: 34 | GrB_Descriptor_t(); 35 | ~GrB_Descriptor_t(); 36 | 37 | bool valid() const; 38 | bool outp_replace() const; 39 | bool mask_scmp() const; 40 | bool inp0_tran() const; 41 | bool inp1_tran() const; 42 | 43 | void setReplace(); 44 | void setSCMP(); 45 | void setTran0(); 46 | void setTran1(); 47 | }; 48 | 49 | #endif // GRB_DESCRIPTOR_T_H 50 | -------------------------------------------------------------------------------- /src/GrB_Mask_t.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | #include "IBM_GraphBLAS.hh" 21 | #include 22 | 23 | GrB_mask_t::GrB_mask_t 24 | ( 25 | bool scmp, 26 | const GrB_Vector_t *m, 27 | const GrB_Vector_t &v 28 | ) 29 | { 30 | if (m == GrB_NULL) 31 | { 32 | _size = v.size(); 33 | _ind = scmp ? new uset(_size) : 0; 34 | } 35 | else 36 | { 37 | _size = m->size(); 38 | _ind = new uset(_size); 39 | if (scmp) 40 | { 41 | for (GrB_Index i = 0; i<_size; i++) _ind->insert(i); 42 | for (auto k : *(m->ind())) if (*((bool*)((void*)((*m)[k](GrB_BOOL))))) _ind->erase(k); 43 | } 44 | else 45 | { 46 | for (auto k : *(m->ind())) if (*((bool*)((void*)((*m)[k](GrB_BOOL))))) _ind->insert(k); 47 | } 48 | } 49 | } 50 | 51 | GrB_mask_t::~GrB_mask_t 52 | ( 53 | ) 54 | { 55 | delete _ind; 56 | } 57 | 58 | const uset* GrB_mask_t::ind 59 | ( 60 | ) const 61 | { 62 | return _ind; 63 | } 64 | 65 | GrB_Index GrB_mask_t::size 66 | ( 67 | ) const 68 | { 69 | return _size; 70 | } 71 | 72 | bool GrB_mask_t::full 73 | ( 74 | ) const 75 | { 76 | if (0 == _ind) return true; 77 | if (_ind->size() == _size) return true; 78 | return false; 79 | } 80 | 81 | GrB_Mask_t::GrB_Mask_t 82 | ( 83 | bool scmp, 84 | const GrB_Matrix M, 85 | const GrB_Matrix_t& C 86 | ) 87 | { 88 | _nrows = (M == GrB_NULL) ? C.nrows() : M->nrows(); 89 | _ncols = (M == GrB_NULL) ? C.ncols() : M->ncols(); 90 | 91 | _rows.resize(_nrows); 92 | for (GrB_Index i=0; i<_nrows; i++) 93 | { 94 | _rows[i] = new GrB_mask_t(scmp,(M == GrB_NULL) ? (GrB_Vector)GrB_NULL : &((*M)[i]), C[i]); 95 | } 96 | 97 | _cols.resize(_ncols); 98 | for (GrB_Index j=0; j<_ncols; j++) 99 | { 100 | _cols[j] = new GrB_mask_t(scmp,(M == GrB_NULL) ? (GrB_Vector)GrB_NULL : &((*M)(j)), C(j)); 101 | } 102 | } 103 | 104 | GrB_Mask_t::~GrB_Mask_t 105 | ( 106 | ) 107 | { 108 | for (GrB_Index i=0; i *_ind; 29 | 30 | public: 31 | 32 | GrB_mask_t(bool, const GrB_Vector_t*, const GrB_Vector_t&); 33 | ~GrB_mask_t(); 34 | 35 | GrB_Index size() const; 36 | const uset *ind() const; 37 | bool full() const; 38 | }; 39 | 40 | struct GrB_Mask_t 41 | { 42 | private: 43 | 44 | GrB_Index _nrows; 45 | GrB_Index _ncols; 46 | vector _rows; 47 | vector _cols; 48 | 49 | public: 50 | 51 | GrB_Mask_t(bool, const GrB_Matrix, const GrB_Matrix_t&); 52 | ~GrB_Mask_t(); 53 | 54 | GrB_Index nrows() const; 55 | GrB_Index ncols() const; 56 | const GrB_mask_t& operator[](GrB_Index) const; 57 | const GrB_mask_t& operator()(GrB_Index) const; 58 | }; 59 | 60 | #endif // GRB_MASK_T_H 61 | -------------------------------------------------------------------------------- /src/GrB_Matrix_t.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | #include "IBM_GraphBLAS.hh" 21 | #include 22 | #include 23 | 24 | GrB_Matrix_t::GrB_Matrix_t 25 | ( 26 | ) 27 | { 28 | _valid = false; 29 | _copy = false; 30 | _D = 0; 31 | _nrows = 0; 32 | _ncols = 0; 33 | } 34 | 35 | GrB_Matrix_t::GrB_Matrix_t 36 | ( 37 | const GrB_Matrix_t& A 38 | ) 39 | { 40 | _valid = true; 41 | _copy = true; 42 | _D = A.D(); 43 | _nrows = A._nrows; 44 | _ncols = A._ncols; 45 | _rows.resize(_nrows); for (GrB_Index i=0; i<_nrows; i++) _rows[i] = A._rows[i]; 46 | _cols.resize(_ncols); for (GrB_Index j=0; j<_ncols; j++) _cols[j] = A._cols[j]; 47 | _nvals = A.nvals(); 48 | } 49 | 50 | GrB_Matrix_t::GrB_Matrix_t 51 | ( 52 | bool transpose, 53 | const GrB_Matrix_t& A 54 | ) 55 | { 56 | _valid = true; 57 | _copy = true; 58 | _D = A.D(); 59 | if (transpose) 60 | { 61 | _nrows = A._ncols; 62 | _ncols = A._nrows; 63 | _rows.resize(_nrows); for (GrB_Index i=0; i<_nrows; i++) _rows[i] = A._cols[i]; 64 | _cols.resize(_ncols); for (GrB_Index j=0; j<_ncols; j++) _cols[j] = A._rows[j]; 65 | } 66 | else 67 | { 68 | _nrows = A._nrows; 69 | _ncols = A._ncols; 70 | _rows.resize(_nrows); for (GrB_Index i=0; i<_nrows; i++) _rows[i] = A._rows[i]; 71 | _cols.resize(_ncols); for (GrB_Index j=0; j<_ncols; j++) _cols[j] = A._cols[j]; 72 | } 73 | _nvals = A.nvals(); 74 | } 75 | 76 | GrB_Matrix_t::GrB_Matrix_t 77 | ( 78 | GrB_Type D, 79 | GrB_Index nrows, 80 | GrB_Index ncols 81 | ) 82 | { 83 | _valid = true; 84 | _copy = false; 85 | _D = D; 86 | _nrows = nrows; 87 | _ncols = ncols; 88 | _nvals = 0; 89 | _rows.resize(nrows); for (GrB_Index i=0; icopy(A[i]); 124 | for (GrB_Index j=0; j<_ncols; j++) _cols[j]->copy(A(j)); 125 | _nvals = A.nvals(); 126 | 127 | return true; 128 | } 129 | 130 | bool GrB_Matrix_t::clear 131 | ( 132 | ) 133 | { 134 | assert(!_copy); 135 | 136 | _nvals = 0; 137 | for (GrB_Index i=0; iclear(); 138 | for (GrB_Index j=0; jclear(); 139 | 140 | return true; 141 | } 142 | 143 | bool GrB_Matrix_t::init 144 | ( 145 | GrB_Type D, 146 | GrB_Index nrows, 147 | GrB_Index ncols 148 | ) 149 | { 150 | assert(!_copy); 151 | 152 | _valid = true; 153 | _copy = false; 154 | _D = D; 155 | _nrows = nrows; 156 | _ncols = ncols; 157 | _nvals = 0; 158 | _rows.resize(nrows); for (GrB_Index i=0; icopy(A(i)); 211 | for (GrB_Index j=0; jcopy(A[j]); 212 | 213 | return true; 214 | } 215 | 216 | #include "template/GrB_Matrix_t_AxB.cc" 217 | 218 | bool GrB_Matrix_t::replace 219 | ( 220 | const GrB_Mask_t& Mask, 221 | const GrB_Matrix_t& Matrix 222 | ) 223 | { 224 | assert(!_copy); 225 | 226 | GrB_Index m = nrows(); 227 | GrB_Index n = ncols(); 228 | assert(m == Matrix.nrows()); 229 | assert(m == Mask.nrows()); 230 | assert(n == Matrix.ncols()); 231 | assert(n == Mask.ncols()); 232 | 233 | for (GrB_Index i = 0; ireplace(Mask[i],Matrix[i]); 234 | for (GrB_Index j = 0; jreplace(Mask(j),Matrix(j)); 235 | _nvals = 0; for (GrB_Index i = 0; invals(); 236 | 237 | return true; 238 | } 239 | 240 | bool GrB_Matrix_t::merge 241 | ( 242 | const GrB_Mask_t& Mask, 243 | const GrB_Matrix_t& Matrix 244 | ) 245 | { 246 | assert(!_copy); 247 | 248 | GrB_Index m = nrows(); 249 | GrB_Index n = ncols(); 250 | assert(m == Matrix.nrows()); 251 | assert(m == Mask.nrows()); 252 | assert(n == Matrix.ncols()); 253 | assert(n == Mask.ncols()); 254 | 255 | for (GrB_Index i = 0; imerge(Mask[i],Matrix[i]); 256 | for (GrB_Index j = 0; jmerge(Mask(j),Matrix(j)); 257 | _nvals = 0; for (GrB_Index i = 0; invals(); 258 | 259 | return true; 260 | } 261 | 262 | bool GrB_Matrix_t::clear 263 | ( 264 | GrB_Index i, 265 | GrB_Index j 266 | ) 267 | { 268 | assert(!_copy); 269 | 270 | assert(i < nrows()); 271 | assert(j < ncols()); 272 | 273 | if (_rows[i]->ind()->count(j)) 274 | { 275 | _nvals--; 276 | _rows[i]->clear(j); 277 | _cols[j]->clear(i); 278 | } 279 | 280 | return true; 281 | } 282 | 283 | void GrB_Matrix_t::addElement 284 | ( 285 | GrB_Index i, 286 | GrB_Index j, 287 | const Scalar& s, 288 | const GrB_BinaryOp_t& dup 289 | ) 290 | { 291 | assert(!_copy); 292 | 293 | assert(i < nrows()); 294 | assert(j < ncols()); 295 | 296 | _nvals += 1 - _rows[i]->ind()->count(j); 297 | _rows[i]->addElement(j,s,dup); 298 | _cols[j]->addElement(i,s,dup); 299 | } 300 | 301 | void GrB_Matrix_t::addElement 302 | ( 303 | GrB_Index i, 304 | GrB_Index j, 305 | const Scalar& s 306 | ) 307 | { 308 | assert(!_copy); 309 | 310 | assert(i < nrows()); 311 | assert(j < ncols()); 312 | 313 | clear(i,j); 314 | _nvals++; 315 | 316 | _rows[i]->addElement(j,s); 317 | _cols[j]->addElement(i,s); 318 | } 319 | 320 | const GrB_Vector_t& GrB_Matrix_t::operator[] 321 | ( 322 | GrB_Index i 323 | ) const 324 | { 325 | return *(_rows[i]); 326 | } 327 | 328 | const GrB_Vector_t& GrB_Matrix_t::operator() 329 | ( 330 | GrB_Index j 331 | ) const 332 | { 333 | return *(_cols[j]); 334 | } 335 | 336 | bool GrB_Matrix_t::mul 337 | ( 338 | const GrB_BinaryOp_t *op, 339 | const GrB_Matrix_t& A, 340 | const GrB_Matrix_t& B 341 | ) 342 | { 343 | assert(!_copy); 344 | 345 | for (GrB_Index i=0; imul(op,A[i],B[i]); 346 | for (GrB_Index j=0; jmul(op,A(j),B(j)); 347 | return true; 348 | } 349 | 350 | bool GrB_Matrix_t::mul 351 | ( 352 | const GrB_Monoid_t *op, 353 | const GrB_Matrix_t& A, 354 | const GrB_Matrix_t& B 355 | ) 356 | { 357 | assert(!_copy); 358 | 359 | for (GrB_Index i=0; imul(op,A[i],B[i]); 360 | for (GrB_Index j=0; jmul(op,A(j),B(j)); 361 | return true; 362 | } 363 | 364 | bool GrB_Matrix_t::mul 365 | ( 366 | const GrB_Semiring_t *op, 367 | const GrB_Matrix_t& A, 368 | const GrB_Matrix_t& B 369 | ) 370 | { 371 | assert(!_copy); 372 | 373 | for (GrB_Index i=0; imul(op,A[i],B[i]); 374 | for (GrB_Index j=0; jmul(op,A(j),B(j)); 375 | return true; 376 | } 377 | 378 | bool GrB_Matrix_t::add 379 | ( 380 | const GrB_BinaryOp_t *op, 381 | const GrB_Matrix_t& A, 382 | const GrB_Matrix_t& B 383 | ) 384 | { 385 | assert(!_copy); 386 | 387 | for (GrB_Index i=0; iadd(op,A[i],B[i]); 388 | for (GrB_Index j=0; jadd(op,A(j),B(j)); 389 | return true; 390 | } 391 | 392 | bool GrB_Matrix_t::add 393 | ( 394 | const GrB_Monoid_t *op, 395 | const GrB_Matrix_t& A, 396 | const GrB_Matrix_t& B 397 | ) 398 | { 399 | assert(!_copy); 400 | 401 | for (GrB_Index i=0; iadd(op,A[i],B[i]); 402 | for (GrB_Index j=0; jadd(op,A(j),B(j)); 403 | return true; 404 | } 405 | 406 | bool GrB_Matrix_t::add 407 | ( 408 | const GrB_Semiring_t *op, 409 | const GrB_Matrix_t& A, 410 | const GrB_Matrix_t& B 411 | ) 412 | { 413 | assert(!_copy); 414 | 415 | for (GrB_Index i=0; iadd(op,A[i],B[i]); 416 | for (GrB_Index j=0; jadd(op,A(j),B(j)); 417 | return true; 418 | } 419 | -------------------------------------------------------------------------------- /src/GrB_Matrix_t.hh: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | #ifndef GRB_MATRIX_T_H 21 | #define GRB_MATRIX_T_H 22 | 23 | struct GrB_Matrix_t 24 | { 25 | private: 26 | 27 | GrB_Type _D; 28 | GrB_Index _nrows; 29 | GrB_Index _ncols; 30 | GrB_Index _nvals; 31 | vector _rows; 32 | vector _cols; 33 | bool _valid; 34 | bool _copy; 35 | 36 | public: 37 | 38 | GrB_Matrix_t(); 39 | GrB_Matrix_t(const GrB_Matrix_t&); 40 | ~GrB_Matrix_t(); 41 | GrB_Matrix_t(GrB_Type,GrB_Index,GrB_Index); 42 | GrB_Matrix_t(bool, const GrB_Matrix_t&); 43 | 44 | bool valid() const; // object is valid 45 | GrB_Type D() const; // type of elements 46 | GrB_Index nrows() const; // number of rows of matrix 47 | GrB_Index ncols() const; // number of columns of matrix 48 | GrB_Index nvals() const; // number of elements (nonzeros) 49 | bool copy(const GrB_Matrix_t&); // copy from another matrix 50 | bool init(GrB_Type,GrB_Index,GrB_Index); // initialize matrix 51 | bool transpose(const GrB_Matrix_t&); // transpose matrix 52 | bool clear(); // clear all elements 53 | bool clear(GrB_Index,GrB_Index); // clear element at row,column indices 54 | const GrB_Vector_t& operator[](GrB_Index) const; // row of matrix (read-only) 55 | const GrB_Vector_t& operator()(GrB_Index) const; // column of matrix (read-only) 56 | void AxB(const GrB_Semiring,const GrB_Matrix_t&,const GrB_Matrix_t&); // matrix multiplication 57 | bool add(const GrB_BinaryOp_t*,const GrB_Matrix_t&,const GrB_Matrix_t&); // element-wise addition 58 | bool add(const GrB_Monoid_t* ,const GrB_Matrix_t&,const GrB_Matrix_t&); // element-wise addition 59 | bool add(const GrB_Semiring_t*,const GrB_Matrix_t&,const GrB_Matrix_t&); // element-wise addition 60 | bool mul(const GrB_BinaryOp_t*,const GrB_Matrix_t&,const GrB_Matrix_t&); // element-wise multiply 61 | bool mul(const GrB_Monoid_t* ,const GrB_Matrix_t&,const GrB_Matrix_t&); // element-wise multiply 62 | bool mul(const GrB_Semiring_t*,const GrB_Matrix_t&,const GrB_Matrix_t&); // element-wise multiply 63 | bool replace(const GrB_Mask_t&,const GrB_Matrix_t&); // replace with new matrix 64 | bool merge(const GrB_Mask_t&,const GrB_Matrix_t&); // merge with new matrix 65 | void addElement(GrB_Index,GrB_Index,const Scalar&); // add element to matrix 66 | void addElement(GrB_Index,GrB_Index,const Scalar&,const GrB_BinaryOp_t&); // add element with accumulation 67 | }; 68 | 69 | #endif // GRB_MATRIX_T_H 70 | -------------------------------------------------------------------------------- /src/GrB_Monoid_t.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | #include "IBM_GraphBLAS.hh" 21 | #include 22 | #include 23 | 24 | GrB_Monoid_t::GrB_Monoid_t 25 | ( 26 | GrB_Type D, 27 | void (*op)(void*, const void*, const void*), 28 | const void* zero, 29 | size_t len 30 | ) 31 | { 32 | _valid = true; 33 | _D = D; 34 | _op = op; 35 | _zero = malloc(len); 36 | assert(_zero); // (JEM) need a better error handling 37 | memcpy(_zero,zero,len); 38 | _len = len; 39 | assert(D->size() == len); 40 | } 41 | 42 | GrB_Monoid_t::~GrB_Monoid_t 43 | ( 44 | ) 45 | { 46 | free(_zero); 47 | } 48 | 49 | bool GrB_Monoid_t::valid 50 | ( 51 | ) const 52 | { 53 | return _valid; 54 | } 55 | 56 | GrB_Type GrB_Monoid_t::D 57 | ( 58 | ) const 59 | { 60 | return _D; 61 | } 62 | 63 | GrB_Type GrB_Monoid_t::D_in_1 64 | ( 65 | ) const 66 | { 67 | return _D; 68 | } 69 | 70 | GrB_Type GrB_Monoid_t::D_in_2 71 | ( 72 | ) const 73 | { 74 | return _D; 75 | } 76 | 77 | GrB_Type GrB_Monoid_t::D_out 78 | ( 79 | ) const 80 | { 81 | return _D; 82 | } 83 | 84 | size_t GrB_Monoid_t::len 85 | ( 86 | ) const 87 | { 88 | return _len; 89 | } 90 | 91 | void (*GrB_Monoid_t::op() const)(void*, const void*, const void*) 92 | { 93 | return _op; 94 | } 95 | 96 | void GrB_Monoid_t::f 97 | ( 98 | void *out, 99 | const void *in1, 100 | const void *in2 101 | ) const 102 | { 103 | return _op(out,in1,in2); 104 | } 105 | 106 | Scalar GrB_Monoid_t::zero 107 | ( 108 | ) const 109 | { 110 | Scalar z(_D,_zero); 111 | return z; 112 | } 113 | -------------------------------------------------------------------------------- /src/GrB_Monoid_t.hh: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | #ifndef GRB_MONOID_T_H 21 | #define GRB_MONOID_T_H 22 | 23 | struct GrB_Monoid_t 24 | { 25 | private: 26 | 27 | GrB_Type _D; 28 | void (*_op)(void*, const void*, const void*); 29 | void *_zero; 30 | size_t _len; 31 | bool _valid; 32 | 33 | public: 34 | 35 | GrB_Monoid_t(GrB_Type, 36 | void (*)(void*, const void*, const void*), 37 | const void*, size_t); 38 | ~GrB_Monoid_t(); 39 | 40 | bool valid() const; 41 | GrB_Type D() const; 42 | GrB_Type D_out() const; 43 | GrB_Type D_in_1() const; 44 | GrB_Type D_in_2() const; 45 | Scalar zero() const; 46 | size_t len() const; 47 | void (*op() const)(void*, const void*, const void*); 48 | void f(void*, const void*, const void*) const; 49 | }; 50 | 51 | #endif // GRB_MONOID_T_H 52 | -------------------------------------------------------------------------------- /src/GrB_Semiring_t.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | #include "IBM_GraphBLAS.hh" 21 | #include 22 | #include 23 | 24 | GrB_Semiring_t::GrB_Semiring_t 25 | ( 26 | GrB_Type D_out, 27 | GrB_Type D_in_1, 28 | GrB_Type D_in_2, 29 | void (*add)(void*, const void*, const void*), 30 | void (*mul)(void*, const void*, const void*), 31 | const void* zero, 32 | size_t len 33 | ) 34 | { 35 | _valid = true; 36 | _D_out = D_out; 37 | _D_in_1 = D_in_1; 38 | _D_in_2 = D_in_2; 39 | _add = add; 40 | _mul = mul; 41 | _zero = malloc(len); 42 | assert(_zero); // (JEM) need a better error handling 43 | memcpy(_zero,zero,len); 44 | _len = len; 45 | } 46 | 47 | GrB_Semiring_t::~GrB_Semiring_t 48 | ( 49 | ) 50 | { 51 | free(_zero); 52 | } 53 | 54 | bool GrB_Semiring_t::valid 55 | ( 56 | ) const 57 | { 58 | return _valid; 59 | } 60 | 61 | GrB_Type GrB_Semiring_t::D_in_1 62 | ( 63 | ) const 64 | { 65 | return _D_in_1; 66 | } 67 | 68 | GrB_Type GrB_Semiring_t::D_in_2 69 | ( 70 | ) const 71 | { 72 | return _D_in_2; 73 | } 74 | 75 | GrB_Type GrB_Semiring_t::D_out 76 | ( 77 | ) const 78 | { 79 | return _D_out; 80 | } 81 | 82 | void GrB_Semiring_t::add 83 | ( 84 | void *out, 85 | const void *in1, 86 | const void *in2 87 | ) const 88 | { 89 | return _add(out,in1,in2); 90 | } 91 | 92 | void GrB_Semiring_t::mul 93 | ( 94 | void *out, 95 | const void *in1, 96 | const void *in2 97 | ) const 98 | { 99 | return _mul(out,in1,in2); 100 | } 101 | 102 | Scalar GrB_Semiring_t::zero 103 | ( 104 | ) const 105 | { 106 | Scalar z(_D_out,_zero); 107 | return z; 108 | } 109 | -------------------------------------------------------------------------------- /src/GrB_Semiring_t.hh: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | #ifndef GRB_SEMIRING_T_H 21 | #define GRB_SEMIRING_T_H 22 | 23 | struct GrB_Semiring_t 24 | { 25 | private: 26 | 27 | GrB_Type _D_in_1; 28 | GrB_Type _D_in_2; 29 | GrB_Type _D_out; 30 | void (*_add)(void*, const void*, const void*); 31 | void (*_mul)(void*, const void*, const void*); 32 | void *_zero; 33 | size_t _len; 34 | bool _valid; 35 | 36 | public: 37 | GrB_Semiring_t(GrB_Type, GrB_Type, GrB_Type, 38 | void(*)(void*, const void*, const void*), 39 | void(*)(void*, const void*, const void*), 40 | const void*, size_t); 41 | ~GrB_Semiring_t(); 42 | 43 | bool valid() const; 44 | GrB_Type D_in_1() const; 45 | GrB_Type D_in_2() const; 46 | GrB_Type D_out () const; 47 | void add(void*, const void*, const void*) const; 48 | void mul(void*, const void*, const void*) const; 49 | Scalar zero() const; 50 | size_t len() const; 51 | }; 52 | 53 | #endif // GRB_SEMIRING_T_H 54 | -------------------------------------------------------------------------------- /src/GrB_Type_t.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | #include "IBM_GraphBLAS.hh" 21 | #include 22 | #include 23 | 24 | GrB_Type_t::GrB_Type_t 25 | ( 26 | size_t size 27 | ) 28 | { 29 | _size = size; 30 | _valid = true; 31 | } 32 | 33 | GrB_Type_t::~GrB_Type_t 34 | ( 35 | ) 36 | { 37 | } 38 | 39 | bool GrB_Type_t::valid 40 | ( 41 | ) const 42 | { 43 | return _valid; 44 | } 45 | 46 | size_t GrB_Type_t::size 47 | ( 48 | ) const 49 | { 50 | return _size; 51 | } 52 | 53 | bool GrB_Type_t::predefined 54 | ( 55 | ) const 56 | { 57 | return false; 58 | } 59 | 60 | bool GrB_Type_t::compatible 61 | ( 62 | GrB_Type T 63 | ) const 64 | { 65 | if (this->predefined() && T->predefined()) return true; 66 | if (this == T) return true; 67 | return false; 68 | } 69 | 70 | void *GrB_Type_t::clone 71 | ( 72 | const void *x 73 | ) const 74 | { 75 | void *y = malloc(size()); 76 | memcpy(y,x,size()); 77 | return y; 78 | } 79 | 80 | void *GrB_Type_t::clone 81 | ( 82 | const void *x, 83 | const GrB_Type type 84 | ) const 85 | { 86 | assert(type == this); 87 | return clone(x); 88 | } 89 | 90 | void GrB_Type_t::clone 91 | ( 92 | bool *y, 93 | const void *x 94 | ) const 95 | { 96 | assert(0 == 1); 97 | } 98 | 99 | void GrB_Type_t::clone 100 | ( 101 | int8_t *y, 102 | const void *x 103 | ) const 104 | { 105 | assert(0 == 1); 106 | } 107 | 108 | void GrB_Type_t::clone 109 | ( 110 | uint8_t *y, 111 | const void *x 112 | ) const 113 | { 114 | assert(0 == 1); 115 | } 116 | 117 | void GrB_Type_t::clone 118 | ( 119 | int16_t *y, 120 | const void *x 121 | ) const 122 | { 123 | assert(0 == 1); 124 | } 125 | 126 | void GrB_Type_t::clone 127 | ( 128 | uint16_t *y, 129 | const void *x 130 | ) const 131 | { 132 | assert(0 == 1); 133 | } 134 | 135 | void GrB_Type_t::clone 136 | ( 137 | int32_t *y, 138 | const void *x 139 | ) const 140 | { 141 | assert(0 == 1); 142 | } 143 | 144 | void GrB_Type_t::clone 145 | ( 146 | uint32_t *y, 147 | const void *x 148 | ) const 149 | { 150 | assert(0 == 1); 151 | } 152 | 153 | void GrB_Type_t::clone 154 | ( 155 | int64_t *y, 156 | const void *x 157 | ) const 158 | { 159 | assert(0 == 1); 160 | } 161 | 162 | void GrB_Type_t::clone 163 | ( 164 | uint64_t *y, 165 | const void *x 166 | ) const 167 | { 168 | assert(0 == 1); 169 | } 170 | 171 | void GrB_Type_t::clone 172 | ( 173 | float *y, 174 | const void *x 175 | ) const 176 | { 177 | assert(0 == 1); 178 | } 179 | 180 | void GrB_Type_t::clone 181 | ( 182 | double *y, 183 | const void *x 184 | ) const 185 | { 186 | assert(0 == 1); 187 | } 188 | 189 | template 190 | Type::Type 191 | ( 192 | ) : GrB_Type_t(sizeof(T)) 193 | { 194 | } 195 | 196 | template 197 | Type::~Type 198 | ( 199 | ) 200 | { 201 | } 202 | 203 | template 204 | bool Type::predefined 205 | ( 206 | ) const 207 | { 208 | return true; 209 | } 210 | 211 | template 212 | bool Type::compatible 213 | ( 214 | GrB_Type D 215 | ) const 216 | { 217 | if (predefined() && D->predefined()) return true; 218 | if (this == D) return true; 219 | return false; 220 | } 221 | 222 | template 223 | void *Type::clone 224 | ( 225 | void const *x, 226 | const GrB_Type type 227 | ) const 228 | { 229 | T *y = new T; 230 | type->clone(y,x); 231 | return y; 232 | } 233 | 234 | template 235 | void Type::clone 236 | ( 237 | bool *y, 238 | const void *x 239 | ) const 240 | { 241 | *y = *((T*)x); 242 | } 243 | 244 | template 245 | void Type::clone 246 | ( 247 | int8_t *y, 248 | const void *x 249 | ) const 250 | { 251 | *y = *((T*)x); 252 | } 253 | 254 | template 255 | void Type::clone 256 | ( 257 | uint8_t *y, 258 | const void *x 259 | ) const 260 | { 261 | *y = *((T*)x); 262 | } 263 | 264 | template 265 | void Type::clone 266 | ( 267 | int16_t *y, 268 | const void *x 269 | ) const 270 | { 271 | *y = *((T*)x); 272 | } 273 | 274 | template 275 | void Type::clone 276 | ( 277 | uint16_t *y, 278 | const void *x 279 | ) const 280 | { 281 | *y = *((T*)x); 282 | } 283 | 284 | template 285 | void Type::clone 286 | ( 287 | int32_t *y, 288 | const void *x 289 | ) const 290 | { 291 | *y = *((T*)x); 292 | } 293 | 294 | template 295 | void Type::clone 296 | ( 297 | uint32_t *y, 298 | const void *x 299 | ) const 300 | { 301 | *y = *((T*)x); 302 | } 303 | 304 | template 305 | void Type::clone 306 | ( 307 | int64_t *y, 308 | const void *x 309 | ) const 310 | { 311 | *y = *((T*)x); 312 | } 313 | 314 | template 315 | void Type::clone 316 | ( 317 | uint64_t *y, 318 | const void *x 319 | ) const 320 | { 321 | *y = *((T*)x); 322 | } 323 | 324 | template 325 | void Type::clone 326 | ( 327 | float *y, 328 | const void *x 329 | ) const 330 | { 331 | *y = *((T*)x); 332 | } 333 | 334 | template 335 | void Type::clone 336 | ( 337 | double *y, 338 | const void *x 339 | ) const 340 | { 341 | *y = *((T*)x); 342 | } 343 | 344 | template class Type; 345 | template class Type; 346 | template class Type; 347 | template class Type; 348 | template class Type; 349 | template class Type; 350 | template class Type; 351 | template class Type; 352 | template class Type; 353 | template class Type; 354 | template class Type; 355 | -------------------------------------------------------------------------------- /src/GrB_Type_t.hh: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | #ifndef GRB_TYPE_T_H 21 | #define GRB_TYPE_T_H 22 | 23 | struct GrB_Type_t 24 | { 25 | private: 26 | 27 | bool _valid; 28 | size_t _size; 29 | 30 | void *clone(const void*) const; 31 | 32 | public: 33 | 34 | GrB_Type_t(size_t); 35 | virtual ~GrB_Type_t(); 36 | 37 | bool valid() const; 38 | size_t size() const; 39 | virtual bool predefined() const; 40 | virtual bool compatible(GrB_Type) const; 41 | virtual void *clone(const void*,const GrB_Type) const; 42 | 43 | virtual void clone(bool* ,const void*) const; 44 | virtual void clone(int8_t* ,const void*) const; 45 | virtual void clone(uint8_t* ,const void*) const; 46 | virtual void clone(int16_t* ,const void*) const; 47 | virtual void clone(uint16_t*,const void*) const; 48 | virtual void clone(int32_t* ,const void*) const; 49 | virtual void clone(uint32_t*,const void*) const; 50 | virtual void clone(int64_t* ,const void*) const; 51 | virtual void clone(uint64_t*,const void*) const; 52 | virtual void clone(float* ,const void*) const; 53 | virtual void clone(double* ,const void*) const; 54 | }; 55 | 56 | template 57 | class Type : public GrB_Type_t 58 | { 59 | public: 60 | 61 | Type(); 62 | virtual ~Type(); 63 | 64 | virtual bool predefined() const; 65 | virtual bool compatible(GrB_Type) const; 66 | virtual void *clone(const void*,const GrB_Type) const; 67 | 68 | virtual void clone(bool* ,const void*) const; 69 | virtual void clone(int8_t* ,const void*) const; 70 | virtual void clone(uint8_t* ,const void*) const; 71 | virtual void clone(int16_t* ,const void*) const; 72 | virtual void clone(uint16_t*,const void*) const; 73 | virtual void clone(int32_t* ,const void*) const; 74 | virtual void clone(uint32_t*,const void*) const; 75 | virtual void clone(int64_t* ,const void*) const; 76 | virtual void clone(uint64_t*,const void*) const; 77 | virtual void clone(float* ,const void*) const; 78 | virtual void clone(double* ,const void*) const; 79 | }; 80 | 81 | #endif // GRB_TYPE_T_H 82 | -------------------------------------------------------------------------------- /src/GrB_UnaryOp_t.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | #include "IBM_GraphBLAS.hh" 21 | #include 22 | 23 | GrB_UnaryOp_t::GrB_UnaryOp_t 24 | ( 25 | GrB_Type D_out, 26 | GrB_Type D_in, 27 | void (*F)(void*, const void*) 28 | ) 29 | { 30 | _D_out = D_out; 31 | _D_in = D_in; 32 | _F = F; 33 | _valid = true; 34 | } 35 | 36 | GrB_UnaryOp_t::~GrB_UnaryOp_t 37 | ( 38 | ) 39 | { 40 | } 41 | 42 | bool GrB_UnaryOp_t::valid 43 | ( 44 | ) const 45 | { 46 | return _valid; 47 | } 48 | 49 | GrB_Type GrB_UnaryOp_t::D_in 50 | ( 51 | ) const 52 | { 53 | return _D_in; 54 | } 55 | 56 | GrB_Type GrB_UnaryOp_t::D_out 57 | ( 58 | ) const 59 | { 60 | return _D_out; 61 | } 62 | 63 | void GrB_UnaryOp_t::f 64 | ( 65 | void *out, 66 | const void *in 67 | ) const 68 | { 69 | return _F(out,in); 70 | } 71 | -------------------------------------------------------------------------------- /src/GrB_UnaryOp_t.hh: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | #ifndef GRB_UNARYOP_T_H 21 | #define GRB_UNARYOP_T_H 22 | 23 | struct GrB_UnaryOp_t 24 | { 25 | private: 26 | 27 | GrB_Type _D_in; 28 | GrB_Type _D_out; 29 | void (*_F)(void*, const void*); 30 | bool _valid; 31 | 32 | public: 33 | 34 | GrB_UnaryOp_t(GrB_Type,GrB_Type,void (*)(void*, const void*)); 35 | ~GrB_UnaryOp_t(); 36 | 37 | bool valid() const; 38 | GrB_Type D_in() const; 39 | GrB_Type D_out() const; 40 | void f(void*, const void*) const; 41 | }; 42 | 43 | #endif // GRB_UNARYOP_T_H 44 | -------------------------------------------------------------------------------- /src/GrB_Vector_t.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | #include "IBM_GraphBLAS.hh" 21 | #include 22 | #include 23 | 24 | GrB_Vector_t::GrB_Vector_t 25 | ( 26 | ) 27 | { 28 | _valid = false; 29 | } 30 | 31 | GrB_Vector_t::GrB_Vector_t 32 | ( 33 | const GrB_Vector_t& u 34 | ) 35 | { 36 | _valid = true; 37 | _D = u.D(); 38 | _size = u.size(); 39 | _ind = new uset(_size); 40 | _map = new umap; 41 | for (auto it : *u._map) 42 | { 43 | _ind->insert(it.first); 44 | (*_map)[it.first] = D()->clone(it.second,u.D()); 45 | } 46 | } 47 | 48 | GrB_Vector_t::GrB_Vector_t 49 | ( 50 | GrB_Type D, 51 | GrB_Index size 52 | ) 53 | { 54 | _valid = true; 55 | _D = D; 56 | _size = size; 57 | _ind = new uset(_size); 58 | _map = new umap; 59 | } 60 | 61 | GrB_Vector_t::~GrB_Vector_t 62 | ( 63 | ) 64 | { 65 | for (auto it = _map->begin(); it != _map->end(); ++it) 66 | { 67 | free(it->second); 68 | } 69 | delete _map; 70 | delete _ind; 71 | } 72 | 73 | bool GrB_Vector_t::valid 74 | ( 75 | ) const 76 | { 77 | return _valid; 78 | } 79 | 80 | GrB_Index GrB_Vector_t::nvals 81 | ( 82 | ) const 83 | { 84 | return _ind->size(); 85 | } 86 | 87 | GrB_Index GrB_Vector_t::size 88 | ( 89 | ) const 90 | { 91 | return _size; 92 | } 93 | 94 | bool GrB_Vector_t::clear 95 | ( 96 | ) 97 | { 98 | if (!_valid) return false; 99 | for (auto it = _map->begin(); it != _map->end(); ++it) 100 | { 101 | free(it->second); 102 | } 103 | delete _map; 104 | delete _ind; 105 | _ind = new uset(_size); 106 | _map = new umap; 107 | return true; 108 | } 109 | 110 | bool GrB_Vector_t::clear 111 | ( 112 | GrB_Index i 113 | ) 114 | { 115 | if (ind()->count(i)) 116 | { 117 | _ind->erase(i); 118 | _map->erase(i); 119 | } 120 | return true; 121 | } 122 | 123 | GrB_Type GrB_Vector_t::D 124 | ( 125 | ) const 126 | { 127 | return _D; 128 | } 129 | 130 | const uset *GrB_Vector_t::ind 131 | ( 132 | ) const 133 | { 134 | return _ind; 135 | } 136 | 137 | const umap *GrB_Vector_t::map 138 | ( 139 | ) const 140 | { 141 | return _map; 142 | } 143 | 144 | void GrB_Vector_t::addElement 145 | ( 146 | GrB_Index i, 147 | const Scalar& x, 148 | const GrB_BinaryOp_t& dup 149 | ) 150 | { 151 | void *y = D()->clone(x,x.D()); 152 | if (_ind->count(i)) 153 | { 154 | dup.f(_map->at(i),_map->at(i),y); 155 | free(y); 156 | } 157 | else 158 | { 159 | (*_map)[i] = y; 160 | _ind->insert(i); 161 | } 162 | } 163 | 164 | void GrB_Vector_t::addElement 165 | ( 166 | GrB_Index i, 167 | const Scalar& s 168 | ) 169 | { 170 | assert(D() == s.D()); 171 | assert(_ind->count(i) == 0); 172 | 173 | void *y = D()->clone(s,s.D()); 174 | (*_map)[i] = y; 175 | _ind->insert(i); 176 | } 177 | 178 | bool GrB_Vector_t::copy 179 | ( 180 | const GrB_Vector_t &u 181 | ) 182 | { 183 | if (_valid) 184 | { 185 | clear(); 186 | _size = u.size(); 187 | _D = u.D(); 188 | } 189 | else 190 | { 191 | _valid = true; 192 | _D = u.D(); 193 | _size = u.size(); 194 | _ind = new uset(_size); 195 | _map = new umap; 196 | } 197 | for (auto it : *u._map) 198 | { 199 | _ind->insert(it.first); 200 | (*_map)[it.first] = D()->clone(it.second,u.D()); 201 | } 202 | 203 | return true; 204 | } 205 | 206 | bool GrB_Vector_t::replace 207 | ( 208 | const GrB_mask_t& mask, 209 | const GrB_Vector_t& Vector 210 | ) 211 | { 212 | assert(size() == mask.size()); 213 | assert(size() == Vector.size()); 214 | 215 | clear(); 216 | if (mask.full()) 217 | { 218 | // full mask - write all elements from vector 219 | for (auto i : (*Vector.ind())) addElement(i,Vector[i](D())); 220 | } 221 | else 222 | { 223 | // partial mask - write only those elements according to mask 224 | for (auto i : (*mask.ind()) * (*Vector.ind())) addElement(i,Vector[i](D())); 225 | } 226 | 227 | return true; 228 | } 229 | 230 | bool GrB_Vector_t::merge 231 | ( 232 | const GrB_mask_t& mask, 233 | const GrB_Vector_t& Vector 234 | ) 235 | { 236 | assert(size() == mask.size()); 237 | assert(size() == Vector.size()); 238 | 239 | if (mask.full()) 240 | { 241 | // full mask - erase all elements and replace with new vector 242 | clear(); 243 | for (auto i : (*Vector.ind())) addElement(i,Vector[i](D())); 244 | } 245 | else 246 | { 247 | // partial mask - replace only those elements according to mask 248 | for (auto i : (*mask.ind())) clear(i); 249 | for (auto i : (*mask.ind()) * (*Vector.ind())) addElement(i,Vector[i](D())); 250 | } 251 | 252 | return true; 253 | } 254 | 255 | bool GrB_Vector_t::add 256 | ( 257 | const GrB_BinaryOp_t *op, 258 | const GrB_Vector_t& u, 259 | const GrB_Vector_t& v 260 | ) 261 | { 262 | clear(); 263 | uset intersection = (*(u.ind())) * (*(v.ind())); 264 | Scalar axb(op->D_out()); 265 | for (auto k : intersection) 266 | { 267 | op->f(axb,u[k](op->D_in_1()),v[k](op->D_in_2())); 268 | addElement(k,axb(D())); 269 | } 270 | for (auto k : *(u.ind()) - intersection) addElement(k,u[k](D())); 271 | for (auto k : *(v.ind()) - intersection) addElement(k,v[k](D())); 272 | return true; 273 | } 274 | 275 | bool GrB_Vector_t::add 276 | ( 277 | const GrB_Monoid_t *op, 278 | const GrB_Vector_t& u, 279 | const GrB_Vector_t& v 280 | ) 281 | { 282 | clear(); 283 | uset intersection = (*(u.ind())) * (*(v.ind())); 284 | Scalar axb(op->D_out()); 285 | for (auto k : intersection) 286 | { 287 | op->f(axb,u[k](op->D_in_1()),v[k](op->D_in_2())); 288 | addElement(k,axb(D())); 289 | } 290 | for (auto k : *(u.ind()) - intersection) addElement(k,u[k](D())); 291 | for (auto k : *(v.ind()) - intersection) addElement(k,v[k](D())); 292 | return true; 293 | } 294 | 295 | bool GrB_Vector_t::add 296 | ( 297 | const GrB_Semiring_t *op, 298 | const GrB_Vector_t& u, 299 | const GrB_Vector_t& v 300 | ) 301 | { 302 | clear(); 303 | uset intersection = (*(u.ind())) * (*(v.ind())); 304 | Scalar axb(op->D_out()); 305 | for (auto k : intersection) 306 | { 307 | op->add(axb,u[k](op->D_in_1()),v[k](op->D_in_2())); 308 | addElement(k,axb(D())); 309 | } 310 | for (auto k : *(u.ind()) - intersection) addElement(k,u[k](D())); 311 | for (auto k : *(v.ind()) - intersection) addElement(k,v[k](D())); 312 | return true; 313 | } 314 | 315 | bool GrB_Vector_t::mul 316 | ( 317 | const GrB_BinaryOp_t *op, 318 | const GrB_Vector_t& u, 319 | const GrB_Vector_t& v 320 | ) 321 | { 322 | clear(); 323 | uset intersection = (*(u.ind())) * (*(v.ind())); 324 | Scalar axb(op->D_out()); 325 | for (auto k : intersection) 326 | { 327 | op->f(axb,u[k](op->D_in_1()),v[k](op->D_in_2())); 328 | addElement(k,axb(D())); 329 | } 330 | return true; 331 | } 332 | 333 | bool GrB_Vector_t::mul 334 | ( 335 | const GrB_Monoid_t *op, 336 | const GrB_Vector_t& u, 337 | const GrB_Vector_t& v 338 | ) 339 | { 340 | clear(); 341 | uset intersection = (*(u.ind())) * (*(v.ind())); 342 | Scalar axb(op->D_out()); 343 | for (auto k : intersection) 344 | { 345 | op->f(axb,u[k](op->D_in_1()),v[k](op->D_in_2())); 346 | addElement(k,axb(D())); 347 | } 348 | return true; 349 | } 350 | 351 | bool GrB_Vector_t::mul 352 | ( 353 | const GrB_Semiring_t *op, 354 | const GrB_Vector_t& u, 355 | const GrB_Vector_t& v 356 | ) 357 | { 358 | clear(); 359 | uset intersection = (*(u.ind())) * (*(v.ind())); 360 | Scalar axb(op->D_out()); 361 | for (auto k : intersection) 362 | { 363 | op->mul(axb,u[k](op->D_in_1()),v[k](op->D_in_2())); 364 | addElement(k,axb(D())); 365 | } 366 | return true; 367 | } 368 | 369 | bool GrB_Vector_t::init 370 | ( 371 | GrB_Type D, 372 | GrB_Index size 373 | ) 374 | { 375 | _valid = true; 376 | _D = D; 377 | _size = size; 378 | _ind = new uset(_size); 379 | _map = new umap; 380 | 381 | return true; 382 | } 383 | 384 | Scalar GrB_Vector_t::operator[] 385 | ( 386 | GrB_Index i 387 | ) const 388 | { 389 | Scalar a(_D,_map->at(i)); 390 | return a; 391 | } 392 | 393 | static 394 | void dot_product 395 | ( 396 | Scalar& sum, 397 | const GrB_Semiring S, 398 | const GrB_Vector_t& u, 399 | const GrB_Vector_t& v, 400 | const uset& intersect 401 | ) 402 | { 403 | Scalar axb = S->zero(); 404 | if ((S->D_in_1() == u.D()) && (S->D_in_2() == v.D())) 405 | { 406 | for (auto k : intersect) 407 | { 408 | S->mul(axb,u.map()->at(k),v.map()->at(k)); 409 | S->add(sum,sum,axb); 410 | } 411 | } 412 | else 413 | { 414 | for (auto k : intersect) 415 | { 416 | S->mul(axb,u[k](S->D_in_1()),v[k](S->D_in_2())); 417 | S->add(sum,sum,axb); 418 | } 419 | } 420 | } 421 | 422 | void GrB_Vector_t::Axb 423 | ( 424 | const GrB_Semiring S, 425 | const GrB_Matrix_t& A, 426 | const GrB_Vector_t& b 427 | ) 428 | { 429 | GrB_Index m = A.nrows(); 430 | GrB_Index n = b.size(); 431 | assert(n == A.ncols()); 432 | 433 | clear(); 434 | for (GrB_Index i = 0; i Intersect(n); 437 | const uset *intersect; 438 | if (A[i].ind()->size() == n) { intersect = b.ind(); } 439 | else if (b.ind()->size() == n) { intersect = A[i].ind(); } 440 | else { Intersect = (*(A[i].ind())) * (*(b.ind())); intersect = &Intersect; } 441 | if (intersect->empty()) continue; 442 | Scalar sum = S->zero(); 443 | dot_product(sum,S,A[i],b,*intersect); 444 | addElement(i,sum(D())); 445 | } 446 | } 447 | 448 | void GrB_Vector_t::axB 449 | ( 450 | const GrB_Semiring S, 451 | const GrB_Vector_t& a, 452 | const GrB_Matrix_t& B 453 | ) 454 | { 455 | GrB_Index m = a.size(); 456 | GrB_Index n = B.ncols(); 457 | assert(m == B.nrows()); 458 | 459 | clear(); 460 | for (GrB_Index j = 0; j Intersect(m); 463 | const uset *intersect; 464 | if (a.ind()->size() == m) { intersect = B(j).ind(); } 465 | else if (B(j).ind()->size() == m) { intersect = a.ind(); } 466 | else { Intersect = (*(a.ind())) * (*(B(j).ind())); intersect = &Intersect; } 467 | if (intersect->empty()) continue; 468 | Scalar sum = S->zero(); 469 | dot_product(sum,S,a,B(j),*intersect); 470 | addElement(j,sum(D())); 471 | } 472 | } 473 | -------------------------------------------------------------------------------- /src/GrB_Vector_t.hh: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | #ifndef GRB_VECTOR_T_H 21 | #define GRB_VECTOR_T_H 22 | 23 | struct GrB_Vector_t 24 | { 25 | private: 26 | 27 | GrB_Type _D; 28 | GrB_Index _size; 29 | uset *_ind; 30 | umap *_map; 31 | bool _valid; 32 | 33 | public: 34 | 35 | GrB_Vector_t(); 36 | GrB_Vector_t(const GrB_Vector_t&); 37 | ~GrB_Vector_t(); 38 | GrB_Vector_t(GrB_Type,GrB_Index); 39 | 40 | bool valid() const; // object is valid 41 | GrB_Index nvals() const; // number of elements (nonzeros) 42 | GrB_Type D() const; // type of elements 43 | GrB_Index size() const; // size of vector 44 | const uset *ind() const; // set of element indices (structure) 45 | const umap *map() const; // map of indices to elements 46 | Scalar operator[](GrB_Index) const; // element access (read-only) 47 | bool init(GrB_Type,GrB_Index); // initialize vector 48 | bool copy(const GrB_Vector_t&); // copy from another vector 49 | bool clear(); // clear all elements 50 | bool clear(GrB_Index); // clear element at index 51 | void addElement(GrB_Index,const Scalar&); // add element to vector 52 | void addElement(GrB_Index,const Scalar&,const GrB_BinaryOp_t&); // add element with accumulation 53 | bool add(const GrB_BinaryOp_t*,const GrB_Vector_t&,const GrB_Vector_t&); // element-wise addition 54 | bool add(const GrB_Monoid_t* ,const GrB_Vector_t&,const GrB_Vector_t&); // element-wise addition 55 | bool add(const GrB_Semiring_t*,const GrB_Vector_t&,const GrB_Vector_t&); // element-wise addition 56 | bool mul(const GrB_BinaryOp_t*,const GrB_Vector_t&,const GrB_Vector_t&); // element-wise multiplication 57 | bool mul(const GrB_Monoid_t* ,const GrB_Vector_t&,const GrB_Vector_t&); // element-wise multiplication 58 | bool mul(const GrB_Semiring_t*,const GrB_Vector_t&,const GrB_Vector_t&); // element-wise multiplication 59 | bool replace(const GrB_mask_t&,const GrB_Vector_t&); // replace with another vector 60 | bool merge(const GrB_mask_t&,const GrB_Vector_t&); // merge with another vector 61 | void axB(const GrB_Semiring,const GrB_Vector_t&,const GrB_Matrix_t&);// vector-matrix multiplication 62 | void Axb(const GrB_Semiring,const GrB_Matrix_t&,const GrB_Vector_t&);// matrix-vector multiplication 63 | }; 64 | 65 | #endif // GRB_VECTOR_T_H 66 | -------------------------------------------------------------------------------- /src/IBM_GraphBLAS.hh: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | #ifndef IBM_GRAPHBLAS_H 21 | #define IBM_GRAPHBLAS_H 22 | 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | #define vector std::vector 31 | 32 | template 33 | struct uset : public std::unordered_set 34 | { 35 | private: 36 | 37 | GrB_Index _capacity; 38 | 39 | public: 40 | 41 | uset(GrB_Index); 42 | virtual ~uset(); 43 | 44 | virtual GrB_Index capacity() const; 45 | virtual bool full() const; 46 | }; 47 | 48 | template 49 | struct umap : public std::unordered_map 50 | { 51 | }; 52 | 53 | void uset_intersection(uset&, const uset&, const uset&); 54 | void uset_union (uset&, const uset&, const uset&); 55 | void uset_difference (uset&, const uset&, const uset&); 56 | uset operator*(const uset& , const uset& ); 57 | uset operator*(const vector&, const uset& ); 58 | uset operator*(const uset& , const vector&); 59 | uset operator-(const uset& , const uset& ); 60 | uset operator+(const uset& , const uset& ); 61 | 62 | #include "Exception.hh" 63 | #include "Scalar.hh" 64 | #include "GrB_Mask_t.hh" 65 | #include "GrB_Vector_t.hh" 66 | #include "GrB_Matrix_t.hh" 67 | #include "GrB_Monoid_t.hh" 68 | #include "GrB_Semiring_t.hh" 69 | #include "GrB_UnaryOp_t.hh" 70 | #include "GrB_BinaryOp_t.hh" 71 | #include "GrB_Descriptor_t.hh" 72 | #include "GrB_Type_t.hh" 73 | 74 | #endif // IBM_GRAPHBLAS_H 75 | -------------------------------------------------------------------------------- /src/Makefile: -------------------------------------------------------------------------------- 1 | # simple Makefile for IBMGraphBLAS, relies on cmake 2 | 3 | default: 4 | ( cd build ; cmake .. ; make ) 5 | # ( cd build ; cmake .. ; make ; cd ../Demo ; ./demo ) 6 | 7 | library: 8 | ( cd build ; cmake .. ; make ) 9 | 10 | #install: 11 | # ( cd build ; cmake .. ; make ; make install ) 12 | 13 | clean: distclean 14 | 15 | purge: distclean 16 | 17 | distclean: 18 | # rm -rf build/* Demo/*_demo.out Demo/complex_demo_out.m 19 | rm -rf build/* 20 | # ( cd Test ; make distclean ) 21 | # ( cd Tcov ; make distclean ) 22 | # ( cd Doc ; make distclean ) -------------------------------------------------------------------------------- /src/Scalar.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | #include "IBM_GraphBLAS.hh" 21 | #include 22 | #include 23 | 24 | Scalar::~Scalar 25 | ( 26 | ) 27 | { 28 | free(_data); 29 | } 30 | 31 | Scalar::Scalar 32 | ( 33 | const Scalar& s 34 | ) 35 | { 36 | _D = s._D; 37 | _data = malloc(_D->size()); 38 | memcpy(_data,s._data,_D->size()); 39 | } 40 | 41 | Scalar::Scalar 42 | ( 43 | GrB_Type D, 44 | const void *data 45 | ) 46 | { 47 | _D = D; 48 | _data = malloc(_D->size()); 49 | memcpy(_data, data, D->size()); 50 | } 51 | 52 | Scalar::Scalar 53 | ( 54 | GrB_Type D 55 | ) 56 | { 57 | _D = D; 58 | _data = malloc(_D->size()); 59 | } 60 | 61 | Scalar& Scalar::operator=( 62 | const Scalar& s 63 | ) 64 | { 65 | if (this == &s) return *this; 66 | free(_data); 67 | _D = s._D; 68 | _data = malloc(_D->size()); 69 | memcpy(_data,s._data,_D->size()); 70 | return *this; 71 | } 72 | 73 | Scalar::operator void* 74 | ( 75 | ) 76 | { 77 | return _data; 78 | } 79 | 80 | Scalar::operator const void* 81 | ( 82 | ) const 83 | { 84 | return _data; 85 | } 86 | 87 | Scalar Scalar::operator() 88 | ( 89 | GrB_Type type 90 | ) const 91 | { 92 | void *temp = type->clone(_data,_D); assert(temp); 93 | Scalar a(type,temp); 94 | free(temp); 95 | return a; 96 | } 97 | 98 | const GrB_Type Scalar::D 99 | ( 100 | ) const 101 | { 102 | return _D; 103 | } 104 | -------------------------------------------------------------------------------- /src/Scalar.hh: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | #ifndef SCALAR_H 21 | #define SCALAR_H 22 | 23 | class Scalar 24 | { 25 | private: 26 | 27 | GrB_Type _D; 28 | void *_data; 29 | 30 | public: 31 | 32 | Scalar(); 33 | Scalar(const Scalar&); 34 | Scalar& operator=(const Scalar&); 35 | ~Scalar(); 36 | 37 | Scalar(GrB_Type,const void*); 38 | Scalar(GrB_Type); 39 | 40 | Scalar operator()(GrB_Type) const; 41 | operator void*(); 42 | operator const void*() const; 43 | const GrB_Type D() const; 44 | }; 45 | 46 | #endif // SCALAR_H 47 | -------------------------------------------------------------------------------- /src/template/GrB_Matrix_assign.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | static 21 | GrB_Info GrB_Matrix_assign_common 22 | ( 23 | GrB_Matrix C, 24 | const GrB_Matrix Mask, 25 | const GrB_BinaryOp accum, 26 | const void *val, 27 | GrB_Type type, 28 | const GrB_Index *row_indices, 29 | GrB_Index nrows, 30 | const GrB_Index *col_indices, 31 | GrB_Index ncols, 32 | const GrB_Descriptor desc 33 | ) 34 | { 35 | try 36 | { 37 | // Check that the input objects are initialized 38 | if ((!C) || (C == GrB_INVALID_HANDLE)) return GrB_UNINITIALIZED_OBJECT; 39 | if ( (Mask == GrB_INVALID_HANDLE)) return GrB_UNINITIALIZED_OBJECT; 40 | if ( (accum == GrB_INVALID_HANDLE)) return GrB_UNINITIALIZED_OBJECT; 41 | if ( (desc == GrB_INVALID_HANDLE)) return GrB_UNINITIALIZED_OBJECT; 42 | 43 | // Check that the objects are valid 44 | if (C && (!C ->valid())) return GrB_INVALID_OBJECT; 45 | if (Mask && (!Mask ->valid())) return GrB_INVALID_OBJECT; 46 | if (accum && (!accum->valid())) return GrB_INVALID_OBJECT; 47 | if (desc && (!desc ->valid())) return GrB_INVALID_OBJECT; 48 | 49 | // Check for null pointers 50 | if (!row_indices) return GrB_NULL_POINTER; 51 | if (!col_indices) return GrB_NULL_POINTER; 52 | if (!val) return GrB_NULL_POINTER; 53 | 54 | // Check domain conformity 55 | if (Mask && (!Mask->D()->predefined())) return GrB_DOMAIN_MISMATCH; 56 | if (!C->D()->compatible(type)) return GrB_DOMAIN_MISMATCH; 57 | if (accum && !C->D()->compatible(accum->D_in_1())) return GrB_DOMAIN_MISMATCH; 58 | if (accum && !C->D()->compatible(accum->D_out())) return GrB_DOMAIN_MISMATCH; 59 | if (accum && !type->compatible(accum->D_in_2())) return GrB_DOMAIN_MISMATCH; 60 | 61 | // Decode descriptor 62 | bool Replace = (desc && desc->outp_replace()); 63 | bool SCMP = (desc && desc->mask_scmp()); 64 | 65 | // Prepare internal matrices for operation 66 | GrB_Matrix_t C_tilde(*C); 67 | GrB_Mask_t M_tilde(SCMP,Mask,*C); 68 | 69 | // Check dimension comformity 70 | if (C_tilde.nrows() != M_tilde.nrows()) return GrB_DIMENSION_MISMATCH; 71 | if (C_tilde.ncols() != M_tilde.ncols()) return GrB_DIMENSION_MISMATCH; 72 | if (nrows > C_tilde.nrows()) return GrB_DIMENSION_MISMATCH; 73 | if (ncols > C_tilde.ncols()) return GrB_DIMENSION_MISMATCH; 74 | 75 | // Create the assignment vector 76 | GrB_Matrix_t T_tilde(type, C_tilde.nrows(), C_tilde.ncols()); 77 | Scalar value(type,val); 78 | for (GrB_Index i = 0; iD_out(),C_tilde.nrows(),C_tilde.ncols()); 97 | Z_tilde.add(accum,C_tilde,T_tilde); 98 | } 99 | else 100 | { 101 | Z_tilde.copy(C_tilde); 102 | for (GrB_Index i=0; ireplace(M_tilde,Z_tilde) : C->merge(M_tilde,Z_tilde); 108 | 109 | return GrB_SUCCESS; 110 | } 111 | catch (const Exception& e) 112 | { 113 | return e.info(); 114 | } 115 | } 116 | 117 | GrB_Info GrB_Matrix_assign_BOOL 118 | ( 119 | GrB_Matrix C, 120 | const GrB_Matrix Mask, 121 | const GrB_BinaryOp accum, 122 | bool val, 123 | const GrB_Index *row_indices, 124 | GrB_Index nrows, 125 | const GrB_Index *col_indices, 126 | GrB_Index ncols, 127 | const GrB_Descriptor desc 128 | ) 129 | { 130 | return GrB_Matrix_assign_common(C,Mask,accum,&val,GrB_BOOL,row_indices,nrows,col_indices,ncols,desc); 131 | } 132 | 133 | GrB_Info GrB_Matrix_assign_INT8 134 | ( 135 | GrB_Matrix C, 136 | const GrB_Matrix Mask, 137 | const GrB_BinaryOp accum, 138 | int8_t val, 139 | const GrB_Index *row_indices, 140 | GrB_Index nrows, 141 | const GrB_Index *col_indices, 142 | GrB_Index ncols, 143 | const GrB_Descriptor desc 144 | ) 145 | { 146 | return GrB_Matrix_assign_common(C,Mask,accum,&val,GrB_INT8,row_indices,nrows,col_indices,ncols,desc); 147 | } 148 | 149 | GrB_Info GrB_Matrix_assign_UINT8 150 | ( 151 | GrB_Matrix C, 152 | const GrB_Matrix Mask, 153 | const GrB_BinaryOp accum, 154 | uint8_t val, 155 | const GrB_Index *row_indices, 156 | GrB_Index nrows, 157 | const GrB_Index *col_indices, 158 | GrB_Index ncols, 159 | const GrB_Descriptor desc 160 | ) 161 | { 162 | return GrB_Matrix_assign_common(C,Mask,accum,&val,GrB_UINT8,row_indices,nrows,col_indices,ncols,desc); 163 | } 164 | 165 | GrB_Info GrB_Matrix_assign_INT16 166 | ( 167 | GrB_Matrix C, 168 | const GrB_Matrix Mask, 169 | const GrB_BinaryOp accum, 170 | int16_t val, 171 | const GrB_Index *row_indices, 172 | GrB_Index nrows, 173 | const GrB_Index *col_indices, 174 | GrB_Index ncols, 175 | const GrB_Descriptor desc 176 | ) 177 | { 178 | return GrB_Matrix_assign_common(C,Mask,accum,&val,GrB_INT16,row_indices,nrows,col_indices,ncols,desc); 179 | } 180 | 181 | GrB_Info GrB_Matrix_assign_UINT16 182 | ( 183 | GrB_Matrix C, 184 | const GrB_Matrix Mask, 185 | const GrB_BinaryOp accum, 186 | uint16_t val, 187 | const GrB_Index *row_indices, 188 | GrB_Index nrows, 189 | const GrB_Index *col_indices, 190 | GrB_Index ncols, 191 | const GrB_Descriptor desc 192 | ) 193 | { 194 | return GrB_Matrix_assign_common(C,Mask,accum,&val,GrB_UINT16,row_indices,nrows,col_indices,ncols,desc); 195 | } 196 | 197 | GrB_Info GrB_Matrix_assign_INT32 198 | ( 199 | GrB_Matrix C, 200 | const GrB_Matrix Mask, 201 | const GrB_BinaryOp accum, 202 | int32_t val, 203 | const GrB_Index *row_indices, 204 | GrB_Index nrows, 205 | const GrB_Index *col_indices, 206 | GrB_Index ncols, 207 | const GrB_Descriptor desc 208 | ) 209 | { 210 | return GrB_Matrix_assign_common(C,Mask,accum,&val,GrB_INT32,row_indices,nrows,col_indices,ncols,desc); 211 | } 212 | 213 | GrB_Info GrB_Matrix_assign_UINT32 214 | ( 215 | GrB_Matrix C, 216 | const GrB_Matrix Mask, 217 | const GrB_BinaryOp accum, 218 | uint32_t val, 219 | const GrB_Index *row_indices, 220 | GrB_Index nrows, 221 | const GrB_Index *col_indices, 222 | GrB_Index ncols, 223 | const GrB_Descriptor desc 224 | ) 225 | { 226 | return GrB_Matrix_assign_common(C,Mask,accum,&val,GrB_UINT32,row_indices,nrows,col_indices,ncols,desc); 227 | } 228 | 229 | GrB_Info GrB_Matrix_assign_INT64 230 | ( 231 | GrB_Matrix C, 232 | const GrB_Matrix Mask, 233 | const GrB_BinaryOp accum, 234 | int64_t val, 235 | const GrB_Index *row_indices, 236 | GrB_Index nrows, 237 | const GrB_Index *col_indices, 238 | GrB_Index ncols, 239 | const GrB_Descriptor desc 240 | ) 241 | { 242 | return GrB_Matrix_assign_common(C,Mask,accum,&val,GrB_INT64,row_indices,nrows,col_indices,ncols,desc); 243 | } 244 | 245 | GrB_Info GrB_Matrix_assign_UINT64 246 | ( 247 | GrB_Matrix C, 248 | const GrB_Matrix Mask, 249 | const GrB_BinaryOp accum, 250 | uint64_t val, 251 | const GrB_Index *row_indices, 252 | GrB_Index nrows, 253 | const GrB_Index *col_indices, 254 | GrB_Index ncols, 255 | const GrB_Descriptor desc 256 | ) 257 | { 258 | return GrB_Matrix_assign_common(C,Mask,accum,&val,GrB_UINT64,row_indices,nrows,col_indices,ncols,desc); 259 | } 260 | 261 | GrB_Info GrB_Matrix_assign_FP32 262 | ( 263 | GrB_Matrix C, 264 | const GrB_Matrix Mask, 265 | const GrB_BinaryOp accum, 266 | float val, 267 | const GrB_Index *row_indices, 268 | GrB_Index nrows, 269 | const GrB_Index *col_indices, 270 | GrB_Index ncols, 271 | const GrB_Descriptor desc 272 | ) 273 | { 274 | return GrB_Matrix_assign_common(C,Mask,accum,&val,GrB_FP32,row_indices,nrows,col_indices,ncols,desc); 275 | } 276 | 277 | GrB_Info GrB_Matrix_assign_FP64 278 | ( 279 | GrB_Matrix C, 280 | const GrB_Matrix Mask, 281 | const GrB_BinaryOp accum, 282 | double val, 283 | const GrB_Index *row_indices, 284 | GrB_Index nrows, 285 | const GrB_Index *col_indices, 286 | GrB_Index ncols, 287 | const GrB_Descriptor desc 288 | ) 289 | { 290 | return GrB_Matrix_assign_common(C,Mask,accum,&val,GrB_FP64,row_indices,nrows,col_indices,ncols,desc); 291 | } 292 | 293 | GrB_Info GrB_Matrix_assign_UDT 294 | ( 295 | GrB_Matrix C, 296 | const GrB_Matrix Mask, 297 | const GrB_BinaryOp accum, 298 | const void *val, 299 | const GrB_Index *row_indices, 300 | GrB_Index nrows, 301 | const GrB_Index *col_indices, 302 | GrB_Index ncols, 303 | const GrB_Descriptor desc 304 | ) 305 | { 306 | return GrB_Matrix_assign_common(C,Mask,accum,&val,C->D(),row_indices,nrows,col_indices,ncols,desc); 307 | } 308 | -------------------------------------------------------------------------------- /src/template/GrB_Matrix_reduce.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | static 21 | GrB_Info GrB_Matrix_reduce_common 22 | ( 23 | void *val, 24 | GrB_Type type, 25 | const GrB_BinaryOp accum, 26 | const GrB_Monoid op, 27 | const GrB_Matrix A, 28 | const GrB_Descriptor desc 29 | ) 30 | { 31 | try 32 | { 33 | // Check that the input objects are initialized 34 | if ((!val) ) return GrB_NULL_POINTER; 35 | if ( (accum == GrB_INVALID_HANDLE)) return GrB_UNINITIALIZED_OBJECT; 36 | if ((!op) || (op == GrB_INVALID_HANDLE)) return GrB_UNINITIALIZED_OBJECT; 37 | if ((!A) || (A == GrB_INVALID_HANDLE)) return GrB_UNINITIALIZED_OBJECT; 38 | if ( (desc == GrB_INVALID_HANDLE)) return GrB_UNINITIALIZED_OBJECT; 39 | 40 | // Check that the objects are valid 41 | if (accum && (!accum->valid())) return GrB_INVALID_OBJECT; 42 | if (op && (!op ->valid())) return GrB_INVALID_OBJECT; 43 | if (A && (!A ->valid())) return GrB_INVALID_OBJECT; 44 | if (desc && (!desc ->valid())) return GrB_INVALID_OBJECT; 45 | 46 | // Check domain conformity 47 | if (!A->D()->compatible(op->D()) ) return GrB_DOMAIN_MISMATCH; 48 | if (accum && !type->compatible(accum->D_in_1()) ) return GrB_DOMAIN_MISMATCH; 49 | if (accum && !type->compatible(accum->D_out()) ) return GrB_DOMAIN_MISMATCH; 50 | if (accum && !op->D()->compatible(accum->D_in_2())) return GrB_DOMAIN_MISMATCH; 51 | if (!accum && !type->compatible(op->D()) ) return GrB_DOMAIN_MISMATCH; 52 | 53 | // Perform the reduction 54 | Scalar sum = op->zero(); 55 | for (GrB_Index i=0; inrows(); i++) 56 | { 57 | for (auto k : *((*A)[i].ind())) 58 | { 59 | Scalar a = (*A)[i][k]; 60 | op->f(sum,sum,a(op->D())); 61 | } 62 | } 63 | 64 | // Compute final result 65 | if (accum) 66 | { 67 | Scalar v(type,val); 68 | Scalar final(accum->D_out()); 69 | accum->f(final,v(accum->D_in_1()),sum(accum->D_in_2())); 70 | memcpy(val,final(type),type->size()); 71 | } 72 | else 73 | { 74 | memcpy(val,sum(type),type->size()); 75 | } 76 | 77 | return GrB_SUCCESS; 78 | } 79 | catch (const Exception& e) 80 | { 81 | return e.info(); 82 | } 83 | } 84 | 85 | GrB_Info GrB_Matrix_reduce_BOOL 86 | ( 87 | bool *val, 88 | const GrB_BinaryOp accum, 89 | const GrB_Monoid op, 90 | const GrB_Matrix A, 91 | const GrB_Descriptor desc 92 | ) 93 | { 94 | return GrB_Matrix_reduce_common(val,GrB_BOOL,accum,op,A,desc); 95 | } 96 | 97 | GrB_Info GrB_Matrix_reduce_INT8 98 | ( 99 | int8_t *val, 100 | const GrB_BinaryOp accum, 101 | const GrB_Monoid op, 102 | const GrB_Matrix A, 103 | const GrB_Descriptor desc 104 | ) 105 | { 106 | return GrB_Matrix_reduce_common(val,GrB_INT8,accum,op,A,desc); 107 | } 108 | 109 | GrB_Info GrB_Matrix_reduce_UINT8 110 | ( 111 | uint8_t *val, 112 | const GrB_BinaryOp accum, 113 | const GrB_Monoid op, 114 | const GrB_Matrix A, 115 | const GrB_Descriptor desc 116 | ) 117 | { 118 | return GrB_Matrix_reduce_common(val,GrB_UINT8,accum,op,A,desc); 119 | } 120 | 121 | GrB_Info GrB_Matrix_reduce_INT16 122 | ( 123 | int16_t *val, 124 | const GrB_BinaryOp accum, 125 | const GrB_Monoid op, 126 | const GrB_Matrix A, 127 | const GrB_Descriptor desc 128 | ) 129 | { 130 | return GrB_Matrix_reduce_common(val,GrB_INT16,accum,op,A,desc); 131 | } 132 | 133 | GrB_Info GrB_Matrix_reduce_UINT16 134 | ( 135 | uint16_t *val, 136 | const GrB_BinaryOp accum, 137 | const GrB_Monoid op, 138 | const GrB_Matrix A, 139 | const GrB_Descriptor desc 140 | ) 141 | { 142 | return GrB_Matrix_reduce_common(val,GrB_UINT16,accum,op,A,desc); 143 | } 144 | 145 | GrB_Info GrB_Matrix_reduce_INT32 146 | ( 147 | int32_t *val, 148 | const GrB_BinaryOp accum, 149 | const GrB_Monoid op, 150 | const GrB_Matrix A, 151 | const GrB_Descriptor desc 152 | ) 153 | { 154 | return GrB_Matrix_reduce_common(val,GrB_INT32,accum,op,A,desc); 155 | } 156 | 157 | GrB_Info GrB_Matrix_reduce_UINT32 158 | ( 159 | uint32_t *val, 160 | const GrB_BinaryOp accum, 161 | const GrB_Monoid op, 162 | const GrB_Matrix A, 163 | const GrB_Descriptor desc 164 | ) 165 | { 166 | return GrB_Matrix_reduce_common(val,GrB_UINT32,accum,op,A,desc); 167 | } 168 | 169 | GrB_Info GrB_Matrix_reduce_INT64 170 | ( 171 | int64_t *val, 172 | const GrB_BinaryOp accum, 173 | const GrB_Monoid op, 174 | const GrB_Matrix A, 175 | const GrB_Descriptor desc 176 | ) 177 | { 178 | return GrB_Matrix_reduce_common(val,GrB_INT64,accum,op,A,desc); 179 | } 180 | 181 | GrB_Info GrB_Matrix_reduce_UINT64 182 | ( 183 | uint64_t *val, 184 | const GrB_BinaryOp accum, 185 | const GrB_Monoid op, 186 | const GrB_Matrix A, 187 | const GrB_Descriptor desc 188 | ) 189 | { 190 | return GrB_Matrix_reduce_common(val,GrB_UINT64,accum,op,A,desc); 191 | } 192 | 193 | GrB_Info GrB_Matrix_reduce_FP32 194 | ( 195 | float *val, 196 | const GrB_BinaryOp accum, 197 | const GrB_Monoid op, 198 | const GrB_Matrix A, 199 | const GrB_Descriptor desc 200 | ) 201 | { 202 | return GrB_Matrix_reduce_common(val,GrB_FP32,accum,op,A,desc); 203 | } 204 | 205 | GrB_Info GrB_Matrix_reduce_FP64 206 | ( 207 | double *val, 208 | const GrB_BinaryOp accum, 209 | const GrB_Monoid op, 210 | const GrB_Matrix A, 211 | const GrB_Descriptor desc 212 | ) 213 | { 214 | return GrB_Matrix_reduce_common(val,GrB_FP64,accum,op,A,desc); 215 | } 216 | 217 | GrB_Info GrB_Matrix_reduce_UDT 218 | ( 219 | void *val, 220 | const GrB_BinaryOp accum, 221 | const GrB_Monoid op, 222 | const GrB_Matrix A, 223 | const GrB_Descriptor desc 224 | ) 225 | { 226 | if ((!A) || (A == GrB_INVALID_HANDLE)) return GrB_UNINITIALIZED_OBJECT; 227 | return GrB_Matrix_reduce_common(val,A->D(),accum,op,A,desc); 228 | } 229 | -------------------------------------------------------------------------------- /src/template/GrB_Matrix_t_AxB.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | static 21 | void dot_product 22 | ( 23 | Scalar& sum, 24 | const GrB_Semiring S, 25 | const GrB_Vector_t& u, 26 | const GrB_Vector_t& v, 27 | const uset& intersect 28 | ) 29 | { 30 | Scalar axb = S->zero(); 31 | if ((S->D_in_1() == u.D()) && (S->D_in_2() == v.D())) 32 | { 33 | for (auto k : intersect) 34 | { 35 | S->mul(axb,u.map()->at(k),v.map()->at(k)); 36 | S->add(sum,sum,axb); 37 | } 38 | } 39 | else 40 | { 41 | for (auto k : intersect) 42 | { 43 | S->mul(axb,u[k](S->D_in_1()),v[k](S->D_in_2())); 44 | S->add(sum,sum,axb); 45 | } 46 | } 47 | } 48 | 49 | void GrB_Matrix_t::AxB 50 | ( 51 | const GrB_Semiring S, 52 | const GrB_Matrix_t& A, 53 | const GrB_Matrix_t& B 54 | ) 55 | { 56 | GrB_Index m = A.nrows(); 57 | GrB_Index n = B.ncols(); 58 | GrB_Index p = A.ncols(); assert(p == B.nrows()); 59 | 60 | clear(); 61 | for (GrB_Index i = 0; i Intersect(p); 66 | const uset *intersect; 67 | if (A[i].ind()->size() == p) { intersect = B(j).ind(); } 68 | else if (B(j).ind()->size() == p) { intersect = A[i].ind(); } 69 | else { Intersect = (*(A[i].ind())) * (*(B(j).ind())); intersect = &Intersect; } 70 | if (intersect->empty()) continue; 71 | Scalar sum = S->zero(); 72 | dot_product(sum,S,A[i],B(j),*intersect); 73 | addElement(i,j,sum(D())); 74 | } 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/template/GrB_Vector_assign.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | static 21 | GrB_Info GrB_Vector_assign_common 22 | ( 23 | GrB_Vector w, 24 | const GrB_Vector mask, 25 | const GrB_BinaryOp accum, 26 | const void *val, 27 | GrB_Type type, 28 | const GrB_Index *indices, 29 | GrB_Index nindices, 30 | const GrB_Descriptor desc 31 | ) 32 | { 33 | try 34 | { 35 | // Check that the input objects are initialized 36 | if ((!w) || (w == GrB_INVALID_HANDLE)) return GrB_UNINITIALIZED_OBJECT; 37 | if ( (mask == GrB_INVALID_HANDLE)) return GrB_UNINITIALIZED_OBJECT; 38 | if ( (accum == GrB_INVALID_HANDLE)) return GrB_UNINITIALIZED_OBJECT; 39 | if ( (desc == GrB_INVALID_HANDLE)) return GrB_UNINITIALIZED_OBJECT; 40 | 41 | // Check that the objects are valid 42 | if (w && (!w ->valid())) return GrB_INVALID_OBJECT; 43 | if (mask && (!mask ->valid())) return GrB_INVALID_OBJECT; 44 | if (accum && (!accum->valid())) return GrB_INVALID_OBJECT; 45 | if (desc && (!desc ->valid())) return GrB_INVALID_OBJECT; 46 | 47 | // Check for null pointers 48 | if (!indices) return GrB_NULL_POINTER; 49 | if (!val) return GrB_NULL_POINTER; 50 | 51 | // Check domain conformity 52 | if (mask && (!mask->D()->predefined())) return GrB_DOMAIN_MISMATCH; 53 | if (!w->D()->compatible(type)) return GrB_DOMAIN_MISMATCH; 54 | if (accum && !w->D()->compatible(accum->D_in_1())) return GrB_DOMAIN_MISMATCH; 55 | if (accum && !w->D()->compatible(accum->D_out())) return GrB_DOMAIN_MISMATCH; 56 | if (accum && !type->compatible(accum->D_in_2())) return GrB_DOMAIN_MISMATCH; 57 | 58 | // Decode descriptor 59 | bool Replace = (desc && desc->outp_replace()); 60 | bool SCMP = (desc && desc->mask_scmp()); 61 | 62 | // Prepare internal vectors for operation 63 | GrB_Vector_t w_tilde(*w); 64 | GrB_mask_t m_tilde(SCMP,mask,*w); 65 | 66 | // Check dimension comformity 67 | if (w_tilde.size() != m_tilde.size()) return GrB_DIMENSION_MISMATCH; 68 | if (nindices > w_tilde.size()) return GrB_DIMENSION_MISMATCH; 69 | 70 | // Create the assignment vector 71 | GrB_Vector_t t_tilde(type, w_tilde.size()); 72 | Scalar value(type,val); 73 | if (indices == GrB_ALL) for (GrB_Index i = 0; iD_out(),w_tilde.size()); 81 | z_tilde.add(accum,w_tilde,t_tilde); 82 | } 83 | else 84 | { 85 | z_tilde.copy(w_tilde); 86 | for (auto i : *(t_tilde.ind())) { z_tilde.clear(i); z_tilde.addElement(i,t_tilde[i](z_tilde.D())); } 87 | } 88 | 89 | // Mask and replace 90 | Replace ? w->replace(m_tilde,z_tilde) : w->merge(m_tilde,z_tilde); 91 | 92 | return GrB_SUCCESS; 93 | } 94 | catch (const Exception& e) 95 | { 96 | return e.info(); 97 | } 98 | } 99 | 100 | GrB_Info GrB_Vector_assign_BOOL 101 | ( 102 | GrB_Vector w, 103 | const GrB_Vector mask, 104 | const GrB_BinaryOp accum, 105 | bool val, 106 | const GrB_Index *indices, 107 | GrB_Index nindices, 108 | const GrB_Descriptor desc 109 | ) 110 | { 111 | return GrB_Vector_assign_common(w,mask,accum,&val,GrB_BOOL,indices,nindices,desc); 112 | } 113 | 114 | GrB_Info GrB_Vector_assign_INT8 115 | ( 116 | GrB_Vector w, 117 | const GrB_Vector mask, 118 | const GrB_BinaryOp accum, 119 | int8_t val, 120 | const GrB_Index *indices, 121 | GrB_Index nindices, 122 | const GrB_Descriptor desc 123 | ) 124 | { 125 | return GrB_Vector_assign_common(w,mask,accum,&val,GrB_INT8,indices,nindices,desc); 126 | } 127 | 128 | GrB_Info GrB_Vector_assign_UINT8 129 | ( 130 | GrB_Vector w, 131 | const GrB_Vector mask, 132 | const GrB_BinaryOp accum, 133 | uint8_t val, 134 | const GrB_Index *indices, 135 | GrB_Index nindices, 136 | const GrB_Descriptor desc 137 | ) 138 | { 139 | return GrB_Vector_assign_common(w,mask,accum,&val,GrB_UINT8,indices,nindices,desc); 140 | } 141 | 142 | GrB_Info GrB_Vector_assign_INT16 143 | ( 144 | GrB_Vector w, 145 | const GrB_Vector mask, 146 | const GrB_BinaryOp accum, 147 | int16_t val, 148 | const GrB_Index *indices, 149 | GrB_Index nindices, 150 | const GrB_Descriptor desc 151 | ) 152 | { 153 | return GrB_Vector_assign_common(w,mask,accum,&val,GrB_INT16,indices,nindices,desc); 154 | } 155 | 156 | GrB_Info GrB_Vector_assign_UINT16 157 | ( 158 | GrB_Vector w, 159 | const GrB_Vector mask, 160 | const GrB_BinaryOp accum, 161 | uint16_t val, 162 | const GrB_Index *indices, 163 | GrB_Index nindices, 164 | const GrB_Descriptor desc 165 | ) 166 | { 167 | return GrB_Vector_assign_common(w,mask,accum,&val,GrB_UINT16,indices,nindices,desc); 168 | } 169 | 170 | GrB_Info GrB_Vector_assign_INT32 171 | ( 172 | GrB_Vector w, 173 | const GrB_Vector mask, 174 | const GrB_BinaryOp accum, 175 | int32_t val, 176 | const GrB_Index *indices, 177 | GrB_Index nindices, 178 | const GrB_Descriptor desc 179 | ) 180 | { 181 | return GrB_Vector_assign_common(w,mask,accum,&val,GrB_INT32,indices,nindices,desc); 182 | } 183 | 184 | GrB_Info GrB_Vector_assign_UINT32 185 | ( 186 | GrB_Vector w, 187 | const GrB_Vector mask, 188 | const GrB_BinaryOp accum, 189 | uint32_t val, 190 | const GrB_Index *indices, 191 | GrB_Index nindices, 192 | const GrB_Descriptor desc 193 | ) 194 | { 195 | return GrB_Vector_assign_common(w,mask,accum,&val,GrB_UINT32,indices,nindices,desc); 196 | } 197 | 198 | GrB_Info GrB_Vector_assign_INT64 199 | ( 200 | GrB_Vector w, 201 | const GrB_Vector mask, 202 | const GrB_BinaryOp accum, 203 | int64_t val, 204 | const GrB_Index *indices, 205 | GrB_Index nindices, 206 | const GrB_Descriptor desc 207 | ) 208 | { 209 | return GrB_Vector_assign_common(w,mask,accum,&val,GrB_INT64,indices,nindices,desc); 210 | } 211 | 212 | GrB_Info GrB_Vector_assign_UINT64 213 | ( 214 | GrB_Vector w, 215 | const GrB_Vector mask, 216 | const GrB_BinaryOp accum, 217 | uint64_t val, 218 | const GrB_Index *indices, 219 | GrB_Index nindices, 220 | const GrB_Descriptor desc 221 | ) 222 | { 223 | return GrB_Vector_assign_common(w,mask,accum,&val,GrB_UINT64,indices,nindices,desc); 224 | } 225 | 226 | GrB_Info GrB_Vector_assign_FP32 227 | ( 228 | GrB_Vector w, 229 | const GrB_Vector mask, 230 | const GrB_BinaryOp accum, 231 | float val, 232 | const GrB_Index *indices, 233 | GrB_Index nindices, 234 | const GrB_Descriptor desc 235 | ) 236 | { 237 | return GrB_Vector_assign_common(w,mask,accum,&val,GrB_FP32,indices,nindices,desc); 238 | } 239 | 240 | GrB_Info GrB_Vector_assign_FP64 241 | ( 242 | GrB_Vector w, 243 | const GrB_Vector mask, 244 | const GrB_BinaryOp accum, 245 | double val, 246 | const GrB_Index *indices, 247 | GrB_Index nindices, 248 | const GrB_Descriptor desc 249 | ) 250 | { 251 | return GrB_Vector_assign_common(w,mask,accum,&val,GrB_FP64,indices,nindices,desc); 252 | } 253 | 254 | GrB_Info GrB_Vector_assign_UDT 255 | ( 256 | GrB_Vector w, 257 | const GrB_Vector mask, 258 | const GrB_BinaryOp accum, 259 | const void *val, 260 | const GrB_Index *indices, 261 | GrB_Index nindices, 262 | const GrB_Descriptor desc 263 | ) 264 | { 265 | return GrB_Vector_assign_common(w,mask,accum,&val,w->D(),indices,nindices,desc); 266 | } 267 | -------------------------------------------------------------------------------- /src/template/GrB_Vector_reduce.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | static 21 | GrB_Info GrB_Vector_reduce_common 22 | ( 23 | void *val, 24 | GrB_Type type, 25 | const GrB_BinaryOp accum, 26 | const GrB_Monoid op, 27 | const GrB_Vector u, 28 | const GrB_Descriptor desc 29 | ) 30 | { 31 | try 32 | { 33 | // Check that the input objects are initialized 34 | if ((!val) ) return GrB_NULL_POINTER; 35 | if ( (accum == GrB_INVALID_HANDLE)) return GrB_UNINITIALIZED_OBJECT; 36 | if ((!op) || (op == GrB_INVALID_HANDLE)) return GrB_UNINITIALIZED_OBJECT; 37 | if ((!u) || (u == GrB_INVALID_HANDLE)) return GrB_UNINITIALIZED_OBJECT; 38 | if ( (desc == GrB_INVALID_HANDLE)) return GrB_UNINITIALIZED_OBJECT; 39 | 40 | // Check that the objects are valid 41 | if (accum && (!accum->valid())) return GrB_INVALID_OBJECT; 42 | if (op && (!op ->valid())) return GrB_INVALID_OBJECT; 43 | if (u && (!u ->valid())) return GrB_INVALID_OBJECT; 44 | if (desc && (!desc ->valid())) return GrB_INVALID_OBJECT; 45 | 46 | // Check domain conformity 47 | if (!u->D()->compatible(op->D()) ) return GrB_DOMAIN_MISMATCH; 48 | if (accum && !type->compatible(accum->D_in_1()) ) return GrB_DOMAIN_MISMATCH; 49 | if (accum && !type->compatible(accum->D_out()) ) return GrB_DOMAIN_MISMATCH; 50 | if (accum && !op->D()->compatible(accum->D_in_2())) return GrB_DOMAIN_MISMATCH; 51 | if (!accum && !type->compatible(op->D()) ) return GrB_DOMAIN_MISMATCH; 52 | 53 | // Perform the reduction 54 | Scalar sum = op->zero(); 55 | for (auto k : *(u->ind())) 56 | { 57 | Scalar a = (*u)[k]; 58 | op->f(sum,sum,a(op->D())); 59 | } 60 | 61 | // Compute final result 62 | if (accum) 63 | { 64 | Scalar v(type,val); 65 | Scalar final(accum->D_out()); 66 | accum->f(final,v(accum->D_in_1()),sum(accum->D_in_2())); 67 | memcpy(val,final(type),type->size()); 68 | } 69 | else 70 | { 71 | memcpy(val,sum(type),type->size()); 72 | } 73 | 74 | return GrB_SUCCESS; 75 | } 76 | catch (const Exception& e) 77 | { 78 | return e.info(); 79 | } 80 | } 81 | 82 | GrB_Info GrB_Vector_reduce_BOOL 83 | ( 84 | bool *val, 85 | const GrB_BinaryOp accum, 86 | const GrB_Monoid op, 87 | const GrB_Vector u, 88 | const GrB_Descriptor desc 89 | ) 90 | { 91 | return GrB_Vector_reduce_common(val,GrB_BOOL,accum,op,u,desc); 92 | } 93 | 94 | GrB_Info GrB_Vector_reduce_INT8 95 | ( 96 | int8_t *val, 97 | const GrB_BinaryOp accum, 98 | const GrB_Monoid op, 99 | const GrB_Vector u, 100 | const GrB_Descriptor desc 101 | ) 102 | { 103 | return GrB_Vector_reduce_common(val,GrB_INT8,accum,op,u,desc); 104 | } 105 | 106 | GrB_Info GrB_Vector_reduce_UINT8 107 | ( 108 | uint8_t *val, 109 | const GrB_BinaryOp accum, 110 | const GrB_Monoid op, 111 | const GrB_Vector u, 112 | const GrB_Descriptor desc 113 | ) 114 | { 115 | return GrB_Vector_reduce_common(val,GrB_UINT8,accum,op,u,desc); 116 | } 117 | 118 | GrB_Info GrB_Vector_reduce_INT16 119 | ( 120 | int16_t *val, 121 | const GrB_BinaryOp accum, 122 | const GrB_Monoid op, 123 | const GrB_Vector u, 124 | const GrB_Descriptor desc 125 | ) 126 | { 127 | return GrB_Vector_reduce_common(val,GrB_INT16,accum,op,u,desc); 128 | } 129 | 130 | GrB_Info GrB_Vector_reduce_UINT16 131 | ( 132 | uint16_t *val, 133 | const GrB_BinaryOp accum, 134 | const GrB_Monoid op, 135 | const GrB_Vector u, 136 | const GrB_Descriptor desc 137 | ) 138 | { 139 | return GrB_Vector_reduce_common(val,GrB_UINT16,accum,op,u,desc); 140 | } 141 | 142 | GrB_Info GrB_Vector_reduce_INT32 143 | ( 144 | int32_t *val, 145 | const GrB_BinaryOp accum, 146 | const GrB_Monoid op, 147 | const GrB_Vector u, 148 | const GrB_Descriptor desc 149 | ) 150 | { 151 | return GrB_Vector_reduce_common(val,GrB_INT32,accum,op,u,desc); 152 | } 153 | 154 | GrB_Info GrB_Vector_reduce_UINT32 155 | ( 156 | uint32_t *val, 157 | const GrB_BinaryOp accum, 158 | const GrB_Monoid op, 159 | const GrB_Vector u, 160 | const GrB_Descriptor desc 161 | ) 162 | { 163 | return GrB_Vector_reduce_common(val,GrB_UINT32,accum,op,u,desc); 164 | } 165 | 166 | GrB_Info GrB_Vector_reduce_INT64 167 | ( 168 | int64_t *val, 169 | const GrB_BinaryOp accum, 170 | const GrB_Monoid op, 171 | const GrB_Vector u, 172 | const GrB_Descriptor desc 173 | ) 174 | { 175 | return GrB_Vector_reduce_common(val,GrB_INT64,accum,op,u,desc); 176 | } 177 | 178 | GrB_Info GrB_Vector_reduce_UINT64 179 | ( 180 | uint64_t *val, 181 | const GrB_BinaryOp accum, 182 | const GrB_Monoid op, 183 | const GrB_Vector u, 184 | const GrB_Descriptor desc 185 | ) 186 | { 187 | return GrB_Vector_reduce_common(val,GrB_UINT64,accum,op,u,desc); 188 | } 189 | 190 | GrB_Info GrB_Vector_reduce_FP32 191 | ( 192 | float *val, 193 | const GrB_BinaryOp accum, 194 | const GrB_Monoid op, 195 | const GrB_Vector u, 196 | const GrB_Descriptor desc 197 | ) 198 | { 199 | return GrB_Vector_reduce_common(val,GrB_FP32,accum,op,u,desc); 200 | } 201 | 202 | GrB_Info GrB_Vector_reduce_FP64 203 | ( 204 | double *val, 205 | const GrB_BinaryOp accum, 206 | const GrB_Monoid op, 207 | const GrB_Vector u, 208 | const GrB_Descriptor desc 209 | ) 210 | { 211 | return GrB_Vector_reduce_common(val,GrB_FP64,accum,op,u,desc); 212 | } 213 | 214 | GrB_Info GrB_Vector_reduce_UDT 215 | ( 216 | void *val, 217 | const GrB_BinaryOp accum, 218 | const GrB_Monoid op, 219 | const GrB_Vector u, 220 | const GrB_Descriptor desc 221 | ) 222 | { 223 | if ((!u) || (u == GrB_INVALID_HANDLE)) return GrB_UNINITIALIZED_OBJECT; 224 | return GrB_Vector_reduce_common(val,u->D(),accum,op,u,desc); 225 | } 226 | -------------------------------------------------------------------------------- /src/template/GrB_mxm.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | GrB_Info GrB_mxm 21 | ( 22 | GrB_Matrix C, 23 | const GrB_Matrix Mask, 24 | const GrB_BinaryOp accum, 25 | const GrB_Semiring op, 26 | const GrB_Matrix A, 27 | const GrB_Matrix B, 28 | const GrB_Descriptor desc 29 | ) 30 | { 31 | try 32 | { 33 | // Check that the input objects are initialized 34 | if ((!C) || (C == GrB_INVALID_HANDLE)) return GrB_UNINITIALIZED_OBJECT; 35 | if ( (Mask == GrB_INVALID_HANDLE)) return GrB_UNINITIALIZED_OBJECT; 36 | if ( (accum == GrB_INVALID_HANDLE)) return GrB_UNINITIALIZED_OBJECT; 37 | if ((!op) || (op == GrB_INVALID_HANDLE)) return GrB_UNINITIALIZED_OBJECT; 38 | if ((!A) || (A == GrB_INVALID_HANDLE)) return GrB_UNINITIALIZED_OBJECT; 39 | if ((!B) || (B == GrB_INVALID_HANDLE)) return GrB_UNINITIALIZED_OBJECT; 40 | if ( (desc == GrB_INVALID_HANDLE)) return GrB_UNINITIALIZED_OBJECT; 41 | 42 | // Check that the objects are valid 43 | if (C && (!C ->valid())) return GrB_INVALID_OBJECT; 44 | if (Mask && (!Mask ->valid())) return GrB_INVALID_OBJECT; 45 | if (accum && (!accum->valid())) return GrB_INVALID_OBJECT; 46 | if (op && (!op ->valid())) return GrB_INVALID_OBJECT; 47 | if (A && (!A ->valid())) return GrB_INVALID_OBJECT; 48 | if (B && (!B ->valid())) return GrB_INVALID_OBJECT; 49 | if (desc && (!desc ->valid())) return GrB_INVALID_OBJECT; 50 | 51 | // Check domain conformity 52 | if (Mask && (!Mask->D()->predefined()) ) return GrB_DOMAIN_MISMATCH; 53 | if (!A->D()->compatible(op->D_in_1()) ) return GrB_DOMAIN_MISMATCH; 54 | if (!B->D()->compatible(op->D_in_2()) ) return GrB_DOMAIN_MISMATCH; 55 | if (!C->D()->compatible(op->D_out()) ) return GrB_DOMAIN_MISMATCH; 56 | if (accum && !C->D()->compatible(accum->D_in_1()) ) return GrB_DOMAIN_MISMATCH; 57 | if (accum && !C->D()->compatible(accum->D_out()) ) return GrB_DOMAIN_MISMATCH; 58 | if (accum && !op->D_out()->compatible(accum->D_in_2())) return GrB_DOMAIN_MISMATCH; 59 | 60 | // Decode descriptor 61 | bool Replace = (desc && desc->outp_replace()); 62 | bool SCMP = (desc && desc->mask_scmp()); 63 | bool tranA = (desc && desc->inp0_tran()); 64 | bool tranB = (desc && desc->inp1_tran()); 65 | 66 | // Prepare internal matrices for operation 67 | GrB_Matrix_t C_tilde(*C); 68 | GrB_Mask_t M_tilde(SCMP,Mask,*C); 69 | GrB_Matrix_t A_tilde(tranA,*A); 70 | GrB_Matrix_t B_tilde(tranB,*B); 71 | 72 | // Check dimension comformity 73 | if (C_tilde.nrows() != M_tilde.nrows()) return GrB_DIMENSION_MISMATCH; 74 | if (C_tilde.ncols() != M_tilde.ncols()) return GrB_DIMENSION_MISMATCH; 75 | if (C_tilde.nrows() != A_tilde.nrows()) return GrB_DIMENSION_MISMATCH; 76 | if (C_tilde.ncols() != B_tilde.ncols()) return GrB_DIMENSION_MISMATCH; 77 | if (A_tilde.ncols() != B_tilde.nrows()) return GrB_DIMENSION_MISMATCH; 78 | 79 | // Perform the multiplication 80 | GrB_Matrix_t T_tilde(op->D_out(),A_tilde.nrows(),B_tilde.ncols()); 81 | T_tilde.AxB(op,A_tilde,B_tilde); 82 | 83 | // Accumulate as necessary 84 | GrB_Matrix_t Z_tilde; 85 | accum ? Z_tilde.init(accum->D_out(),C_tilde.nrows(),C_tilde.ncols()) 86 | && Z_tilde.add(accum,C_tilde,T_tilde) 87 | : Z_tilde.copy(T_tilde); 88 | 89 | // Mask and replace 90 | Replace ? C->replace(M_tilde,Z_tilde) : C->merge(M_tilde,Z_tilde); 91 | 92 | return GrB_SUCCESS; 93 | } 94 | catch (const Exception& e) 95 | { 96 | return e.info(); 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /test/src/test_operations.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | #include 21 | #include "gtest/gtest.h" 22 | 23 | namespace { 24 | 25 | class GrBt_Operations: public ::testing::Test { 26 | protected: 27 | // Any of the following functions can be removed if its body is empty 28 | GrBt_Operations() { 29 | // Do set-up work for each test here. 30 | } 31 | 32 | virtual ~GrBt_Operations() { 33 | // Do clean-up work that doesn't throw exceptions here. 34 | } 35 | 36 | // If the constructor and destructor are not enough for setting up 37 | // and cleaning up each test then define the following methods: 38 | 39 | virtual void SetUp() { 40 | // Code here will be called immediately after the constructor (right 41 | // before each test). 42 | } 43 | 44 | virtual void TearDown() { 45 | // Code here will be called immediately after each test (right 46 | // before the destructor). 47 | } 48 | 49 | // Objects declared here can be used by all tests in the test case for this class. 50 | static double vectors_dot_prod(const double *x, const double *y, int n) 51 | { 52 | double res = 0.0; 53 | int i; 54 | for (i = 0; i < n; i++) 55 | { 56 | res += x[i] * y[i]; 57 | } 58 | return res; 59 | } 60 | 61 | static void matrix_vector_mult(const double **mat, const double *vec, 62 | double *result, int rows, int cols) { // in matrix form: result = mat * vec; 63 | int i; 64 | for (i = 0; i < rows; i++) { 65 | result[i] = vectors_dot_prod(mat[i], vec, cols); 66 | } 67 | } 68 | }; 69 | // Tests that the Foo::Bar() method does Abc. 70 | TEST_F(GrBt_Operations, mTv) { 71 | ASSERT_EQ(GrB_init (GrB_NONBLOCKING), GrB_SUCCESS); 72 | const GrB_Index nv = 3; 73 | 74 | // **** 75 | // setup test matricies 76 | GrB_Index r; 77 | GrB_Index c; 78 | 79 | int **mv = (int **) malloc(nv * sizeof(int*)); 80 | for (r = 0; r < nv; r++) { 81 | mv[r] = (int *) malloc(nv * sizeof(int)); 82 | } 83 | for (r = 0; r < nv; r++) { 84 | for (c = 0; c < nv; c++) { 85 | int32_t val = c * nv + r; 86 | mv[r][c] = val; 87 | } 88 | } 89 | 90 | 91 | // GrB Matrix 92 | GrB_Matrix m; 93 | ASSERT_EQ(GrB_Matrix_new (&m, GrB_INT32, nv, nv), GrB_SUCCESS); 94 | for (r = 0; r < nv; r++) { 95 | for (c = 0; c < nv; c++) { 96 | int32_t val = mv[r][c]; 97 | ASSERT_EQ(GrB_Matrix_setElement_INT32(m, val, r, c), GrB_SUCCESS); 98 | } 99 | } 100 | // check test matrix assignments 101 | for (r = 0; r < nv; r++) { 102 | for (c = 0; c < nv; c++) { 103 | int32_t eval; 104 | ASSERT_EQ(GrB_Matrix_extractElement_INT32(&eval, m, r, c), GrB_SUCCESS); 105 | ASSERT_EQ(eval, mv[r][c]); 106 | } 107 | } 108 | 109 | // **** 110 | // setup test vectors 111 | // int *v = (int *) malloc(nv * sizeof(int)); 112 | // GrB Vector 113 | GrB_Vector v; 114 | ASSERT_EQ(GrB_Vector_new (&v, GrB_INT32, nv), GrB_SUCCESS); 115 | for (r = 0; r < nv; r++) { 116 | int32_t val = r; 117 | ASSERT_EQ(GrB_Vector_setElement_INT32(v, val, r), GrB_SUCCESS); 118 | } 119 | // check vector assignments 120 | for (r = 0; r < nv; r++) { 121 | int32_t eval; 122 | ASSERT_EQ(GrB_Vector_extractElement_INT32(&eval, v, r), GrB_SUCCESS); 123 | } 124 | 125 | // set up semiring 126 | GrB_Monoid f; 127 | ASSERT_EQ(GrB_Monoid_new_INT32(&f, GrB_PLUS_INT32, 0), GrB_SUCCESS); 128 | GrB_Semiring sr; 129 | ASSERT_EQ(GrB_Semiring_new(&sr, f, GrB_TIMES_INT32), GrB_SUCCESS); 130 | 131 | // descriptor 132 | GrB_Descriptor desc; 133 | ASSERT_EQ(GrB_Descriptor_new (&desc), GrB_SUCCESS); 134 | ASSERT_EQ(GrB_Descriptor_set (desc, GrB_OUTP, GrB_REPLACE), GrB_SUCCESS); 135 | // ASSERT_EQ(GrB_Descriptor_set (desc, GrB_MASK, GrB_SCMP), GrB_SUCCESS); 136 | // ASSERT_EQ(GrB_Descriptor_set (desc, GrB_INP0, GrB_TRAN), GrB_SUCCESS); 137 | 138 | // target vector 139 | GrB_Vector u; 140 | ASSERT_EQ(GrB_Vector_new (&u, GrB_INT32, nv), GrB_SUCCESS); 141 | 142 | // multiplication 143 | ASSERT_EQ(GrB_mxv (u, (const GrB_Vector)GrB_NULL, (const GrB_BinaryOp)GrB_NULL, sr, m, v, desc), GrB_SUCCESS); 144 | 145 | // result 146 | static int32_t ua[3] = {15, 18, 21}; 147 | // check result 148 | for (r = 0; r < nv; r++) { 149 | int32_t eval; 150 | ASSERT_EQ(GrB_Vector_extractElement_INT32(&eval, u, r), GrB_SUCCESS); 151 | EXPECT_EQ(ua[r], eval); 152 | } 153 | 154 | // **** 155 | // set up semiring using polymorphic interface 156 | GrB_Monoid fPoly; 157 | ASSERT_EQ(GrB_Monoid_new(&fPoly, GrB_PLUS_INT32, 0), GrB_SUCCESS); 158 | GrB_Semiring srPoly; 159 | ASSERT_EQ(GrB_Semiring_new(&srPoly, fPoly, GrB_TIMES_INT32), GrB_SUCCESS); 160 | // multiplication 161 | ASSERT_EQ(GrB_mxv (u, (const GrB_Vector)GrB_NULL, (const GrB_BinaryOp)GrB_NULL, sr, m, v, desc), GrB_SUCCESS); 162 | // check result 163 | for (r = 0; r < nv; r++) { 164 | int32_t eval; 165 | ASSERT_EQ(GrB_Vector_extractElement_INT32(&eval, u, r), GrB_SUCCESS); 166 | EXPECT_EQ(ua[r], eval); 167 | } 168 | 169 | // const std::string input_filepath = "this/package/testdata/myinputfile.dat"; 170 | // const std::string output_filepath = "this/package/testdata/myoutputfile.dat"; 171 | // Foo f; 172 | // EXPECT_EQ(0, f.Bar(input_filepath, output_filepath)); 173 | // EXPECT_EQ(0, 0); 174 | } 175 | 176 | //// Tests that Foo does Xyz. 177 | //TEST_F(GrBt_Operations, DoesXyz) { 178 | // // Exercises the Xyz feature of Foo. 179 | //} 180 | 181 | } // namespace 182 | //test("LagSmpContext.mTv") { 183 | // val nv = 3 184 | // val hc: LagContext = LagContext.getLagSmpContext(nv) 185 | // val sparseValueInt = 0 186 | // val mv = Vector.tabulate(nv, nv)((r, c) => c * nv + r) 187 | // val m = hc.mFromMap(LagContext.mapFromSeqOfSeq(mv, sparseValueInt), sparseValueInt) 188 | // val v = hc.vFromSeq(Range(0, nv).toVector, sparseValueInt) 189 | // // object add_mul extends LagSemiring[Int] { 190 | // // override val addition = (x: Int, y: Int) => x + y 191 | // // val multiplication = (x: Int, y: Int) => x * y 192 | // // val zero = 0 193 | // // val one = 1 194 | // // } 195 | // val add_mul = LagSemiring.plus_times[Int] 196 | // val u = hc.mTv(add_mul, m, v) 197 | // val ua = Vector(15, 18, 21) 198 | // assert(ua.corresponds(hc.vToVector(u))(_ == _)) 199 | //} 200 | 201 | --------------------------------------------------------------------------------