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

add to cpu vecops the function execute_program #689

Merged
merged 4 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
35 changes: 33 additions & 2 deletions icicle/backend/cpu/src/field/cpu_vec_ops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
#include <sys/types.h>
#include <vector>

#include "icicle/program/program.h"
#include "cpu_program_executor.h"

using namespace field_config;
using namespace icicle;

Expand All @@ -32,7 +35,7 @@ enum VecOperation {
REPLACE_ELEMENTS,
OUT_OF_PLACE_MATRIX_TRANSPOSE,

NOF_OPERATIONS
NOF_VECOPS_OPERATIONS
};

/**
Expand Down Expand Up @@ -329,7 +332,7 @@ class VectorOpTask : public TaskBase

// An array of available function pointers arranged according to the VecOperation enum
using FunctionPtr = void (VectorOpTask::*)();
static constexpr std::array<FunctionPtr, static_cast<int>(NOF_OPERATIONS)> functionPtrs = {
static constexpr std::array<FunctionPtr, static_cast<int>(NOF_VECOPS_OPERATIONS)> functionPtrs = {
mickeyasa marked this conversation as resolved.
Show resolved Hide resolved
&VectorOpTask::vector_add, // VECTOR_ADD,
&VectorOpTask::vector_sub, // VECTOR_SUB,
&VectorOpTask::vector_mul, // VECTOR_MUL,
Expand Down Expand Up @@ -838,6 +841,34 @@ eIcicleError cpu_highest_non_zero_idx(

REGISTER_HIGHEST_NON_ZERO_IDX_BACKEND("CPU", cpu_highest_non_zero_idx<scalar_t>);

/*********************************** Execute program ***********************************/
template <typename T>
eIcicleError cpu_execute_program(std::vector<T*>& data, Program<T>& program, uint64_t size, const VecOpsConfig& config)
{
if (data.size() != program.m_nof_parameters) {
ICICLE_LOG_ERROR << "Program has " << program.m_nof_parameters << " while data has " << data.size()
<< " parameters";
return eIcicleError::INVALID_ARGUMENT;
}
const uint64_t total_nof_operations = size * config.batch_size;
CpuProgramExecutor prog_executor(program);
// init prog_executor to point to data vectors
for (int param_idx = 0; param_idx < program.m_nof_parameters; ++param_idx) {
prog_executor.m_variable_ptrs[param_idx] = data[param_idx];
}

// run over all elements in the arrays and execute the program
for (uint64_t i = 0; i < total_nof_operations; i++) {
prog_executor.execute();
for (auto& var_ptr : prog_executor.m_variable_ptrs) {
var_ptr++;
}
}
return eIcicleError::SUCCESS;
}

// REGISTER_EXECUTE_PROGRAM_BACKEND("CPU", cpu_execute_program<scalar_t>);
mickeyasa marked this conversation as resolved.
Show resolved Hide resolved

/*********************************** Polynomial evaluation ***********************************/

template <typename T>
Expand Down
17 changes: 16 additions & 1 deletion icicle/include/icicle/vec_ops.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "icicle/fields/field.h"
#include "icicle/utils/utils.h"
#include "icicle/config_extension.h"

#include "icicle/program/program.h"
namespace icicle {

/*************************** Frontend APIs ***************************/
Expand Down Expand Up @@ -358,6 +358,21 @@ namespace icicle {
template <typename T>
eIcicleError highest_non_zero_idx(const T* vec_in, uint64_t size, const VecOpsConfig& config, int64_t* out_idx);

/**
* @brief Run a program (lambda function) on a set of vector element wise.
* For all i in range [0..size-1], execute the program on element i of each vector
*
* @tparam T Type of the elements in the vectors.
* @param data_vec vector of arrays. Those arrays contains the parameters for the program.
* @param program a class that describes the functionality to run on each set of entries at the vectors.
* @param size Size of each arrays. The program will be executed size times
* @param config Configuration for the operation.
* @return eIcicleError Error code indicating success or failure.
*/

template <typename T>
eIcicleError execute_program(std::vector<T*>& data, Program<T>& program, uint64_t size, const VecOpsConfig& config);

/**
* @brief Evaluates a polynomial at given domain points.
*
Expand Down
Loading