SuperAnon
SuperAnon

Reputation: 1

OpenMP on MacOS for a C++ CLion Project

I'm attempting to use the OpenMP library for a C++ CLion Project on MacOS, but it doesn't seem to be working well. I can use the omp.h header file, but when I run this test code (by GPT):

std::cout << "Maximum available threads: " << omp_get_max_threads() << std::endl;

    // Basic parallel region test
#pragma omp parallel
    {
#pragma omp critical
        {
            std::cout << "Hello from thread " << omp_get_thread_num()
                      << " of " << omp_get_num_threads() << std::endl;
        }
    }

    // Simple parallel for test
    int sum = 0;
#pragma omp parallel for reduction(+:sum)
    for(int i = 0; i < 100; i++) {
        sum += i;
    }
    std::cout << "Parallel sum result: " << sum << std::endl;

    return 0;
}

I get the output:

Maximum available threads: 14 Hello from thread 0 of 1 Parallel sum result: 4950

Which I understand should not be right, as only one thread seems to be used.

I've tried:

  1. Using the Homebrew clang and clang+ compilers in the toolchains and the following in CMakeLists.txt
  2. Using the Homebrew gcc and g++ compilers in the toolchains

In both, I've added this to the CMakeLists.txt file:

find_package(OpenMP REQUIRED)
if (OpenMP_CXX_FOUND)
    target_link_libraries(SymReg PUBLIC OpenMP::OpenMP_CXX)
endif()

And I get this output: Errors

Here is my current CMakeLists.txt

cmake_minimum_required(VERSION 3.28)
project(SymReg)

set(CMAKE_CXX_STANDARD 20)

# Define the SymEngine, GMP, and Flint include and library directories
set(SYMENGINE_INCLUDE_DIRS /opt/homebrew/opt/symengine/include)
set(SYMENGINE_LIBRARY_DIRS /opt/homebrew/opt/symengine/lib)
set(GMP_INCLUDE_DIRS /opt/homebrew/opt/gmp/include)
set(GMP_LIBRARY_DIRS /opt/homebrew/opt/gmp/lib)
set(MPFR_INCLUDE_DIRS /opt/homebrew/opt/mpfr/include)
set(MPFR_LIBRARY_DIRS /opt/homebrew/opt/mpfr/lib)
set(MPC_INCLUDE_DIRS /opt/homebrew/opt/libmpc/include)
set(MPC_LIBRARY_DIRS /opt/homebrew/opt/libmpc/lib)
set(FLINT_INCLUDE_DIRS /opt/homebrew/opt/flint/include)
set(FLINT_LIBRARY_DIRS /opt/homebrew/opt/flint/lib)
set(OMP_INCLUDE_DIRS /opt/homebrew/opt/libomp/include)
set(OMP_LIBRARY_DIRS /opt/homebrew/opt/libomp/lib)

# Enable Debugging
set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -g")

# Include directories
include_directories(${SYMENGINE_INCLUDE_DIRS})
include_directories(${GMP_INCLUDE_DIRS})
include_directories(${MPFR_INCLUDE_DIRS})
include_directories(${MPC_INCLUDE_DIRS})
include_directories(${FLINT_INCLUDE_DIRS})
include_directories(${OMP_INCLUDE_DIRS})

# Link directories
link_directories(${SYMENGINE_LIBRARY_DIRS})
link_directories(${GMP_LIBRARY_DIRS})
link_directories(${MPFR_LIBRARY_DIRS})
link_directories(${MPC_LIBRARY_DIRS})
link_directories(${FLINT_LIBRARY_DIRS})
link_directories(${OMP_LIBRARY_DIRS})

# Add your executable
add_executable(SymReg main.cpp
        individual_representation.cpp
        individual_representation.h
        fitness_function.cpp
        fitness_function.h
        individual_selection.cpp
        individual_selection.h
        genetic_algorithm.cpp
        genetic_algorithm.h
        model.cpp
        model.h
)

# Link SymEngine, GMP, MPFR, MPC, and Flint
target_link_libraries(SymReg symengine gmp mpfr mpc flint omp)

And here is an image of my toolchains in CLion

Toolchains

I'm quite new to CMake and CLion, so anything helps

Upvotes: 0

Views: 46

Answers (0)

Related Questions