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

feat(backend): Add Metal Backend #638

Closed
wants to merge 1 commit into from
Closed
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
44 changes: 44 additions & 0 deletions icicle/backend/metal/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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()

107 changes: 107 additions & 0 deletions icicle/backend/metal/metal_device_api.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@

#include <iostream>
#include <cstring>
#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);