-
Notifications
You must be signed in to change notification settings - Fork 0
/
v3_3mergeOmpInStatic.c
168 lines (127 loc) · 4.57 KB
/
v3_3mergeOmpInStatic.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
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
#include "mmio.h"
#include "coo2csc.h"
#include "timediff.h"
#include <string.h>
extern const char *__progname;
int main(int argc, char const *argv[])
{
int ret_code;
MM_typecode matcode;
FILE *f;
uint32_t M, N, nnz;
uint32_t *I, *J;
uint32_t *c3;
uint32_t isOneBased=0;
struct timespec tStart,tEnd;
//--------------------------------------------------file reading-----------------------------------------------
if(argc!=4){
printf("\nusage: ./v3_3mergeOmpInStatic ./*.mtx num_threads times_to_run\n");
exit(1);
}
if(atoi(argv[2])>omp_get_max_threads()||atoi(argv[2])<1){
printf("num_threads should be between 1 and %d on this machine\n",omp_get_max_threads());
exit(1);
}
omp_set_num_threads(atoi(argv[2]));
int threads =atoi(argv[2]);
if(atoi(argv[3])<0 || atoi(argv[3])>100){
printf("1 <= num_times <= 100\n");
exit(1);
}
int num_times=atoi(argv[3]);
if ((f = fopen(argv[1], "r")) == NULL)
exit(1);
if (mm_read_banner(f, &matcode) != 0){
printf("Could not process Matrix Market banner.\n");
exit(1);
}
if(!mm_is_symmetric(matcode)){
printf("matrix %s isnt symmetric\n",argv[1]);
exit(1);
}
if ((ret_code = mm_read_mtx_crd_size(f, &M, &N, &nnz)) !=0)
exit(1);
I = (uint32_t *) malloc(nnz * sizeof(uint32_t));
J = (uint32_t *) malloc(nnz * sizeof(uint32_t));
for (uint32_t i=0; i<nnz; i++)
{
fscanf(f, "%u %u\n", &I[i], &J[i]);
I[i]--; /* adjust from 1-based to 0-based */
J[i]--;
}
if (f !=stdin) fclose(f);
//------------------------------------------coo2csc, memmory allocation and variable creation-----------------------------------------------
c3 =(uint32_t *)malloc(M * sizeof(uint32_t));
uint32_t * csc_row = (uint32_t *)malloc((M + 1) * sizeof(uint32_t));
uint32_t * csc_col = (uint32_t *)malloc(nnz * sizeof(uint32_t));
coo2csc(csc_col,csc_row,I,J,nnz,M,isOneBased);
free(I);
free(J);
uint32_t i,j;
uint32_t n1,n2,t1,t2;
long long triangles;
double minTime = 100;
//------------temporary threadprivate c3 array------------
uint32_t *c3temp[threads];
//--------------------------------------------------main algorithm-----------------------------------------------
for(int times=0;times<num_times;times++){
for(i=0;i<M;i++) c3[i]=0;
clock_gettime(CLOCK_MONOTONIC, &tStart);
#pragma omp parallel private(i,j,t1,t2,n1,n2)
{
int threadid=omp_get_thread_num();
c3temp[threadid] = (uint32_t *)malloc(M * sizeof(uint32_t));
memcpy(c3temp[threadid],c3,M*sizeof(uint32_t));
for(i=0;i<M-2;i++){
#pragma omp for schedule(static) nowait
for(j=csc_row[i];j<csc_row[i+1]-1;j++){
t1=csc_row[csc_col[j]];
t2=j+1;
n1=csc_row[csc_col[j]+1];
n2=csc_row[i+1];
while(t1<n1 && t2<n2){
if(csc_col[t1]==csc_col[t2]){
c3temp[threadid][i]++; c3temp[threadid][csc_col[j]]++; c3temp[threadid][csc_col[t2]]++;
t1++;
t2++;
}else if(csc_col[t1]<csc_col[t2]){
t1++;
}else{
t2++;
}
}
}
}
//------------final c3 evaluation and memory deallocation------------
#pragma omp critical
{
for(j=0;j<M;j++){
c3[j]+=c3temp[threadid][j];
}
}
free(c3temp[threadid]);
}
/* for(i=0;i<threads;i++){
for(j=0;j<M;j++){
c3[j]+=c3temp[i][j];
}
free(c3temp[i]);
} */
clock_gettime(CLOCK_MONOTONIC, &tEnd);
struct timespec tResult = diff(tStart,tEnd);
double timeR = (double)(tResult.tv_sec+(double)tResult.tv_nsec/1000000000);
if(timeR<minTime)
minTime=timeR;
triangles=0;
for(i = 0; i<M;i++)
triangles+=c3[i];
}
printf("%s,%d,%.9lf,%lld\n",__progname,threads,minTime, triangles/3);
free(csc_col);
free(csc_row);
free(c3);
return 0;
}