nmatrix-lapacke 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/ext/nmatrix/data/complex.h +364 -0
- data/ext/nmatrix/data/data.h +638 -0
- data/ext/nmatrix/data/meta.h +64 -0
- data/ext/nmatrix/data/ruby_object.h +389 -0
- data/ext/nmatrix/math/asum.h +120 -0
- data/ext/nmatrix/math/cblas_enums.h +36 -0
- data/ext/nmatrix/math/cblas_templates_core.h +507 -0
- data/ext/nmatrix/math/gemm.h +241 -0
- data/ext/nmatrix/math/gemv.h +178 -0
- data/ext/nmatrix/math/getrf.h +255 -0
- data/ext/nmatrix/math/getrs.h +121 -0
- data/ext/nmatrix/math/imax.h +79 -0
- data/ext/nmatrix/math/laswp.h +165 -0
- data/ext/nmatrix/math/long_dtype.h +49 -0
- data/ext/nmatrix/math/math.h +744 -0
- data/ext/nmatrix/math/nrm2.h +160 -0
- data/ext/nmatrix/math/rot.h +117 -0
- data/ext/nmatrix/math/rotg.h +106 -0
- data/ext/nmatrix/math/scal.h +71 -0
- data/ext/nmatrix/math/trsm.h +332 -0
- data/ext/nmatrix/math/util.h +148 -0
- data/ext/nmatrix/nm_memory.h +60 -0
- data/ext/nmatrix/nmatrix.h +408 -0
- data/ext/nmatrix/ruby_constants.h +106 -0
- data/ext/nmatrix/storage/common.h +176 -0
- data/ext/nmatrix/storage/dense/dense.h +128 -0
- data/ext/nmatrix/storage/list/list.h +137 -0
- data/ext/nmatrix/storage/storage.h +98 -0
- data/ext/nmatrix/storage/yale/class.h +1139 -0
- data/ext/nmatrix/storage/yale/iterators/base.h +142 -0
- data/ext/nmatrix/storage/yale/iterators/iterator.h +130 -0
- data/ext/nmatrix/storage/yale/iterators/row.h +449 -0
- data/ext/nmatrix/storage/yale/iterators/row_stored.h +139 -0
- data/ext/nmatrix/storage/yale/iterators/row_stored_nd.h +168 -0
- data/ext/nmatrix/storage/yale/iterators/stored_diagonal.h +123 -0
- data/ext/nmatrix/storage/yale/math/transpose.h +110 -0
- data/ext/nmatrix/storage/yale/yale.h +202 -0
- data/ext/nmatrix/types.h +54 -0
- data/ext/nmatrix/util/io.h +115 -0
- data/ext/nmatrix/util/sl_list.h +143 -0
- data/ext/nmatrix/util/util.h +78 -0
- data/ext/nmatrix_lapacke/extconf.rb +200 -0
- data/ext/nmatrix_lapacke/lapacke.cpp +100 -0
- data/ext/nmatrix_lapacke/lapacke/include/lapacke.h +16445 -0
- data/ext/nmatrix_lapacke/lapacke/include/lapacke_config.h +119 -0
- data/ext/nmatrix_lapacke/lapacke/include/lapacke_mangling.h +17 -0
- data/ext/nmatrix_lapacke/lapacke/include/lapacke_mangling_with_flags.h +17 -0
- data/ext/nmatrix_lapacke/lapacke/include/lapacke_utils.h +579 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_cgeev.c +89 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_cgeev_work.c +141 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_cgesdd.c +106 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_cgesdd_work.c +158 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_cgesvd.c +94 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_cgesvd_work.c +149 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_cgetrf.c +51 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_cgetrf_work.c +83 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_cgetri.c +77 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_cgetri_work.c +89 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_cgetrs.c +56 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_cgetrs_work.c +102 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_cpotrf.c +50 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_cpotrf_work.c +82 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_cpotri.c +50 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_cpotri_work.c +82 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_cpotrs.c +55 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_cpotrs_work.c +101 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_dgeev.c +78 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_dgeev_work.c +136 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_dgesdd.c +88 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_dgesdd_work.c +153 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_dgesvd.c +83 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_dgesvd_work.c +144 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_dgetrf.c +50 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_dgetrf_work.c +81 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_dgetri.c +75 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_dgetri_work.c +87 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_dgetrs.c +55 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_dgetrs_work.c +99 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_dpotrf.c +50 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_dpotrf_work.c +81 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_dpotri.c +50 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_dpotri_work.c +81 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_dpotrs.c +54 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_dpotrs_work.c +97 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_sgeev.c +78 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_sgeev_work.c +134 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_sgesdd.c +88 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_sgesdd_work.c +152 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_sgesvd.c +83 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_sgesvd_work.c +143 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_sgetrf.c +50 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_sgetrf_work.c +81 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_sgetri.c +75 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_sgetri_work.c +87 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_sgetrs.c +55 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_sgetrs_work.c +99 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_spotrf.c +50 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_spotrf_work.c +81 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_spotri.c +50 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_spotri_work.c +81 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_spotrs.c +54 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_spotrs_work.c +97 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_zgeev.c +89 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_zgeev_work.c +141 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_zgesdd.c +106 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_zgesdd_work.c +158 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_zgesvd.c +94 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_zgesvd_work.c +149 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_zgetrf.c +51 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_zgetrf_work.c +83 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_zgetri.c +77 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_zgetri_work.c +89 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_zgetrs.c +56 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_zgetrs_work.c +102 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_zpotrf.c +50 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_zpotrf_work.c +82 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_zpotri.c +50 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_zpotri_work.c +82 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_zpotrs.c +55 -0
- data/ext/nmatrix_lapacke/lapacke/src/lapacke_zpotrs_work.c +101 -0
- data/ext/nmatrix_lapacke/lapacke/utils/lapacke_cge_nancheck.c +62 -0
- data/ext/nmatrix_lapacke/lapacke/utils/lapacke_cge_trans.c +65 -0
- data/ext/nmatrix_lapacke/lapacke/utils/lapacke_cpo_nancheck.c +43 -0
- data/ext/nmatrix_lapacke/lapacke/utils/lapacke_cpo_trans.c +45 -0
- data/ext/nmatrix_lapacke/lapacke/utils/lapacke_ctr_nancheck.c +85 -0
- data/ext/nmatrix_lapacke/lapacke/utils/lapacke_ctr_trans.c +85 -0
- data/ext/nmatrix_lapacke/lapacke/utils/lapacke_dge_nancheck.c +62 -0
- data/ext/nmatrix_lapacke/lapacke/utils/lapacke_dge_trans.c +65 -0
- data/ext/nmatrix_lapacke/lapacke/utils/lapacke_dpo_nancheck.c +43 -0
- data/ext/nmatrix_lapacke/lapacke/utils/lapacke_dpo_trans.c +45 -0
- data/ext/nmatrix_lapacke/lapacke/utils/lapacke_dtr_nancheck.c +85 -0
- data/ext/nmatrix_lapacke/lapacke/utils/lapacke_dtr_trans.c +85 -0
- data/ext/nmatrix_lapacke/lapacke/utils/lapacke_lsame.c +41 -0
- data/ext/nmatrix_lapacke/lapacke/utils/lapacke_sge_nancheck.c +62 -0
- data/ext/nmatrix_lapacke/lapacke/utils/lapacke_sge_trans.c +65 -0
- data/ext/nmatrix_lapacke/lapacke/utils/lapacke_spo_nancheck.c +43 -0
- data/ext/nmatrix_lapacke/lapacke/utils/lapacke_spo_trans.c +45 -0
- data/ext/nmatrix_lapacke/lapacke/utils/lapacke_str_nancheck.c +85 -0
- data/ext/nmatrix_lapacke/lapacke/utils/lapacke_str_trans.c +85 -0
- data/ext/nmatrix_lapacke/lapacke/utils/lapacke_xerbla.c +46 -0
- data/ext/nmatrix_lapacke/lapacke/utils/lapacke_zge_nancheck.c +62 -0
- data/ext/nmatrix_lapacke/lapacke/utils/lapacke_zge_trans.c +65 -0
- data/ext/nmatrix_lapacke/lapacke/utils/lapacke_zpo_nancheck.c +43 -0
- data/ext/nmatrix_lapacke/lapacke/utils/lapacke_zpo_trans.c +45 -0
- data/ext/nmatrix_lapacke/lapacke/utils/lapacke_ztr_nancheck.c +85 -0
- data/ext/nmatrix_lapacke/lapacke/utils/lapacke_ztr_trans.c +85 -0
- data/ext/nmatrix_lapacke/lapacke_nmatrix.h +16 -0
- data/ext/nmatrix_lapacke/make_lapacke_cpp.rb +9 -0
- data/ext/nmatrix_lapacke/math_lapacke.cpp +967 -0
- data/ext/nmatrix_lapacke/math_lapacke/cblas_local.h +576 -0
- data/ext/nmatrix_lapacke/math_lapacke/cblas_templates_lapacke.h +51 -0
- data/ext/nmatrix_lapacke/math_lapacke/lapacke_templates.h +356 -0
- data/ext/nmatrix_lapacke/nmatrix_lapacke.cpp +42 -0
- data/lib/nmatrix/lapack_ext_common.rb +69 -0
- data/lib/nmatrix/lapacke.rb +213 -0
- data/spec/00_nmatrix_spec.rb +730 -0
- data/spec/01_enum_spec.rb +190 -0
- data/spec/02_slice_spec.rb +389 -0
- data/spec/03_nmatrix_monkeys_spec.rb +78 -0
- data/spec/2x2_dense_double.mat +0 -0
- data/spec/4x4_sparse.mat +0 -0
- data/spec/4x5_dense.mat +0 -0
- data/spec/blas_spec.rb +193 -0
- data/spec/elementwise_spec.rb +303 -0
- data/spec/homogeneous_spec.rb +99 -0
- data/spec/io/fortran_format_spec.rb +88 -0
- data/spec/io/harwell_boeing_spec.rb +98 -0
- data/spec/io/test.rua +9 -0
- data/spec/io_spec.rb +149 -0
- data/spec/lapack_core_spec.rb +482 -0
- data/spec/leakcheck.rb +16 -0
- data/spec/math_spec.rb +730 -0
- data/spec/nmatrix_yale_resize_test_associations.yaml +2802 -0
- data/spec/nmatrix_yale_spec.rb +286 -0
- data/spec/plugins/lapacke/lapacke_spec.rb +303 -0
- data/spec/rspec_monkeys.rb +56 -0
- data/spec/rspec_spec.rb +34 -0
- data/spec/shortcuts_spec.rb +310 -0
- data/spec/slice_set_spec.rb +157 -0
- data/spec/spec_helper.rb +140 -0
- data/spec/stat_spec.rb +203 -0
- data/spec/test.pcd +20 -0
- data/spec/utm5940.mtx +83844 -0
- metadata +262 -0
@@ -0,0 +1,60 @@
|
|
1
|
+
/////////////////////////////////////////////////////////////////////
|
2
|
+
// = NMatrix
|
3
|
+
//
|
4
|
+
// A linear algebra library for scientific computation in Ruby.
|
5
|
+
// NMatrix is part of SciRuby.
|
6
|
+
//
|
7
|
+
// NMatrix was originally inspired by and derived from NArray, by
|
8
|
+
// Masahiro Tanaka: http://narray.rubyforge.org
|
9
|
+
//
|
10
|
+
// == Copyright Information
|
11
|
+
//
|
12
|
+
// SciRuby is Copyright (c) 2010 - 2014, Ruby Science Foundation
|
13
|
+
// NMatrix is Copyright (c) 2012 - 2014, John Woods and the Ruby Science Foundation
|
14
|
+
//
|
15
|
+
// Please see LICENSE.txt for additional copyright notices.
|
16
|
+
//
|
17
|
+
// == Contributing
|
18
|
+
//
|
19
|
+
// By contributing source code to SciRuby, you agree to be bound by
|
20
|
+
// our Contributor Agreement:
|
21
|
+
//
|
22
|
+
// * https://github.com/SciRuby/sciruby/wiki/Contributor-Agreement
|
23
|
+
//
|
24
|
+
// == nm_memory.h
|
25
|
+
//
|
26
|
+
// Macros for memory allocation and freeing
|
27
|
+
|
28
|
+
/**
|
29
|
+
* We define these macros, which just call the ruby ones, as this makes
|
30
|
+
* debugging memory issues (particularly those involving interaction with
|
31
|
+
* the ruby GC) easier, as it's posssible to add debugging code temporarily.
|
32
|
+
*/
|
33
|
+
#ifndef __NM_MEMORY_H__
|
34
|
+
#define __NM_MEMORY_H__
|
35
|
+
|
36
|
+
#include <ruby.h>
|
37
|
+
|
38
|
+
#define NM_ALLOC(type) (ALLOC(type))
|
39
|
+
|
40
|
+
#define NM_ALLOC_N(type, n) (ALLOC_N(type, n))
|
41
|
+
|
42
|
+
#define NM_REALLOC_N(var, type, n) (REALLOC_N(var, type, n))
|
43
|
+
|
44
|
+
#define NM_ALLOCA_N(type, n) (ALLOCA_N(type, n))
|
45
|
+
|
46
|
+
#define NM_FREE(var) (xfree(var))
|
47
|
+
|
48
|
+
#define NM_ALLOC_NONRUBY(type) ((type*) malloc(sizeof(type)))
|
49
|
+
|
50
|
+
//Defines whether to do conservative gc registrations, i.e. those
|
51
|
+
//registrations that we're not that sure are necessary.
|
52
|
+
//#define NM_GC_CONSERVATIVE
|
53
|
+
|
54
|
+
#ifdef NM_GC_CONSERVATIVE
|
55
|
+
#define NM_CONSERVATIVE(statement) (statement)
|
56
|
+
#else
|
57
|
+
#define NM_CONSERVATIVE(statement)
|
58
|
+
#endif //NM_GC_CONSERVATIVE
|
59
|
+
|
60
|
+
#endif
|
@@ -0,0 +1,408 @@
|
|
1
|
+
/////////////////////////////////////////////////////////////////////
|
2
|
+
// = NMatrix
|
3
|
+
//
|
4
|
+
// A linear algebra library for scientific computation in Ruby.
|
5
|
+
// NMatrix is part of SciRuby.
|
6
|
+
//
|
7
|
+
// NMatrix was originally inspired by and derived from NArray, by
|
8
|
+
// Masahiro Tanaka: http://narray.rubyforge.org
|
9
|
+
//
|
10
|
+
// == Copyright Information
|
11
|
+
//
|
12
|
+
// SciRuby is Copyright (c) 2010 - 2014, Ruby Science Foundation
|
13
|
+
// NMatrix is Copyright (c) 2012 - 2014, John Woods and the Ruby Science Foundation
|
14
|
+
//
|
15
|
+
// Please see LICENSE.txt for additional copyright notices.
|
16
|
+
//
|
17
|
+
// == Contributing
|
18
|
+
//
|
19
|
+
// By contributing source code to SciRuby, you agree to be bound by
|
20
|
+
// our Contributor Agreement:
|
21
|
+
//
|
22
|
+
// * https://github.com/SciRuby/sciruby/wiki/Contributor-Agreement
|
23
|
+
//
|
24
|
+
// == nmatrix.h
|
25
|
+
//
|
26
|
+
// C and C++ API for NMatrix, and main header file.
|
27
|
+
|
28
|
+
#ifndef NMATRIX_H
|
29
|
+
#define NMATRIX_H
|
30
|
+
|
31
|
+
/*
|
32
|
+
* Standard Includes
|
33
|
+
*/
|
34
|
+
|
35
|
+
#include <ruby.h>
|
36
|
+
|
37
|
+
#ifdef __cplusplus
|
38
|
+
#include <cmath>
|
39
|
+
#include <cstring>
|
40
|
+
#else
|
41
|
+
#include <math.h>
|
42
|
+
#include <string.h>
|
43
|
+
#endif
|
44
|
+
|
45
|
+
#ifdef BENCHMARK
|
46
|
+
// SOURCE: http://stackoverflow.com/questions/2349776/how-can-i-benchmark-a-c-program-easily
|
47
|
+
#ifdef __cplusplus
|
48
|
+
#include <sys/ctime>
|
49
|
+
#include <sys/cresource>
|
50
|
+
#else
|
51
|
+
#include <sys/time.h>
|
52
|
+
#include <sys/resource.h>
|
53
|
+
#endif
|
54
|
+
#endif
|
55
|
+
|
56
|
+
#ifdef __cplusplus
|
57
|
+
#include "nm_memory.h"
|
58
|
+
#endif
|
59
|
+
|
60
|
+
/*
|
61
|
+
* Macros
|
62
|
+
*/
|
63
|
+
|
64
|
+
#define RUBY_ZERO INT2FIX(0)
|
65
|
+
|
66
|
+
#ifndef SIZEOF_INT
|
67
|
+
#error SIZEOF_INT undefined
|
68
|
+
#else
|
69
|
+
#if SIZEOF_INT == 8
|
70
|
+
#define DEFAULT_DTYPE INT64
|
71
|
+
#define SIZE_T INT64
|
72
|
+
#else
|
73
|
+
#if SIZEOF_INT == 4
|
74
|
+
#define DEFAULT_DTYPE INT32
|
75
|
+
#define SIZE_T INT32
|
76
|
+
#else
|
77
|
+
#if SIZEOF_INT == 2
|
78
|
+
#define DEFAULT_DTYPE INT16
|
79
|
+
#define SIZE_T INT16
|
80
|
+
#else
|
81
|
+
#error Unhandled SIZEOF_INT -- please #define SIZE_T and DEFAULT_DTYPE manually.
|
82
|
+
#endif
|
83
|
+
#endif
|
84
|
+
#endif
|
85
|
+
#endif
|
86
|
+
|
87
|
+
/*
|
88
|
+
* == Macros for Concurrent C and C++ Header Maintenance
|
89
|
+
*
|
90
|
+
* These macros look complicated, but they're really not so bad. They're also important: they ensure that whether our
|
91
|
+
* header file (nmatrix.h) is read by a C++ or a C compiler, all the same data structures and enumerators exist, albeit
|
92
|
+
* with slightly different names.
|
93
|
+
*
|
94
|
+
* "But wait," you say, "You use structs. Structs exist in C and C++. Why use a macro to set them up?"
|
95
|
+
*
|
96
|
+
* Well, in C, you have to be explicit about what a struct is. You can actually get around that requirement by using a
|
97
|
+
* typedef:
|
98
|
+
*
|
99
|
+
* typedef struct STORAGE { ... } STORAGE;
|
100
|
+
*
|
101
|
+
* Also, we use C++ inheritance, which is obviously not allowed in C. So we have to ensure that the base class's members
|
102
|
+
* are exposed properly to our child classes.
|
103
|
+
*
|
104
|
+
* The macros also allow us to put all of our C++ types into namespaces. For C, we prefix everything with either nm_ or
|
105
|
+
* NM_ to distinguish our declarations from those in other libraries.
|
106
|
+
*/
|
107
|
+
|
108
|
+
|
109
|
+
#ifdef __cplusplus /* These are the C++ versions of the macros. */
|
110
|
+
|
111
|
+
/*
|
112
|
+
* If no block is given, return an enumerator. This copied straight out of ruby's include/ruby/intern.h.
|
113
|
+
*
|
114
|
+
* rb_enumeratorize is located in enumerator.c.
|
115
|
+
*
|
116
|
+
* VALUE rb_enumeratorize(VALUE obj, VALUE meth, int argc, VALUE *argv) {
|
117
|
+
* return enumerator_init(enumerator_allocate(rb_cEnumerator), obj, meth, argc, argv);
|
118
|
+
* }
|
119
|
+
*/
|
120
|
+
|
121
|
+
//opening portion -- this allows unregistering any objects in use before returning
|
122
|
+
#define RETURN_SIZED_ENUMERATOR_PRE do { \
|
123
|
+
if (!rb_block_given_p()) {
|
124
|
+
|
125
|
+
//remaining portion
|
126
|
+
#ifdef RUBY_2
|
127
|
+
#ifndef RETURN_SIZED_ENUMERATOR
|
128
|
+
#undef RETURN_SIZED_ENUMERATOR
|
129
|
+
// Ruby 2.0 and higher has rb_enumeratorize_with_size instead of rb_enumeratorize.
|
130
|
+
// We want to support both in the simplest way possible.
|
131
|
+
#define RETURN_SIZED_ENUMERATOR(obj, argc, argv, size_fn) \
|
132
|
+
return rb_enumeratorize_with_size((obj), ID2SYM(rb_frame_this_func()), (argc), (argv), (size_fn)); \
|
133
|
+
} \
|
134
|
+
} while (0)
|
135
|
+
#endif
|
136
|
+
#else
|
137
|
+
#undef RETURN_SIZED_ENUMERATOR
|
138
|
+
#define RETURN_SIZED_ENUMERATOR(obj, argc, argv, size_fn) \
|
139
|
+
return rb_enumeratorize((obj), ID2SYM(rb_frame_this_func()), (argc), (argv)); \
|
140
|
+
} \
|
141
|
+
} while (0)
|
142
|
+
#endif
|
143
|
+
|
144
|
+
#define NM_DECL_ENUM(enum_type, name) nm::enum_type name
|
145
|
+
#define NM_DECL_STRUCT(type, name) type name;
|
146
|
+
|
147
|
+
#define NM_DEF_STORAGE_ELEMENTS \
|
148
|
+
NM_DECL_ENUM(dtype_t, dtype); \
|
149
|
+
size_t dim; \
|
150
|
+
size_t* shape; \
|
151
|
+
size_t* offset; \
|
152
|
+
int count; \
|
153
|
+
STORAGE* src;
|
154
|
+
|
155
|
+
#define NM_DEF_STORAGE_CHILD_STRUCT_PRE(name) struct name : STORAGE {
|
156
|
+
#define NM_DEF_STORAGE_STRUCT_POST(name) };
|
157
|
+
|
158
|
+
#define NM_DEF_STORAGE_STRUCT \
|
159
|
+
struct STORAGE { \
|
160
|
+
NM_DEF_STORAGE_ELEMENTS; \
|
161
|
+
};
|
162
|
+
|
163
|
+
#define NM_DEF_STRUCT_PRE(name) struct name {
|
164
|
+
#define NM_DEF_STRUCT_POST(name) };
|
165
|
+
|
166
|
+
#define NM_DEF_ENUM(name, ...) \
|
167
|
+
namespace nm { \
|
168
|
+
enum name { \
|
169
|
+
__VA_ARGS__ \
|
170
|
+
}; \
|
171
|
+
} // end of namespace nm
|
172
|
+
|
173
|
+
#else /* These are the C versions of the macros. */
|
174
|
+
|
175
|
+
#define NM_DECL_ENUM(enum_type, name) nm_ ## enum_type name
|
176
|
+
#define NM_DECL_STRUCT(type, name) struct NM_ ## type name;
|
177
|
+
|
178
|
+
#define NM_DEF_STORAGE_ELEMENTS \
|
179
|
+
NM_DECL_ENUM(dtype_t, dtype); \
|
180
|
+
size_t dim; \
|
181
|
+
size_t* shape; \
|
182
|
+
size_t* offset; \
|
183
|
+
int count; \
|
184
|
+
NM_DECL_STRUCT(STORAGE*, src);
|
185
|
+
#define NM_DEF_STORAGE_CHILD_STRUCT_PRE(name) typedef struct NM_ ## name { \
|
186
|
+
NM_DEF_STORAGE_ELEMENTS;
|
187
|
+
|
188
|
+
#define NM_DEF_STORAGE_STRUCT_POST(name) } NM_ ## name;
|
189
|
+
|
190
|
+
#define NM_DEF_STORAGE_STRUCT \
|
191
|
+
typedef struct NM_STORAGE { \
|
192
|
+
NM_DEF_STORAGE_ELEMENTS; \
|
193
|
+
} NM_STORAGE;
|
194
|
+
|
195
|
+
#define NM_DEF_STRUCT_PRE(name) typedef struct NM_ ## name {
|
196
|
+
#define NM_DEF_STRUCT_POST(name) } NM_ ## name;
|
197
|
+
|
198
|
+
#define NM_DEF_ENUM(name, ...) \
|
199
|
+
typedef enum nm_ ## name { \
|
200
|
+
__VA_ARGS__ \
|
201
|
+
} nm_ ## name;
|
202
|
+
|
203
|
+
#endif /* End of C/C++ Parallel Header Macro Definitions */
|
204
|
+
|
205
|
+
|
206
|
+
/*
|
207
|
+
* Types
|
208
|
+
*/
|
209
|
+
|
210
|
+
#define NM_NUM_DTYPES 10 // data/data.h
|
211
|
+
#define NM_NUM_STYPES 3 // storage/storage.h
|
212
|
+
|
213
|
+
//#ifdef __cplusplus
|
214
|
+
//namespace nm {
|
215
|
+
//#endif
|
216
|
+
|
217
|
+
/* Storage Type -- Dense or Sparse */
|
218
|
+
NM_DEF_ENUM(stype_t, DENSE_STORE = 0,
|
219
|
+
LIST_STORE = 1,
|
220
|
+
YALE_STORE = 2);
|
221
|
+
|
222
|
+
/* Data Type */
|
223
|
+
NM_DEF_ENUM(dtype_t, BYTE = 0, // unsigned char
|
224
|
+
INT8 = 1, // char
|
225
|
+
INT16 = 2, // short
|
226
|
+
INT32 = 3, // int
|
227
|
+
INT64 = 4, // long
|
228
|
+
FLOAT32 = 5, // float
|
229
|
+
FLOAT64 = 6, // double
|
230
|
+
COMPLEX64 = 7, // Complex64 class
|
231
|
+
COMPLEX128 = 8, // Complex128 class
|
232
|
+
RUBYOBJ = 9); // Ruby VALUE type
|
233
|
+
|
234
|
+
NM_DEF_ENUM(symm_t, NONSYMM = 0,
|
235
|
+
SYMM = 1,
|
236
|
+
SKEW = 2,
|
237
|
+
HERM = 3,
|
238
|
+
UPPER = 4,
|
239
|
+
LOWER = 5);
|
240
|
+
|
241
|
+
//#ifdef __cplusplus
|
242
|
+
//}; // end of namespace nm
|
243
|
+
//#endif
|
244
|
+
|
245
|
+
/* struct STORAGE */
|
246
|
+
NM_DEF_STORAGE_STRUCT;
|
247
|
+
|
248
|
+
/* Dense Storage */
|
249
|
+
NM_DEF_STORAGE_CHILD_STRUCT_PRE(DENSE_STORAGE); // struct DENSE_STORAGE : STORAGE {
|
250
|
+
void* elements; // should go first to align with void* a in yale and NODE* first in list.
|
251
|
+
size_t* stride;
|
252
|
+
NM_DEF_STORAGE_STRUCT_POST(DENSE_STORAGE); // };
|
253
|
+
|
254
|
+
/* Yale Storage */
|
255
|
+
NM_DEF_STORAGE_CHILD_STRUCT_PRE(YALE_STORAGE);
|
256
|
+
void* a; // should go first
|
257
|
+
size_t ndnz; // Strictly non-diagonal non-zero count!
|
258
|
+
size_t capacity;
|
259
|
+
size_t* ija;
|
260
|
+
NM_DEF_STORAGE_STRUCT_POST(YALE_STORAGE);
|
261
|
+
|
262
|
+
// FIXME: NODE and LIST should be put in some kind of namespace or something, at least in C++.
|
263
|
+
NM_DEF_STRUCT_PRE(NODE); // struct NODE {
|
264
|
+
size_t key;
|
265
|
+
void* val;
|
266
|
+
NM_DECL_STRUCT(NODE*, next); // NODE* next;
|
267
|
+
NM_DEF_STRUCT_POST(NODE); // };
|
268
|
+
|
269
|
+
NM_DEF_STRUCT_PRE(LIST); // struct LIST {
|
270
|
+
NM_DECL_STRUCT(NODE*, first); // NODE* first;
|
271
|
+
NM_DEF_STRUCT_POST(LIST); // };
|
272
|
+
|
273
|
+
/* List-of-Lists Storage */
|
274
|
+
NM_DEF_STORAGE_CHILD_STRUCT_PRE(LIST_STORAGE); // struct LIST_STORAGE : STORAGE {
|
275
|
+
// List storage specific elements.
|
276
|
+
void* default_val;
|
277
|
+
NM_DECL_STRUCT(LIST*, rows); // LIST* rows;
|
278
|
+
NM_DEF_STORAGE_STRUCT_POST(LIST_STORAGE); // };
|
279
|
+
|
280
|
+
|
281
|
+
|
282
|
+
/* NMATRIX Object */
|
283
|
+
NM_DEF_STRUCT_PRE(NMATRIX); // struct NMATRIX {
|
284
|
+
NM_DECL_ENUM(stype_t, stype); // stype_t stype; // Method of storage (csc, dense, etc).
|
285
|
+
NM_DECL_STRUCT(STORAGE*, storage); // STORAGE* storage; // Pointer to storage struct.
|
286
|
+
NM_DEF_STRUCT_POST(NMATRIX); // };
|
287
|
+
|
288
|
+
/* Structs for dealing with VALUEs in use so that they don't get GC'd */
|
289
|
+
|
290
|
+
NM_DEF_STRUCT_PRE(NM_GC_LL_NODE); // struct NM_GC_LL_NODE {
|
291
|
+
VALUE* val; // VALUE* val;
|
292
|
+
size_t n; // size_t n;
|
293
|
+
NM_DECL_STRUCT(NM_GC_LL_NODE*, next); // NM_GC_LL_NODE* next;
|
294
|
+
NM_DEF_STRUCT_POST(NM_GC_LL_NODE); // };
|
295
|
+
|
296
|
+
NM_DEF_STRUCT_PRE(NM_GC_HOLDER); // struct NM_GC_HOLDER {
|
297
|
+
NM_DECL_STRUCT(NM_GC_LL_NODE*, start); // NM_GC_LL_NODE* start;
|
298
|
+
NM_DEF_STRUCT_POST(NM_GC_HOLDER); // };
|
299
|
+
|
300
|
+
#define NM_MAX_RANK 15
|
301
|
+
|
302
|
+
#define UnwrapNMatrix(obj,var) Data_Get_Struct(obj, NMATRIX, var)
|
303
|
+
|
304
|
+
#define NM_STORAGE(val) (NM_STRUCT(val)->storage)
|
305
|
+
#ifdef __cplusplus
|
306
|
+
#define NM_STRUCT(val) ((NMATRIX*)(DATA_PTR(val)))
|
307
|
+
#define NM_STORAGE_LIST(val) ((LIST_STORAGE*)(NM_STORAGE(val)))
|
308
|
+
#define NM_STORAGE_YALE(val) ((YALE_STORAGE*)(NM_STORAGE(val)))
|
309
|
+
#define NM_STORAGE_DENSE(val) ((DENSE_STORAGE*)(NM_STORAGE(val)))
|
310
|
+
#else
|
311
|
+
#define NM_STRUCT(val) ((struct NM_NMATRIX*)(DATA_PTR(val)))
|
312
|
+
#define NM_STORAGE_LIST(val) ((struct NM_LIST_STORAGE*)(NM_STORAGE(val)))
|
313
|
+
#define NM_STORAGE_YALE(val) ((struct NM_YALE_STORAGE*)(NM_STORAGE(val)))
|
314
|
+
#define NM_STORAGE_DENSE(val) ((struct NM_DENSE_STORAGE*)(NM_STORAGE(val)))
|
315
|
+
#endif
|
316
|
+
|
317
|
+
#define NM_SRC(val) (NM_STORAGE(val)->src)
|
318
|
+
#define NM_DIM(val) (NM_STORAGE(val)->dim)
|
319
|
+
#define NM_DTYPE(val) (NM_STORAGE(val)->dtype)
|
320
|
+
#define NM_STYPE(val) (NM_STRUCT(val)->stype)
|
321
|
+
#define NM_SHAPE(val,i) (NM_STORAGE(val)->shape[(i)])
|
322
|
+
#define NM_SHAPE0(val) (NM_STORAGE(val)->shape[0])
|
323
|
+
#define NM_SHAPE1(val) (NM_STORAGE(val)->shape[1])
|
324
|
+
#define NM_DEFAULT_VAL(val) (NM_STORAGE_LIST(val)->default_val)
|
325
|
+
|
326
|
+
#define NM_DENSE_COUNT(val) (nm_storage_count_max_elements(NM_STORAGE_DENSE(val)))
|
327
|
+
#define NM_DENSE_ELEMENTS(val) (NM_STORAGE_DENSE(val)->elements)
|
328
|
+
#define NM_SIZEOF_DTYPE(val) (DTYPE_SIZES[NM_DTYPE(val)])
|
329
|
+
#define NM_REF(val,slice) (RefFuncs[NM_STYPE(val)]( NM_STORAGE(val), slice, NM_SIZEOF_DTYPE(val) ))
|
330
|
+
|
331
|
+
#define NM_MAX(a,b) (((a)>(b))?(a):(b))
|
332
|
+
#define NM_MIN(a,b) (((a)>(b))?(b):(a))
|
333
|
+
#define NM_SWAP(a,b,tmp) {(tmp)=(a);(a)=(b);(b)=(tmp);}
|
334
|
+
|
335
|
+
#define NM_CHECK_ALLOC(x) if (!x) rb_raise(rb_eNoMemError, "insufficient memory");
|
336
|
+
|
337
|
+
#define RB_FILE_EXISTS(fn) (rb_funcall(rb_const_get(rb_cObject, rb_intern("File")), rb_intern("exists?"), 1, (fn)) == Qtrue)
|
338
|
+
|
339
|
+
#define CheckNMatrixType(v) if (TYPE(v) != T_DATA || (RDATA(v)->dfree != (RUBY_DATA_FUNC)nm_delete && RDATA(v)->dfree != (RUBY_DATA_FUNC)nm_delete_ref)) rb_raise(rb_eTypeError, "expected NMatrix on left-hand side of operation");
|
340
|
+
|
341
|
+
#define NM_IsNMatrix(obj) \
|
342
|
+
(rb_obj_is_kind_of(obj, cNMatrix) == Qtrue)
|
343
|
+
|
344
|
+
#define NM_IsNVector(obj) \
|
345
|
+
(rb_obj_is_kind_of(obj, cNVector) == Qtrue)
|
346
|
+
|
347
|
+
#define RB_P(OBJ) \
|
348
|
+
rb_funcall(rb_stderr, rb_intern("print"), 1, rb_funcall(OBJ, rb_intern("object_id"), 0)); \
|
349
|
+
rb_funcall(rb_stderr, rb_intern("puts"), 1, rb_funcall(OBJ, rb_intern("inspect"), 0));
|
350
|
+
|
351
|
+
|
352
|
+
#ifdef __cplusplus
|
353
|
+
typedef VALUE (*METHOD)(...);
|
354
|
+
|
355
|
+
//}; // end of namespace nm
|
356
|
+
#endif
|
357
|
+
|
358
|
+
// In the init code below, we need to use NMATRIX for c++ and NM_NMATRIX for c
|
359
|
+
// this macro chooses the correct one:
|
360
|
+
#ifdef __cplusplus
|
361
|
+
#define _NMATRIX NMATRIX
|
362
|
+
#define _STORAGE STORAGE
|
363
|
+
#else
|
364
|
+
#define _NMATRIX NM_NMATRIX
|
365
|
+
#define _STORAGE NM_STORAGE
|
366
|
+
#endif
|
367
|
+
|
368
|
+
/*
|
369
|
+
* Functions
|
370
|
+
*/
|
371
|
+
|
372
|
+
#ifdef __cplusplus
|
373
|
+
extern "C" {
|
374
|
+
#endif
|
375
|
+
|
376
|
+
void Init_nmatrix();
|
377
|
+
// External API
|
378
|
+
VALUE rb_nmatrix_dense_create(NM_DECL_ENUM(dtype_t, dtype), size_t* shape, size_t dim, void* elements, size_t length);
|
379
|
+
VALUE rb_nvector_dense_create(NM_DECL_ENUM(dtype_t, dtype), void* elements, size_t length);
|
380
|
+
|
381
|
+
NM_DECL_ENUM(dtype_t, nm_dtype_guess(VALUE)); // (This is a function)
|
382
|
+
NM_DECL_ENUM(dtype_t, nm_dtype_min(VALUE));
|
383
|
+
|
384
|
+
// Non-API functions needed by other cpp files.
|
385
|
+
_NMATRIX* nm_create(NM_DECL_ENUM(stype_t, stype), _STORAGE* storage);
|
386
|
+
_NMATRIX* nm_cast_with_ctype_args(_NMATRIX* self, NM_DECL_ENUM(stype_t, new_stype), NM_DECL_ENUM(dtype_t, new_dtype), void* init_ptr);
|
387
|
+
VALUE nm_cast(VALUE self, VALUE new_stype_symbol, VALUE new_dtype_symbol, VALUE init);
|
388
|
+
void nm_mark(_NMATRIX* mat);
|
389
|
+
void nm_delete(_NMATRIX* mat);
|
390
|
+
void nm_delete_ref(_NMATRIX* mat);
|
391
|
+
void nm_register_values(VALUE* vals, size_t n);
|
392
|
+
void nm_register_value(VALUE* val);
|
393
|
+
void nm_unregister_value(VALUE* val);
|
394
|
+
void nm_unregister_values(VALUE* vals, size_t n);
|
395
|
+
void nm_register_storage(NM_DECL_ENUM(stype_t, stype), const _STORAGE* storage);
|
396
|
+
void nm_unregister_storage(NM_DECL_ENUM(stype_t, stype), const _STORAGE* storage);
|
397
|
+
void nm_register_nmatrix(_NMATRIX* nmatrix);
|
398
|
+
void nm_unregister_nmatrix(_NMATRIX* nmatrix);
|
399
|
+
void nm_completely_unregister_value(VALUE* val);
|
400
|
+
|
401
|
+
#ifdef __cplusplus
|
402
|
+
}
|
403
|
+
#endif
|
404
|
+
|
405
|
+
#undef _NMATRIX
|
406
|
+
#undef _STORAGE
|
407
|
+
|
408
|
+
#endif // NMATRIX_H
|