Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/openmp #200

Merged
merged 16 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ addons:
- jpeg-turbo
- jpeg-xl
- libavif
- libomp
- libpng
- libtiff
- webp
Expand Down Expand Up @@ -43,6 +44,7 @@ jobs:
name: "macOS 12.6"
env:
- CMAKE_PREFIX_PATH="/usr/local/opt/jpeg-turbo;$CMAKE_PREFIX_PATH"
- OpenMP_ROOT=/usr/local/opt/libomp

before_script:
- |-
Expand Down Expand Up @@ -90,6 +92,7 @@ script:
cd "$TRAVIS_BUILD_DIR"

cd "$project_path"
rm -rf build
mkdir build
cd build

Expand All @@ -113,6 +116,7 @@ script:
cd "$TRAVIS_BUILD_DIR"

cd "$project_path"
rm -rf build
mkdir build
cd build

Expand Down
9 changes: 9 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ include(sail_check_builtin_bswap)
include(sail_check_c11_thread_local)
include(sail_check_include)
include(sail_check_init_once_execute_once)
include(sail_check_openmp)
include(sail_codec)
include(sail_enable_asan)
include(sail_enable_pch)
Expand Down Expand Up @@ -77,6 +78,7 @@ endif()
option(SAIL_BUILD_APPS "Build applications." ON)
option(SAIL_BUILD_EXAMPLES "Build examples." ON)
option(SAIL_DEV "Enable developer mode. Be more strict when compiling source code, for example." OFF)
option(SAIL_ENABLE_OPENMP "Enable OpenMP support if it's available in the compiler." ON)
set(SAIL_ENABLE_CODECS "" CACHE STRING "Forcefully enable the codecs specified in this ';'-separated list. \
If an enabled codec fails to find its dependencies, the configuration process fails. \
One can also specify not just individual codecs but codec groups by their priority like that: highest-priority;xbm. \
Expand All @@ -95,6 +97,12 @@ option(SAIL_THIRD_PARTY_CODECS_PATH "Enable loading third-party codecs from the
the SAIL_THIRD_PARTY_CODECS_PATH environment variable." ON)
option(SAIL_THREAD_SAFE "Enable working in multi-threaded environments by locking the internal context with a mutex." ON)

if (SAIL_ENABLE_OPENMP)
sail_check_openmp()
else()
set(SAIL_HAVE_OPENMP_DISPLAY "OFF (forced)" CACHE INTERNAL "")
endif()

# When we compile for VCPKG, VCPKG_TARGET_TRIPLET is defined
#
if (VCPKG_TARGET_TRIPLET)
Expand Down Expand Up @@ -327,6 +335,7 @@ message("*")
message("* SAIL_HAVE_BUILTIN_BSWAP16: ${SAIL_HAVE_BUILTIN_BSWAP16_DISPLAY}")
message("* SAIL_HAVE_BUILTIN_BSWAP32: ${SAIL_HAVE_BUILTIN_BSWAP32_DISPLAY}")
message("* SAIL_HAVE_BUILTIN_BSWAP64: ${SAIL_HAVE_BUILTIN_BSWAP64_DISPLAY}")
message("* SAIL_HAVE_OPENMP: ${SAIL_HAVE_OPENMP_DISPLAY}")
message("*")
message("* [*] - these options depend on other options, their values may be altered by CMake.")
message("* For example, if you configure with -DBUILD_SHARED_LIBS=OFF -DSAIL_COMBINE_CODECS=OFF,")
Expand Down
60 changes: 60 additions & 0 deletions cmake/sail_check_openmp.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Intended to be included by SAIL.
#
function(sail_check_openmp)
# This may require setting OpenMP_ROOT on macOS
#
find_package(OpenMP COMPONENTS C)

if (OpenMP_FOUND)
# We want OpenMP 3.0 to support unsigned integers in loops.
# The default OpenMP implementation in MSVC 2022 still supports 2.0,
# so switch to the LLVM option with OpenMP 3.1.
#
if (MSVC)
set(SAIL_OPENMP_FLAGS "/openmp:llvm" CACHE INTERNAL "")
else()
set(SAIL_OPENMP_FLAGS ${OpenMP_C_FLAGS} CACHE INTERNAL "")
endif()

set(SAIL_OPENMP_INCLUDE_DIRS ${OpenMP_C_INCLUDE_DIRS} CACHE INTERNAL "")

# Build a list of direct paths to libraries. This is needed on macOS with brew specifically
# as the installed libomp version lays in /usr/local/opt/libomp and is not globally visible.
#
foreach(lib IN LISTS OpenMP_C_LIB_NAMES)
set(SAIL_OPENMP_LIBS ${SAIL_OPENMP_LIBS} "${OpenMP_${lib}_LIBRARY}" CACHE INTERNAL "")
endforeach()

# Try to compile a sample program to make sure the compiler
# supports at least OpenMP 3.0 with unsigned integers in for loops.
#
cmake_push_check_state(RESET)
set(CMAKE_REQUIRED_FLAGS ${SAIL_OPENMP_FLAGS})
set(CMAKE_REQUIRED_INCLUDES ${SAIL_OPENMP_INCLUDE_DIRS})
set(CMAKE_REQUIRED_LIBRARIES ${SAIL_OPENMP_LIBS})

check_c_source_compiles(
"
#include <stdio.h>

int main(int argc, char *argv[]) {
unsigned i;
#pragma omp parallel for
for (i = 0; i < 10; i++) {
printf(\"%d\", i);
}

return 0;
}
"
SAIL_HAVE_OPENMP
)
cmake_pop_check_state()
endif()

if (SAIL_HAVE_OPENMP)
set(SAIL_HAVE_OPENMP_DISPLAY ON CACHE INTERNAL "")
else()
set(SAIL_HAVE_OPENMP_DISPLAY OFF CACHE INTERNAL "")
endif()
endfunction()
10 changes: 10 additions & 0 deletions src/sail-common/image.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*/

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

Expand Down Expand Up @@ -213,3 +214,12 @@ sail_status_t sail_mirror_horizontally(struct sail_image *image) {

return SAIL_OK;
}

void* sail_scan_line(const struct sail_image *image, unsigned row) {

if (SAIL_UNLIKELY(image == NULL || image->pixels == NULL)) {
return NULL;
}

return (uint8_t *)image->pixels + image->bytes_per_line * row;
}
6 changes: 6 additions & 0 deletions src/sail-common/image.h
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,12 @@ SAIL_EXPORT sail_status_t sail_mirror_vertically(struct sail_image *image);
*/
SAIL_EXPORT sail_status_t sail_mirror_horizontally(struct sail_image *image);

/*
* Returns the scan line at the given row.
* Return NULL if the image or its pixels is NULL.
*/
SAIL_EXPORT void* sail_scan_line(const struct sail_image *image, unsigned row);

/* extern "C" */
#ifdef __cplusplus
}
Expand Down
6 changes: 6 additions & 0 deletions src/sail-manip/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ endif()
#
target_include_directories(sail-manip PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/..>)

if (SAIL_HAVE_OPENMP)
target_compile_options(sail-manip PRIVATE ${SAIL_OPENMP_FLAGS})
target_include_directories(sail-manip PRIVATE ${SAIL_OPENMP_INCLUDE_DIRS})
target_link_libraries(sail-manip PRIVATE ${SAIL_OPENMP_LIBS})
endif()

target_link_libraries(sail-manip PUBLIC sail-common)

# pkg-config integration
Expand Down
Loading