()
| 1090 | |
| 1091 | #[test] |
| 1092 | fn accum_using_channel() { |
| 1093 | // ANCHOR: accum_using_channel |
| 1094 | let ops: Vec<_> = vec![Op::Add, Op::Sub, Op::Div, Op::Mul, Op::Add, Op::Div]; |
| 1095 | let ops_len: usize = ops.len(); |
| 1096 | |
| 1097 | // Set active GPU/device on main thread on which |
| 1098 | // subsequent Array objects are created |
| 1099 | set_device(0); |
| 1100 | |
| 1101 | let mut c = constant(0.0f32, dim4!(3, 3)); |
| 1102 | let a = constant(1.0f32, dim4!(3, 3)); |
| 1103 | let b = constant(2.0f32, dim4!(3, 3)); |
| 1104 | |
| 1105 | let (tx, rx) = mpsc::channel(); |
| 1106 | |
| 1107 | let threads: Vec<_> = ops |
| 1108 | .into_iter() |
| 1109 | .map(|op| { |
| 1110 | // a and b are internally reference counted by ArrayFire. Unless there |
| 1111 | // is prior known need that they may be modified, you can simply clone |
| 1112 | // the objects pass them to threads |
| 1113 | let x = a.clone(); |
| 1114 | let y = b.clone(); |
| 1115 | |
| 1116 | let tx_clone = tx.clone(); |
| 1117 | |
| 1118 | thread::spawn(move || { |
| 1119 | //Both of objects are created on device 0 in main thread |
| 1120 | //Every thread needs to set the device that it is going to |
| 1121 | //work on. Note that all Array objects must have been created |
| 1122 | //on same device as of date this is written on. |
| 1123 | set_device(0); |
| 1124 | |
| 1125 | let c = match op { |
| 1126 | Op::Add => x + y, |
| 1127 | Op::Sub => x - y, |
| 1128 | Op::Div => x / y, |
| 1129 | Op::Mul => x * y, |
| 1130 | }; |
| 1131 | tx_clone.send(c).unwrap(); |
| 1132 | }) |
| 1133 | }) |
| 1134 | .collect(); |
| 1135 | |
| 1136 | for _i in 0..ops_len { |
| 1137 | c += rx.recv().unwrap(); |
| 1138 | } |
| 1139 | |
| 1140 | //Need to join other threads as main thread holds arrayfire context |
| 1141 | for child in threads { |
| 1142 | let _ = child.join(); |
| 1143 | } |
| 1144 | |
| 1145 | //af_print!("C after accumulating results", &c); |
| 1146 | //[3 3 1 1] |
| 1147 | // 8.0000 8.0000 8.0000 |
| 1148 | // 8.0000 8.0000 8.0000 |
| 1149 | // 8.0000 8.0000 8.0000 |
nothing calls this directly
no test coverage detected