Skip to content

Compile using aligned_alloc() from Boost. - #11

Open
m000 wants to merge 3 commits into
victorvde:masterfrom
m000:master
Open

Compile using aligned_alloc() from Boost.#11
m000 wants to merge 3 commits into
victorvde:masterfrom
m000:master

Conversation

@m000

@m000 m000 commented Jul 19, 2020

Copy link
Copy Markdown

Some minor modifications to compile when aligned_alloc() is not provided by the system libraries. A notable example of such systems are MacOS versions < 10.15.
To resolve this, we link against Boost's implementation of aligned_alloc. A wrapper is used to link the Boost C++ library with the C code of jpeg2png. This behaviour is enabled by setting the BOOST build variable.

After this, jpeg2png can be compiled on MacOS < 10.15 using: CC=clang CXX=clang++ MACOS=1 BOOST=1 OPENMP=0 make (uses Boost aligned_alloc, and libjpeg from homebrew)

For MacOS >= 10.15, this should work: CC=clang CXX=clang++ MACOS=1 OPENMP=0 make (uses system's aligned_alloc, and libjpeg from homebrew)

@m000
m000 force-pushed the master branch 2 times, most recently from 0b5d07d to 693d8e9 Compare July 19, 2020 18:11
@m000 m000 changed the title Compile on MacOS 10.14 (Mojave) Compile using aligned_alloc() from Boost. Jul 19, 2020
Useful for MacOS < 10.15 (Catalina).
@sandsmark

sandsmark commented Jul 22, 2020

Copy link
Copy Markdown

macos has posix_memalign(), much better than dragging in boost just for this. boost isn't exactly something you want...

boost's aligned_alloc is just some tens of headers of complicated C++ that in the end just calls posix_memalign() on macos and other "posix" platforms (however it defines it).

edit:
can you test this patch on macos? it should be identical to what boost ends up doing on macos etc.:

diff --git utils.h utils.h
  index 2116fef..e19d3b9 100644
  --- utils.h
  +++ utils.h
  @@ -89,10 +89,13 @@ static inline float sqf(float x) {
   static inline void *alloc_simd(size_t n) {
   #if defined(_WIN32)
           void *p = _aligned_malloc(n, 16);
  -#else
  -        void *p = aligned_alloc(16, n);
  -#endif
           if(!p) { die("allocation error"); }
  +#else
  +        void *p = NULL;
  +        if (posix_memalign(&p, 16, n) != 0) {
  +            die("aligned allocation error");
  +        }
  +#endif
           ASSUME_ALIGNED(p);
           return p;
   }

@m000

m000 commented Jul 29, 2020

Copy link
Copy Markdown
Author

@sandsmark Didn't know about posix_memalign() (didn't show up in any search :)). Will test and report back.

@sandsmark

Copy link
Copy Markdown

sorry for the less than cordial tone, btw, I just had some recent less-than-stellar run-ins with boost. :-)

but yeah, posix_memalign is what boost uses as well, so the behavior should be identical.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants