Skip to content

Instantly share code, notes, and snippets.

@biochem-fan
Last active October 1, 2022 08:29
Show Gist options
  • Save biochem-fan/31864239460769d2a4a3585e4959d298 to your computer and use it in GitHub Desktop.
Save biochem-fan/31864239460769d2a4a3585e4959d298 to your computer and use it in GitHub Desktop.
Homebrew libomp GCC incompatibility
# A simple project to show including /opt/homebrew/include
# in the GCC search path breaks OpenMP because GCC picks up
# libomp headers for LLVM.
# https://github.com/3dem/relion/issues/913#issuecomment-1263499265
# On Mac OS X:
# CC=gcc-12 cmake ..; make; ./omp_test # hangs
# CC=gcc-12 cmake .. -DAVOID_JPEG=ON; make; ./omp_test # OK
# gcc-12 -o omp_test ../main.c -fopenmp; ./omp_test # OK
# cmake ..; make; # picks up AppleClang and fails to link
# export PATH="/opt/homebrew/opt/llvm/bin:$PATH"
# export LDFLAGS="-L/opt/homebrew/opt/llvm/lib"
# export CPPFLAGS="-I/opt/homebrew/opt/llvm/include"
# CC=clang cmake ..; make; ./omp_test # OK
# On Linux (Ubuntu 20.04 LTS):
# cmake ..; make; ./omp_test # OK
# CC=icc cmake ..; make; ./omp_test # OK
cmake_minimum_required(VERSION 3.1)
project(omp_test)
find_package(OpenMP REQUIRED)
message("OpenMP CFLAGS = " ${OpenMP_C_FLAGS})
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
add_executable(omp_test "main.c")
if (NOT AVOID_JPEG)
find_package(JPEG REQUIRED)
message("JPEG HEADERS in " ${JPEG_INCLUDE_DIRS})
target_link_libraries(omp_test ${JPEG_LIBRARY})
include_directories(omp_test ${JPEG_INCLUDE_DIRS})
endif()
#include <stdio.h>
#include <omp.h>
// A check to detect incompatible OpenMP header in GCC
// as suggested by Filipe Maia in
// https://github.com/3dem/relion/issues/913#issuecomment-1263499265
#if defined(__GNUC__) && !defined(__llvm__) && !defined(__INTEL_COMPILER)
#ifndef _LIBGOMP_OMP_LOCK_DEFINED
#warning "Probably you are wrong OpenMP headers!!"
#endif
#endif
#define N 5
static omp_lock_t global_mutex[N];
int main()
{
printf("started\n");
for (int i = 0; i < N; i++)
{
omp_init_lock(global_mutex + i);
printf("init %d\n", i);
}
#pragma omp parallel for num_threads (3)
for (int i = 0; i < N; i++)
{
omp_set_lock(global_mutex + i);
printf("locked %d\n", i);
omp_unset_lock(global_mutex + i);
printf("released %d\n", i);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment