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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]

use core::mem::MaybeUninit;
use std::ffi::{c_char, CStr};
use std::sync::atomic::{AtomicBool, AtomicIsize, Ordering};
use std::sync::{Arc, Mutex, MutexGuard};

use once_cell::sync::Lazy;
use suitesparse_graphblas_sys::{
    GrB_BinaryOp, GrB_BinaryOp_error, GrB_Descriptor, GrB_Descriptor_error, GrB_IndexUnaryOp,
    GrB_IndexUnaryOp_error, GrB_Matrix, GrB_Matrix_error, GrB_Monoid, GrB_Monoid_error, GrB_Scalar,
    GrB_Scalar_error, GrB_Semiring, GrB_Semiring_error, GrB_Type, GrB_Type_error, GrB_UnaryOp,
    GrB_UnaryOp_error, GrB_Vector, GrB_Vector_error, GrB_finalize, GxB_SelectOp,
    GxB_SelectOp_error,
};

use crate::bindings_to_graphblas_implementation::{
    GrB_Info,
    GrB_Info_GrB_DIMENSION_MISMATCH,
    GrB_Info_GrB_DOMAIN_MISMATCH,
    GrB_Info_GrB_EMPTY_OBJECT,
    GrB_Info_GrB_INDEX_OUT_OF_BOUNDS,
    GrB_Info_GrB_INSUFFICIENT_SPACE,
    GrB_Info_GrB_INVALID_INDEX,
    GrB_Info_GrB_INVALID_OBJECT,
    GrB_Info_GrB_INVALID_VALUE,
    GrB_Info_GrB_NOT_IMPLEMENTED,
    GrB_Info_GrB_NO_VALUE,
    GrB_Info_GrB_NULL_POINTER,
    GrB_Info_GrB_OUTPUT_NOT_EMPTY,
    GrB_Info_GrB_OUT_OF_MEMORY,
    GrB_Info_GrB_PANIC,
    GrB_Info_GrB_SUCCESS,
    GrB_Info_GrB_UNINITIALIZED_OBJECT,
    GrB_Info_GxB_EXHAUSTED,
    GrB_Mode,
    GrB_Mode_GrB_BLOCKING,
    GrB_Mode_GrB_NONBLOCKING,
    // GrB_error,
    GrB_init,
};

use crate::error::SparseLinearAlgebraError;
use crate::error::{GraphblasError, GraphblasErrorType};
use crate::error::{SystemError, SystemErrorType};

/*
TO REVIEW: The GraphBLAS context can only be initialized once per process (i.e. not per thread)
Also, after calling GrB_finalize(), the process must be restarted before GrB_init() can be called again.
These limtations are not compatible with the context concept implemented in this module.

This module assumes that multiple contexts can be started, closed, and re-started per thread.
It is expected that a future-version of GraphBLAS will support this concept.
If this expectation is false, or given the current GraphBLAS C-specifcation version 2.0,
the context concet implemented in this module is not useful.
*/

// lazy_static! {
//     static ref NUMBER_OF_READY_CONTEXTS: Mutex<AtomicIsize> = Mutex::new(AtomicIsize::new(0));
// }

// lazy_static! {
//     static ref IS_GRAPHBLAS_BUSY: Mutex<AtomicBool> = Mutex::new(AtomicBool::new(false));
// }

// static NUMBER_OF_READY_CONTEXTS: Arc<Mutex<AtomicIsize>> = Arc::new(Mutex::new(AtomicIsize::new(0)));
// static IS_GRAPHBLAS_BUSY: Arc<Mutex<AtomicBool>> = Arc::new(Mutex::new(AtomicBool::new(false)));

static NUMBER_OF_READY_CONTEXTS: Lazy<Mutex<AtomicIsize>> =
    Lazy::new(|| Mutex::new(AtomicIsize::new(0)));
static IS_GRAPHBLAS_BUSY: Lazy<Mutex<AtomicBool>> =
    Lazy::new(|| Mutex::new(AtomicBool::new(false)));

// fn is_context_initialized(number_of_ready_contexts: MutexGuard<AtomicUsize>) -> bool {
//     number_of_ready_contexts.load(Ordering::SeqCst) > 0
// }

pub trait ContextTrait {
    fn context(&self) -> Arc<Context>;
    fn context_ref(&self) -> &Arc<Context>;
}

#[derive(Clone, Debug, PartialEq)]
pub enum Mode {
    Blocking,
    NonBlocking,
}

