Skip to content

Commit

Permalink
Implement some GPU detection stuff
Browse files Browse the repository at this point in the history
OpenGL is literal hell on earth, I cri everytiem

Ref: #2
  • Loading branch information
nabijaczleweli committed Jun 6, 2016
1 parent 3248e9d commit 0130ec6
Show file tree
Hide file tree
Showing 11 changed files with 272 additions and 17 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ stlib : $(BUILD)/libinfoware$(ARCH)


$(BUILD)/$(PREDLL)infoware$(DLL) : $(OBJECTS)
$(CXX) $(CXXAR) -shared $(PIC) -o$@ $^
$(CXX) $(CXXAR) -shared $(PIC) -o$@ $^ $(LDAR)

$(BUILD)/libinfoware$(ARCH) : $(OBJECTS)
$(AR) crs $@ $^


$(BUILD)/obj/%$(OBJ) : src/%.cpp
@mkdir -p $(dir $@)
$(CXX) $(CXXAR) $(PIC) -Iinclude -c -o$@ $^
$(CXX) $(CXXAR) $(CXXAR_LIB) $(PIC) -Iinclude -c -o$@ $^

$(BUILD)/examples/%$(EXE) : examples/%.cpp $(OBJECTS)
@mkdir -p $(dir $@)
$(CXX) $(CXXAR) -Iinclude -o$@ $^
$(CXX) $(CXXAR) -Iinclude -o$@ $^ $(LDAR)
39 changes: 25 additions & 14 deletions configMakefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,37 @@


ifeq "$(OS)" "Windows_NT"
DLL = .dll
PREDLL =
PIC =
EXE = .exe
DLL := .dll
PREDLL :=
PIC :=
EXE := .exe
else
PREDLL = lib
PIC = -fPIC
PREDLL := lib
PIC := -fPIC
ifeq "$(OS)" "Darwin"
DLL = .dylib
DLL := .dylib
else
DLL = .so
DLL := .so
endif
EXE =
EXE :=
endif

ifeq "$(BUILD)" ""
BUILD = out
BUILD := out
endif

OBJ = .o
ARCH = .a
AR = ar
CXXAR = -pedantic -O3 -fomit-frame-pointer -std=c++14 -Wall -Wextra -pipe
ifdef USE_OPENCL
LDAR_OPENCL := -lOpenCL
CXXAR_OPENCL := -DINFOWARE_USE_OPENCL
else
LDAR_OPENCL :=
CXXAR_OPENCL :=
endif

LDAR := $(LDAR_OPENCL)
CXXAR_LIB := $(CXXAR_OPENCL)

OBJ := .o
ARCH := .a
AR := ar
CXXAR := -pedantic -O3 -fomit-frame-pointer -std=c++14 -Wall -Wextra -pipe
21 changes: 21 additions & 0 deletions examples/gpu_device_properties_cache_size.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// infoware - C++ System information Library
//
// Written in 2016 by nabijaczleweli <[email protected]> and ThePhD <[email protected]>
//
// To the extent possible under law, the author(s) have dedicated all copyright and related
// and neighboring rights to this software to the public domain worldwide. This software is
// distributed without any warranty.
//
// You should have received a copy of the CC0 Public Domain Dedication along with this software.
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>


#include "infoware/gpu.hpp"
#include <iostream>


int main() {
for(auto&& props : iware::gpu::device_properties())
std::cout << props.cache_size << ' ';
std::cout << '\n';
}
21 changes: 21 additions & 0 deletions examples/gpu_device_properties_memory_size.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// infoware - C++ System information Library
//
// Written in 2016 by nabijaczleweli <[email protected]> and ThePhD <[email protected]>
//
// To the extent possible under law, the author(s) have dedicated all copyright and related
// and neighboring rights to this software to the public domain worldwide. This software is
// distributed without any warranty.
//
// You should have received a copy of the CC0 Public Domain Dedication along with this software.
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>


#include "infoware/gpu.hpp"
#include <iostream>


int main() {
for(auto&& props : iware::gpu::device_properties())
std::cout << props.memory_size << ' ';
std::cout << '\n';
}
21 changes: 21 additions & 0 deletions examples/gpu_device_properties_name.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// infoware - C++ System information Library
//
// Written in 2016 by nabijaczleweli <[email protected]> and ThePhD <[email protected]>
//
// To the extent possible under law, the author(s) have dedicated all copyright and related
// and neighboring rights to this software to the public domain worldwide. This software is
// distributed without any warranty.
//
// You should have received a copy of the CC0 Public Domain Dedication along with this software.
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>


#include "infoware/gpu.hpp"
#include <iostream>


int main() {
for(auto&& props : iware::gpu::device_properties())
std::cout << props.name << ' ';
std::cout << '\n';
}
34 changes: 34 additions & 0 deletions examples/gpu_device_properties_vendor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// infoware - C++ System information Library
//
// Written in 2016 by nabijaczleweli <[email protected]> and ThePhD <[email protected]>
//
// To the extent possible under law, the author(s) have dedicated all copyright and related
// and neighboring rights to this software to the public domain worldwide. This software is
// distributed without any warranty.
//
// You should have received a copy of the CC0 Public Domain Dedication along with this software.
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>


#include "infoware/gpu.hpp"
#include <iostream>


