-
Notifications
You must be signed in to change notification settings - Fork 0
/
nl-benchmark.cc
109 lines (94 loc) · 3.21 KB
/
nl-benchmark.cc
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
/*
nl reader benchmark
Copyright (C) 2014 AMPL Optimization Inc
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies and that
both that the copyright notice and this permission notice and warranty
disclaimer appear in supporting documentation.
The author and AMPL Optimization Inc disclaim all warranties with
regard to this software, including all implied warranties of
merchantability and fitness. In no event shall the author be liable
for any special, indirect or consequential damages or any damages
whatsoever resulting from loss of use, data or profits, whether in an
action of contract, negligence or other tortious action, arising out
of or in connection with the use or performance of this software.
Author: Victor Zverovich
*/
#include <cstring>
#include "mp/clock.h"
#include "mp/nl-reader.h"
#include "mp/problem.h"
#include "mp/problem-builder.h"
#include "asl.h"
#undef filename
// Computes time to run f.
template <typename Func>
void Time(Func func, char **filenames) {
for (; *filenames; ++filenames) {
const char *filename = *filenames;
mp::steady_clock::time_point start = mp::steady_clock::now();
func(filename);
double time = mp::GetTimeAndReset(start);
fmt::print("{}\t{}\n", filename, time);
}
}
// Reads a file and returns the last character.
std::size_t ReadFile(const char *filename) {
enum {BUFFER_SIZE = 4096};
char buffer[BUFFER_SIZE];
std::FILE *f = std::fopen(filename, "r");
std::size_t size = 0;
for (;;) {
std::size_t num_read = std::fread(buffer, 1, BUFFER_SIZE, f);
size += num_read;
if (num_read < BUFFER_SIZE) {
if (!std::feof(f))
throw std::runtime_error("I/O error");
break;
}
}
std::fclose(f);
return size;
}
// Reads an nl file using mp::ReadNLFile.
void ReadNLFileUsingMP(const char *filename) {
mp::NullNLHandler<int> handler;
mp::ReadNLFile(filename, handler);
}
// Reads an nl file and build an mp::Problem object using mp::ReadNLFile.
void ReadNLFileAndBuildProblemUsingMP(const char *filename) {
mp::Problem problem;
mp::ReadNLFile(filename, problem);
}
// Reads an nl file using ASL functions jac0dim and fg_read.
void ReadNLFileUsingASL(const char *filename) {
ASL *asl = ASL_alloc(ASL_read_fg);
FILE *nl = jac0dim(filename, 0);
fg_read(nl, ASL_allow_missing_funcs);
ASL_free(&asl);
}
int PrintUsage(char **argv) {
fmt::print(stderr, "usage: {} [io|mp-read|mp-build|asl] FILE...\n", argv[0]);
return 0;
}
int main(int argc, char **argv) {
try {
if (argc < 3)
return PrintUsage(argv);
const char *method = argv[1];
char **filenames = argv + 2;
if (std::strcmp(method, "io") == 0)
Time(ReadFile, filenames);
else if (std::strcmp(method, "mp-read") == 0)
Time(ReadNLFileUsingMP, filenames);
else if (std::strcmp(method, "mp-build") == 0)
Time(ReadNLFileAndBuildProblemUsingMP, filenames);
else if (std::strcmp(method, "asl") == 0)
Time(ReadNLFileUsingASL, filenames);
else PrintUsage(argv);
} catch (const std::exception &e) {
fmt::print(stderr, "Error: {}\n", e.what());
return 1;
}
}