From 1febb81481bd94665ddf6d6a7bdb13e1114b416a Mon Sep 17 00:00:00 2001 From: William Ayd Date: Mon, 17 Jun 2024 10:16:05 -0400 Subject: [PATCH] feat: Add Meson support in nanoarrow_device (#484) Co-authored-by: Will Ayd --- ci/scripts/build-with-meson.sh | 5 ++ meson.options | 4 ++ src/nanoarrow/meson.build | 54 ++++++++++++++++++- src/nanoarrow/nanoarrow_device_metal.cc | 5 +- subprojects/metal-cpp.wrap | 26 +++++++++ .../packagefiles/metal-cpp/meson.build | 25 +++++++++ 6 files changed, 116 insertions(+), 3 deletions(-) create mode 100644 subprojects/metal-cpp.wrap create mode 100644 subprojects/packagefiles/metal-cpp/meson.build diff --git a/ci/scripts/build-with-meson.sh b/ci/scripts/build-with-meson.sh index 80fbed3ee..d8c2cf1bd 100755 --- a/ci/scripts/build-with-meson.sh +++ b/ci/scripts/build-with-meson.sh @@ -71,8 +71,10 @@ function main() { -Db_sanitize="address,undefined" \ -Dtests=true \ -Dipc=true \ + -Ddevice=true \ -Dbenchmarks=false \ -Db_coverage=false + meson compile export ASAN_OPTIONS=allocator_may_return_null=1 # allow ENOMEM tests meson test --print-errorlogs @@ -83,6 +85,7 @@ function main() { -Db_sanitize=none \ -Dtests=true \ -Dipc=true \ + -Ddevice=true \ -Dbenchmarks=false \ -Db_coverage=false meson compile @@ -94,6 +97,7 @@ function main() { -Db_sanitize=none \ -Dtests=false \ -Dipc=true \ + -Ddevice=true \ -Dbenchmarks=true \ -Db_coverage=false meson compile @@ -105,6 +109,7 @@ function main() { -Db_sanitize=none \ -Dtests=true \ -Dipc=true \ + -Ddevice=true \ -Dbenchmarks=false \ -Db_coverage=true diff --git a/meson.options b/meson.options index b91509820..3e9f60fba 100644 --- a/meson.options +++ b/meson.options @@ -24,3 +24,7 @@ option('integration_tests', type: 'boolean', value: false) option('namespace', type: 'string', description: 'A prefix for exported symbols') +option('device', type: 'boolean', description: 'Build device libraries', value: false) +option('metal', type: 'boolean', description: 'Build Apple metal libraries', + value: false) +option('cuda', type: 'boolean', description: 'Build CUDA libraries', value: false) diff --git a/src/nanoarrow/meson.build b/src/nanoarrow/meson.build index cdbd6ed0f..bda83fb3a 100644 --- a/src/nanoarrow/meson.build +++ b/src/nanoarrow/meson.build @@ -80,6 +80,35 @@ if get_option('ipc') dependencies: [nanoarrow_dep, flatcc_dep]) endif +needs_device = get_option('device') or get_option('metal') or get_option('cuda') +if needs_device + device_deps = [nanoarrow_dep] + device_srcs = ['nanoarrow_device.c'] + device_defines = [] + + if get_option('metal') + metal_dep = dependency('appleframeworks', modules: ['Foundation', 'Metal']) + metal_cpp_dep = dependency('metal-cpp') + device_deps += metal_dep + device_deps += metal_cpp_dep + device_srcs += 'nanoarrow_device_metal.cc' + device_defines += '-DNANOARROW_DEVICE_WITH_METAL' + endif + + if get_option('cuda') + error('CUDA support with the Meson build system is not implemented') + endif + + nanoarrow_device_lib = build_target( + 'nanoarrow_device', + sources: device_srcs, + dependencies: device_deps, + install: true, + target_type: libtype, + cpp_args: device_defines, + ) +endif + if get_option('tests') or get_option('integration_tests') nlohmann_json_dep = dependency('nlohmann_json') @@ -100,7 +129,7 @@ if get_option('tests') # https://mesonbuild.com/Unit-tests.html#coverage arrow_dep = dependency('arrow') - gtest_dep = dependency('gtest', fallback: ['gtest', 'gtest_main_dep']) + gtest_dep = dependency('gtest_main') gmock_dep = dependency('gmock') nlohmann_json_dep = dependency('nlohmann_json') @@ -185,4 +214,27 @@ if get_option('tests') test(name, exc, timeout: config['timeout']) endforeach endif + + if needs_device + device_tests = ['nanoarrow_device', 'nanoarrow_device_hpp'] + foreach device_test : device_tests + exc = executable( + device_test.replace('_', '-') + '-test', + device_test + '_test.cc', + link_with: nanoarrow_device_lib, + dependencies: [nanoarrow_dep, gtest_dep], + ) + test(device_test.replace('_', '-'), exc) + endforeach + + if get_option('metal') + exc = executable( + 'nanoarrow-device-metal-test', + 'nanoarrow_device_metal_test.cc', + link_with: nanoarrow_device_lib, + dependencies: [nanoarrow_dep, gtest_dep, metal_cpp_dep], + ) + test('nanoarrow-device-metal', exc) + endif + endif endif diff --git a/src/nanoarrow/nanoarrow_device_metal.cc b/src/nanoarrow/nanoarrow_device_metal.cc index 43f064898..0db18f2c0 100644 --- a/src/nanoarrow/nanoarrow_device_metal.cc +++ b/src/nanoarrow/nanoarrow_device_metal.cc @@ -163,7 +163,7 @@ static ArrowErrorCode ArrowDeviceMetalArrayInit(struct ArrowDevice* device, } // One can create a new event with mtl_device->newSharedEvent(); - private_data->event = sync_event; + private_data->event = static_cast(sync_event); memset(device_array, 0, sizeof(struct ArrowDeviceArray)); device_array->array = *array; @@ -330,7 +330,8 @@ static ArrowErrorCode ArrowDeviceMetalArrayMove(struct ArrowDevice* device_src, return ENOTSUP; } - NANOARROW_RETURN_NOT_OK(ArrowDeviceArrayInit(device_dst, dst, &src->array)); + NANOARROW_RETURN_NOT_OK( + ArrowDeviceArrayInit(device_dst, dst, &src->array, src->sync_event)); return NANOARROW_OK; } else if (device_src->device_type == ARROW_DEVICE_METAL && diff --git a/subprojects/metal-cpp.wrap b/subprojects/metal-cpp.wrap new file mode 100644 index 000000000..aa85e3ebf --- /dev/null +++ b/subprojects/metal-cpp.wrap @@ -0,0 +1,26 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +[wrap-file] +directory = metal-cpp +source_url = https://developer.apple.com/metal/cpp/files/metal-cpp_macOS12_iOS15.zip +source_filename = metal-cpp_macOS12_iOS15.zip +source_hash = a4e2d4668951b6f2595618ed8c5dc514fc94fda5487fc722b1c1ff29d7b524f7 +patch_directory = metal-cpp + +[provide] +metal-cpp = metal_cpp_dep diff --git a/subprojects/packagefiles/metal-cpp/meson.build b/subprojects/packagefiles/metal-cpp/meson.build new file mode 100644 index 000000000..ba3beb405 --- /dev/null +++ b/subprojects/packagefiles/metal-cpp/meson.build @@ -0,0 +1,25 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +project( + 'metal-cpp', + 'cpp', + license : 'Apache-2.0', +) + +incdir = include_directories('.') +metal_cpp_dep = declare_dependency(include_directories: incdir)