mirror of
https://github.com/pseXperiments/icicle.git
synced 2026-01-09 15:37:58 -05:00
add runtime api to load backend from default installdir and use it everywhere instead of specifying the install dir in the code
This commit is contained in:
committed by
Koren-Brand
parent
67afbd035f
commit
94facf80db
@@ -8,9 +8,9 @@ show_help() {
|
||||
echo "Usage: $0 [-d DEVICE_TYPE] [-b BACKEND_INSTALL_DIR]"
|
||||
echo
|
||||
echo "Options:"
|
||||
echo " -d DEVICE_TYPE Specify the device type (default: CPU)"
|
||||
echo " -b BACKEND_INSTALL_DIR Specify the backend installation directory (default: empty)"
|
||||
echo " -h Show this help message"
|
||||
echo " -d DEVICE_TYPE Specify the device type (default: CPU)"
|
||||
echo " -b ICICLE_BACKEND_INSTALL_DIR Specify the backend installation directory (default: empty)"
|
||||
echo " -h Show this help message"
|
||||
exit 0
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ while getopts ":d:b:h" opt; do
|
||||
DEVICE_TYPE=$OPTARG
|
||||
;;
|
||||
b )
|
||||
BACKEND_INSTALL_DIR="$(realpath ${OPTARG})"
|
||||
ICICLE_BACKEND_INSTALL_DIR="$(realpath ${OPTARG})"
|
||||
;;
|
||||
h )
|
||||
show_help
|
||||
@@ -39,7 +39,7 @@ done
|
||||
|
||||
# Set default values if not provided
|
||||
: "${DEVICE_TYPE:=CPU}"
|
||||
: "${BACKEND_INSTALL_DIR:=}"
|
||||
: "${ICICLE_BACKEND_INSTALL_DIR:=}"
|
||||
|
||||
# Create necessary directories
|
||||
mkdir -p build/example
|
||||
@@ -49,11 +49,13 @@ ICILE_DIR=$(realpath "../../../icicle_v3/")
|
||||
ICICLE_CUDA_SOURCE_DIR="${ICILE_DIR}/backend/cuda"
|
||||
|
||||
# Build Icicle and the example app that links to it
|
||||
if [ "$DEVICE_TYPE" == "CUDA" ] && [ ! -d "${BACKEND_INSTALL_DIR}" ] && [ -d "${ICICLE_CUDA_SOURCE_DIR}" ]; then
|
||||
if [ "$DEVICE_TYPE" == "CUDA" ] && [ ! -d "${ICICLE_BACKEND_INSTALL_DIR}" ] && [ -d "${ICICLE_CUDA_SOURCE_DIR}" ]; then
|
||||
echo "Building icicle with CUDA backend"
|
||||
BACKEND_INSTALL_DIR="./target/release/deps/icicle/lib/backend"
|
||||
cargo run --release --features=cuda -- --device-type "${DEVICE_TYPE}" --backend-install-dir "${BACKEND_INSTALL_DIR}"
|
||||
cargo build --release --features=cuda
|
||||
export ICICLE_BACKEND_INSTALL_DIR=$(realpath "./target/release/deps/icicle/lib/backend")
|
||||
cargo run --release --features=cuda -- --device-type "${DEVICE_TYPE}"
|
||||
else
|
||||
echo "Building icicle without CUDA backend, BACKEND_INSTALL_DIR=${BACKEND_INSTALL_DIR}"
|
||||
cargo run --release -- --device-type "${DEVICE_TYPE}" --backend-install-dir "${BACKEND_INSTALL_DIR}"
|
||||
echo "Building icicle without CUDA backend, BACKEND_INSTALL_DIR=${ICICLE_BACKEND_INSTALL_DIR}"
|
||||
export ICICLE_BACKEND_INSTALL_DIR="$ICICLE_BACKEND_INSTALL_DIR";
|
||||
cargo run --release -- --device-type "${DEVICE_TYPE}"
|
||||
fi
|
||||
|
||||
@@ -25,23 +25,16 @@ struct Args {
|
||||
/// Device type (e.g., "CPU", "CUDA")
|
||||
#[arg(short, long, default_value = "CPU")]
|
||||
device_type: String,
|
||||
|
||||
/// Backend installation directory
|
||||
#[arg(short, long, default_value = "")]
|
||||
backend_install_dir: String,
|
||||
}
|
||||
|
||||
// Load backend and set device
|
||||
fn try_load_and_set_backend_device(args: &Args) {
|
||||
if !args
|
||||
.backend_install_dir
|
||||
.is_empty()
|
||||
{
|
||||
println!("Trying to load backend from {}", &args.backend_install_dir);
|
||||
icicle_runtime::runtime::load_backend(&args.backend_install_dir).unwrap();
|
||||
if args.device_type != "CPU" {
|
||||
icicle_runtime::runtime::load_backend_from_env_or_default().unwrap();
|
||||
}
|
||||
println!("Setting device {}", args.device_type);
|
||||
icicle_runtime::set_device(&icicle_runtime::Device::new(&args.device_type, 0)).unwrap();
|
||||
let device = icicle_runtime::Device::new(&args.device_type, 0 /* =device_id*/);
|
||||
icicle_runtime::set_device(&device).unwrap();
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
@@ -8,9 +8,9 @@ show_help() {
|
||||
echo "Usage: $0 [-d DEVICE_TYPE] [-b BACKEND_INSTALL_DIR]"
|
||||
echo
|
||||
echo "Options:"
|
||||
echo " -d DEVICE_TYPE Specify the device type (default: CPU)"
|
||||
echo " -b BACKEND_INSTALL_DIR Specify the backend installation directory (default: empty)"
|
||||
echo " -h Show this help message"
|
||||
echo " -d DEVICE_TYPE Specify the device type (default: CPU)"
|
||||
echo " -b ICICLE_BACKEND_INSTALL_DIR Specify the backend installation directory (default: empty)"
|
||||
echo " -h Show this help message"
|
||||
exit 0
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ while getopts ":d:b:h" opt; do
|
||||
DEVICE_TYPE=$OPTARG
|
||||
;;
|
||||
b )
|
||||
BACKEND_INSTALL_DIR="$(realpath ${OPTARG})"
|
||||
ICICLE_BACKEND_INSTALL_DIR="$(realpath ${OPTARG})"
|
||||
;;
|
||||
h )
|
||||
show_help
|
||||
@@ -39,7 +39,7 @@ done
|
||||
|
||||
# Set default values if not provided
|
||||
: "${DEVICE_TYPE:=CPU}"
|
||||
: "${BACKEND_INSTALL_DIR:=}"
|
||||
: "${ICICLE_BACKEND_INSTALL_DIR:=}"
|
||||
|
||||
# Create necessary directories
|
||||
mkdir -p build/example
|
||||
@@ -49,11 +49,13 @@ ICILE_DIR=$(realpath "../../../icicle_v3/")
|
||||
ICICLE_CUDA_SOURCE_DIR="${ICILE_DIR}/backend/cuda"
|
||||
|
||||
# Build Icicle and the example app that links to it
|
||||
if [ "$DEVICE_TYPE" == "CUDA" ] && [ ! -d "${BACKEND_INSTALL_DIR}" ] && [ -d "${ICICLE_CUDA_SOURCE_DIR}" ]; then
|
||||
if [ "$DEVICE_TYPE" == "CUDA" ] && [ ! -d "${ICICLE_BACKEND_INSTALL_DIR}" ] && [ -d "${ICICLE_CUDA_SOURCE_DIR}" ]; then
|
||||
echo "Building icicle with CUDA backend"
|
||||
BACKEND_INSTALL_DIR="./target/release/deps/icicle/lib/backend"
|
||||
cargo run --release --features=cuda -- --device-type "${DEVICE_TYPE}" --backend-install-dir "${BACKEND_INSTALL_DIR}"
|
||||
cargo build --release --features=cuda
|
||||
export ICICLE_BACKEND_INSTALL_DIR=$(realpath "./target/release/deps/icicle/lib/backend")
|
||||
cargo run --release --features=cuda -- --device-type "${DEVICE_TYPE}"
|
||||
else
|
||||
echo "Building icicle without CUDA backend, BACKEND_INSTALL_DIR=${BACKEND_INSTALL_DIR}"
|
||||
cargo run --release -- --device-type "${DEVICE_TYPE}" --backend-install-dir "${BACKEND_INSTALL_DIR}"
|
||||
echo "Building icicle without CUDA backend, BACKEND_INSTALL_DIR=${ICICLE_BACKEND_INSTALL_DIR}"
|
||||
export ICICLE_BACKEND_INSTALL_DIR="$ICICLE_BACKEND_INSTALL_DIR";
|
||||
cargo run --release -- --device-type "${DEVICE_TYPE}"
|
||||
fi
|
||||
|
||||
@@ -19,23 +19,16 @@ struct Args {
|
||||
/// Device type (e.g., "CPU", "CUDA")
|
||||
#[arg(short, long, default_value = "CPU")]
|
||||
device_type: String,
|
||||
|
||||
/// Backend installation directory
|
||||
#[arg(short, long, default_value = "")]
|
||||
backend_install_dir: String,
|
||||
}
|
||||
|
||||
// Load backend and set device
|
||||
fn try_load_and_set_backend_device(args: &Args) {
|
||||
if !args
|
||||
.backend_install_dir
|
||||
.is_empty()
|
||||
{
|
||||
println!("Trying to load backend from {}", &args.backend_install_dir);
|
||||
icicle_runtime::runtime::load_backend(&args.backend_install_dir).unwrap();
|
||||
if args.device_type != "CPU" {
|
||||
icicle_runtime::runtime::load_backend_from_env_or_default().unwrap();
|
||||
}
|
||||
println!("Setting device {}", args.device_type);
|
||||
icicle_runtime::set_device(&icicle_runtime::Device::new(&args.device_type, 0)).unwrap();
|
||||
let device = icicle_runtime::Device::new(&args.device_type, 0 /* =device_id*/);
|
||||
icicle_runtime::set_device(&device).unwrap();
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
@@ -8,9 +8,9 @@ show_help() {
|
||||
echo "Usage: $0 [-d DEVICE_TYPE] [-b BACKEND_INSTALL_DIR]"
|
||||
echo
|
||||
echo "Options:"
|
||||
echo " -d DEVICE_TYPE Specify the device type (default: CPU)"
|
||||
echo " -b BACKEND_INSTALL_DIR Specify the backend installation directory (default: empty)"
|
||||
echo " -h Show this help message"
|
||||
echo " -d DEVICE_TYPE Specify the device type (default: CPU)"
|
||||
echo " -b ICICLE_BACKEND_INSTALL_DIR Specify the backend installation directory (default: empty)"
|
||||
echo " -h Show this help message"
|
||||
exit 0
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ while getopts ":d:b:h" opt; do
|
||||
DEVICE_TYPE=$OPTARG
|
||||
;;
|
||||
b )
|
||||
BACKEND_INSTALL_DIR="$(realpath ${OPTARG})"
|
||||
ICICLE_BACKEND_INSTALL_DIR="$(realpath ${OPTARG})"
|
||||
;;
|
||||
h )
|
||||
show_help
|
||||
@@ -39,7 +39,7 @@ done
|
||||
|
||||
# Set default values if not provided
|
||||
: "${DEVICE_TYPE:=CPU}"
|
||||
: "${BACKEND_INSTALL_DIR:=}"
|
||||
: "${ICICLE_BACKEND_INSTALL_DIR:=}"
|
||||
|
||||
# Create necessary directories
|
||||
mkdir -p build/example
|
||||
@@ -49,11 +49,13 @@ ICILE_DIR=$(realpath "../../../icicle_v3/")
|
||||
ICICLE_CUDA_SOURCE_DIR="${ICILE_DIR}/backend/cuda"
|
||||
|
||||
# Build Icicle and the example app that links to it
|
||||
if [ "$DEVICE_TYPE" == "CUDA" ] && [ ! -d "${BACKEND_INSTALL_DIR}" ] && [ -d "${ICICLE_CUDA_SOURCE_DIR}" ]; then
|
||||
if [ "$DEVICE_TYPE" == "CUDA" ] && [ ! -d "${ICICLE_BACKEND_INSTALL_DIR}" ] && [ -d "${ICICLE_CUDA_SOURCE_DIR}" ]; then
|
||||
echo "Building icicle with CUDA backend"
|
||||
BACKEND_INSTALL_DIR="./target/release/deps/icicle/lib/backend"
|
||||
cargo run --release --features=cuda -- --device-type "${DEVICE_TYPE}" --backend-install-dir "${BACKEND_INSTALL_DIR}"
|
||||
cargo build --release --features=cuda
|
||||
export ICICLE_BACKEND_INSTALL_DIR=$(realpath "./target/release/deps/icicle/lib/backend")
|
||||
cargo run --release --features=cuda -- --device-type "${DEVICE_TYPE}"
|
||||
else
|
||||
echo "Building icicle without CUDA backend, BACKEND_INSTALL_DIR=${BACKEND_INSTALL_DIR}"
|
||||
cargo run --release -- --device-type "${DEVICE_TYPE}" --backend-install-dir "${BACKEND_INSTALL_DIR}"
|
||||
echo "Building icicle without CUDA backend, BACKEND_INSTALL_DIR=${ICICLE_BACKEND_INSTALL_DIR}"
|
||||
export ICICLE_BACKEND_INSTALL_DIR="$ICICLE_BACKEND_INSTALL_DIR";
|
||||
cargo run --release -- --device-type "${DEVICE_TYPE}"
|
||||
fi
|
||||
|
||||
@@ -25,20 +25,12 @@ struct Args {
|
||||
/// Device type (e.g., "CPU", "CUDA")
|
||||
#[arg(short, long, default_value = "CPU")]
|
||||
device_type: String,
|
||||
|
||||
/// Backend installation directory
|
||||
#[arg(short, long, default_value = "/opt/icicle/backend")]
|
||||
backend_install_dir: String,
|
||||
}
|
||||
|
||||
// Load backend and set device
|
||||
fn try_load_and_set_backend_device(args: &Args) {
|
||||
if !args
|
||||
.backend_install_dir
|
||||
.is_empty()
|
||||
{
|
||||
println!("Trying to load backend from {}", &args.backend_install_dir);
|
||||
icicle_runtime::runtime::load_backend(&args.backend_install_dir).unwrap();
|
||||
if args.device_type != "CPU" {
|
||||
icicle_runtime::runtime::load_backend_from_env_or_default().unwrap();
|
||||
}
|
||||
println!("Setting device {}", args.device_type);
|
||||
let device = icicle_runtime::Device::new(&args.device_type, 0 /* =device_id*/);
|
||||
|
||||
@@ -23,7 +23,8 @@ namespace icicle {
|
||||
STREAM_DESTRUCTION_FAILED, ///< Stream destruction failed
|
||||
API_NOT_IMPLEMENTED, ///< The API is not implemented for a device
|
||||
INVALID_ARGUMENT, ///< Invalid argument passed
|
||||
UNKNOWN_ERROR ///< An unknown error occurred
|
||||
BACKEND_LOAD_FAILED, ///< Failed to load the backend
|
||||
UNKNOWN_ERROR, ///< An unknown error occurred
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -59,6 +60,8 @@ namespace icicle {
|
||||
return "eIcicleError::API_NOT_IMPLEMENTED";
|
||||
case eIcicleError::INVALID_ARGUMENT:
|
||||
return "eIcicleError::INVALID_ARGUMENT";
|
||||
case eIcicleError::BACKEND_LOAD_FAILED:
|
||||
return "eIcicleError::BACKEND_LOAD_FAILED";
|
||||
case eIcicleError::UNKNOWN_ERROR:
|
||||
default:
|
||||
return "eIcicleError::UNKNOWN_ERROR";
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
using namespace icicle;
|
||||
|
||||
/**
|
||||
* @brief Search and load icicle backed to process
|
||||
* @brief load icicle backed to process from install dir
|
||||
*
|
||||
|
||||
* @param path Path of the backend library or directory where backend libraries are installed
|
||||
@@ -16,6 +16,18 @@ using namespace icicle;
|
||||
*/
|
||||
extern "C" eIcicleError icicle_load_backend(const char* path, bool is_recursive);
|
||||
|
||||
/**
|
||||
* @brief Attempts to load the backend from either the environment variable or the default install directory.
|
||||
*
|
||||
* This function first checks if the environment variable `ICICLE_BACKEND_INSTALL_DIR` is set and points to an existing
|
||||
* directory. If so, it attempts to load the backend from that directory. If the environment variable is not set or the
|
||||
* directory does not exist, it falls back to the default directory (`/opt/icicle/backend`). If neither option is
|
||||
* successful, the function returns an error.
|
||||
*
|
||||
* @return eIcicleError The status of the backend loading operation, indicating success or failure.
|
||||
*/
|
||||
extern "C" eIcicleError icicle_load_backend_from_env_or_default();
|
||||
|
||||
/**
|
||||
* @brief Set active device for thread
|
||||
*
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include <dirent.h>
|
||||
#include <sys/stat.h>
|
||||
#include <string>
|
||||
#include <filesystem>
|
||||
|
||||
#include "icicle/runtime.h"
|
||||
#include "icicle/device_api.h"
|
||||
@@ -304,4 +305,32 @@ extern "C" eIcicleError icicle_load_backend(const char* path, bool is_recursive)
|
||||
}
|
||||
|
||||
return eIcicleError::SUCCESS;
|
||||
}
|
||||
|
||||
extern "C" eIcicleError icicle_load_backend_from_env_or_default()
|
||||
{
|
||||
// First, check the environment variable
|
||||
const char* env_dir = std::getenv("ICICLE_BACKEND_INSTALL_DIR");
|
||||
if (env_dir && std::filesystem::exists(env_dir)) {
|
||||
// Attempt to load the backend from the environment variable directory
|
||||
eIcicleError result = icicle_load_backend(env_dir, true /*=recursive*/);
|
||||
if (result == eIcicleError::SUCCESS) {
|
||||
ICICLE_LOG_INFO << "ICICLE backend loaded from $ICICLE_BACKEND_INSTALL_DIR=" << env_dir;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
// If not found or failed, fall back to the default directory
|
||||
const std::string default_dir = "/opt/icicle/backend";
|
||||
if (std::filesystem::exists(default_dir)) {
|
||||
eIcicleError result = icicle_load_backend(default_dir.c_str(), true /*=recursive*/);
|
||||
if (result == eIcicleError::SUCCESS) {
|
||||
ICICLE_LOG_INFO << "ICICLE backend loaded from " << default_dir;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
// If neither works, return a failure status
|
||||
ICICLE_LOG_INFO << "Failed to load backend from any known directory.";
|
||||
return eIcicleError::BACKEND_LOAD_FAILED;
|
||||
}
|
||||
@@ -35,8 +35,9 @@ public:
|
||||
static void SetUpTestSuite()
|
||||
{
|
||||
#ifdef BACKEND_BUILD_DIR
|
||||
icicle_load_backend(BACKEND_BUILD_DIR, true);
|
||||
setenv("ICICLE_BACKEND_INSTALL_DIR", BACKEND_BUILD_DIR, 0 /*=replace*/);
|
||||
#endif
|
||||
icicle_load_backend_from_env_or_default();
|
||||
|
||||
// check targets are loaded and choose main and reference targets
|
||||
auto regsitered_devices = get_registered_devices_list();
|
||||
|
||||
@@ -23,8 +23,9 @@ public:
|
||||
static void SetUpTestSuite()
|
||||
{
|
||||
#ifdef BACKEND_BUILD_DIR
|
||||
icicle_load_backend(BACKEND_BUILD_DIR, true);
|
||||
setenv("ICICLE_BACKEND_INSTALL_DIR", BACKEND_BUILD_DIR, 0 /*=replace*/);
|
||||
#endif
|
||||
icicle_load_backend_from_env_or_default();
|
||||
s_regsitered_devices = get_registered_devices_list();
|
||||
ASSERT_GT(s_regsitered_devices.size(), 0);
|
||||
}
|
||||
|
||||
@@ -38,8 +38,9 @@ public:
|
||||
static void SetUpTestSuite()
|
||||
{
|
||||
#ifdef BACKEND_BUILD_DIR
|
||||
icicle_load_backend(BACKEND_BUILD_DIR, true);
|
||||
setenv("ICICLE_BACKEND_INSTALL_DIR", BACKEND_BUILD_DIR, 0 /*=replace*/);
|
||||
#endif
|
||||
icicle_load_backend_from_env_or_default();
|
||||
|
||||
// check targets are loaded and choose main and reference targets
|
||||
auto regsitered_devices = get_registered_devices_list();
|
||||
|
||||
@@ -36,8 +36,9 @@ public:
|
||||
static void SetUpTestSuite()
|
||||
{
|
||||
#ifdef BACKEND_BUILD_DIR
|
||||
icicle_load_backend(BACKEND_BUILD_DIR, true);
|
||||
setenv("ICICLE_BACKEND_INSTALL_DIR", BACKEND_BUILD_DIR, 0 /*=replace*/);
|
||||
#endif
|
||||
icicle_load_backend_from_env_or_default();
|
||||
s_registered_devices = get_registered_devices_list();
|
||||
|
||||
// init NTT domain
|
||||
|
||||
@@ -15,7 +15,7 @@ fn main() {
|
||||
|
||||
// default backends dir
|
||||
println!(
|
||||
"cargo:rustc-env=DEFAULT_BACKEND_INSTALL_DIR={}/icicle/lib/backend",
|
||||
"cargo:rustc-env=ICICLE_BACKEND_INSTALL_DIR={}/icicle/lib/backend",
|
||||
deps_dir.display()
|
||||
);
|
||||
}
|
||||
|
||||
@@ -19,9 +19,19 @@ pub trait Curve: Debug + PartialEq + Copy + Clone {
|
||||
#[doc(hidden)]
|
||||
fn generate_random_affine_points(size: usize) -> Vec<Affine<Self>>;
|
||||
#[doc(hidden)]
|
||||
fn convert_affine_montgomery(points: *mut Affine<Self>, len: usize, is_into: bool, stream: &IcicleStream) -> eIcicleError;
|
||||
fn convert_affine_montgomery(
|
||||
points: *mut Affine<Self>,
|
||||
len: usize,
|
||||
is_into: bool,
|
||||
stream: &IcicleStream,
|
||||
) -> eIcicleError;
|
||||
#[doc(hidden)]
|
||||
fn convert_projective_montgomery(points: *mut Projective<Self>, len: usize, is_into: bool, stream: &IcicleStream) -> eIcicleError;
|
||||
fn convert_projective_montgomery(
|
||||
points: *mut Projective<Self>,
|
||||
len: usize,
|
||||
is_into: bool,
|
||||
stream: &IcicleStream,
|
||||
) -> eIcicleError;
|
||||
}
|
||||
|
||||
/// A [projective](https://hyperelliptic.org/EFD/g1p/auto-shortw-projective.html) elliptic curve point.
|
||||
@@ -118,14 +128,14 @@ impl<C: Curve> From<Projective<C>> for Affine<C> {
|
||||
|
||||
impl<C: Curve> MontgomeryConvertible for Affine<C> {
|
||||
fn to_mont(values: &mut DeviceSlice<Self>, stream: &IcicleStream) -> eIcicleError {
|
||||
if !values.is_on_active_device(){
|
||||
if !values.is_on_active_device() {
|
||||
panic!("values not allocated on an inactive device");
|
||||
}
|
||||
C::convert_affine_montgomery(unsafe { values.as_mut_ptr() }, values.len(), true, stream)
|
||||
}
|
||||
|
||||
fn from_mont(values: &mut DeviceSlice<Self>, stream: &IcicleStream) -> eIcicleError {
|
||||
if !values.is_on_active_device(){
|
||||
if !values.is_on_active_device() {
|
||||
panic!("values not allocated on an inactive device");
|
||||
}
|
||||
C::convert_affine_montgomery(unsafe { values.as_mut_ptr() }, values.len(), false, stream)
|
||||
@@ -134,14 +144,14 @@ impl<C: Curve> MontgomeryConvertible for Affine<C> {
|
||||
|
||||
impl<C: Curve> MontgomeryConvertible for Projective<C> {
|
||||
fn to_mont(values: &mut DeviceSlice<Self>, stream: &IcicleStream) -> eIcicleError {
|
||||
if !values.is_on_active_device(){
|
||||
if !values.is_on_active_device() {
|
||||
panic!("values not allocated on an inactive device");
|
||||
}
|
||||
C::convert_projective_montgomery(unsafe { values.as_mut_ptr() }, values.len(), true, stream)
|
||||
}
|
||||
|
||||
fn from_mont(values: &mut DeviceSlice<Self>, stream: &IcicleStream) -> eIcicleError {
|
||||
if !values.is_on_active_device(){
|
||||
if !values.is_on_active_device() {
|
||||
panic!("values not allocated on an inactive device");
|
||||
}
|
||||
C::convert_projective_montgomery(unsafe { values.as_mut_ptr() }, values.len(), false, stream)
|
||||
@@ -238,15 +248,7 @@ macro_rules! impl_curve {
|
||||
config.is_result_on_device = true;
|
||||
config.is_async = false;
|
||||
config.stream_handle = (&*stream).into();
|
||||
unsafe {
|
||||
$curve_prefix_ident::_convert_affine_montgomery(
|
||||
points,
|
||||
len,
|
||||
is_into,
|
||||
&config,
|
||||
points
|
||||
)
|
||||
}
|
||||
unsafe { $curve_prefix_ident::_convert_affine_montgomery(points, len, is_into, &config, points) }
|
||||
}
|
||||
|
||||
fn convert_projective_montgomery(
|
||||
@@ -260,15 +262,7 @@ macro_rules! impl_curve {
|
||||
config.is_result_on_device = true;
|
||||
config.is_async = false;
|
||||
config.stream_handle = (&*stream).into();
|
||||
unsafe {
|
||||
$curve_prefix_ident::_convert_projective_montgomery(
|
||||
points,
|
||||
len,
|
||||
is_into,
|
||||
&config,
|
||||
points
|
||||
)
|
||||
}
|
||||
unsafe { $curve_prefix_ident::_convert_projective_montgomery(points, len, is_into, &config, points) }
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -306,4 +300,4 @@ macro_rules! impl_curve_tests {
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -190,7 +190,7 @@ macro_rules! impl_scalar_field {
|
||||
}
|
||||
|
||||
impl MontgomeryConvertibleField<$field_name> for $field_cfg {
|
||||
fn to_mont(values: &mut DeviceSlice<$field_name>, stream: &IcicleStream) -> eIcicleError {
|
||||
fn to_mont(values: &mut DeviceSlice<$field_name>, stream: &IcicleStream) -> eIcicleError {
|
||||
// check device slice is on active device
|
||||
if !values.is_on_active_device() {
|
||||
panic!("input not allocated on an inactive device");
|
||||
|
||||
@@ -3,8 +3,8 @@ pub mod ecntt;
|
||||
pub mod field;
|
||||
pub mod msm;
|
||||
pub mod ntt;
|
||||
pub mod vec_ops;
|
||||
pub mod polynomials;
|
||||
pub mod vec_ops;
|
||||
|
||||
#[doc(hidden)]
|
||||
pub mod test_utilities;
|
||||
|
||||
@@ -33,7 +33,8 @@ pub struct MSMConfig {
|
||||
pub bitsize: i32,
|
||||
|
||||
batch_size: i32,
|
||||
are_bases_shared: bool, /// MSMs in batch share the bases. If false, expecting #bases==#scalars
|
||||
are_bases_shared: bool,
|
||||
/// MSMs in batch share the bases. If false, expecting #bases==#scalars
|
||||
are_scalars_on_device: bool,
|
||||
pub are_scalars_montgomery_form: bool,
|
||||
are_bases_on_device: bool,
|
||||
@@ -54,7 +55,7 @@ pub const IS_BIG_TRIANGLE: &str = "is_big_triangle";
|
||||
impl Default for MSMConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
stream_handle: std::ptr::null_mut(),
|
||||
stream_handle: std::ptr::null_mut(),
|
||||
precompute_factor: 1,
|
||||
c: 0,
|
||||
bitsize: 0,
|
||||
|
||||
@@ -100,7 +100,7 @@ where
|
||||
<C::ScalarField as FieldImpl>::Config: GenerateRandom<C::ScalarField>,
|
||||
{
|
||||
// let test_sizes = [1000, 1 << 16]; //TODO - uncomment this line after implementing fast msm
|
||||
let test_sizes = [100];
|
||||
let test_sizes = [100];
|
||||
// let batch_sizes = [1, 3, 1 << 4];
|
||||
let batch_sizes = [1, 3]; //TODO - uncomment this line after implementing fast msm
|
||||
let mut stream = IcicleStream::create().unwrap();
|
||||
@@ -184,12 +184,12 @@ where
|
||||
{
|
||||
// let test_sizes = [1 << 10, 10000]; // TODO - uncomment this line after implementing fast msm
|
||||
let test_sizes = [1 << 10]; // TODO - remove this line after implementing fast msm
|
||||
// let test_threshold = 1 << 11; // TODO - uncomment this line after implementing fast msm
|
||||
// let batch_sizes = [1, 3, 1 << 4]; // TODO - uncomment this line after implementing fast msm
|
||||
// let test_threshold = 1 << 11; // TODO - uncomment this line after implementing fast msm
|
||||
// let batch_sizes = [1, 3, 1 << 4]; // TODO - uncomment this line after implementing fast msm
|
||||
let batch_sizes = [1, 3]; // TODO - remove this line after implementing fast msm
|
||||
let rng = &mut thread_rng();
|
||||
for test_size in test_sizes {
|
||||
let test_threshold = test_size>>2; // TODO - remove this line after implementing fast msm
|
||||
let test_threshold = test_size >> 2; // TODO - remove this line after implementing fast msm
|
||||
for batch_size in batch_sizes {
|
||||
let points = generate_random_affine_points_with_zeroes::<C>(test_size * batch_size, 100);
|
||||
let mut scalars = vec![C::ScalarField::zero(); test_size * batch_size];
|
||||
|
||||
@@ -239,7 +239,7 @@ where
|
||||
let coset_generators = [
|
||||
F::Config::generate_random(1)[0],
|
||||
get_root_of_unity::<F>(test_size as u64),
|
||||
F::one(),
|
||||
F::one(),
|
||||
];
|
||||
for coset_gen in coset_generators {
|
||||
let mut scalars = F::Config::generate_random(test_size);
|
||||
|
||||
@@ -140,7 +140,7 @@ macro_rules! impl_univariate_polynomial_api {
|
||||
|
||||
pub fn coeffs_mut_slice(&mut self) -> &mut DeviceSlice<$field> {
|
||||
unsafe {
|
||||
let mut len: u64 = 0;
|
||||
let mut len: u64 = 0;
|
||||
let mut coeffs_mut = get_coeffs_ptr(self.handle, &mut len);
|
||||
let s = slice::from_raw_parts_mut(coeffs_mut, len as usize);
|
||||
DeviceSlice::from_mut_slice(s)
|
||||
@@ -413,9 +413,12 @@ macro_rules! impl_polynomial_tests {
|
||||
$field:ident
|
||||
) => {
|
||||
use super::*;
|
||||
use icicle_core::ntt::{get_root_of_unity, initialize_domain, release_domain, NTTDomain, NTTInitDomainConfig, CUDA_NTT_FAST_TWIDDLES_MODE};
|
||||
use icicle_core::vec_ops::{add_scalars, mul_scalars, sub_scalars, VecOps, VecOpsConfig};
|
||||
use icicle_core::ntt::{
|
||||
get_root_of_unity, initialize_domain, release_domain, NTTDomain, NTTInitDomainConfig,
|
||||
CUDA_NTT_FAST_TWIDDLES_MODE,
|
||||
};
|
||||
use icicle_core::test_utilities;
|
||||
use icicle_core::vec_ops::{add_scalars, mul_scalars, sub_scalars, VecOps, VecOpsConfig};
|
||||
use icicle_runtime::memory::{DeviceVec, HostSlice};
|
||||
use std::sync::Once;
|
||||
|
||||
@@ -497,7 +500,7 @@ macro_rules! impl_polynomial_tests {
|
||||
|
||||
fn randomize_poly(size: usize) -> Poly {
|
||||
let coeffs = randomize_coeffs::<$field>(size);
|
||||
Poly::from_coeffs(HostSlice::from_slice(&coeffs), size)
|
||||
Poly::from_coeffs(HostSlice::from_slice(&coeffs), size)
|
||||
}
|
||||
|
||||
static INIT: Once = Once::new();
|
||||
|
||||
@@ -10,7 +10,7 @@ pub static TEST_REF_DEVICE: Lazy<Mutex<Device>> = Lazy::new(|| Mutex::new(Device
|
||||
|
||||
pub fn test_load_and_init_devices() {
|
||||
INIT.get_or_init(move || {
|
||||
runtime::load_backend(env!("DEFAULT_BACKEND_INSTALL_DIR")).unwrap();
|
||||
runtime::load_backend_from_env_or_default().unwrap();
|
||||
let registered_devices = runtime::get_registered_devices().unwrap();
|
||||
assert!(registered_devices.len() >= 2);
|
||||
// select main and ref devices
|
||||
|
||||
@@ -88,13 +88,13 @@ fn check_vec_ops_args<'a, F>(
|
||||
}
|
||||
|
||||
// check device slices are on active device
|
||||
if a.is_on_device() && !a.is_on_active_device(){
|
||||
if a.is_on_device() && !a.is_on_active_device() {
|
||||
panic!("input a is allocated on an inactive device");
|
||||
}
|
||||
if b.is_on_device() && !b.is_on_active_device(){
|
||||
if b.is_on_device() && !b.is_on_active_device() {
|
||||
panic!("input b is allocated on an inactive device");
|
||||
}
|
||||
if result.is_on_device() && !result.is_on_active_device(){
|
||||
if result.is_on_device() && !result.is_on_active_device() {
|
||||
panic!("output is allocated on an inactive device");
|
||||
}
|
||||
|
||||
@@ -196,7 +196,7 @@ macro_rules! impl_vec_ops_field {
|
||||
) => {
|
||||
mod $field_prefix_ident {
|
||||
|
||||
use crate::vec_ops::{$field, HostOrDeviceSlice};
|
||||
use crate::vec_ops::{$field, HostOrDeviceSlice};
|
||||
use icicle_core::vec_ops::VecOpsConfig;
|
||||
use icicle_runtime::errors::eIcicleError;
|
||||
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
#![allow(unused_imports)]
|
||||
use crate::test_utilities;
|
||||
use crate::traits::GenerateRandom;
|
||||
use crate::vec_ops::{add_scalars, mul_scalars, sub_scalars, bit_reverse, bit_reverse_inplace, transpose_matrix, FieldImpl, VecOps, VecOpsConfig};
|
||||
use crate::vec_ops::{
|
||||
add_scalars, bit_reverse, bit_reverse_inplace, mul_scalars, sub_scalars, transpose_matrix, FieldImpl, VecOps,
|
||||
VecOpsConfig,
|
||||
};
|
||||
use icicle_runtime::device::Device;
|
||||
use icicle_runtime::memory::{DeviceVec, HostSlice};
|
||||
use icicle_runtime::{runtime, stream::IcicleStream};
|
||||
@@ -108,7 +111,7 @@ where
|
||||
<F as FieldImpl>::Config: VecOps<F> + GenerateRandom<F>,
|
||||
{
|
||||
test_utilities::test_set_main_device();
|
||||
|
||||
|
||||
const LOG_SIZE: u32 = 20;
|
||||
const TEST_SIZE: usize = 1 << LOG_SIZE;
|
||||
let input_vec = F::Config::generate_random(TEST_SIZE);
|
||||
|
||||
@@ -87,7 +87,7 @@ fn main() {
|
||||
|
||||
// default backends dir
|
||||
println!(
|
||||
"cargo:rustc-env=DEFAULT_BACKEND_INSTALL_DIR={}/lib/backend",
|
||||
"cargo:rustc-env=ICICLE_BACKEND_INSTALL_DIR={}/lib/backend",
|
||||
icicle_install_dir.display()
|
||||
);
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ pub(crate) mod tests {
|
||||
use crate::curve::{CurveCfg, ScalarField};
|
||||
|
||||
use icicle_core::ecntt::tests::*;
|
||||
use icicle_core::impl_ecntt_tests;
|
||||
use icicle_core::impl_ecntt_tests;
|
||||
|
||||
impl_ecntt_tests!(ScalarField, CurveCfg);
|
||||
}
|
||||
|
||||
@@ -2,5 +2,5 @@ pub mod curve;
|
||||
pub mod ecntt;
|
||||
pub mod msm;
|
||||
pub mod ntt;
|
||||
pub mod polynomials;
|
||||
pub mod vec_ops;
|
||||
pub mod polynomials;
|
||||
@@ -16,6 +16,6 @@ pub(crate) mod tests {
|
||||
use icicle_core::impl_ntt_tests;
|
||||
use icicle_core::ntt::tests::*;
|
||||
use serial_test::{parallel, serial};
|
||||
|
||||
|
||||
impl_ntt_tests!(ScalarField);
|
||||
}
|
||||
|
||||
@@ -53,7 +53,7 @@ fn main() {
|
||||
|
||||
// default backends dir
|
||||
println!(
|
||||
"cargo:rustc-env=DEFAULT_BACKEND_INSTALL_DIR={}/lib/backend",
|
||||
"cargo:rustc-env=ICICLE_BACKEND_INSTALL_DIR={}/lib/backend",
|
||||
icicle_install_dir.display()
|
||||
);
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ pub(crate) mod tests {
|
||||
use crate::curve::{CurveCfg, ScalarField};
|
||||
|
||||
use icicle_core::ecntt::tests::*;
|
||||
use icicle_core::impl_ecntt_tests;
|
||||
use icicle_core::impl_ecntt_tests;
|
||||
|
||||
impl_ecntt_tests!(ScalarField, CurveCfg);
|
||||
}
|
||||
|
||||
@@ -2,5 +2,5 @@ pub mod curve;
|
||||
pub mod ecntt;
|
||||
pub mod msm;
|
||||
pub mod ntt;
|
||||
pub mod vec_ops;
|
||||
pub mod polynomials;
|
||||
pub mod vec_ops;
|
||||
|
||||
@@ -53,7 +53,7 @@ fn main() {
|
||||
|
||||
// default backends dir
|
||||
println!(
|
||||
"cargo:rustc-env=DEFAULT_BACKEND_INSTALL_DIR={}/lib/backend",
|
||||
"cargo:rustc-env=ICICLE_BACKEND_INSTALL_DIR={}/lib/backend",
|
||||
icicle_install_dir.display()
|
||||
);
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ pub(crate) mod tests {
|
||||
use crate::curve::{CurveCfg, ScalarField};
|
||||
|
||||
use icicle_core::ecntt::tests::*;
|
||||
use icicle_core::impl_ecntt_tests;
|
||||
use icicle_core::impl_ecntt_tests;
|
||||
|
||||
impl_ecntt_tests!(ScalarField, CurveCfg);
|
||||
}
|
||||
|
||||
@@ -2,5 +2,5 @@ pub mod curve;
|
||||
pub mod ecntt;
|
||||
pub mod msm;
|
||||
pub mod ntt;
|
||||
pub mod vec_ops;
|
||||
pub mod polynomials;
|
||||
pub mod vec_ops;
|
||||
|
||||
@@ -46,7 +46,7 @@ fn main() {
|
||||
|
||||
// default backends dir
|
||||
println!(
|
||||
"cargo:rustc-env=DEFAULT_BACKEND_INSTALL_DIR={}/lib/backend",
|
||||
"cargo:rustc-env=ICICLE_BACKEND_INSTALL_DIR={}/lib/backend",
|
||||
icicle_install_dir.display()
|
||||
);
|
||||
}
|
||||
|
||||
@@ -46,7 +46,7 @@ fn main() {
|
||||
|
||||
// default backends dir
|
||||
println!(
|
||||
"cargo:rustc-env=DEFAULT_BACKEND_INSTALL_DIR={}/lib/backend",
|
||||
"cargo:rustc-env=ICICLE_BACKEND_INSTALL_DIR={}/lib/backend",
|
||||
icicle_install_dir.display()
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
pub mod field;
|
||||
pub mod ntt;
|
||||
pub mod vec_ops;
|
||||
pub mod polynomials;
|
||||
pub mod vec_ops;
|
||||
|
||||
@@ -23,12 +23,12 @@ pub(crate) mod tests {
|
||||
impl_ntt_tests!(ScalarField);
|
||||
|
||||
// Tests against risc0 and plonky3
|
||||
use super::{ExtensionField};
|
||||
use super::ExtensionField;
|
||||
use icicle_core::{
|
||||
ntt::{initialize_domain, ntt_inplace, release_domain, NTTConfig, NTTInitDomainConfig, NTTDir},
|
||||
traits::{FieldImpl, GenerateRandom}
|
||||
ntt::{initialize_domain, ntt_inplace, release_domain, NTTConfig, NTTDir, NTTInitDomainConfig},
|
||||
traits::{FieldImpl, GenerateRandom},
|
||||
};
|
||||
use icicle_runtime::{memory::HostSlice};
|
||||
use icicle_runtime::memory::HostSlice;
|
||||
use risc0_core::field::{
|
||||
baby_bear::{Elem, ExtElem},
|
||||
Elem as FieldElem, RootsOfUnity,
|
||||
|
||||
@@ -45,7 +45,7 @@ fn main() {
|
||||
|
||||
// default backends dir
|
||||
println!(
|
||||
"cargo:rustc-env=DEFAULT_BACKEND_INSTALL_DIR={}/lib/backend",
|
||||
"cargo:rustc-env=ICICLE_BACKEND_INSTALL_DIR={}/lib/backend",
|
||||
icicle_install_dir.display()
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
pub mod field;
|
||||
pub mod ntt;
|
||||
pub mod vec_ops;
|
||||
pub mod polynomials;
|
||||
pub mod vec_ops;
|
||||
|
||||
@@ -15,9 +15,8 @@ pub(crate) mod tests {
|
||||
|
||||
impl_ntt_tests!(ScalarField);
|
||||
|
||||
|
||||
use icicle_core::{
|
||||
ntt::{initialize_domain, ntt_inplace, release_domain, NTTConfig, NTTInitDomainConfig, NTTDir},
|
||||
ntt::{initialize_domain, ntt_inplace, release_domain, NTTConfig, NTTDir, NTTInitDomainConfig},
|
||||
traits::{FieldImpl, GenerateRandom},
|
||||
};
|
||||
use icicle_runtime::memory::HostSlice;
|
||||
@@ -39,9 +38,13 @@ pub(crate) mod tests {
|
||||
|
||||
release_domain::<ScalarField>().unwrap(); // release domain from previous tests, if exists
|
||||
|
||||
let log_sizes = [15, 20];
|
||||
let log_sizes = [15, 20];
|
||||
let lw_root_of_unity = Stark252PrimeField::get_primitive_root_of_unity(log_sizes[log_sizes.len() - 1]).unwrap();
|
||||
initialize_domain(ScalarField::from_bytes_le(&lw_root_of_unity.to_bytes_le()), &NTTInitDomainConfig::default()).unwrap();
|
||||
initialize_domain(
|
||||
ScalarField::from_bytes_le(&lw_root_of_unity.to_bytes_le()),
|
||||
&NTTInitDomainConfig::default(),
|
||||
)
|
||||
.unwrap();
|
||||
for log_size in log_sizes {
|
||||
let ntt_size = 1 << log_size;
|
||||
|
||||
|
||||
@@ -43,7 +43,7 @@ fn main() {
|
||||
|
||||
// default backends dir
|
||||
println!(
|
||||
"cargo:rustc-env=DEFAULT_BACKEND_INSTALL_DIR={}/lib/backend",
|
||||
"cargo:rustc-env=ICICLE_BACKEND_INSTALL_DIR={}/lib/backend",
|
||||
icicle_install_dir.display()
|
||||
);
|
||||
}
|
||||
|
||||
@@ -76,4 +76,3 @@ impl fmt::Debug for Device {
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -55,9 +55,11 @@ impl<T> HostOrDeviceSlice<T> for DeviceSlice<T> {
|
||||
true
|
||||
}
|
||||
|
||||
|
||||
fn is_on_active_device(&self) -> bool {
|
||||
runtime::is_active_device_memory(self.0.as_ptr() as *const c_void)
|
||||
runtime::is_active_device_memory(
|
||||
self.0
|
||||
.as_ptr() as *const c_void,
|
||||
)
|
||||
}
|
||||
|
||||
unsafe fn as_ptr(&self) -> *const T {
|
||||
@@ -125,11 +127,11 @@ impl<T> DeviceSlice<T> {
|
||||
self.len() == val.len(),
|
||||
"In copy from host, destination and source slices have different lengths"
|
||||
);
|
||||
|
||||
|
||||
if self.is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
if !self.is_on_active_device(){
|
||||
if !self.is_on_active_device() {
|
||||
panic!("not allocated on an inactive device");
|
||||
}
|
||||
|
||||
@@ -148,7 +150,7 @@ impl<T> DeviceSlice<T> {
|
||||
if self.is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
if !self.is_on_active_device(){
|
||||
if !self.is_on_active_device() {
|
||||
panic!("not allocated on an inactive device");
|
||||
}
|
||||
|
||||
@@ -166,7 +168,7 @@ impl<T> DeviceSlice<T> {
|
||||
if self.is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
if !self.is_on_active_device(){
|
||||
if !self.is_on_active_device() {
|
||||
panic!("not allocated on an inactive device");
|
||||
}
|
||||
|
||||
@@ -190,10 +192,10 @@ impl<T> DeviceSlice<T> {
|
||||
if self.is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
if !self.is_on_active_device(){
|
||||
if !self.is_on_active_device() {
|
||||
panic!("not allocated on an inactive device");
|
||||
}
|
||||
|
||||
|
||||
let size = size_of::<T>() * self.len();
|
||||
unsafe {
|
||||
runtime::icicle_copy_to_host_async(
|
||||
|
||||
@@ -9,6 +9,7 @@ pub type IcicleStreamHandle = *mut c_void;
|
||||
|
||||
extern "C" {
|
||||
fn icicle_load_backend(path: *const c_char, is_recursive: bool) -> eIcicleError;
|
||||
fn icicle_load_backend_from_env_or_default() -> eIcicleError;
|
||||
fn icicle_set_device(device: &Device) -> eIcicleError;
|
||||
fn icicle_get_active_device(device: &mut Device) -> eIcicleError;
|
||||
fn icicle_is_host_memory(ptr: *const c_void) -> eIcicleError;
|
||||
@@ -42,6 +43,10 @@ extern "C" {
|
||||
fn icicle_get_registered_devices(output: *mut c_char, output_size: usize) -> eIcicleError;
|
||||
}
|
||||
|
||||
pub fn load_backend_from_env_or_default() -> Result<(), eIcicleError> {
|
||||
unsafe { icicle_load_backend_from_env_or_default().wrap() }
|
||||
}
|
||||
|
||||
pub fn load_backend(path: &str) -> Result<(), eIcicleError> {
|
||||
let c_path = CString::new(path).unwrap();
|
||||
unsafe { icicle_load_backend(c_path.as_ptr(), true).wrap() }
|
||||
|
||||
@@ -33,8 +33,7 @@ mod tests {
|
||||
|
||||
fn initialize() {
|
||||
INIT.call_once(|| {
|
||||
// load backends to process
|
||||
load_backend(&env!("DEFAULT_BACKEND_INSTALL_DIR")).unwrap();
|
||||
load_backend_from_env_or_default();
|
||||
let _ = runtime::get_registered_devices().unwrap();
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user