parent
1ed8539e73
commit
bd58d5fd1a
@ -1,4 +1,6 @@
|
||||
/target
|
||||
.idea
|
||||
primes.txt
|
||||
timings.csv
|
||||
timings.csv
|
||||
*.csv
|
||||
*.~lock*
|
@ -0,0 +1,55 @@
|
||||
/*
|
||||
* opencl demos with rust
|
||||
* Copyright (C) 2020 trivernis
|
||||
* See LICENSE for more information
|
||||
*/
|
||||
|
||||
use crate::kernel_controller::KernelController;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
pub struct BenchStatistics {
|
||||
pub calc_count: u32,
|
||||
pub num_tasks: usize,
|
||||
pub write_duration: Duration,
|
||||
pub calc_duration: Duration,
|
||||
pub read_duration: Duration,
|
||||
}
|
||||
|
||||
impl KernelController {
|
||||
/// Benches an integer
|
||||
pub fn bench_int(&self, calc_count: u32, num_tasks: usize) -> ocl::Result<BenchStatistics> {
|
||||
let write_start = Instant::now();
|
||||
let input_buffer = self
|
||||
.pro_que
|
||||
.buffer_builder()
|
||||
.len(num_tasks)
|
||||
.fill_val(0u32)
|
||||
.build()?;
|
||||
let write_duration = write_start.elapsed();
|
||||
|
||||
let kernel = self
|
||||
.pro_que
|
||||
.kernel_builder("bench_int")
|
||||
.arg(calc_count)
|
||||
.arg(&input_buffer)
|
||||
.build()?;
|
||||
let calc_start = Instant::now();
|
||||
unsafe {
|
||||
kernel.enq()?;
|
||||
}
|
||||
self.pro_que.finish()?;
|
||||
let calc_duration = calc_start.elapsed();
|
||||
let mut output = vec![0u32; num_tasks];
|
||||
let read_start = Instant::now();
|
||||
input_buffer.read(&mut output).enq()?;
|
||||
let read_duration = read_start.elapsed();
|
||||
|
||||
Ok(BenchStatistics {
|
||||
num_tasks,
|
||||
calc_count,
|
||||
read_duration,
|
||||
calc_duration,
|
||||
write_duration,
|
||||
})
|
||||
}
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
/*
|
||||
* opencl demos with rust
|
||||
* Copyright (C) 2020 trivernis
|
||||
* See LICENSE for more information
|
||||
*/
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::io::{Result, Write};
|
||||
|
||||
pub struct CSVWriter<W: Write> {
|
||||
inner: W,
|
||||
columns: Vec<String>,
|
||||
}
|
||||
|
||||
impl<W> CSVWriter<W>
|
||||
where
|
||||
W: Write,
|
||||
{
|
||||
/// Creates a new CSVWriter with a defined list of columns
|
||||
pub fn new(writer: W, columns: &[&str]) -> Self {
|
||||
Self {
|
||||
inner: writer,
|
||||
columns: columns.iter().map(|column| column.to_string()).collect(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Adds a new row of values to the file
|
||||
pub fn add_row(&mut self, items: Vec<String>) -> Result<()> {
|
||||
self.inner.write_all(
|
||||
items
|
||||
.iter()
|
||||
.fold("".to_string(), |a, b| format!("{},{}", a, b))
|
||||
.as_bytes(),
|
||||
)?;
|
||||
self.inner.write_all("\n".as_bytes())
|
||||
}
|
||||
|
||||
/// Adds a new row of values stored in a map to the file
|
||||
#[allow(dead_code)]
|
||||
pub fn add_row_map(&mut self, item_map: &HashMap<String, String>) -> Result<()> {
|
||||
let mut items = Vec::new();
|
||||
for key in &self.columns {
|
||||
items.push(item_map.get(key).cloned().unwrap_or("".to_string()));
|
||||
}
|
||||
|
||||
self.add_row(items)
|
||||
}
|
||||
|
||||
pub fn flush(&mut self) -> Result<()> {
|
||||
self.inner.flush()
|
||||
}
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
/*
|
||||
* opencl demos with rust
|
||||
* Copyright (C) 2020 trivernis
|
||||
* See LICENSE for more information
|
||||
*/
|
||||
use crate::output::csv::CSVWriter;
|
||||
use std::fmt::Display;
|
||||
use std::fs::File;
|
||||
use std::io::{BufWriter, Write};
|
||||
use std::sync::mpsc::{channel, Sender};
|
||||
use std::thread::{self, JoinHandle};
|
||||
|
||||
pub mod csv;
|
||||
|
||||
pub fn create_prime_write_thread(
|
||||
mut writer: BufWriter<File>,
|
||||
) -> (Sender<Vec<u64>>, JoinHandle<()>) {
|
||||
let (tx, rx) = channel();
|
||||
let handle = thread::spawn(move || {
|
||||
for primes in rx {
|
||||
for prime in primes {
|
||||
writer.write_all(format!("{}\n", prime).as_bytes()).unwrap();
|
||||
}
|
||||
writer.flush().unwrap();
|
||||
}
|
||||
});
|
||||
|
||||
(tx, handle)
|
||||
}
|
||||
|
||||
pub fn create_csv_write_thread(
|
||||
mut writer: CSVWriter<BufWriter<File>>,
|
||||
) -> (Sender<Vec<String>>, JoinHandle<()>) {
|
||||
let (tx, rx) = channel();
|
||||
let handle = thread::spawn(move || {
|
||||
for row in rx {
|
||||
writer.add_row(row).unwrap();
|
||||
}
|
||||
writer.flush().unwrap();
|
||||
});
|
||||
|
||||
(tx, handle)
|
||||
}
|
Loading…
Reference in New Issue