-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathk2unary.c
More file actions
153 lines (136 loc) · 4.79 KB
/
Copy pathk2unary.c
File metadata and controls
153 lines (136 loc) · 4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/* Demo of unary operations on boolean matrices represented as k2 trees
if compiled with the K2MAT constant
undefined it generates the executable b128sparse.x which (de)compress matrices
in text form to/from the B128 format (one bit x entry)
Copyright August 2025-today --- giovanni.manzini@unipi.it
*/
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <assert.h>
#include <time.h>
#include <limits.h>
// definitions to be used for b128 vs k2t-encoded matrices
#ifdef K2MAT
#include "k2.h"
extern bool Use_all_ones_node; // use the special ALL_ONES node in the result
extern bool Extended_edf; // compute subtree info on the fly
#else // definitions for b128 matrices
#include "b128.h"
#define K2MAT_INITIALIZER B128MAT_INITIALIZER
typedef b128mat_t k2mat_t;
bool Use_all_ones_node; // not used: added for compatibility with k2mat
bool Extended_edf; // not used: added for compatibility with k2mat
#endif
// static functions at the end of the file
static void usage_and_exit(char *name);
int main (int argc, char **argv) {
extern char *optarg;
extern int optind, opterr, optopt;
int verbose=0;
int c;
char iname1[PATH_MAX], oname[PATH_MAX];
#ifdef K2MAT
char *infofile1=NULL;
char *backpfile1=NULL; // file with backpointers
uint32_t rank_block_size = 64; // block size for rank DS
#endif
time_t start_wc = time(NULL);
/* ------------- read options from command line ----------- */
opterr = 0;
char *outfile = NULL;
Use_all_ones_node = true; Extended_edf = false;
while ((c=getopt(argc, argv, "i:r:I:o:hvxe")) != -1) {
switch (c)
{
case 'o':
outfile = optarg; break;
#ifdef K2MAT
case 'I':
backpfile1 = optarg; break;
case 'i':
infofile1 = optarg; break;
case 'e':
Extended_edf = true; break; // compute subtree info on the fly
case 'x':
Use_all_ones_node = false; break;
case 'r':
rank_block_size = atoi(optarg); break; // block size of rank structure
#endif
case 'h':
usage_and_exit(argv[0]); break;
case 'v':
verbose++; break;
case '?':
fprintf(stderr,"Unknown option: %c\n", optopt);
exit(1);
}
}
if(verbose>0) {
fputs("==== Command line:\n",stdout);
for(int i=0;i<argc;i++)
fprintf(stdout," %s",argv[i]);
fputs("\n",stdout);
}
// virtually get rid of options from the command line
optind -=1;
if (argc-optind != 2) usage_and_exit(argv[0]);
argv += optind; argc -= optind;
// create file names
sprintf(iname1,"%s",argv[1]);
if(outfile==NULL) outfile = argv[1];
// init matrix variables (valid for b128 and k2tree)
k2mat_t a=K2MAT_INITIALIZER;
// load first matrix possibly initializing k2 library
#ifdef K2MAT
mload_extended(&a, iname1, infofile1, backpfile1, rank_block_size);
#else
mload_from_file(&a, iname1);
#endif
if (verbose) mshow_stats(&a,iname1,stdout);
// add zero matrix
k2mat_t b=mat_zero(&a), a0=mat_zero(&a);
msum(&b,&a,&a0); // a0 = b+a = 0+a = a
sprintf(oname,"%s.0.txt",outfile);
if(verbose) mshow_stats(&a0,oname,stdout);
mwrite_to_textfile(&a0, oname);
// add main diagonal 1's
madd_identity(&a);
sprintf(oname,"%s.1.txt",outfile);
if(verbose) mshow_stats(&a,oname,stdout);
mwrite_to_textfile(&a, oname);
// squaring (using a single copy of the :a matrix)
k2mat_t asq = K2MAT_INITIALIZER;
mmult(&a,&a,&asq);
if(verbose) mshow_stats(&asq,"(A+I)^2",stdout);
sprintf(oname,"%s.1sq.txt",outfile);
mwrite_to_textfile(&asq,oname);
// done
matrix_free(&a);
minimat_reset(); // reset the minimat library and free minimat product table
// report running time
fprintf(stderr,"Elapsed time: %.8lf secs\n", ((double) (time(NULL)-start_wc)));
if(verbose) fprintf(stderr,"==== Done\n");
return EXIT_SUCCESS;
}
static void usage_and_exit(char *name)
{
fprintf(stderr,"Usage:\n\t %s [options] infile\n\n",name);
fprintf(stderr,"Demo of unary operations on the compressed matrices stored in infile\n\n");
fputs("Options:\n",stderr);
fprintf(stderr,"\t-o out outfile base name (def. infile)\n");
#ifdef K2MAT
fprintf(stderr,"\t-i info infile subtree info file\n");
fprintf(stderr,"\t-I info infile backpointers file\n");
fprintf(stderr,"\t-r size rank block size for k2 compression (def. 64)\n");
fprintf(stderr,"\t-e compute subtree info on the fly (def. no)\n");
fprintf(stderr,"\t-x do not compact new 1's submatrices in the result matrix\n");
#endif
fprintf(stderr,"\t-h show this help message\n");
fprintf(stderr,"\t-v verbose\n\n");
exit(1);
}