impl From<GrB_Mode> for Mode {
    fn from(mode: GrB_Mode) -> Self {
        match mode {
            GrB_Mode_GrB_BLOCKING => Self::Blocking,
            GrB_Mode_GrB_NONBLOCKING => Self::NonBlocking,
            _ => panic!("Context mode not supported: {}", mode),
        }
    }
}

impl Into<GrB_Mode> for Mode {
    fn into(self) -> GrB_Mode {
        match self {
            Self::Blocking => GrB_Mode_GrB_BLOCKING,
            Self::NonBlocking => GrB_Mode_GrB_NONBLOCKING,
        }
    }
}

#[derive(Debug, PartialEq)]
pub enum Context {
    Ready(Ready),
    NotReady(NotReady),
}

impl Context {
    fn new() -> Self {
        Context::NotReady(NotReady {})
    }

    pub fn init_ready(_mode: Mode) -> Result<Arc<Self>, SparseLinearAlgebraError> {
        let mut context = Context::new();
        context.start(Mode::NonBlocking)?;
        Ok(Arc::new(context))
    }

    fn start(&mut self, mode: Mode) -> Result<Status, SparseLinearAlgebraError> {
        let number_of_ready_contexts = NUMBER_OF_READY_CONTEXTS.lock().unwrap();
        // println!("number_of_ready_contexts before starting: {:?}",number_of_ready_contexts.load(Ordering::SeqCst));
        if number_of_ready_contexts.load(Ordering::SeqCst) == 0 {
            let status = initialize(mode.to_owned(), number_of_ready_contexts)?;
            *self = Context::Ready(Ready { mode });
            Ok(status)
        } else {
            number_of_ready_contexts.fetch_add(1, Ordering::SeqCst);
            *self = Context::Ready(Ready { mode });
            Ok(Status::Success)
        }
    }

    // TODO: make this safe to use. At the moment, the graphblas context is dropped automatically after all contexts have been dropped.
    // pub fn stop(&mut self) -> () {
    //     match &*self {
    //         Context::Ready(ready) => {
    //             *self = Context::NotReady(NotReady {});
    //         }
    //         Context::NotReady(_) => (),
    //     }
    // }

    // TODO: check context is Ready
    pub fn call_without_detailed_error_information<F>(
        &self,
        function_to_call: F,
    ) -> Result<Status, SparseLinearAlgebraError>
    where
        F: FnMut() -> GrB_Info,
    {
        call_graphblas_implementation_without_detailed_error_information(function_to_call)
    }
}

fn initialize(
    mode: Mode,
    number_of_ready_contexts: MutexGuard<AtomicIsize>,
) -> Result<Status, SparseLinearAlgebraError> {
    // println!("Trying to initialize a context");
    // let _is_graphblas_busy = IS_GRAPHBLAS_BUSY.lock().unwrap();
    // println!("Got a lock to initialize a context! {:?}", is_graphblas_busy.load(Ordering::SeqCst));
    let status = unsafe {
        graphblas_result(GrB_init(mode.into()), || -> String {
            String::from("Something went wrong while initializing a GraphBLAS context")
        })?
    };
    number_of_ready_contexts.fetch_add(1, Ordering::SeqCst);
    // println!("Initialised a context");
    Ok(status)
}

pub trait CallGraphBlasContext<T> {
    fn call<F>(
        &self,
        function_to_call: F,
        reference_to_debug_info: &T,
    ) -> Result<Status, SparseLinearAlgebraError>
    where
        F: FnMut() -> GrB_Info;
}

#[derive(Debug, PartialEq)]
pub struct NotReady {}

#[derive(Debug, PartialEq)]
pub struct Ready {
    mode: Mode,
    // version: Version
}

impl Ready {
    fn call_without_detailed_error_information<F>(
        &self,
        function_to_call: F,
    ) -> Result<Status, SparseLinearAlgebraError>
    where
        F: FnMut() -> GrB_Info,
    {
        call_graphblas_implementation_without_detailed_error_information(function_to_call)
    }
}

fn call_graphblas_implementation_without_detailed_error_information<F>(
    mut function_to_call: F,
) -> Result<Status, SparseLinearAlgebraError>
where
    F: FnMut() -> GrB_Info,
{
    // thread::sleep(time::Duration::from_secs(2));
    // let _is_graphblas_busy = IS_GRAPHBLAS_BUSY.lock().unwrap();
    graphblas_result(function_to_call(), || -> String {
        String::from("Something went wrong while calling the GraphBLAS context.")
    })
}

