Skip to content

Commit 9f7caca

Browse files
committed
Initial Codebase Commit
1 parent 3bcb695 commit 9f7caca

File tree

16 files changed

+362
-23
lines changed

16 files changed

+362
-23
lines changed

CM_Config/LibraryHandler.cmake

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
find_package(PkgConfig REQUIRED)
2+
3+
pkg_check_modules(LIBPCI REQUIRED libpci)
4+
include_directories(${LIBPCI_INCLUDE_DIRS})
5+
link_directories(${LIBPCI_LIBRARY_DIRS})

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
cmake_minimum_required(VERSION 3.29)
2-
project(CppTemplate)
1+
cmake_minimum_required(VERSION 3.25)
2+
project(RunAsGPU)
33

44
set(CMAKE_CXX_STANDARD 20)
55

Include/ProjectName/MainModule/ModuleHead.hpp

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#ifndef RUNASGPU_GRAPHICALUNIT_HPP
2+
#define RUNASGPU_GRAPHICALUNIT_HPP
3+
4+
#include <string>
5+
6+
enum GraphicalUnitConnector {
7+
// The specific GPU is powered by the internal graphical processing unit (iGPU)
8+
// through the CPU
9+
INTERNAL = 0,
10+
11+
// The specific GPU is powered by its own dedicated graphical processing unit (dGPU)
12+
DEDICATED = 1,
13+
14+
// The specific GPU is connected via USB-C (most likely Thunderbolt)
15+
EXTERNAL = 2
16+
};
17+
18+
struct GraphicalUnit {
19+
int vendor;
20+
int product;
21+
std::string vendorName;
22+
std::string productName;
23+
std::string fullName;
24+
GraphicalUnitConnector connector;
25+
};
26+
27+
#endif //RUNASGPU_GRAPHICALUNIT_HPP
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#ifndef RUNASGPU_HELPPAGE_HPP
2+
#define RUNASGPU_HELPPAGE_HPP
3+
4+
#ifndef ABOAWT_HELPPAGE_H
5+
#define ABOAWT_HELPPAGE_H
6+
7+
#include <string>
8+
#include <iostream>
9+
#include <vector>
10+
11+
class HelpPage {
12+
private:
13+
struct Item {
14+
std::string arg;
15+
std::string equalDesc;
16+
std::string description;
17+
18+
Item(std::string& a, std::string& e, std::string& d);
19+
};
20+
21+
int spaceWidth, startSpaceWidth;
22+
std::vector<Item> argItemList;
23+
std::string descSep = "=";
24+
25+
public:
26+
HelpPage();
27+
28+
void setSpaceWidth(int width);
29+
[[nodiscard]] int getSpaceWidth() const;
30+
31+
void setStartSpaceWidth(int width);
32+
[[nodiscard]] int getStartSpaceWidth() const;
33+
34+
void setDescSeparator(const std::string& sep);
35+
[[nodiscard]] std::string getDescSeparator() const;
36+
37+
void addArg(std::string arg, std::string assignableDesc, std::string description);
38+
[[nodiscard]] std::string display() const;
39+
void display(std::ostream& os) const;
40+
41+
void free();
42+
};
43+
44+
#endif //ABOAWT_HELPPAGE_H
45+
46+
#endif //RUNASGPU_HELPPAGE_HPP

Include/RunAsGPU/Shared/Runner.hpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#ifndef RUNASGPU_RUNNER_HPP
2+
#define RUNASGPU_RUNNER_HPP
3+
4+
#include <RunAsGPU/Shared/GraphicalUnit.hpp>
5+
6+
#include <vector>
7+
#include <map>
8+
9+
namespace Runner {
10+
11+
std::vector<GraphicalUnit> ListGraphicalUnits();
12+
13+
void RunApplication(const GraphicalUnit& unit, const std::string& bin, const std::vector<std::string>& args, const std::map<std::string, std::string>& env_map);
14+
15+
}
16+
17+
#endif //RUNASGPU_RUNNER_HPP

Include/RunAsGPU/Shared/Shared.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#ifndef RUNASGPU_SHARED_HPP
2+
#define RUNASGPU_SHARED_HPP
3+
4+
#include <filesystem>
5+
6+
std::filesystem::path GetExecutablePath();
7+
8+
#endif //RUNASGPU_SHARED_HPP

