From d4e8aea425844dec778a75a9a75b06cbd28bbc7f Mon Sep 17 00:00:00 2001 From: mmdeuss Date: Mon, 20 May 2019 19:35:17 +0200 Subject: [PATCH] Fix compilation for Xcode 10.2.1 by * explicitly setting c++11 in cmake to allow >> without space at end of templates * adjusting glut includes for mac * using Eigens handmade aligned allocator because std::aligned_allocator is missing in Apple Clang * switching from [4][3] to fix-size Eigen matrix to fix error in deletion in std::vector --- AVX_math.h | 25 +++++++++++++++++++++++-- CMakeLists.txt | 9 ++++++--- FastCorotFEM.cpp | 18 +++++++++--------- FastCorotFEM.h | 2 +- main.cpp | 5 +++++ utilities/MiniGL.cpp | 6 +++--- 6 files changed, 47 insertions(+), 18 deletions(-) diff --git a/AVX_math.h b/AVX_math.h index 2a9ae01..c1b1350 100644 --- a/AVX_math.h +++ b/AVX_math.h @@ -468,6 +468,23 @@ class Quaternion8f } }; +//from Eigen/src/Core/util/Memory.h, added alignment parameter +inline void* handmade_aligned_malloc(std::size_t size, std::size_t alignment) +{ + void *original = std::malloc(size+alignment); + if (original == 0) return 0; + void *aligned = reinterpret_cast((reinterpret_cast(original) & ~(std::size_t(alignment-1))) + alignment); + *(reinterpret_cast(aligned) - 1) = original; + return aligned; +} + +//from Eigen/src/Core/util/Memory.h +inline void handmade_aligned_free(void *ptr) +{ + if (ptr) std::free(*(reinterpret_cast(ptr) - 1)); +} + + // ---------------------------------------------------------------------------------------------- //alligned allocator so that vectorized types can be used in std containers //from: https://stackoverflow.com/questions/8456236/how-is-a-vectors-data-aligned @@ -503,9 +520,11 @@ class AlignmentAllocator { inline pointer allocate(size_type n) { #ifdef _WIN32 return (pointer)_aligned_malloc(n * sizeof(value_type), N); -#elif __linux__ +#elif defined(__linux__) // NB! Argument order is opposite from MSVC/Windows return (pointer) aligned_alloc(N, n * sizeof(value_type)); +#elif defined(__APPLE__) + return (pointer) handmade_aligned_malloc(n * sizeof(value_type), N); #else #error "Unknown platform" #endif @@ -514,8 +533,10 @@ class AlignmentAllocator { inline void deallocate(pointer p, size_type) { #ifdef _WIN32 _aligned_free(p); -#elif __linux__ +#elif defined(__linux__) free(p); +#elif defined(__APPLE__) + handmade_aligned_free(p); #else #error "Unknown platform" #endif diff --git a/CMakeLists.txt b/CMakeLists.txt index 005ea82..edb540f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -38,6 +38,8 @@ if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") add_compile_options(/arch:AVX) elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") add_compile_options("-mavx") +elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "AppleClang") + add_compile_options("-mavx") endif() set(EIGEN3_INCLUDE_DIR "${CMAKE_SOURCE_DIR}/extern/eigen") @@ -65,8 +67,9 @@ if (WIN32) add_dependencies(FastCorotDemo freeglut) else() find_package(GLUT REQUIRED) -endif() - +endif() + +set_target_properties(FastCorotDemo PROPERTIES CXX_STANDARD 11) target_link_libraries(FastCorotDemo ${OPENGL_LIBRARIES} ${GLUT_LIBRARIES}) -add_definitions(-DDATA_PATH="${PROJECT_PATH}/meshes/") \ No newline at end of file +add_definitions(-DDATA_PATH="${PROJECT_PATH}/meshes/") diff --git a/FastCorotFEM.cpp b/FastCorotFEM.cpp index 592a204..ee86085 100644 --- a/FastCorotFEM.cpp +++ b/FastCorotFEM.cpp @@ -62,7 +62,7 @@ bool FastCorotFEM::initialize( vector> triplets_M; triplets_M.reserve(4 * nTets); vector Kreal(nTets); - vector Dt(nTets); + vector > Dt(nTets); vector Dm_inv(nTets); vector rest_volume(nTets); vector invMass(nVerts); @@ -98,16 +98,16 @@ bool FastCorotFEM::initialize( //compute matrix D_t from Eq. (9) (actually Dt[t] is D_t^T) for (int k = 0; k < 3; k++) - Dt[t][0][k] = -Dm_inv[t](0, k) - Dm_inv[t](1, k) - Dm_inv[t](2, k); + Dt[t](0,k) = -Dm_inv[t](0, k) - Dm_inv[t](1, k) - Dm_inv[t](2, k); for (int j = 1; j < 4; j++) for (int k = 0; k < 3; k++) - Dt[t][j][k] = Dm_inv[t](j - 1, k); + Dt[t](j,k) = Dm_inv[t](j - 1, k); //initialize the matrix D for (int i = 0; i<4; i++) for (int j = 0; j < 3; j++) - triplets_D.push_back(Triplet(9 * t + 3 * j, it[i], Dt[t][i][j])); + triplets_D.push_back(Triplet(9 * t + 3 * j, it[i], Dt[t](i,j))); } //set matrices @@ -577,8 +577,8 @@ void FastCorotFEM::convertToAVX(const vector& v, vector& v, - vector>>>& vAVX) + const vector >& v, + vector > > >& vAVX) { int regularPart = (nTets / 8) * 8; for (int i = 0; i < regularPart; i += 8) @@ -588,7 +588,7 @@ void FastCorotFEM::convertToAVX( { vAVX[i / 8][j].resize(3); for (int k = 0; k < 3; k++) - vAVX[i / 8][j][k] = Scalarf8(v[i + 0][j][k], v[i + 1][j][k], v[i + 2][j][k], v[i + 3][j][k], v[i + 4][j][k], v[i + 5][j][k], v[i + 6][j][k], v[i + 7][j][k]); + vAVX[i / 8][j][k] = Scalarf8(v[i + 0](j,k), v[i + 1](j,k), v[i + 2](j,k), v[i + 3](j,k), v[i + 4](j,k), v[i + 5](j,k), v[i + 6](j,k), v[i + 7](j,k)); } } @@ -601,8 +601,8 @@ void FastCorotFEM::convertToAVX( { Real vtmp[8]; for (int i = regularPart; i < regularPart + 8; i++) - if (i < nTets) vtmp[i - regularPart] = v[i][j][k]; - else vtmp[i - regularPart] = v[nTets - 1][j][k]; + if (i < nTets) vtmp[i - regularPart] = v[i](j,k); + else vtmp[i - regularPart] = v[nTets - 1](j,k); vAVX[regularPart/ 8][j][k] = Scalarf8(vtmp[0], vtmp[1], vtmp[2], vtmp[3], vtmp[4], vtmp[5], vtmp[6], vtmp[7]); } diff --git a/FastCorotFEM.h b/FastCorotFEM.h index e909786..89731e2 100644 --- a/FastCorotFEM.h +++ b/FastCorotFEM.h @@ -91,7 +91,7 @@ class FastCorotFEM std::vector>& vAVX); void convertToAVX( - const std::vector& v, + const std::vector>& v, std::vector>>>& vAVX); }; #endif \ No newline at end of file diff --git a/main.cpp b/main.cpp index 85b723b..fb22e2b 100644 --- a/main.cpp +++ b/main.cpp @@ -2,7 +2,12 @@ #include "utilities/MiniGL.h" #include "utilities/Timing.h" #include "TetModel.h" + +#ifdef __APPLE__ +#include +#else #include "GL/glut.h" +#endif INIT_TIMING diff --git a/utilities/MiniGL.cpp b/utilities/MiniGL.cpp index a5c6c02..35b1769 100644 --- a/utilities/MiniGL.cpp +++ b/utilities/MiniGL.cpp @@ -9,13 +9,13 @@ #ifdef __APPLE__ #include #include +#include #else #include "GL/gl.h" #include "GL/glu.h" -#endif - #include "GL/glut.h" #include "GL/freeglut_ext.h" +#endif #define _USE_MATH_DEFINES @@ -944,7 +944,7 @@ void MiniGL::breakPointMainLoop() m_breakPointLoop = true; while (m_breakPointLoop) { - glutMainLoopEvent(); + glutMainLoop(); } } }