impl Ready {
    fn finalize_context(&self) -> Result<Status, SparseLinearAlgebraError> {
        Ok(self.call_without_detailed_error_information(|| unsafe { GrB_finalize() })?)
    }
}

impl Drop for Ready {
    fn drop(&mut self) -> () {
        let number_of_ready_contexts = NUMBER_OF_READY_CONTEXTS.lock().unwrap();
        if number_of_ready_contexts.load(Ordering::SeqCst) == 0 {
            self.finalize_context().unwrap();
        }
    }
}

macro_rules! implement_CallGraphBlasContext {
    ($graphblas_type: ty, $error_retrieval_function: ident) => {
        paste::paste! {
            impl CallGraphBlasContext<$graphblas_type> for Context {
                // TODO: check Context state is Ready
                fn call<F>(
                    &self,
                    mut function_to_call: F,
                    reference_to_debug_info: &$graphblas_type,
                ) -> Result<Status, SparseLinearAlgebraError>
                where
                    F: FnMut() -> GrB_Info,
                {
                    let get_detailed_error_information =
                        [<generate_closure_to_retrieve_detailed_error_message_ $graphblas_type>](reference_to_debug_info);
                    // thread::sleep(time::Duration::from_secs(2));
                    // let _is_graphblas_busy = IS_GRAPHBLAS_BUSY.lock().unwrap();
                    graphblas_result(function_to_call(), get_detailed_error_information)
                }
            }

            fn [<generate_closure_to_retrieve_detailed_error_message_ $graphblas_type>]<'a>(
                struct_with_debugging_info: &'a $graphblas_type,
            ) -> impl Fn() -> String + 'a {
                return || -> String {
                    let mut graphblas_error_message: MaybeUninit<*const c_char> = MaybeUninit::uninit();
                    let graphblas_call_status;
                    unsafe {
                        // graphblas_call_status = suitesparse_graphblas_sys::GrB_error(
                        graphblas_call_status = $error_retrieval_function(
                            graphblas_error_message.as_mut_ptr(),
                            *struct_with_debugging_info,
                        );
                    }
                    let graphblas_error_message = unsafe { graphblas_error_message.assume_init() };
                    match graphblas_call_status {
                        GrB_Info_GrB_SUCCESS => {
                            let message;
                            unsafe {
                                message = CStr::from_ptr(graphblas_error_message).to_str();
                            }
                            match message {
                                Ok(message) => message.to_owned(),
                                Err(error) => format!("Something went wrong while calling the GraphBLAS implementation. Unable to parse detailed error message due to: {}", error)
                            }
                        }
                        _ => return String::from("Something went wrong while calling the GraphBLAS implementation. Unable to retrieve more detailed error information.")
                    }
                };
            }
        }
    };
}

implement_CallGraphBlasContext!(GrB_Type, GrB_Type_error);
implement_CallGraphBlasContext!(GrB_Scalar, GrB_Scalar_error);
implement_CallGraphBlasContext!(GrB_Vector, GrB_Vector_error);
implement_CallGraphBlasContext!(GrB_Matrix, GrB_Matrix_error);
implement_CallGraphBlasContext!(GrB_Descriptor, GrB_Descriptor_error);
implement_CallGraphBlasContext!(GrB_UnaryOp, GrB_UnaryOp_error);
implement_CallGraphBlasContext!(GrB_BinaryOp, GrB_BinaryOp_error);
implement_CallGraphBlasContext!(GrB_Semiring, GrB_Semiring_error);
implement_CallGraphBlasContext!(GrB_Monoid, GrB_Monoid_error);
implement_CallGraphBlasContext!(GrB_IndexUnaryOp, GrB_IndexUnaryOp_error);
implement_CallGraphBlasContext!(GxB_SelectOp, GxB_SelectOp_error);

fn graphblas_result<F>(
    grb_info: GrB_Info,
    get_detailed_error_information: F,
) -> Result<Status, SparseLinearAlgebraError>
where
    F: Fn() -> String,
{
    let status = Status::from(grb_info);
    match status {
        Status::Success => Ok(Status::Success),
        _ => Err(status.into_sparse_linear_algebra_error(get_detailed_error_information())),
    }
}

// TODO: consider the use of https://crates.io/crates/enum_primitive