Sources/AppCLI/MainCLI.cpp

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#include <iostream>
2+
3+
#include <RunAsGPU/Shared/Shared.hpp>
4+
#include <RunAsGPU/Shared/HelpPage.hpp>
5+
#include <RunAsGPU/Shared/Runner.hpp>
6+
7+
namespace fs = std::filesystem;
8+
9+
void list() {
10+
std::vector<GraphicalUnit> gpu_list = Runner::ListGraphicalUnits();
11+
for (int i = 0; i < gpu_list.size(); i++) {
12+
const GraphicalUnit& unit = gpu_list[i];
13+
14+
std::cout << "GPU - " << unit.vendorName << " (" << std::hex << unit.vendor << ")\n";
15+
std::cout << "\tName = " << unit.productName << " (" << std::hex << unit.product << ")\n";
16+
std::cout << "\tID = " << i << "\n";
17+
std::cout << "\tConnector = ";
18+
19+
switch (unit.connector) {
20+
case EXTERNAL:
21+
std::cout << "External Graphics Card" << std::endl;
22+
break;
23+
case INTERNAL:
24+
std::cout << "Internal GPU" << std::endl;
25+
break;
26+
case DEDICATED:
27+
std::cout << "Dedicated GPU" << std::endl;
28+
break;
29+
}
30+
31+
if (i != gpu_list.size() - 1)
32+
std::cout << std::endl;
33+
}
34+
}
35+
36+
int main(int argc, char **argv) {
37+
if (argc == 1 || (argc == 2 && (std::string(argv[1]) == "--help" || std::string(argv[1]) == "-h"))) {
38+
std::string fn = GetExecutablePath().filename().string();
39+
40+
std::cout << "RunAsGPU v1.0" << std::endl;
41+
std::cout << "usage: ./" << fn << " [action] [options] [exec [..args]]" << std::endl << std::endl;
42+
std::cout << "actions:" << std::endl;
43+
44+
HelpPage actions;
45+
actions.setStartSpaceWidth(4);
46+
actions.setDescSeparator(" ");
47+
actions.addArg("run", "", "Run an executable");
48+
actions.addArg("list", "", "List all active GPUs");
49+
actions.display(std::cout);
50+
51+
std::cout << std::endl;
52+
std::cout << "options:" << std::endl;
53+
54+
HelpPage options;
55+
options.setStartSpaceWidth(4);
56+
options.setSpaceWidth(3);
57+
options.setDescSeparator("=");
58+
options.addArg("-ui | --unit-id", "ID", "Select a specific GPU by its given ID");
59+
options.addArg("-fu | --find-unit", "[GPU]", "Find a GPU by its identification (Vendor ID, Device / Product ID, Name)");
60+
options.addArg("-fnu | --find-unit-name", "[Name]", "Find a GPU by the name");
61+
options.addArg("-fvu | --find-unit-vendor", "[Vendor]", "Find a GPU by the name or ID of the Vendor (e.g. NVIDIA)");
62+
options.addArg("-fpu | --find-unit-product", "[Product]", "Find a GPU by the name or ID of the Product (e.g. GeForce 6800 XT)");
63+
options.addArg("-ni | --non-interactive", "", "Will not prompt for GPU selection (e.g. running as a script)");
64+
options.display(std::cout);
65+
66+
return 0;
67+
}
68+
69+
std::vector<std::string> args;
70+
std::string action = argv[1];
71+
if (action == "list") {
72+
list();
73+
return 0;
74+
}
75+
76+
return 0;
77+
}

Sources/AppCLI/SourceInfo.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
CollectSources(${PROJECT_MODULE_ROOT} ModSources)
2+
3+
add_executable(RunAsGPU-CLI ${ModSources})
4+
target_link_libraries(RunAsGPU-CLI PRIVATE ${LIBPCI_LIBRARIES} RunAsGPU-shared)

Sources/AppShared/Detect.cpp

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include <RunAsGPU/Shared/GraphicalUnit.hpp>
2+
#include <RunAsGPU/Shared/Runner.hpp>
3+
4+
#include <iostream>
5+
#include <string>
6+
#include <cstring>
7+
#include <fstream>
8+
9+
extern "C" {
10+
#include <pci/pci.h>
11+
}
12+
13+
namespace Runner {
14+
15+
std::vector<GraphicalUnit> ListGraphicalUnits() {
16+
std::vector<GraphicalUnit> gpu_units;
17+
18+
struct pci_access *pacc;
19+
struct pci_dev *dev;
20+
char vendor_name[1024], device_name[2048];
21+
22+
pacc = pci_alloc();
23+
if (!pacc) {
24+
std::cerr << "Failed to allocate pci_alloc" << std::endl;
25+
return gpu_units;
26+
}
27+
28+
pci_init(pacc);
29+
pci_scan_bus(pacc);
30+
31+
for (dev = pacc->devices; dev; dev = dev->next) {
32+
pci_fill_info(dev, PCI_FILL_IDENT | PCI_FILL_CLASS);
33+
34+
// 0x03?? = Display Controllers
35+
// 0x0300 = VGA compatible controller
36+
// 0x0301 = XGA compatible controller (old, no longer used)
37+
// 0x0302 = 3D controller
38+
// 0x0380 = Display Controller
39+
if (dev->device_class != 0x0300 && dev->device_class != 0x0301 && dev->device_class != 0x0302 && dev->device_class != 0x0380)
40+
continue;
41+
42+
pci_lookup_name(pacc, vendor_name, sizeof(vendor_name), PCI_LOOKUP_VENDOR, dev->vendor_id);
43+
pci_lookup_name(pacc, device_name, sizeof(device_name), PCI_LOOKUP_DEVICE, dev->vendor_id, dev->device_id);
44+
45+
GraphicalUnitConnector connector;
46+
47+
bool is_igpu = dev->vendor_id == 0x8086 || (dev->vendor_id == 0x1002 && dev->device_id < 0x6800);
48+
bool is_dgpu = dev->bus >= 1;
49+
50+
if (is_igpu)
51+
connector = INTERNAL;
52+
else if (is_dgpu)
53+
connector = DEDICATED;
54+
else
55+
connector = EXTERNAL;
56+
57+
GraphicalUnit gpu{
58+
.vendor = dev->vendor_id,
59+
.product = dev->device_id,
60+
.vendorName = std::string(vendor_name),
61+
.productName = std::string(device_name),
62+
.fullName = std::string(vendor_name) + " " + std::string(device_name),
63+
.connector = connector
64+
};
65+
66+
gpu_units.push_back(gpu);
67+
}
68+
69+
pci_cleanup(pacc);
70+
71+
return gpu_units;
72+
}
73+
74+
}

0 commit comments

Comments
 (0)