-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathDistParNelderMead_Driver.cpp
54 lines (50 loc) · 1.51 KB
/
DistParNelderMead_Driver.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
/*
* NelderMead_Driver.cpp
*
* Based on the implementations by Kyle Klein and Jeff Borggaard.
*
*/
#include <cmath>
#include <iostream>
#include <mpi.h>
#include "DistParNelderMead.hpp"
#include "ObjFunction.hpp"
int main(int argc, char **argv) {
int rank = 0, size = 1;
int prob_size = 300, pointsPerIter = 1, max_iterations = -1;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if (argc <= 2) {
std::cerr << "Error: incorrect usage ./execname <prob_size>"
"<points per iter> <max iterations (optional)>\n";
exit(1);
}
if (argc > 3) {
max_iterations = atoi(argv[3]);
} else {
max_iterations = -1;
}
prob_size = atoi(argv[1]);
pointsPerIter = atoi(argv[2]);
double *guess = new double[prob_size];
for (int i = 0; i < prob_size; i++)
guess[i] = -1.0;
DistParNelderMead *solver = new DistParNelderMead(guess, 1.0, prob_size, objFunction3,
rank, size, pointsPerIter);
double t1, t2;
t1 = MPI_Wtime();
double *answer = solver->solve(max_iterations);
t2 = MPI_Wtime();
if (rank == 0) {
std::cout << "Elapsed time during solve: " << t2 - t1 << std::endl;
for (int i = 0; i < prob_size - 1; i++) {
std::cout << answer[i] << ", ";
}
std::cout << answer[prob_size - 1] << std::endl;
}
delete solver;
delete[] answer;
MPI_Finalize();
return 0;
}