forked from jmhays/MIMatrix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
102 lines (88 loc) · 2.89 KB
/
main.cpp
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
#include <iostream>
#include <getopt.h>
#include "MIUtils.h"
#include "MI.h"
#include <omp.h>
struct globalArgs_t {
const char *inFileName; /* -f option */
const char *outFileName; /* -o option */
int numThreads; /* -n option */
const char *delimiter; /* -d option */
bool metric; /* -m option*/
} globalArgs;
static const char *optString = "f:o:n:d:mh?";
void display_usage(void) {
puts("\n========MI MATRIX CALCULATOR========\n");
puts("Information - calculates MI matrix from input file");
puts("-f\t\tFull path to input file.");
puts("-d\t\tDelimiter of csv file");
puts("-m\t\tMetric option");
puts("-o\t\tOutput filename for MI matrix.");
puts("-n\t\tInteger number of threads for parallel calculation");
exit( EXIT_FAILURE );
}
using namespace std;
int main(int argc, char *argv[]) {
cout << "\t====================\n"
"\tMI MATRIX CALCULATOR\n"
"\t====================" << endl;
/*
* OPTION PARSING
*/
int opt = 0;
// Initialize global arguments
globalArgs.inFileName = NULL;
globalArgs.outFileName = NULL;
globalArgs.numThreads = 1; // default is to run sequentially
globalArgs.delimiter = ",";
globalArgs.metric = 0;
opt = getopt(argc, argv, optString);
while (opt != -1) {
switch (opt) {
case 'f':
globalArgs.inFileName = optarg;
break;
case 'o':
globalArgs.outFileName = optarg;
break;
case 'n':
globalArgs.numThreads = atoi(optarg);
break;
case 'd':
globalArgs.delimiter = optarg;
break;
case 'm':
globalArgs.metric = 1;
break;
case 'h':
case '?':
display_usage();
break;
default:
break;
}
opt = getopt(argc, argv, optString);
}
/*
* READ INPUT FILE
*/
auto data = readCSV(globalArgs.inFileName,
globalArgs.delimiter);
auto numFeatures = data.size();
/*
* BEGIN MI CALCULATIONS
*/
vector<vector<float>> miMatrix(numFeatures, vector<float>(numFeatures, 0));
auto shardedIndices = shardIndices(numFeatures, globalArgs.numThreads);
printf("Main sees %i available threads\n", omp_get_max_threads());
printf("Starting a pool of %i threads\n", globalArgs.numThreads);
clock_t t;
t = clock();
#pragma omp parallel for
for(int i = 0; i < globalArgs.numThreads; i++) {
miMatrixBlock(data, miMatrix, shardedIndices[i], globalArgs.metric);
}
t = (clock() - t);
printf("Time to complete all MI calculations: %f sec\n", (float)t/CLOCKS_PER_SEC);
dumpCSV(globalArgs.outFileName, globalArgs.delimiter, miMatrix);
}