Nicolas Winkler 6 anni fa
parent
commit
1b9cc12e03

+ 35 - 16
libmandel/CMakeLists.txt

@@ -1,15 +1,31 @@
 cmake_minimum_required(VERSION 3.9)
 
 set(CMAKE_CXX_STANDARD 17)
+OPTION(ARCH "Target Architecture" X86_64)
 project(mandel VERSION 1.0.0 DESCRIPTION "library for mandelbrot calculations")
 
-find_package(OpenCL REQUIRED)
+find_package(OpenCL)
 find_package(OpenMP)
 
 
-FILE(GLOB MandelSources src/*.cpp
-FILE(GLOB MandelHeaders include/*.h))
+#FILE(GLOB MandelSources src/*.cpp)
+SET(MandelSources
+    src/ClGenerators.cpp
+    src/CpuGenerators.cpp
+    src/Generators.cpp
+    src/mandel.cpp
+    src/CpuGeneratorsAVX.cpp
+    src/CpuGeneratorsSSE2.cpp
+    src/Hardware.cpp
+    src/MandelUtil.cpp
+)
+FILE(GLOB MandelHeaders include/*.h)
+
+if (NOT (ARCH EQUAL X86_64 OR ARCH EQUAL X86))
+    list(REMOVE_ITEM MandelSources src/CpuGeneratorsAVX.cpp src/CpuGeneratorsSSE2.cpp)
+endif()
 
+    message(${MandelSources})
 
 include_directories(
     "include"
@@ -17,28 +33,31 @@ include_directories(
 )
 link_directories(${OpenCL_LIBRARY})
 
-if (APPLE)
+if (APPLE AND OpenCL_FOUND)
     SET(CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} -framework OpenCL")
 endif()
 
-if (MSVC)
-    set_source_files_properties(src/CpuGeneratorsAVX.cpp PROPERTIES COMPILE_FLAGS /arch:AVX)
-else()
-    set_source_files_properties(src/CpuGeneratorsAVX.cpp PROPERTIES COMPILE_FLAGS -mavx)
-endif(MSVC)
+if (${ARCH} EQUAL X86_64 OR ${ARCH} EQUAL X86)
+    if (MSVC)
+        set_source_files_properties(src/CpuGeneratorsAVX.cpp PROPERTIES COMPILE_FLAGS /arch:AVX)
+    else()
+        set_source_files_properties(src/CpuGeneratorsAVX.cpp PROPERTIES COMPILE_FLAGS -mavx)
+    endif(MSVC)
 
 
-if (MSVC)
-    set_source_files_properties(src/CpuGeneratorsSSE2.cpp PROPERTIES COMPILE_FLAGS /arch:SSE2)
-else()
-    set_source_files_properties(src/CpuGeneratorsSSE2.cpp PROPERTIES COMPILE_FLAGS -msse2)
-endif(MSVC)
+    if (MSVC)
+        set_source_files_properties(src/CpuGeneratorsSSE2.cpp PROPERTIES COMPILE_FLAGS /arch:SSE2)
+    else()
+        set_source_files_properties(src/CpuGeneratorsSSE2.cpp PROPERTIES COMPILE_FLAGS -msse2)
+    endif(MSVC)
+endif()
 
 add_library(mandel STATIC ${MandelSources})
 
 
 if(OpenMP_CXX_FOUND)
-    target_link_libraries(mandel PUBLIC OpenCL::OpenCL OpenMP::OpenMP_CXX)
-else()
+    target_link_libraries(mandel PUBLIC OpenMP::OpenMP_CXX)
+endif()
+if(OpenCL_FOUND)
     target_link_libraries(mandel OpenCL::OpenCL)
 endif()

+ 17 - 0
libmandel/include/Hardware.h

@@ -10,21 +10,38 @@ namespace mnd
 
 class mnd::CpuInfo
 {
+public:
+    enum class Arch
+    {
+        X86,
+        X86_64,
+        ARM,
+        ARM64
+    };
+private:
     std::string vendor;
     std::string brand;
 
+    Arch arch;
+
     bool sse2;
     bool avx;
     bool avx512;
+    bool neon;
 public:
     CpuInfo(void);
 
     inline const std::string& getVendor(void) const { return vendor; };
     inline const std::string& getBrand(void) const { return brand; };
 
+
+#if defined(__x86_64__) || defined(_M_X64) || defined(__i386) || defined(_M_IX86) 
     inline bool hasSse2(void) const { return sse2; };
     inline bool hasAvx(void) const { return avx; };
     inline bool hasAvx512(void) const { return avx512; };
+#elif defined(__arm__) || defined(__aarch64__)
+    inline bool hasNeon(void) const { return neon; };
+#endif
 };
 
 #endif // MANDEL_HARDWARE_H

+ 25 - 0
libmandel/src/Hardware.cpp

@@ -5,17 +5,28 @@
 #include <bitset>
 #include <cstring>
 
+#if defined(__x86_64__) || defined(_M_X64) || defined(__i386) || defined(_M_IX86)
 #ifdef __GNUC__
 #include <cpuid.h>
 #else
 #include <intrin.h>
 #endif
+#endif
 
 using mnd::CpuInfo;
 
 
+#if defined(__x86_64__) || defined(_M_X64) || defined(__i386) || defined(_M_IX86) 
 CpuInfo::CpuInfo(void)
 {
+
+
+#if defined(__x86_64__) || defined(_M_X64)
+    arch = Arch::X86_64;
+#else
+    arch = Arch::X86;
+#endif
+
     std::array<unsigned int, 4> dat;
     std::vector<std::array<unsigned int, 4>> cpuData;
     std::vector<std::array<unsigned int, 4>> extData;
@@ -94,3 +105,17 @@ CpuInfo::CpuInfo(void)
     avx512 = ebx7[16];
 }
 
+#elif defined(__arm__) || defined(__aarch64__)
+
+CpuInfo::CpuInfo(void)
+{
+#if defined(__aarch64__)
+    arch = Arch::ARM64;
+#else
+    arch = Arch::ARM;
+
+    // TODO implement check
+    neon = false;
+#endif
+}
+#endif

+ 5 - 1
libmandel/src/mandel.cpp

@@ -52,6 +52,8 @@ mnd::Generator* MandelDevice::getGenerator128(void) const
 
 MandelContext::MandelContext(void)
 {
+
+#if defined(__x86_64__) || defined(_M_X64) || defined(__i386) || defined(_M_IX86) 
     if (cpuInfo.hasAvx()) {
         cpuGeneratorFloat = std::make_unique<CpuGeneratorAvxFloat>();
         cpuGeneratorDouble = std::make_unique<CpuGeneratorAvxDouble>();
@@ -60,7 +62,9 @@ MandelContext::MandelContext(void)
         cpuGeneratorFloat = std::make_unique<CpuGeneratorSse2Float>();
         cpuGeneratorDouble = std::make_unique<CpuGeneratorSse2Double>();
     }
-    else {
+    else
+#endif
+    {
         cpuGeneratorFloat = std::make_unique<CpuGeneratorFloat>();
         cpuGeneratorDouble = std::make_unique<CpuGeneratorDouble>();
     }