#[derive(Debug, Clone, PartialEq)]
pub enum Status {
    Success,
    NoValue,
    UnitializedObject,
    InvalidObject,
    NotImplemented,
    NullPointer,
    InvalidValue,
    InvalidIndex,
    DomainMismatch,
    DimensionMismatch,
    EmptyObject,
    OutputNotEmpty,
    OutOfMemory,
    InsufficientSpace,
    IndexOutOfBounds,
    IteratorExhausted,
    Panic,
    UnknownStatusType,
}

impl From<GrB_Info> for Status {
    fn from(status: GrB_Info) -> Self {
        match status {
            GrB_Info_GrB_SUCCESS => Self::Success,
            GrB_Info_GrB_NO_VALUE => Self::NoValue,
            GrB_Info_GrB_UNINITIALIZED_OBJECT => Self::UnitializedObject,
            GrB_Info_GrB_INVALID_OBJECT => Self::InvalidObject,
            GrB_Info_GrB_NOT_IMPLEMENTED => Self::NotImplemented,
            GrB_Info_GrB_NULL_POINTER => Self::NullPointer,
            GrB_Info_GrB_INVALID_VALUE => Self::InvalidValue,
            GrB_Info_GrB_INVALID_INDEX => Self::InvalidIndex,
            GrB_Info_GrB_DOMAIN_MISMATCH => Self::DomainMismatch,
            GrB_Info_GrB_DIMENSION_MISMATCH => Self::DimensionMismatch,
            GrB_Info_GrB_EMPTY_OBJECT => Self::EmptyObject,
            GrB_Info_GrB_OUTPUT_NOT_EMPTY => Self::OutputNotEmpty,
            GrB_Info_GrB_OUT_OF_MEMORY => Self::OutOfMemory,
            GrB_Info_GrB_INSUFFICIENT_SPACE => Self::InsufficientSpace,
            GrB_Info_GrB_INDEX_OUT_OF_BOUNDS => Self::IndexOutOfBounds,
            GrB_Info_GxB_EXHAUSTED => Self::IteratorExhausted,
            GrB_Info_GrB_PANIC => Self::Panic,
            _ => Self::UnknownStatusType,
        }
    }
}