int main() {
for(auto&& props : iware::gpu::device_properties())
switch(props.vendor) {
case iware::gpu::vendor_t::intel:
std::cout << "Intel ";
break;
case iware::gpu::vendor_t::AMD:
std::cout << "AMD ";
break;
case iware::gpu::vendor_t::NVidia:
std::cout << "NVidia ";
break;
case iware::gpu::vendor_t::unknown:
std::cout << "Unknown ";
break;
}
std::cout << '\n';
}
41 changes: 41 additions & 0 deletions include/infoware/gpu.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// infoware - C++ System information Library
//
// Written in 2016 by nabijaczleweli <[email protected]> and ThePhD <[email protected]>
//
// To the extent possible under law, the author(s) have dedicated all copyright and related
// and neighboring rights to this software to the public domain worldwide. This software is
// distributed without any warranty.
//
// You should have received a copy of the CC0 Public Domain Dedication along with this software.
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>


#pragma once


#include <cstdint>
#include <string>
#include <vector>


namespace iware {
namespace gpu {
enum class vendor_t {
intel,
AMD,
NVidia,
unknown,
};

struct device_properties_t {
vendor_t vendor;
std::string name;
std::size_t memory_size;
std::size_t cache_size;
};


/// Returns all GPU's properties.
std::vector<device_properties_t> device_properties();
}
}
1 change: 1 addition & 0 deletions include/infoware/infoware.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@


#include "cpu.hpp"
#include "gpu.hpp"
#include "system.hpp"
67 changes: 67 additions & 0 deletions src/gpu/memory/OpenCL.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// infoware - C++ System information Library
//
// Written in 2016 by nabijaczleweli <[email protected]> and ThePhD <[email protected]>
//
// To the extent possible under law, the author(s) have dedicated all copyright and related
// and neighboring rights to this software to the public domain worldwide. This software is
// distributed without any warranty.
//
// You should have received a copy of the CC0 Public Domain Dedication along with this software.
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>


#ifdef INFOWARE_USE_OPENCL


#include "infoware/gpu.hpp"
#include <cstdlib>
#ifdef __APPLE__
#include <OpenCL/opencl.h>
#else
#include <CL/cl.h>
#endif


static iware::gpu::vendor_t parse_vendor(const char* name) {
if(!strcmp(name, "Intel(R) Corporation"))
return iware::gpu::vendor_t::intel;
else if(!strcmp(name, "Advanced Micro Devices, Inc."))
return iware::gpu::vendor_t::AMD;
else if(!strcmp(name, "NVIDIA Corporation"))
return iware::gpu::vendor_t::NVidia;
else
return iware::gpu::vendor_t::unknown;
}


std::vector<iware::gpu::device_properties_t> iware::gpu::device_properties() {
cl_platform_id platforms[64];
cl_uint platforms_used;
clGetPlatformIDs(sizeof platforms / sizeof(*platforms), platforms, &platforms_used);

std::vector<iware::gpu::device_properties_t> ret;
for(auto i = 0u; i < platforms_used; ++i) {
cl_device_id devices[64];
cl_uint devices_used;
clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_GPU, sizeof devices / sizeof(*devices), devices, &devices_used);

for(auto j = 0u; j < devices_used; ++j) {
char name[256];
cl_ulong cache;
cl_ulong memory;

clGetDeviceInfo(devices[j], CL_DEVICE_GLOBAL_MEM_CACHE_SIZE, sizeof(cache), &cache, nullptr);
clGetDeviceInfo(devices[j], CL_DEVICE_GLOBAL_MEM_SIZE, sizeof(memory), &memory, nullptr);
clGetDeviceInfo(devices[j], CL_DEVICE_VENDOR, sizeof name, &name, nullptr);
const auto vendor = parse_vendor(name);
clGetDeviceInfo(devices[j], CL_DEVICE_NAME, sizeof name, &name, nullptr);

ret.push_back({vendor, name, memory, cache});
}
}

return ret;
}


#endif
14 changes: 14 additions & 0 deletions src/gpu/memory/OpenGL.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// infoware - C++ System information Library
//
// Written in 2016 by nabijaczleweli <[email protected]> and ThePhD <[email protected]>
//
// To the extent possible under law, the author(s) have dedicated all copyright and related
// and neighboring rights to this software to the public domain worldwide. This software is
// distributed without any warranty.
//
// You should have received a copy of the CC0 Public Domain Dedication along with this software.
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>


// OpenGL is literaly hitler to initialise a windowless context for, so no game.
// Leave it for someone smarter than me :G
24 changes: 24 additions & 0 deletions src/gpu/memory/blank_all.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// infoware - C++ System information Library
//
// Written in 2016 by nabijaczleweli <[email protected]> and ThePhD <[email protected]>
//
// To the extent possible under law, the author(s) have dedicated all copyright and related
// and neighboring rights to this software to the public domain worldwide. This software is
// distributed without any warranty.
//
// You should have received a copy of the CC0 Public Domain Dedication along with this software.
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>


#ifndef INFOWARE_USE_OPENCL


#include "infoware/gpu.hpp"


std::vector<iware::gpu::device_properties_t> iware::gpu::device_properties() {
return {};
}


#endif

0 comments on commit 0130ec6

Please sign in to comment.