From c9f6e5fd35852dc1499f2306aeb1ada2273f90ab Mon Sep 17 00:00:00 2001 From: PatStiles Date: Wed, 30 Oct 2024 00:32:22 -0300 Subject: [PATCH] create metal backend lib + add interface --- icicle/backend/metal/CMakeLists.txt | 44 +++++++++ icicle/backend/metal/metal_device_api.cpp | 107 ++++++++++++++++++++++ 2 files changed, 151 insertions(+) create mode 100644 icicle/backend/metal/CMakeLists.txt create mode 100644 icicle/backend/metal/metal_device_api.cpp diff --git a/icicle/backend/metal/CMakeLists.txt b/icicle/backend/metal/CMakeLists.txt new file mode 100644 index 000000000..985aba7b3 --- /dev/null +++ b/icicle/backend/metal/CMakeLists.txt @@ -0,0 +1,44 @@ +cmake_minimum_required(VERSION 3.18) + +# CPU backend is built directly into icicle library + +target_sources(icicle_device PRIVATE src/cpu_device_api.cpp) + +# field API library +if (FIELD) + target_sources(icicle_field PRIVATE + src/field/cpu_vec_ops.cpp + ) + if (NTT) + target_sources(icicle_field PRIVATE src/field/cpu_ntt.cpp src/polynomials/cpu_polynomial_backend.cpp) + endif() + if (POSEIDON) + target_sources(icicle_field PRIVATE src/hash/cpu_poseidon.cpp) + endif() + target_include_directories(icicle_field PRIVATE include) +endif() # FIELD + +# curve API library +if (CURVE) + target_sources(icicle_curve PRIVATE + src/curve/cpu_mont_conversion.cpp + ) + if (MSM) + target_sources(icicle_curve PRIVATE src/curve/cpu_msm.cpp) + endif() + if (ECNTT) + target_sources(icicle_curve PRIVATE src/curve/cpu_ecntt.cpp) + endif() + # incdir is PUBLIC because config-extension headers are exposed to app + target_include_directories(icicle_curve PUBLIC include) +endif() + +if (HASH) + target_sources(icicle_hash PRIVATE + src/hash/cpu_keccak.cpp + src/hash/cpu_blake2s.cpp + src/hash/cpu_merkle_tree.cpp + ) + target_include_directories(icicle_hash PUBLIC include) +endif() + diff --git a/icicle/backend/metal/metal_device_api.cpp b/icicle/backend/metal/metal_device_api.cpp new file mode 100644 index 000000000..0641a3410 --- /dev/null +++ b/icicle/backend/metal/metal_device_api.cpp @@ -0,0 +1,107 @@ + +#include +#include +#include "icicle/device_api.h" +#include "icicle/errors.h" +#include "icicle/utils/log.h" + +using namespace icicle; + +class MetalDeviceAPI : public DeviceAPI +{ +public: + eIcicleError set_device(const Device& device) override + { + return (device.id == 0) ? eIcicleError::SUCCESS : eIcicleError::INVALID_DEVICE; + } + + eIcicleError get_device_count(int& device_count) const override + { + device_count = 1; + return eIcicleError::SUCCESS; + } + + // Memory management + eIcicleError allocate_memory(void** ptr, size_t size) const override + { + //TODO + return (*ptr == nullptr) ? eIcicleError::ALLOCATION_FAILED : eIcicleError::SUCCESS; + } + + eIcicleError allocate_memory_async(void** ptr, size_t size, icicleStreamHandle stream) const override + { + return MetalDeviceAPI::allocate_memory(ptr, size); + } + + eIcicleError free_memory(void* ptr) const override + { + //TODO + return eIcicleError::SUCCESS; + } + + eIcicleError free_memory_async(void* ptr, icicleStreamHandle stream) const override + { + return MetalDeviceAPI::free_memory(ptr); + } + + eIcicleError get_available_memory(size_t& total /*OUT*/, size_t& free /*OUT*/) const override + { + //TODO + return eIcicleError::API_NOT_IMPLEMENTED; + } + + eIcicleError memset(void* ptr, int value, size_t size) const override + { + //TODO + return eIcicleError::SUCCESS; + } + + eIcicleError memset_async(void* ptr, int value, size_t size, icicleStreamHandle stream) const override + { + //TODO + return eIcicleError::SUCCESS; + } + + eIcicleError memCopy(void* dst, const void* src, size_t size) const + { + //TODO: + return eIcicleError::SUCCESS; + } + + // Data transfer + eIcicleError copy(void* dst, const void* src, size_t size, eCopyDirection direction) const override + { + return memCopy(dst, src, size); + } + + eIcicleError copy_async( + void* dst, const void* src, size_t size, eCopyDirection direction, icicleStreamHandle stream) const override + { + return memCopy(dst, src, size); + } + + // Synchronization + eIcicleError synchronize(icicleStreamHandle stream = nullptr) const override { return eIcicleError::SUCCESS; } + + // Stream management + eIcicleError create_stream(icicleStreamHandle* stream) const override + { + *stream = nullptr; // no streams for CPU + return eIcicleError::SUCCESS; + } + + eIcicleError destroy_stream(icicleStreamHandle stream) const override + { + return (nullptr == stream) ? eIcicleError::SUCCESS : eIcicleError::STREAM_DESTRUCTION_FAILED; + } + + eIcicleError get_device_properties(DeviceProperties& properties) const override + { + properties.using_host_memory = true; + properties.num_memory_regions = 0; + properties.supports_pinned_memory = false; + return eIcicleError::SUCCESS; + } +}; + +REGISTER_DEVICE_API("METAL", MetalDeviceAPI);