impl Status {
    fn into_sparse_linear_algebra_error(
        self,
        detailed_error_information: String,
    ) -> SparseLinearAlgebraError {
        match self {
            Status::Success => SystemError::new(
                SystemErrorType::CreateGraphBlasErrorOnSuccessValue,
                format!("Logic error, called into<GraphBlasError> for success status"),
                None,
            )
            .into(),
            Status::NoValue => {
                GraphblasError::new(GraphblasErrorType::NoValue, detailed_error_information).into()
            }
            Status::UnitializedObject => GraphblasError::new(
                GraphblasErrorType::UnitializedObject,
                detailed_error_information,
            )
            .into(),
            Status::InvalidObject => GraphblasError::new(
                GraphblasErrorType::InvalidObject,
                detailed_error_information,
            )
            .into(),
            Status::NotImplemented => GraphblasError::new(
                GraphblasErrorType::NotImplemented,
                detailed_error_information,
            )
            .into(),
            Status::NullPointer => {
                GraphblasError::new(GraphblasErrorType::NullPointer, detailed_error_information)
                    .into()
            }
            Status::InvalidValue => {
                GraphblasError::new(GraphblasErrorType::InvalidValue, detailed_error_information)
                    .into()
            }
            Status::InvalidIndex => {
                GraphblasError::new(GraphblasErrorType::InvalidIndex, detailed_error_information)
                    .into()
            }
            Status::DomainMismatch => GraphblasError::new(
                GraphblasErrorType::DomainMismatch,
                detailed_error_information,
            )
            .into(),
            Status::DimensionMismatch => GraphblasError::new(
                GraphblasErrorType::DimensionMismatch,
                detailed_error_information,
            )
            .into(),
            Status::EmptyObject => {
                GraphblasError::new(GraphblasErrorType::EmptyObject, detailed_error_information)
                    .into()
            }
            Status::OutputNotEmpty => GraphblasError::new(
                GraphblasErrorType::OutputNotEmpty,
                detailed_error_information,
            )
            .into(),
            Status::OutOfMemory => {
                GraphblasError::new(GraphblasErrorType::OutOfMemory, detailed_error_information)
                    .into()
            }
            Status::InsufficientSpace => GraphblasError::new(
                GraphblasErrorType::InsufficientSpace,
                detailed_error_information,
            )
            .into(),
            Status::IndexOutOfBounds => GraphblasError::new(
                GraphblasErrorType::IndexOutOfBounds,
                detailed_error_information,
            )
            .into(),
            Status::IteratorExhausted => GraphblasError::new(
                GraphblasErrorType::IteratorExhausted,
                detailed_error_information,
            )
            .into(),
            Status::Panic => {
                GraphblasError::new(GraphblasErrorType::Panic, detailed_error_information).into()
            }
            Status::UnknownStatusType => SystemError::new(
                SystemErrorType::UnsupportedGraphBlasErrorValue,
                String::from("Something went wrong while calling the GrapBLAS implementation"),
                None,
            )
            .into(),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    // #[test]
    // fn init_graphblas() {
    //     let info = unsafe { GrB_init(GrB_Mode_GrB_NONBLOCKING) };
    //     let status = Status::from(info);
    //     println!("GrB_init status={:?}", status);
    //     assert_eq!(status, Status::Success);
    // }
    // #[test]
    // fn init_graphblas_2() {
    //     let info = unsafe { GrB_init(GrB_Mode_GrB_NONBLOCKING) };
    //     let status = Status::from(info);
    //     println!("GrB_init status={:?}", status);
    //     assert_eq!(status, Status::Success);
    // }

    #[test]
    fn start_and_drop_context() {
        let mut context = Context::new();
        context.start(Mode::NonBlocking).unwrap();

        // assert_eq!(
        //     context,
        //     Context::Ready(Ready {
        //         mode: Mode::NonBlocking
        //     })
        // );

        // // the Ready instance will get dropped and (falsely) substract 1 to the number of active contexts
        // // To compensate this test-specific error, manually increase the context count
        // let number_of_ready_contexts = NUMBER_OF_READY_CONTEXTS.lock().unwrap();
        // number_of_ready_contexts.fetch_add(1, Ordering::SeqCst);
    }

    #[test]
    fn start_and_drop_context_2() {
        let mut context = Context::new();
        context.start(Mode::NonBlocking).unwrap();
        // let mut context = Context::init_ready(Mode::NonBlocking).unwrap();

        // assert_eq!(
        //     context,
        //     Context::Ready(Ready {
        //         mode: Mode::NonBlocking
        //     })
        // );
        // // the Ready instance will get dropped and (falsely) substract 1 to the number of active contexts
        // // To compensate this test-specific error, manually increase the context count
        // let number_of_ready_contexts = NUMBER_OF_READY_CONTEXTS.lock().unwrap();
        // number_of_ready_contexts.fetch_add(1, Ordering::SeqCst);
    }

    #[test]
    fn start_and_drop_context_3() {
        let _context = Context::init_ready(Mode::NonBlocking).unwrap();

        // assert_eq!(
        //     context,
        //     Arc::new(Context::Ready(Ready {
        //         mode: Mode::NonBlocking
        //     }))
        // );

        // // the Ready instance will get dropped and (falsely) substract 1 to the number of active contexts
        // // To compensate this test-specific error, manually increase the context count
        // let number_of_ready_contexts = NUMBER_OF_READY_CONTEXTS.lock().unwrap();
        // number_of_ready_contexts.fetch_add(1, Ordering::SeqCst);
    }

    #[test]
    fn start_and_drop_context_4() {
        let _context = Context::init_ready(Mode::NonBlocking).unwrap();

        // assert_eq!(
        //     context,
        //     Arc::new(Context::Ready(Ready {
        //         mode: Mode::NonBlocking
        //     }))
        // );

        // // the Ready instance will get dropped and (falsely) substract 1 to the number of active contexts
        // // To compensate this test-specific error, manually increase the context count
        // let number_of_ready_contexts = NUMBER_OF_READY_CONTEXTS.lock().unwrap();
        // number_of_ready_contexts.fetch_add(1, Ordering::SeqCst);
    }

    #[test]
    fn start_and_drop_context_5() {
        let _context = Context::init_ready(Mode::NonBlocking).unwrap();

        // assert_eq!(
        //     context,
        //     Arc::new(Context::Ready(Ready {
        //         mode: Mode::NonBlocking
        //     }))
        // );

        // // the Ready instance will get dropped and (falsely) substract 1 to the number of active contexts
        // // To compensate this test-specific error, manually increase the context count
        // let number_of_ready_contexts = NUMBER_OF_READY_CONTEXTS.lock().unwrap();
        // number_of_ready_contexts.fetch_add(1, Ordering::SeqCst);
    }
}