-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgenerator_seq.c
executable file
·142 lines (126 loc) · 4.24 KB
/
generator_seq.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
/* Copyright (C) 2009-2010 The Trustees of Indiana University. */
/* */
/* Use, modification and distribution is subject to the Boost Software */
/* License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at */
/* http://www.boost.org/LICENSE_1_0.txt) */
/* */
/* Authors: Jeremiah Willcock */
/* Andrew Lumsdaine */
/* Revised */
#include <math.h>
#include <stdlib.h>
#include <stdint.h>
#ifndef __STDC_FORMAT_MACROS
#define __STDC_FORMAT_MACROS
#endif
#include <inttypes.h>
#include <stdio.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
#include <getopt.h>
#include "make_graph.h"
inline double get_time() {
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec + tv.tv_usec * 1.e-6;
}
void printError() {
fprintf(stderr, "usage: ./generator_seq <# of vertices (log 2 base)> <-e intNumber [optional: average # of edges per vertex, defualt to be 16> <-o outputFileName [optional: default to stdout]> <-s randomSeedInt [optional: default to use the current time]> <-b [optional: default is ascii version, -b for binary version]>\n");
exit(0);
}
int main(int argc, char* argv[]) {
struct timeval currentTime;
gettimeofday(¤tTime, NULL);
int seed = currentTime.tv_sec ^ currentTime.tv_usec;
seed ^= seed >> 12;
seed ^= seed << 25;
seed ^= seed >> 27;
FILE *fout;
if (argc < 2 || argc > 10) {
printError();
}
// define all the variables
int log_numverts = -1;
char * filename = "";
long int numEdges;
double start, time_taken, start_write, time_taken_write;
int64_t nedges;
packed_edge* result;
int binary = 0; // set default to be not binary, normal tsv
numEdges = 16; // default 16
fout = stdout; // default the stdout
int opt;
while(optind < argc) {
if ((opt = getopt(argc, argv, "+e:o:s:b")) != -1) {
switch (opt) {
case 'e':
numEdges = atoi(optarg);
break;
case 'o':
filename = optarg;
fout = fopen(optarg, "wb");
if (fout == NULL) {
fprintf(stderr, "%s -- ", optarg);
perror("fopen for write failed");
exit(1);
}
break;
case 's':
seed = atoi(optarg);
break;
case 'b':
binary = 1;
break;
default:
printError();
break;
}
} else {
if(argv[optind] == NULL) {
printError();
} else {
log_numverts = atoi(argv[optind]); // In base 2
optind++;
}
}
}
if( log_numverts < 0 ) {
printError();
}
//Start of graph generation timing
start = get_time();
make_graph(log_numverts, numEdges << log_numverts, seed, seed, &nedges, &result);
time_taken = get_time() - start;
printf("For 2^%d\n", log_numverts);
printf("\t%f seconds for making graph\n", time_taken);
if (binary == 0) {
// print to the file
start_write = get_time();
for (int i = 0; i < (numEdges << log_numverts); i++) {
fprintf(fout, "%lu\t%lu\n", get_v0_from_edge(result + i), get_v1_from_edge(result + i));
}
time_taken_write = get_time() - start_write;
printf("\t%f seconds for writing ascii version\n", time_taken_write);
} else {
// need to print binary
start_write = get_time();
for (int i = 0; i < (numEdges << log_numverts); i++) {
uint32_t from = get_v0_from_edge(result + i);
uint32_t to = get_v1_from_edge(result + i);
// add the check for not exceed the uint32_t max
fwrite((const void*) & from,sizeof(uint32_t),1,fout);
fwrite((const void*) & to,sizeof(uint32_t),1,fout);
}
time_taken_write = get_time() - start_write;
printf("\t%f seconds for writing binary version\n", time_taken_write);
}
int check_correctness;
check_correctness = fclose(fout);
if (check_correctness == EOF) {
fprintf(stderr, "%s -- ", filename);
perror("fclose failed");
exit(1);
}
return 0;
}