>>83821770

fn main() {

use std::sync::{Arc, Mutex};

let fidx = |x: i32, y: i32| (x + y * 64) as usize;

let in_bounds = |x: i32, y: i32| (0..64).contains(&x) && (0..64).contains(&y);

let nthreads = 4;

let width = 64 / nthreads;

let mut x = vec![0; 64 * 64];

let out = Arc::new(Mutex::new(vec![0; 64 * 64]));

let mut vt = vec![];

let kernel = [[0, 1, 0], [1, 0, 1], [0, 1, 0]];

x[fidx(64 / 2, 64 / 2)] = 1;

x[fidx(64 / 2 - 1, 64 / 2)] = 1;

x[fidx(64 / 2 + 1, 64 / 2)] = 1;

x[fidx(64 / 2, 64 / 2 - 1)] = 1;

x[fidx(64 / 2, 64 / 2 + 1)] = 1;

let shared_x = Arc::new(x);

for l in 0..nthreads {

let out = Arc::clone(&out);

let x = Arc::clone(&shared_x);

vt.push(std::thread::spawn(move || {

for k in 0..width {

for j in 1..64 {

let i = l * width + k;

let index = fidx(i, j);

for ky in -1..=1 {

for kx in -1..=1 {

if in_bounds(i + kx, j + ky) {

let mut out = out.lock().unwrap();

out[index] += kernel[(ky + 1) as usize][(kx + 1) as usize]

* x[fidx(i + kx, j + ky)];

}

}

}

}

}

}));

}

for t in vt {

t.join().unwrap();

}

let out = out.lock().unwrap();

for i in 0..64 {

for l in 0..64 {

print!("{} ", out[l + i * 64]);

}

println!();

}