1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
// Copyright 2019-2022 Clemens Lutz
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use super::morsel_dispatcher::MorselDispatcher;
use super::HetMorselExecutor;
use crate::error::*;
use crate::runtime::memory::LaunchableMem;
use crate::runtime::memory::LaunchableSlice;
use rustacuda::memory::DeviceCopy;
use rustacuda::stream::Stream;
use std::mem::{self, size_of, MaybeUninit};
use std::sync::Arc;

pub trait IntoHetMorselIterator<'a> {
    /// The type of the iterator to produce.
    type Iter;

    /// Create an iterator from a value.
    ///
    /// See module-level documentation for details.
    fn into_het_morsel_iter<'h: 'a>(
        &'a mut self,
        executor: &'h mut HetMorselExecutor,
    ) -> Self::Iter;
}

impl<'a, 'r, 's, R, S> IntoHetMorselIterator<'a> for (&'r mut [R], &'s mut [S])
where
    'r: 'a,
    's: 'a,
    R: 'a + Copy + DeviceCopy + Send + Sync,
    S: 'a + Copy + DeviceCopy + Send + Sync,
{
    type Iter = HetMorselIterator2<'a, R, S>;

    fn into_het_morsel_iter<'h: 'a>(
        &'a mut self,
        executor: &'h mut HetMorselExecutor,
    ) -> Self::Iter {
        assert_eq!(self.0.len(), self.1.len());

        Self::Iter {
            data: (self.0, self.1),
            executor,
        }
    }
}

pub struct HetMorselIterator2<'a, R, S> {
    data: (&'a mut [R], &'a mut [S]),
    executor: &'a mut HetMorselExecutor,
}

pub struct StatefulHetMorselIterator2<'a, R, S, CWS: Send, GWS: Send> {
    data: (&'a mut [R], &'a mut [S]),
    executor: &'a mut HetMorselExecutor,
    cpu_worker_states: Vec<CWS>,
    gpu_worker_states: Vec<GWS>,
}

impl<'a, R, S> HetMorselIterator2<'a, R, S> {
    pub fn with_state<CpuF, GpuF, CWS, GWS>(
        self,
        cpu_init: CpuF,
        gpu_init: GpuF,
    ) -> Result<StatefulHetMorselIterator2<'a, R, S, CWS, GWS>>
    where
        CpuF: Fn(u16) -> Result<CWS> + Send + Sync,
        GpuF: Fn(u16, &Stream) -> Result<GWS> + Send + Sync,
        CWS: Send,
        GWS: Send,
    {
        let cpu_thread_pool = &mut self.executor.cpu_thread_pool;
        let gpu_thread_pool = &mut self.executor.gpu_thread_pool;

        let cpu_workers = self.executor.cpu_workers;
        let gpu_workers = self.executor.gpu_workers;
        let streams = &mut self.executor.streams;

        let mut cpu_uninit_states: Vec<MaybeUninit<CWS>> =
            (0..cpu_workers).map(|_| MaybeUninit::uninit()).collect();
        let mut gpu_uninit_states: Vec<MaybeUninit<GWS>> =
            (0..gpu_workers).map(|_| MaybeUninit::uninit()).collect();

        let cus = &mut cpu_uninit_states;
        let gus = &mut gpu_uninit_states;

        cpu_thread_pool.scope(move |cpu_scope| {
            gpu_thread_pool.scope(move |gpu_scope| {
                let cpu_af = Arc::new(cpu_init);
                let gpu_af = Arc::new(gpu_init);

                for (thread_id, uninit_state) in (0..).zip(cus.iter_mut()) {
                    let af = cpu_af.clone();

                    cpu_scope.spawn(move |_| {
                        let state: CWS = af(thread_id).expect("Failed to run CPU worker");
                        *uninit_state = MaybeUninit::new(state);
                    });
                }

                for ((thread_id, uninit_state), stream) in
                    (0..).zip(gus.iter_mut()).zip(streams.iter_mut())
                {
                    let af = gpu_af.clone();

                    gpu_scope.spawn(move |_| {
                        let state: GWS = af(thread_id, stream).expect("Failed to run GPU worker");
                        *uninit_state = MaybeUninit::new(state);
                    });
                }
            });
        });

        let cpu_worker_states: Vec<CWS> = unsafe {
            cpu_uninit_states
                .into_iter()
                .map(|state| state.assume_init())
                .collect()
        };
        let gpu_worker_states: Vec<GWS> = unsafe {
            gpu_uninit_states
                .into_iter()
                .map(|state| state.assume_init())
                .collect()
        };

        Ok(StatefulHetMorselIterator2 {
            data: self.data,
            executor: self.executor,
            cpu_worker_states,
            gpu_worker_states,
        })
    }

