-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
67 lines (56 loc) · 1.87 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
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <chrono>
#include <cstdlib>
#include <sstream>
#include <iomanip>
#include <array>
#include <memory> // Para shared_ptr
#include <omp.h>
using namespace std;
// Función para ejecutar un comando y capturar su salida, imprimiéndola directamente
string runCommand(const string &command) {
array<char, 128> buffer;
string result;
// Renombrar la variable para evitar conflictos con `popen`
shared_ptr<FILE> cmdPipe(popen(command.c_str(), "r"), pclose);
if (!cmdPipe) {
cerr << "Error: Failed to run command: " << command << endl;
return "";
}
// Leer la salida del comando y mostrarla en tiempo real
while (fgets(buffer.data(), buffer.size(), cmdPipe.get()) != nullptr) {
cout << buffer.data(); // Imprimir en consola
result += buffer.data(); // Guardar en la cadena result
}
return result;
}
int main(int argc, char **argv) {
if (argc != 4) {
cerr << "Usage: " << argv[0] << " <number of elements> <mode (0=CPU, 1=GPU)> <number of threads>\n";
return 1;
}
int n = stoi(argv[1]);
int modo = stoi(argv[2]);
int nt = stoi(argv[3]);
if (n <= 0 || nt <= 0 || (modo != 0 && modo != 1)) {
cerr << "Invalid arguments. Ensure n > 0, nt > 0, and mode is 0 or 1.\n";
return 1;
}
string program;
if (modo == 0) {
program = "./main_cpu";
cout << "CPU mode selected\n";
cout << "Executing CPU mode...\n";
runCommand(program + " " + to_string(n) + " " + to_string(nt));
} else {
program = "./main_cuda";
cout << "GPU mode selected\n";
cout << "Executing GPU mode...\n";
runCommand(program + " " + to_string(n) + " " + to_string(nt));
}
cout << "\n===============================================\n";
return 0;
}