Skip to content

Commit

Permalink
Merge pull request iden3#107 from iden3/adding-parallel-components
Browse files Browse the repository at this point in the history
adding definition of parallel at component level
  • Loading branch information
clararod9 authored Aug 26, 2022
2 parents 9fb57b6 + 6bad344 commit 5703a80
Show file tree
Hide file tree
Showing 58 changed files with 835 additions and 291 deletions.
16 changes: 8 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion circom/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "circom"
version = "2.0.7"
version = "2.0.8"
authors = ["Costa Group UCM","iden3"]
edition = "2018"

Expand Down
2 changes: 1 addition & 1 deletion code_producers/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "code_producers"
version = "2.0.7"
version = "2.0.8"
authors = ["Costa Group UCM","iden3"]
edition = "2018"

Expand Down
85 changes: 72 additions & 13 deletions code_producers/src/c_elements/c_code_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,11 @@ pub fn function_table() -> CInstruction {
format!("{}", FUNCTION_TABLE)
}

pub const FUNCTION_TABLE_PARALLEL: &str = "_functionTableParallel";
pub fn function_table_parallel() -> CInstruction {
format!("{}", FUNCTION_TABLE_PARALLEL)
}

pub const SIGNAL_VALUES: &str = "signalValues";
pub fn declare_signal_values() -> CInstruction {
format!("FrElement* {} = {}->{}", SIGNAL_VALUES, CIRCOM_CALC_WIT, SIGNAL_VALUES)
Expand Down Expand Up @@ -282,6 +287,17 @@ pub fn my_subcomponents() -> CInstruction {
format!("{}", MY_SUBCOMPONENTS)
}

pub const MY_SUBCOMPONENTS_PARALLEL: &str = "mySubcomponentsParallel";
pub fn declare_my_subcomponents_parallel() -> CInstruction {
format!(
"bool* {} = {}->componentMemory[{}].subcomponentsParallel",
MY_SUBCOMPONENTS_PARALLEL, CIRCOM_CALC_WIT, CTX_INDEX
)
}
pub fn my_subcomponents_parallel() -> CInstruction {
format!("{}", MY_SUBCOMPONENTS_PARALLEL)
}

pub const CIRCUIT_CONSTANTS: &str = "circuitConstants";
pub fn declare_circuit_constants() -> CInstruction {
format!("FrElement* {} = {}->{}", CIRCUIT_CONSTANTS, CIRCOM_CALC_WIT, CIRCUIT_CONSTANTS)
Expand Down Expand Up @@ -347,6 +363,15 @@ pub fn set_list(elems: Vec<usize>) -> String {
set_string
}

pub fn set_list_bool(elems: Vec<bool>) -> String {
let mut set_string = "{".to_string();
for elem in elems {
set_string = format!("{}{},", set_string, elem);
}
set_string.pop();
set_string.push('}');
set_string
}

pub fn add_return() -> String {
"return;".to_string()
Expand Down Expand Up @@ -388,12 +413,11 @@ pub fn merge_code(instructions: Vec<String>) -> String {
code
}

pub fn collect_template_headers(instances: &TemplateList) -> Vec<String> {
pub fn collect_template_headers(instances: &TemplateListParallel) -> Vec<String> {
let mut template_headers = vec![];
for instance in instances {
let params_run = vec![declare_ctx_index(), declare_circom_calc_wit()];
let params_run = argument_list(params_run);
let run_header = format!("void {}_run({});", instance, params_run);
let params_create = vec![
declare_signal_offset(),
declare_component_offset(),
Expand All @@ -402,9 +426,18 @@ pub fn collect_template_headers(instances: &TemplateList) -> Vec<String> {
declare_component_father(),
];
let params_create = argument_list(params_create);
let create_header = format!("void {}_create({});", instance, params_create);
template_headers.push(create_header);
template_headers.push(run_header);
if instance.is_parallel{
let run_header = format!("void {}_run_parallel({});", instance.name, params_run);
let create_header = format!("void {}_create_parallel({});", instance.name, params_create);
template_headers.push(create_header);
template_headers.push(run_header);
}
if instance.is_not_parallel{
let run_header = format!("void {}_run({});", instance.name, params_run);
let create_header = format!("void {}_create({});", instance.name, params_create);
template_headers.push(create_header);
template_headers.push(run_header);
}
}
template_headers
}
Expand Down Expand Up @@ -645,16 +678,34 @@ pub fn generate_dat_file(dat_file: &mut dyn Write, producer: &CProducer) -> std:
*/
Ok(())
}

pub fn generate_function_list(_producer: &CProducer, list: &TemplateList) -> String {
let mut func_list = "".to_string();
pub fn generate_function_list(_producer: &CProducer, list: &TemplateListParallel) -> (String, String) {
let mut func_list= "".to_string();
let mut func_list_parallel= "".to_string();
if list.len() > 0 {
func_list.push_str(&format!("\n{}_run", list[0]));
for i in 1..list.len() {
func_list.push_str(&format!(",\n{}_run", list[i]));
if list[0].is_parallel{
func_list_parallel.push_str(&format!("\n{}_run_parallel",list[0].name));
}else{
func_list_parallel.push_str(&format!("\nNULL"));
}
if list[0].is_not_parallel{
func_list.push_str(&format!("\n{}_run",list[0].name));
}else{
func_list.push_str(&format!("\nNULL"));
}
for i in 1..list.len() {
if list[i].is_parallel{
func_list_parallel.push_str(&format!(",\n{}_run_parallel",list[i].name));
}else{
func_list_parallel.push_str(&format!(",\nNULL"));
}
if list[i].is_not_parallel{
func_list.push_str(&format!(",\n{}_run",list[i].name));
}else{
func_list.push_str(&format!(",\nNULL"));
}
}
}
func_list
(func_list, func_list_parallel)
}

pub fn generate_message_list_def(_producer: &CProducer, message_list: &MessageList) -> Vec<String> {
Expand Down Expand Up @@ -852,10 +903,18 @@ pub fn generate_c_file(name: String, producer: &CProducer) -> std::io::Result<()
let mut run_defs = collect_template_headers(producer.get_template_instance_list());
code.append(&mut run_defs);

let (func_list_no_parallel, func_list_parallel) = generate_function_list(producer, producer.get_template_instance_list());

code.push(format!(
"Circom_TemplateFunction _functionTable[{}] = {{ {} }};",
producer.get_number_of_template_instances(),
generate_function_list(producer, producer.get_template_instance_list())
func_list_no_parallel,
));

code.push(format!(
"Circom_TemplateFunction _functionTableParallel[{}] = {{ {} }};",
producer.get_number_of_template_instances(),
func_list_parallel,
));

code.push(format!("uint get_size_of_input_hashmap() {{return {};}}\n", len));
Expand Down
1 change: 1 addition & 0 deletions code_producers/src/c_elements/common/circom.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ struct Circom_Component {
std::string componentName;
u64 idFather;
u32* subcomponents;
bool* subcomponentsParallel;
bool *outputIsSet; //one for each output
std::mutex *mutexes; //one for each output
std::condition_variable *cvs;
Expand Down
11 changes: 8 additions & 3 deletions code_producers/src/c_elements/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub use crate::components::*;
pub type CInstruction = String;
pub struct CProducer {
pub main_header: String,
pub main_is_parallel: bool,
//pub fr_memory_size: usize, // depending of the prime; missing in build.rs
pub has_parallelism: bool,
pub number_of_main_outputs: usize,
Expand All @@ -21,7 +22,7 @@ pub struct CProducer {
pub main_input_list: InputList,
pub witness_to_signal_list: SignalList,
pub io_map: TemplateInstanceIOMap,
pub template_instance_list: TemplateList,
pub template_instance_list: TemplateListParallel,
pub message_list: MessageList,
pub field_tracking: Vec<String>,
pub major_version: usize,
Expand Down Expand Up @@ -59,6 +60,7 @@ impl Default for CProducer {
);
CProducer {
main_header: "Main_0".to_string(),
main_is_parallel: false,
has_parallelism: false,
main_signal_offset: 1,
prime: "21888242871839275222246405745257275088548364400416034343698204186575808495617"
Expand Down Expand Up @@ -88,7 +90,7 @@ impl Default for CProducer {
size_32_bit: 8,
size_32_shift: 5,
io_map: my_map, //TemplateInstanceIOMap::new(),
template_instance_list: [].to_vec(),
template_instance_list: Vec::new(),
major_version: 0,
minor_version: 0,
patch_version: 0,
Expand All @@ -111,6 +113,9 @@ impl CProducer {
pub fn get_main_header(&self) -> &str {
&self.main_header
}
pub fn get_main_is_parallel(&self) -> bool {
self.main_is_parallel
}
pub fn get_has_parallelism(&self) -> bool {
self.has_parallelism
}
Expand Down Expand Up @@ -144,7 +149,7 @@ impl CProducer {
pub fn get_io_map(&self) -> &TemplateInstanceIOMap {
&self.io_map
}
pub fn get_template_instance_list(&self) -> &TemplateList {
pub fn get_template_instance_list(&self) -> &TemplateListParallel {
&self.template_instance_list
}
pub fn get_number_of_template_instances(&self) -> usize {
Expand Down
6 changes: 6 additions & 0 deletions code_producers/src/components/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ pub struct IODef {
// It is an array that contains (name, start position, size)
pub type InputList = Vec<(String, usize, usize)>;
pub type TemplateList = Vec<String>;
pub struct InfoParallel{
pub name: String,
pub is_parallel: bool,
pub is_not_parallel: bool,
}
pub type TemplateListParallel = Vec<InfoParallel>;
pub type SignalList = Vec<usize>;
pub type InputOutputList = Vec<IODef>;
pub type TemplateInstanceIOMap = BTreeMap<usize, InputOutputList>;
Expand Down
2 changes: 1 addition & 1 deletion compiler/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "compiler"
version = "2.0.7"
version = "2.0.8"
authors = ["Costa Group UCM","iden3"]
edition = "2018"

Expand Down
Loading

0 comments on commit 5703a80

Please sign in to comment.