-
Notifications
You must be signed in to change notification settings - Fork 0
/
V3.c
196 lines (135 loc) · 4.81 KB
/
V3.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
#include <stdbool.h>
#include "mmio.c"
struct timespec t_start, t_end;
u_int32_t coo2csc(
u_int32_t * const row, /*!< CSC row start indices */
u_int32_t * const col, /*!< CSC column indices */
u_int32_t const * const row_coo, /*!< COO row indices */
u_int32_t const * const col_coo, /*!< COO column indices */
u_int32_t const nnz, /*!< Number of nonzero elements */
u_int32_t const n, /*!< Number of rows/columns */
u_int32_t const isOneBased /*!< Whether COO is 0- or 1-based */
) {
// ----- cannot assume that input is already 0!
for(u_int32_t l = 0; l < n+1; l++) col[l] = 0;
// ----- find the correct column sizes
for(u_int32_t l = 0; l < nnz; l++)
col[col_coo[l] - isOneBased]++;
// ----- cumulative sum
for(u_int32_t i = 0, cumsum = 0; i < n; i++) {
u_int32_t temp = col[i];
col[i] = cumsum;
cumsum += temp;
}
col[n] = nnz;
// ----- copy the row indices to the correct place
for(u_int32_t l = 0; l < nnz; l++) {
u_int32_t col_l;
col_l = col_coo[l] - isOneBased;
u_int32_t dst = col[col_l];
row[dst] = row_coo[l] - isOneBased;
col[col_l]++;
}
// ----- revert the column pointers
for(u_int32_t i = 0, last = 0; i < n; i++) {
u_int32_t temp = col[i];
col[i] = last;
last = temp;
}
return n;
}
/* Reads a MMfile */
u_int32_t cooReader(char* name, u_int32_t* I, u_int32_t* J, u_int32_t* II, u_int32_t* JJ){
int ret_code;
MM_typecode matcode;
FILE *f;
int M, N, nz;
int i;
double *val;
if ((f = fopen( name, "r")) == NULL)
exit(1);
if (mm_read_banner(f, &matcode) != 0)
{
printf("Could not process Matrix Market banner.\n");
exit(1);
}
/* This is how one can screen matrix types if their application */
/* only supports a subset of the Matrix Market data types. */
if (mm_is_complex(matcode) && mm_is_matrix(matcode) &&
mm_is_sparse(matcode))
{
printf("Sorry, this application does not support ");
printf("Market Market type: [%s]\n", mm_typecode_to_str(matcode));
exit(1);
}
/* find out size of sparse matrix .... */
if ((ret_code = mm_read_mtx_crd_size(f, &M, &N, &nz)) !=0)
exit(1);
/* reseve memory for matrices */
I = (int *) malloc(nz * sizeof(int));
J = (int *) malloc(nz * sizeof(int));
/* NOTE: when reading in doubles, ANSI C requires the use of the "l" */
/* specifier as in "%lg", "%lf", "%le", otherwise errors will occur */
/* (ANSI C X3.159-1989, Sec. 4.9.6.2, p. 136 lines 13-15) */
for (i=0; i<nz; i++)
{
fscanf(f, "%d %d \n", &I[i], &J[i]);
I[i]--; /* adjust from 1-based to 0-based */
J[i]--;
}
if (f !=stdin) fclose(f);
/************************/
/* now write out matrix */
/************************/
mm_write_banner(stdout, matcode);
mm_write_mtx_crd_size(stdout, M, N, nz);
// for (i=0; i<nz; i++)
// fprintf(stdout, "%d %d \n", I[i]+1, J[i]+1);
//return converter(I,J,val,nz,nz,nz,II,JJ);
printf("nzz=%d\n",nz);
return coo2csc(II, JJ, I, J,nz, N,0);
}
int* V3(int* row, int* col, int N){
int tr = 0;
int *colPsearch;
int *c3 = (int *)calloc(N, sizeof(int));
clock_gettime(CLOCK_REALTIME, &t_start);
for(int i=0; i<N; i++){
for(int e=row[i]; e<row[i+1]; e++){
for(int j=row[i]; j<row[i+1]; j++){
for(int z=row[col[e]]; z<row[col[e]+1]; z++){
if(col[z] == col[j]){
c3[i]++;
c3[col[e]]++;
c3[col[j]]++;
break;
}
}
}
}
}
clock_gettime(CLOCK_REALTIME, &t_end);
for(int i=0; i<N; i++)
tr+=c3[i];
double duration = ((t_end.tv_sec - t_start.tv_sec) * 1000000 + (t_end.tv_nsec - t_start.tv_nsec) / 1000) / 1000000.0;
printf("~ TRIANGLES: %d\n",tr/3);
printf("~ Duration : %f\n", duration);
return c3;
}
void main(int argc, char *argv[]){
char* str = argv[1];
int combinationsNum = atoi(argv[2]);
int rowsNum = atoi(argv[3]);
u_int32_t *I;
u_int32_t *J;
u_int32_t *CSCrows = (u_int32_t *) malloc(combinationsNum * sizeof(u_int32_t));
u_int32_t *CSCcols = (u_int32_t *) malloc((rowsNum + 1)* sizeof(u_int32_t));
u_int32_t rowptrSize = cooReader(str, I, J, CSCrows, CSCcols);
printf("Graph : %s\n", str);
V3(CSCcols, CSCrows, rowptrSize);
}