    pub fn fold<CpuF, GpuF>(&mut self, cpu_f: CpuF, gpu_f: GpuF) -> Result<()>
    where
        R: Copy + DeviceCopy + Send + Sync,
        S: Copy + DeviceCopy + Send + Sync,
        CpuF: Fn((&[R], &[S])) -> Result<()> + Send + Sync,
        GpuF: Fn((LaunchableSlice<'_, R>, LaunchableSlice<'_, S>), &Stream) -> Result<()>
            + Send
            + Sync,
    {
        // Create dummy state
        let cpu_worker_states = vec![(); self.executor.cpu_workers];
        let gpu_worker_states = vec![(); self.executor.gpu_workers];

        let r: &mut [R] = self.data.0;
        let s: &mut [S] = self.data.1;
        let data = (r, s);

        let mut iter = StatefulHetMorselIterator2 {
            data,
            executor: self.executor,
            cpu_worker_states,
            gpu_worker_states,
        };

        // Wrap StatefulHetMorselIterator2::fold with dummy state
        iter.fold(|data, _| cpu_f(data), |data, _, stream| gpu_f(data, stream))
    }
}

impl<'a, R, S, CWS: Send, GWS: Send> StatefulHetMorselIterator2<'a, R, S, CWS, GWS> {
    pub fn fold<CpuF, GpuF>(&mut self, cpu_f: CpuF, gpu_f: GpuF) -> Result<()>
    where
        R: Copy + DeviceCopy + Send + Sync,
        S: Copy + DeviceCopy + Send + Sync,
        CpuF: Fn((&[R], &[S]), &mut CWS) -> Result<()> + Send + Sync,
        GpuF: Fn((LaunchableSlice<'_, R>, LaunchableSlice<'_, S>), &mut GWS, &Stream) -> Result<()>
            + Send
            + Sync,
    {
        let cpu_morsel_len =
            self.executor.morsel_spec.cpu_morsel_bytes / (size_of::<R>() + size_of::<S>());
        let gpu_morsel_len =
            self.executor.morsel_spec.gpu_morsel_bytes / (size_of::<R>() + size_of::<S>());
        let dispatcher = MorselDispatcher::new(self.data.0.len(), cpu_morsel_len, gpu_morsel_len);
        let dispatcher_ref = &dispatcher;

        let cpu_thread_pool = &mut self.executor.cpu_thread_pool;
        let gpu_thread_pool = &mut self.executor.gpu_thread_pool;

        let cpu_worker_states = &mut self.cpu_worker_states;
        let gpu_worker_states = &mut self.gpu_worker_states;
        let streams = &mut self.executor.streams;

        let ro_data = (self.data.0.as_ref(), self.data.1.as_ref());

        cpu_thread_pool.scope(move |cpu_scope| {
            gpu_thread_pool.scope(move |gpu_scope| {
                let cpu_af = Arc::new(cpu_f);
                let gpu_af = Arc::new(gpu_f);

                for state in cpu_worker_states.iter_mut() {
                    let af = cpu_af.clone();

                    cpu_scope.spawn(move |_| {
                        Self::cpu_worker(dispatcher_ref, ro_data, af, state)
                            .expect("Failed to run CPU worker");
                    });
                }

                for (state, stream) in gpu_worker_states.iter_mut().zip(streams.iter_mut()) {
                    let af = gpu_af.clone();

                    gpu_scope.spawn(move |_| {
                        Self::gpu_worker(dispatcher_ref, ro_data, af, state, stream)
                            .expect("Failed to run GPU worker");
                    });
                }
            });
        });

        Ok(())
    }

    fn cpu_worker<F>(
        dispatcher: &MorselDispatcher,
        data: (&[R], &[S]),
        f: Arc<F>,
        state: &mut CWS,
    ) -> Result<()>
    where
        F: Fn((&[R], &[S]), &mut CWS) -> Result<()>,
    {
        for morsel in dispatcher.cpu_iter() {
            f((&data.0[morsel.clone()], &data.1[morsel.clone()]), state)?;
        }

        Ok(())
    }

    fn gpu_worker<F>(
        dispatcher: &MorselDispatcher,
        data: (&[R], &[S]),
        f: Arc<F>,
        state: &mut GWS,
        stream: &Stream,
    ) -> Result<()>
    where
        F: Fn((LaunchableSlice<'_, R>, LaunchableSlice<'_, S>), &mut GWS, &Stream) -> Result<()>,
    {
        for morsel in dispatcher.gpu_iter() {
            f(
                (
                    data.0[morsel.clone()].as_launchable_slice(),
                    data.1[morsel.clone()].as_launchable_slice(),
                ),
                state,
                stream,
            )?
        }

        // synchronize to not queue up _all_ morsels on the stream
        stream.synchronize()?;

        Ok(())
    }
}

impl<'a, R, S, CWS: Send, GWS: Send> Drop for StatefulHetMorselIterator2<'a, R, S, CWS, GWS> {
    fn drop(&mut self) {
        let executor = &mut self.executor;

        let cpu_worker_states = &mut self.cpu_worker_states;
        let gpu_worker_states = &mut self.gpu_worker_states;
        let streams = &mut executor.streams;

        executor.cpu_thread_pool.scope(move |cpu_scope| {
            for state in cpu_worker_states.iter_mut() {
                cpu_scope.spawn(move |_| {
                    mem::drop(state);
                });
            }
        });

        executor.gpu_thread_pool.scope(move |gpu_scope| {
            for (state, stream) in gpu_worker_states.iter_mut().zip(streams.iter_mut()) {
                gpu_scope.spawn(move |_| {
                    mem::drop(state);
                    mem::drop(stream);
                });
            }
        });
    }
}