Skip to content

Commit b215673

Browse files
madhav-madhusoodananMadhav Madhusoodanan
authored andcommitted
fixed too many files open issue
1 parent 0b6d424 commit b215673

File tree

4 files changed

+36
-24
lines changed

4 files changed

+36
-24
lines changed

crates/intrinsic-test/src/arm/functions.rs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ use super::argument::Argument;
22
use super::config::{AARCH_CONFIGURATIONS, POLY128_OSTREAM_DEF, build_notices};
33
use super::format::Indentation;
44
use super::intrinsic::Intrinsic;
5-
use crate::common::gen_c::{compile_c, create_c_files, generate_c_program};
6-
use crate::common::gen_rust::{compile_rust, create_rust_files, generate_rust_program};
5+
use crate::common::gen_c::{compile_c, create_c_filenames, generate_c_program};
6+
use crate::common::gen_rust::{compile_rust, create_rust_filenames, generate_rust_program};
7+
use crate::common::write_file;
78
use itertools::Itertools;
89
use rayon::prelude::*;
9-
use std::io::Write;
10+
use std::collections::BTreeMap;
1011

1112
// The number of times each intrinsic will be called.
1213
const PASSES: u32 = 20;
@@ -149,13 +150,14 @@ fn generate_rust_program_arm(intrinsic: &Intrinsic, target: &str) -> String {
149150
}
150151

151152
fn compile_c_arm(
152-
intrinsics_name_list: Vec<String>,
153+
intrinsics_name_list: &Vec<String>,
154+
filename_mapping: BTreeMap<&String, String>,
153155
compiler: &str,
154156
target: &str,
155157
cxx_toolchain_dir: Option<&str>,
156158
) -> bool {
157-
let compiler_commands = intrinsics_name_list.iter().map(|intrinsic_name|{
158-
let c_filename = format!(r#"c_programs/{intrinsic_name}.cpp"#);
159+
let compiler_commands = intrinsics_name_list.iter().map(|intrinsic_name| {
160+
let c_filename = filename_mapping.get(intrinsic_name).unwrap();
159161
let flags = std::env::var("CPPFLAGS").unwrap_or("".into());
160162
let arch_flags = if target.contains("v7") {
161163
"-march=armv8.6-a+crypto+crc+dotprod+fp16"
@@ -223,24 +225,29 @@ pub fn build_c(
223225
target: &str,
224226
cxx_toolchain_dir: Option<&str>,
225227
) -> bool {
226-
let _ = std::fs::create_dir("c_programs");
227228
let intrinsics_name_list = intrinsics
228229
.par_iter()
229230
.map(|i| i.name.clone())
230231
.collect::<Vec<_>>();
231-
let file_mapping = create_c_files(&intrinsics_name_list);
232+
let filename_mapping = create_c_filenames(&intrinsics_name_list);
232233

233234
intrinsics.par_iter().for_each(|i| {
234235
let c_code = generate_c_program_arm(&["arm_neon.h", "arm_acle.h", "arm_fp16.h"], i, target);
235-
match file_mapping.get(&i.name) {
236-
Some(mut file) => file.write_all(c_code.into_bytes().as_slice()).unwrap(),
236+
match filename_mapping.get(&i.name) {
237+
Some(filename) => write_file(filename, c_code),
237238
None => {}
238239
};
239240
});
240241

241242
match compiler {
242243
None => true,
243-
Some(compiler) => compile_c_arm(intrinsics_name_list, compiler, target, cxx_toolchain_dir),
244+
Some(compiler) => compile_c_arm(
245+
&intrinsics_name_list,
246+
filename_mapping,
247+
compiler,
248+
target,
249+
cxx_toolchain_dir,
250+
),
244251
}
245252
}
246253

@@ -254,12 +261,12 @@ pub fn build_rust(
254261
.par_iter()
255262
.map(|i| i.name.clone())
256263
.collect::<Vec<_>>();
257-
let file_mapping = create_rust_files(&intrinsics_name_list);
264+
let filename_mapping = create_rust_filenames(&intrinsics_name_list);
258265

259266
intrinsics.par_iter().for_each(|i| {
260-
let c_code = generate_rust_program_arm(i, target);
261-
match file_mapping.get(&i.name) {
262-
Some(mut file) => file.write_all(c_code.into_bytes().as_slice()).unwrap(),
267+
let rust_code = generate_rust_program_arm(i, target);
268+
match filename_mapping.get(&i.name) {
269+
Some(filename) => write_file(filename, rust_code),
263270
None => {}
264271
}
265272
});

crates/intrinsic-test/src/common/gen_c.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use itertools::Itertools;
22
use rayon::prelude::*;
33
use std::collections::BTreeMap;
4-
use std::fs::File;
54
use std::process::Command;
65

76
pub fn generate_c_program(
@@ -79,14 +78,14 @@ pub fn compile_c(compiler_commands: &[String]) -> bool {
7978
.is_none()
8079
}
8180

82-
pub fn create_c_files(identifiers: &Vec<String>) -> BTreeMap<&String, File> {
81+
pub fn create_c_filenames(identifiers: &Vec<String>) -> BTreeMap<&String, String> {
82+
let _ = std::fs::create_dir("c_programs");
8383
identifiers
8484
.par_iter()
8585
.map(|identifier| {
8686
let c_filename = format!(r#"c_programs/{identifier}.cpp"#);
87-
let file = File::create(&c_filename).unwrap();
8887

89-
(identifier, file)
88+
(identifier, c_filename)
9089
})
91-
.collect::<BTreeMap<&String, File>>()
90+
.collect::<BTreeMap<&String, String>>()
9291
}

crates/intrinsic-test/src/common/gen_rust.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,16 +115,15 @@ path = "{binary}/main.rs""#,
115115
}
116116
}
117117

118-
pub fn create_rust_files(identifiers: &Vec<String>) -> BTreeMap<&String, File> {
118+
pub fn create_rust_filenames(identifiers: &Vec<String>) -> BTreeMap<&String, String> {
119119
identifiers
120120
.par_iter()
121121
.map(|identifier| {
122122
let rust_dir = format!(r#"rust_programs/{}"#, identifier);
123123
let _ = std::fs::create_dir_all(&rust_dir);
124124
let rust_filename = format!(r#"{rust_dir}/main.rs"#);
125-
let file = File::create(&rust_filename).unwrap();
126125

127-
(identifier, file)
126+
(identifier, rust_filename)
128127
})
129-
.collect::<BTreeMap<&String, File>>()
128+
.collect::<BTreeMap<&String, String>>()
130129
}

crates/intrinsic-test/src/common/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
use crate::common::types::ProcessedCli;
2+
use std::fs::File;
3+
use std::io::Write;
24

35
pub mod compare;
46
pub mod gen_c;
@@ -14,3 +16,8 @@ pub trait SupportedArchitectureTest {
1416
fn build_rust_file(&self) -> bool;
1517
fn compare_outputs(&self) -> bool;
1618
}
19+
20+
pub fn write_file(filename: &String, code: String) {
21+
let mut file = File::create(&filename).unwrap();
22+
file.write_all(code.into_bytes().as_slice()).unwrap();
23+
}

0 commit comments

Comments
 (0)