ruby-dnn 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (127) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +8 -0
  3. data/.travis.yml +5 -0
  4. data/CODE_OF_CONDUCT.md +74 -0
  5. data/Gemfile +6 -0
  6. data/LICENSE.txt +21 -0
  7. data/README.md +42 -0
  8. data/Rakefile +10 -0
  9. data/bin/console +14 -0
  10. data/bin/setup +8 -0
  11. data/lib/dnn.rb +14 -0
  12. data/lib/dnn/core/activations.rb +116 -0
  13. data/lib/dnn/core/error.rb +13 -0
  14. data/lib/dnn/core/initializers.rb +46 -0
  15. data/lib/dnn/core/layers.rb +366 -0
  16. data/lib/dnn/core/model.rb +158 -0
  17. data/lib/dnn/core/optimizers.rb +113 -0
  18. data/lib/dnn/core/util.rb +24 -0
  19. data/lib/dnn/core/version.rb +3 -0
  20. data/lib/dnn/ext/cifar10/Makefile +263 -0
  21. data/lib/dnn/ext/cifar10/cifar10_ext.c +52 -0
  22. data/lib/dnn/ext/cifar10/cifar10_ext.o +0 -0
  23. data/lib/dnn/ext/cifar10/cifar10_ext.so +0 -0
  24. data/lib/dnn/ext/cifar10/extconf.rb +3 -0
  25. data/lib/dnn/ext/cifar10/numo/compat.h +23 -0
  26. data/lib/dnn/ext/cifar10/numo/extconf.h +13 -0
  27. data/lib/dnn/ext/cifar10/numo/intern.h +117 -0
  28. data/lib/dnn/ext/cifar10/numo/narray.h +430 -0
  29. data/lib/dnn/ext/cifar10/numo/ndloop.h +94 -0
  30. data/lib/dnn/ext/cifar10/numo/template.h +149 -0
  31. data/lib/dnn/ext/cifar10/numo/types/bit.h +33 -0
  32. data/lib/dnn/ext/cifar10/numo/types/complex.h +409 -0
  33. data/lib/dnn/ext/cifar10/numo/types/complex_macro.h +377 -0
  34. data/lib/dnn/ext/cifar10/numo/types/dcomplex.h +44 -0
  35. data/lib/dnn/ext/cifar10/numo/types/dfloat.h +42 -0
  36. data/lib/dnn/ext/cifar10/numo/types/float_def.h +34 -0
  37. data/lib/dnn/ext/cifar10/numo/types/float_macro.h +186 -0
  38. data/lib/dnn/ext/cifar10/numo/types/int16.h +24 -0
  39. data/lib/dnn/ext/cifar10/numo/types/int32.h +24 -0
  40. data/lib/dnn/ext/cifar10/numo/types/int64.h +24 -0
  41. data/lib/dnn/ext/cifar10/numo/types/int8.h +24 -0
  42. data/lib/dnn/ext/cifar10/numo/types/int_macro.h +41 -0
  43. data/lib/dnn/ext/cifar10/numo/types/real_accum.h +486 -0
  44. data/lib/dnn/ext/cifar10/numo/types/robj_macro.h +75 -0
  45. data/lib/dnn/ext/cifar10/numo/types/robject.h +27 -0
  46. data/lib/dnn/ext/cifar10/numo/types/scomplex.h +44 -0
  47. data/lib/dnn/ext/cifar10/numo/types/sfloat.h +43 -0
  48. data/lib/dnn/ext/cifar10/numo/types/uint16.h +21 -0
  49. data/lib/dnn/ext/cifar10/numo/types/uint32.h +21 -0
  50. data/lib/dnn/ext/cifar10/numo/types/uint64.h +21 -0
  51. data/lib/dnn/ext/cifar10/numo/types/uint8.h +21 -0
  52. data/lib/dnn/ext/cifar10/numo/types/uint_macro.h +32 -0
  53. data/lib/dnn/ext/cifar10/numo/types/xint_macro.h +189 -0
  54. data/lib/dnn/ext/image_io/Makefile +263 -0
  55. data/lib/dnn/ext/image_io/extconf.rb +3 -0
  56. data/lib/dnn/ext/image_io/image_io_ext.c +89 -0
  57. data/lib/dnn/ext/image_io/image_io_ext.so +0 -0
  58. data/lib/dnn/ext/image_io/numo/compat.h +23 -0
  59. data/lib/dnn/ext/image_io/numo/extconf.h +13 -0
  60. data/lib/dnn/ext/image_io/numo/intern.h +117 -0
  61. data/lib/dnn/ext/image_io/numo/narray.h +430 -0
  62. data/lib/dnn/ext/image_io/numo/ndloop.h +94 -0
  63. data/lib/dnn/ext/image_io/numo/template.h +149 -0
  64. data/lib/dnn/ext/image_io/numo/types/bit.h +33 -0
  65. data/lib/dnn/ext/image_io/numo/types/complex.h +409 -0
  66. data/lib/dnn/ext/image_io/numo/types/complex_macro.h +377 -0
  67. data/lib/dnn/ext/image_io/numo/types/dcomplex.h +44 -0
  68. data/lib/dnn/ext/image_io/numo/types/dfloat.h +42 -0
  69. data/lib/dnn/ext/image_io/numo/types/float_def.h +34 -0
  70. data/lib/dnn/ext/image_io/numo/types/float_macro.h +186 -0
  71. data/lib/dnn/ext/image_io/numo/types/int16.h +24 -0
  72. data/lib/dnn/ext/image_io/numo/types/int32.h +24 -0
  73. data/lib/dnn/ext/image_io/numo/types/int64.h +24 -0
  74. data/lib/dnn/ext/image_io/numo/types/int8.h +24 -0
  75. data/lib/dnn/ext/image_io/numo/types/int_macro.h +41 -0
  76. data/lib/dnn/ext/image_io/numo/types/real_accum.h +486 -0
  77. data/lib/dnn/ext/image_io/numo/types/robj_macro.h +75 -0
  78. data/lib/dnn/ext/image_io/numo/types/robject.h +27 -0
  79. data/lib/dnn/ext/image_io/numo/types/scomplex.h +44 -0
  80. data/lib/dnn/ext/image_io/numo/types/sfloat.h +43 -0
  81. data/lib/dnn/ext/image_io/numo/types/uint16.h +21 -0
  82. data/lib/dnn/ext/image_io/numo/types/uint32.h +21 -0
  83. data/lib/dnn/ext/image_io/numo/types/uint64.h +21 -0
  84. data/lib/dnn/ext/image_io/numo/types/uint8.h +21 -0
  85. data/lib/dnn/ext/image_io/numo/types/uint_macro.h +32 -0
  86. data/lib/dnn/ext/image_io/numo/types/xint_macro.h +189 -0
  87. data/lib/dnn/ext/image_io/stb_image.h +7462 -0
  88. data/lib/dnn/ext/image_io/stb_image_write.h +1568 -0
  89. data/lib/dnn/ext/mnist/Makefile +263 -0
  90. data/lib/dnn/ext/mnist/extconf.rb +3 -0
  91. data/lib/dnn/ext/mnist/mnist_ext.c +49 -0
  92. data/lib/dnn/ext/mnist/mnist_ext.o +0 -0
  93. data/lib/dnn/ext/mnist/mnist_ext.so +0 -0
  94. data/lib/dnn/ext/mnist/numo/compat.h +23 -0
  95. data/lib/dnn/ext/mnist/numo/extconf.h +13 -0
  96. data/lib/dnn/ext/mnist/numo/intern.h +117 -0
  97. data/lib/dnn/ext/mnist/numo/narray.h +430 -0
  98. data/lib/dnn/ext/mnist/numo/ndloop.h +94 -0
  99. data/lib/dnn/ext/mnist/numo/template.h +149 -0
  100. data/lib/dnn/ext/mnist/numo/types/bit.h +33 -0
  101. data/lib/dnn/ext/mnist/numo/types/complex.h +409 -0
  102. data/lib/dnn/ext/mnist/numo/types/complex_macro.h +377 -0
  103. data/lib/dnn/ext/mnist/numo/types/dcomplex.h +44 -0
  104. data/lib/dnn/ext/mnist/numo/types/dfloat.h +42 -0
  105. data/lib/dnn/ext/mnist/numo/types/float_def.h +34 -0
  106. data/lib/dnn/ext/mnist/numo/types/float_macro.h +186 -0
  107. data/lib/dnn/ext/mnist/numo/types/int16.h +24 -0
  108. data/lib/dnn/ext/mnist/numo/types/int32.h +24 -0
  109. data/lib/dnn/ext/mnist/numo/types/int64.h +24 -0
  110. data/lib/dnn/ext/mnist/numo/types/int8.h +24 -0
  111. data/lib/dnn/ext/mnist/numo/types/int_macro.h +41 -0
  112. data/lib/dnn/ext/mnist/numo/types/real_accum.h +486 -0
  113. data/lib/dnn/ext/mnist/numo/types/robj_macro.h +75 -0
  114. data/lib/dnn/ext/mnist/numo/types/robject.h +27 -0
  115. data/lib/dnn/ext/mnist/numo/types/scomplex.h +44 -0
  116. data/lib/dnn/ext/mnist/numo/types/sfloat.h +43 -0
  117. data/lib/dnn/ext/mnist/numo/types/uint16.h +21 -0
  118. data/lib/dnn/ext/mnist/numo/types/uint32.h +21 -0
  119. data/lib/dnn/ext/mnist/numo/types/uint64.h +21 -0
  120. data/lib/dnn/ext/mnist/numo/types/uint8.h +21 -0
  121. data/lib/dnn/ext/mnist/numo/types/uint_macro.h +32 -0
  122. data/lib/dnn/ext/mnist/numo/types/xint_macro.h +189 -0
  123. data/lib/dnn/lib/cifar10.rb +26 -0
  124. data/lib/dnn/lib/image_io.rb +33 -0
  125. data/lib/dnn/lib/mnist.rb +61 -0
  126. data/ruby-dnn.gemspec +41 -0
  127. metadata +225 -0
@@ -0,0 +1,52 @@
1
+ #include <ruby.h>
2
+ #include <stdlib.h>
3
+ #include "numo/narray.h"
4
+
5
+ #define CIFAR10_WIDTH 32
6
+ #define CIFAR10_HEIGHT 32
7
+ #define CIFAR10_CHANNEL 3
8
+ #define CIFAR10_CLASSES 10
9
+
10
+ VALUE cifar10_load(VALUE self, VALUE rb_bin, VALUE rb_num_datas) {
11
+ char* bin = StringValuePtr(rb_bin);
12
+ int num_datas = FIX2INT(rb_num_datas);
13
+ char script[64];
14
+ VALUE rb_na_x;
15
+ VALUE rb_na_y;
16
+ VALUE rb_xy;
17
+ narray_data_t* na_data_x;
18
+ narray_data_t* na_data_y;
19
+ int i;
20
+ int j = 0;
21
+ int k = 0;
22
+ int size = CIFAR10_WIDTH * CIFAR10_HEIGHT * CIFAR10_CHANNEL;
23
+
24
+ sprintf(script, "Numo::UInt8.zeros(%d, %d, %d, %d)", num_datas, CIFAR10_WIDTH, CIFAR10_HEIGHT, CIFAR10_CHANNEL);
25
+ rb_na_x = rb_eval_string(&script[0]);
26
+ na_data_x = RNARRAY_DATA(rb_na_x);
27
+ for(i = 0; i < 64; i++) {
28
+ script[i] = 0;
29
+ }
30
+ sprintf(script, "Numo::UInt8.zeros(%d, %d)", num_datas, CIFAR10_CLASSES);
31
+ rb_na_y = rb_eval_string(&script[0]);
32
+ na_data_y = RNARRAY_DATA(rb_na_y);
33
+
34
+ for (i = 0; i < num_datas; i++) {
35
+ na_data_y->ptr[i] = bin[j];
36
+ j++;
37
+ memcpy(&na_data_x->ptr[k], &bin[j], size);
38
+ j += size;
39
+ k += size;
40
+ }
41
+
42
+ rb_xy = rb_ary_new3(2, rb_na_x, rb_na_y);
43
+ return rb_xy;
44
+ }
45
+
46
+ void Init_cifar10_ext() {
47
+ VALUE rb_dnn;
48
+ VALUE rb_cifar10;
49
+ rb_dnn = rb_define_module("DNN");
50
+ rb_cifar10 = rb_define_module_under(rb_dnn, "CIFAR10");
51
+ rb_define_singleton_method(rb_cifar10, "_cifar10_load", cifar10_load, 2);
52
+ }
Binary file
Binary file
@@ -0,0 +1,3 @@
1
+ require "mkmf"
2
+
3
+ create_makefile("cifar10_ext")
@@ -0,0 +1,23 @@
1
+ #ifndef COMPAT_H
2
+ #define COMPAT_H
3
+
4
+ #if !defined RSTRING_LEN
5
+ #define RSTRING_LEN(a) RSTRING(a)->len
6
+ #endif
7
+ #if !defined RSTRING_PTR
8
+ #define RSTRING_PTR(a) RSTRING(a)->ptr
9
+ #endif
10
+ #if !defined RARRAY_LEN
11
+ #define RARRAY_LEN(a) RARRAY(a)->len
12
+ #endif
13
+ #if !defined RARRAY_PTR
14
+ #define RARRAY_PTR(a) RARRAY(a)->ptr
15
+ #endif
16
+ #if !defined RARRAY_AREF
17
+ #define RARRAY_AREF(a,i) RARRAY_PTR(a)[i]
18
+ #endif
19
+ #if !defined RARRAY_ASET
20
+ #define RARRAY_ASET(a,i,v) (RARRAY_PTR(a)[i] = v)
21
+ #endif
22
+
23
+ #endif /* ifndef COMPAT_H */
@@ -0,0 +1,13 @@
1
+ #ifndef NUMO_EXTCONF_H
2
+ #define NUMO_EXTCONF_H
3
+ #define HAVE_STDBOOL_H 1
4
+ #define HAVE_STDINT_H 1
5
+ #define HAVE_TYPE_BOOL 1
6
+ #define HAVE_TYPE_UINT8_T 1
7
+ #define HAVE_TYPE_UINT16_T 1
8
+ #define HAVE_TYPE_INT32_T 1
9
+ #define HAVE_TYPE_UINT32_T 1
10
+ #define HAVE_TYPE_INT64_T 1
11
+ #define HAVE_TYPE_UINT64_T 1
12
+ #define HAVE_RB_CCOMPLEX 1
13
+ #endif
@@ -0,0 +1,117 @@
1
+ /*
2
+ intern.h
3
+ Numerical Array Extension for Ruby
4
+ (C) Copyright 1999-2017 by Masahiro TANAKA
5
+ */
6
+ #ifndef INTERN_H
7
+ #define INTERN_H
8
+
9
+ #define rb_narray_new nary_new
10
+ VALUE nary_new(VALUE elem, int ndim, size_t *shape);
11
+ #define rb_narray_view_new nary_view_new
12
+ VALUE nary_view_new(VALUE elem, int ndim, size_t *shape);
13
+ #define rb_narray_debug_info nary_debug_info
14
+ VALUE nary_debug_info(VALUE);
15
+
16
+ #define na_make_view nary_make_view
17
+ VALUE nary_make_view(VALUE self);
18
+
19
+ #define na_s_allocate nary_s_allocate
20
+ VALUE nary_s_allocate(VALUE klass);
21
+ #define na_s_allocate_view nary_s_allocate_view
22
+ VALUE nary_s_allocate_view(VALUE klass);
23
+ #define na_s_new_like nary_s_new_like
24
+ VALUE nary_s_new_like(VALUE type, VALUE obj);
25
+
26
+ void na_alloc_shape(narray_t *na, int ndim);
27
+ void na_array_to_internal_shape(VALUE self, VALUE ary, size_t *shape);
28
+ void na_index_arg_to_internal_order(int argc, VALUE *argv, VALUE self);
29
+ void na_setup_shape(narray_t *na, int ndim, size_t *shape);
30
+
31
+ #define na_get_elmsz nary_element_stride
32
+ //#define na_element_stride nary_element_stride
33
+ unsigned int nary_element_stride(VALUE nary);
34
+ #define na_dtype_elmsz nary_dtype_element_stride
35
+ size_t nary_dtype_element_stride(VALUE klass);
36
+
37
+ #define na_get_pointer nary_get_pointer
38
+ char *nary_get_pointer(VALUE);
39
+ #define na_get_pointer_for_write nary_get_pointer_for_write
40
+ char *nary_get_pointer_for_write(VALUE);
41
+ #define na_get_pointer_for_read nary_get_pointer_for_read
42
+ char *nary_get_pointer_for_read(VALUE);
43
+ #define na_get_pointer_for_read_write nary_get_pointer_for_read_write
44
+ char *nary_get_pointer_for_read_write(VALUE);
45
+ #define na_get_offset nary_get_offset
46
+ size_t nary_get_offset(VALUE self);
47
+
48
+ #define na_copy_flags nary_copy_flags
49
+ void nary_copy_flags(VALUE src, VALUE dst);
50
+
51
+ #define na_check_ladder nary_check_ladder
52
+ VALUE nary_check_ladder(VALUE self, int start_dim);
53
+ #define na_check_contiguous nary_check_contiguous
54
+ VALUE nary_check_contiguous(VALUE self);
55
+
56
+ #define na_flatten_dim nary_flatten_dim
57
+ VALUE nary_flatten_dim(VALUE self, int sd);
58
+
59
+ #define na_flatten nary_flatten
60
+ VALUE nary_flatten(VALUE);
61
+
62
+ #define na_copy nary_dup
63
+ VALUE nary_dup(VALUE);
64
+
65
+ #define na_store nary_store
66
+ VALUE nary_store(VALUE self, VALUE src);
67
+
68
+ #define na_upcast numo_na_upcast
69
+ VALUE numo_na_upcast(VALUE type1, VALUE type2);
70
+
71
+ void na_release_lock(VALUE); // currently do nothing
72
+
73
+ // used in reduce methods
74
+ #define na_reduce_dimension nary_reduce_dimension
75
+ VALUE nary_reduce_dimension(int argc, VALUE *argv, int naryc, VALUE *naryv,
76
+ ndfunc_t *ndf, na_iter_func_t nan_iter);
77
+
78
+ #define na_reduce_options nary_reduce_options
79
+ VALUE nary_reduce_options(VALUE axes, VALUE *opts, int naryc, VALUE *naryv,
80
+ ndfunc_t *ndf);
81
+
82
+ // ndloop
83
+ VALUE na_ndloop(ndfunc_t *nf, int argc, ...);
84
+ VALUE na_ndloop2(ndfunc_t *nf, VALUE args);
85
+ VALUE na_ndloop3(ndfunc_t *nf, void *ptr, int argc, ...);
86
+ VALUE na_ndloop4(ndfunc_t *nf, void *ptr, VALUE args);
87
+
88
+ VALUE na_ndloop_cast_narray_to_rarray(ndfunc_t *nf, VALUE nary, VALUE fmt);
89
+ VALUE na_ndloop_store_rarray(ndfunc_t *nf, VALUE nary, VALUE rary);
90
+ VALUE na_ndloop_store_rarray2(ndfunc_t *nf, VALUE nary, VALUE rary, VALUE opt);
91
+ VALUE na_ndloop_inspect(VALUE nary, na_text_func_t func, VALUE opt);
92
+ VALUE na_ndloop_with_index(ndfunc_t *nf, int argc, ...);
93
+
94
+ #define na_info_str nary_info_str
95
+ VALUE nary_info_str(VALUE);
96
+
97
+ #define na_test_reduce nary_test_reduce
98
+ bool nary_test_reduce(VALUE reduce, int dim);
99
+
100
+ void nary_step_array_index(VALUE self, size_t ary_size, size_t *plen, ssize_t *pbeg, ssize_t *pstep);
101
+ void nary_step_sequence(VALUE self, size_t *plen, double *pbeg, double *pstep);
102
+
103
+ // used in aref, aset
104
+ #define na_get_result_dimension nary_get_result_dimension
105
+ int nary_get_result_dimension(VALUE self, int argc, VALUE *argv, ssize_t stride, size_t *pos_idx);
106
+ #define na_aref_main nary_aref_main
107
+ VALUE nary_aref_main(int nidx, VALUE *idx, VALUE self, int keep_dim, int nd);
108
+
109
+ #include "ruby/version.h"
110
+
111
+ #if RUBY_API_VERSION_CODE == 20100 // 2.1.0
112
+ int rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *);
113
+ VALUE rb_extract_keywords(VALUE *orighash);
114
+ #endif
115
+
116
+
117
+ #endif /* ifndef INTERN_H */
@@ -0,0 +1,430 @@
1
+ /*
2
+ narray.h
3
+ Numerical Array Extension for Ruby
4
+ (C) Copyright 1999-2017 by Masahiro TANAKA
5
+ */
6
+ #ifndef NARRAY_H
7
+ #define NARRAY_H
8
+
9
+ #if defined(__cplusplus)
10
+ extern "C" {
11
+ #if 0
12
+ } /* satisfy cc-mode */
13
+ #endif
14
+ #endif
15
+
16
+ #define NARRAY_VERSION "0.9.1.1"
17
+ #define NARRAY_VERSION_CODE 911
18
+
19
+ #include <math.h>
20
+ #include "numo/compat.h"
21
+ #include "numo/template.h"
22
+ #include "numo/extconf.h"
23
+
24
+ #ifdef HAVE_STDBOOL_H
25
+ # include <stdbool.h>
26
+ #endif
27
+
28
+ #ifdef HAVE_STDINT_H
29
+ # include <stdint.h>
30
+ #endif
31
+
32
+ #ifdef HAVE_SYS_TYPES_H
33
+ # include <sys/types.h>
34
+ #endif
35
+
36
+ #ifndef HAVE_U_INT8_T
37
+ # ifdef HAVE_UINT8_T
38
+ typedef uint8_t u_int8_t;
39
+ # endif
40
+ #endif
41
+
42
+ #ifndef HAVE_U_INT16_T
43
+ # ifdef HAVE_UINT16_T
44
+ typedef uint16_t u_int16_t;
45
+ # endif
46
+ #endif
47
+
48
+ #ifndef HAVE_U_INT32_T
49
+ # ifdef HAVE_UINT32_T
50
+ typedef uint32_t u_int32_t;
51
+ # endif
52
+ #endif
53
+
54
+ #ifndef HAVE_U_INT64_T
55
+ # ifdef HAVE_UINT64_T
56
+ typedef uint64_t u_int64_t;
57
+ # endif
58
+ #endif
59
+
60
+ #define SZF PRI_SIZE_PREFIX // defined in ruby.h
61
+
62
+ #if SIZEOF_LONG==8
63
+ # define NUM2INT64(x) NUM2LONG(x)
64
+ # define INT642NUM(x) LONG2NUM(x)
65
+ # define NUM2UINT64(x) NUM2ULONG(x)
66
+ # define UINT642NUM(x) ULONG2NUM(x)
67
+ # ifndef PRId64
68
+ # define PRId64 "ld"
69
+ # endif
70
+ # ifndef PRIu64
71
+ # define PRIu64 "lu"
72
+ # endif
73
+ #elif SIZEOF_LONG_LONG==8
74
+ # define NUM2INT64(x) NUM2LL(x)
75
+ # define INT642NUM(x) LL2NUM(x)
76
+ # define NUM2UINT64(x) NUM2ULL(x)
77
+ # define UINT642NUM(x) ULL2NUM(x)
78
+ # ifndef PRId64
79
+ # define PRId64 "lld"
80
+ # endif
81
+ # ifndef PRIu64
82
+ # define PRIu64 "llu"
83
+ # endif
84
+ #endif
85
+
86
+ #if SIZEOF_LONG==4
87
+ # define NUM2INT32(x) NUM2LONG(x)
88
+ # define INT322NUM(x) LONG2NUM(x)
89
+ # define NUM2UINT32(x) NUM2ULONG(x)
90
+ # define UINT322NUM(x) ULONG2NUM(x)
91
+ # ifndef PRId32
92
+ # define PRId32 "ld"
93
+ # endif
94
+ # ifndef PRIu32
95
+ # define PRIu32 "lu"
96
+ # endif
97
+ #elif SIZEOF_INT==4
98
+ # define NUM2INT32(x) NUM2INT(x)
99
+ # define INT322NUM(x) INT2NUM(x)
100
+ # define NUM2UINT32(x) NUM2UINT(x)
101
+ # define UINT322NUM(x) UINT2NUM(x)
102
+ # ifndef PRId32
103
+ # define PRId32 "d"
104
+ # endif
105
+ # ifndef PRIu32
106
+ # define PRIu32 "u"
107
+ # endif
108
+ #endif
109
+
110
+ #ifndef HAVE_TYPE_BOOL
111
+ typedef int bool;
112
+ #endif
113
+ #ifndef FALSE /* in case these macros already exist */
114
+ # define FALSE 0 /* values of bool */
115
+ #endif
116
+ #ifndef TRUE
117
+ # define TRUE 1
118
+ #endif
119
+
120
+ typedef struct { float dat[2]; } scomplex;
121
+ typedef struct { double dat[2]; } dcomplex;
122
+ typedef int fortran_integer;
123
+
124
+ #define REAL(x) ((x).dat[0])
125
+ #define IMAG(x) ((x).dat[1])
126
+
127
+ extern int na_debug_flag;
128
+
129
+ #ifndef NARRAY_C
130
+ extern VALUE numo_cNArray;
131
+ extern VALUE rb_mNumo;
132
+ extern VALUE nary_eCastError;
133
+ extern VALUE nary_eShapeError;
134
+ extern VALUE nary_eOperationError;
135
+ extern VALUE nary_eDimensionError;
136
+ extern VALUE nary_eValueError;
137
+ extern const rb_data_type_t na_data_type;
138
+
139
+ //EXTERN const int na_sizeof[NA_NTYPES+1];
140
+ #endif
141
+
142
+ #define cNArray numo_cNArray
143
+ #define mNumo rb_mNumo
144
+ //#define na_upcast(x,y) numo_na_upcast(x,y)
145
+
146
+ /* global variables within this module */
147
+ extern VALUE numo_cBit;
148
+ extern VALUE numo_cDFloat;
149
+ extern VALUE numo_cSFloat;
150
+ extern VALUE numo_cDComplex;
151
+ extern VALUE numo_cSComplex;
152
+ extern VALUE numo_cInt64;
153
+ extern VALUE numo_cInt32;
154
+ extern VALUE numo_cInt16;
155
+ extern VALUE numo_cInt8;
156
+ extern VALUE numo_cUInt64;
157
+ extern VALUE numo_cUInt32;
158
+ extern VALUE numo_cUInt16;
159
+ extern VALUE numo_cUInt8;
160
+ extern VALUE numo_cRObject;
161
+ extern VALUE na_cStep;
162
+ #ifndef HAVE_RB_CCOMPLEX
163
+ extern VALUE rb_cComplex;
164
+ #endif
165
+
166
+ extern VALUE sym_reduce;
167
+ extern VALUE sym_option;
168
+ extern VALUE sym_loop_opt;
169
+ extern VALUE sym_init;
170
+
171
+ #define NARRAY_DATA_T 0x1
172
+ #define NARRAY_VIEW_T 0x2
173
+ #define NARRAY_FILEMAP_T 0x3
174
+
175
+ typedef struct RNArray {
176
+ unsigned char ndim; // # of dimensions
177
+ unsigned char type;
178
+ unsigned char flag[2]; // flags
179
+ unsigned short elmsz; // element size
180
+ size_t size; // # of total elements
181
+ size_t *shape; // # of elements for each dimension
182
+ VALUE reduce;
183
+ } narray_t;
184
+
185
+
186
+ typedef struct RNArrayData {
187
+ narray_t base;
188
+ char *ptr;
189
+ } narray_data_t;
190
+
191
+
192
+ typedef union {
193
+ ssize_t stride;
194
+ size_t *index;
195
+ } stridx_t;
196
+
197
+ typedef struct RNArrayView {
198
+ narray_t base;
199
+ VALUE data; // data object
200
+ size_t offset; // offset of start point from data pointer
201
+ // :in units of elm.unit_bits
202
+ // address_unit pointer_unit access_unit data_unit
203
+ // elm.step_unit = elm.bit_size / elm.access_unit
204
+ // elm.step_unit = elm.size_bits / elm.unit_bits
205
+ stridx_t *stridx; // stride or indices of data pointer for each dimension
206
+ } narray_view_t;
207
+
208
+
209
+ // filemap is unimplemented
210
+ typedef struct RNArrayFileMap {
211
+ narray_t base;
212
+ char *ptr;
213
+ #ifdef WIN32
214
+ HANDLE hFile;
215
+ HANDLE hMap;
216
+ #else // POSIX mmap
217
+ int prot;
218
+ int flag;
219
+ #endif
220
+ } narray_filemap_t;
221
+
222
+
223
+ // this will be revised in future.
224
+ typedef struct {
225
+ unsigned int element_bits;
226
+ unsigned int element_bytes;
227
+ unsigned int element_stride;
228
+ } narray_type_info_t;
229
+
230
+
231
+ static inline narray_t *
232
+ na_get_narray_t(VALUE obj)
233
+ {
234
+ narray_t *na;
235
+
236
+ Check_TypedStruct(obj,&na_data_type);
237
+ na = (narray_t*)DATA_PTR(obj);
238
+ return na;
239
+ }
240
+
241
+ static inline narray_t *
242
+ _na_get_narray_t(VALUE obj, unsigned char na_type)
243
+ {
244
+ narray_t *na;
245
+
246
+ Check_TypedStruct(obj,&na_data_type);
247
+ na = (narray_t*)DATA_PTR(obj);
248
+ if (na->type != na_type) {
249
+ rb_bug("unknown type 0x%x (0x%x given)", na_type, na->type);
250
+ }
251
+ return na;
252
+ }
253
+
254
+ #define na_get_narray_data_t(obj) (narray_data_t*)_na_get_narray_t(obj,NARRAY_DATA_T)
255
+ #define na_get_narray_view_t(obj) (narray_view_t*)_na_get_narray_t(obj,NARRAY_VIEW_T)
256
+ #define na_get_narray_filemap_t(obj) (narray_filemap_t*)_na_get_narray_t(obj,NARRAY_FILEMAP_T)
257
+
258
+ #define GetNArray(obj,var) TypedData_Get_Struct(obj, narray_t, &na_data_type, var)
259
+ #define GetNArrayView(obj,var) TypedData_Get_Struct(obj, narray_view_t, &na_data_type, var)
260
+ #define GetNArrayData(obj,var) TypedData_Get_Struct(obj, narray_data_t, &na_data_type, var)
261
+
262
+ #define SDX_IS_STRIDE(x) ((x).stride&0x1)
263
+ #define SDX_IS_INDEX(x) (!SDX_IS_STRIDE(x))
264
+ #define SDX_GET_STRIDE(x) ((x).stride>>1)
265
+ #define SDX_GET_INDEX(x) ((x).index)
266
+
267
+ #define SDX_SET_STRIDE(x,s) ((x).stride=((s)<<1)|0x1)
268
+ #define SDX_SET_INDEX(x,idx) ((x).index=idx)
269
+
270
+ #define RNARRAY(val) ((narray_t*)DATA_PTR(val))
271
+ #define RNARRAY_DATA(val) ((narray_data_t*)DATA_PTR(val))
272
+ #define RNARRAY_VIEW(val) ((narray_view_t*)DATA_PTR(val))
273
+ #define RNARRAY_FILEMAP(val) ((narray_filemap_t*)DATA_PTR(val))
274
+
275
+ #define RNARRAY_NDIM(val) (RNARRAY(val)->ndim)
276
+ #define RNARRAY_TYPE(val) (RNARRAY(val)->type)
277
+ #define RNARRAY_FLAG(val) (RNARRAY(val)->flag)
278
+ #define RNARRAY_SIZE(val) (RNARRAY(val)->size)
279
+ #define RNARRAY_SHAPE(val) (RNARRAY(val)->shape)
280
+ #define RNARRAY_REDUCE(val) (RNARRAY(val)->reduce)
281
+
282
+ #define RNARRAY_DATA_PTR(val) (RNARRAY_DATA(val)->ptr)
283
+ #define RNARRAY_VIEW_DATA(val) (RNARRAY_VIEW(val)->data)
284
+ #define RNARRAY_VIEW_OFFSET(val) (RNARRAY_VIEW(val)->offset)
285
+ #define RNARRAY_VIEW_STRIDX(val) (RNARRAY_VIEW(val)->stridx)
286
+
287
+ #define NA_NDIM(na) (((narray_t*)na)->ndim)
288
+ #define NA_TYPE(na) (((narray_t*)na)->type)
289
+ #define NA_SIZE(na) (((narray_t*)na)->size)
290
+ #define NA_SHAPE(na) (((narray_t*)na)->shape)
291
+ #define NA_REDUCE(na) (((narray_t*)na)->reduce)
292
+
293
+ #define NA_FLAG(obj) (na_get_narray_t(obj)->flag)
294
+ #define NA_FLAG0(obj) (NA_FLAG(obj)[0])
295
+ #define NA_FLAG1(obj) (NA_FLAG(obj)[1])
296
+
297
+ #define NA_DATA(na) ((narray_data_t*)(na))
298
+ #define NA_VIEW(na) ((narray_view_t*)(na))
299
+ #define NA_DATA_PTR(na) (NA_DATA(na)->ptr)
300
+ #define NA_VIEW_DATA(na) (NA_VIEW(na)->data)
301
+ #define NA_VIEW_OFFSET(na) (NA_VIEW(na)->offset)
302
+ #define NA_VIEW_STRIDX(na) (NA_VIEW(na)->stridx)
303
+
304
+ #define NA_IS_INDEX_AT(na,i) (SDX_IS_INDEX(NA_VIEW_STRIDX(na)[i]))
305
+ #define NA_IS_STRIDE_AT(na,i) (SDX_IS_STRIDE(NA_VIEW_STRIDX(na)[i]))
306
+ #define NA_INDEX_AT(na,i) (SDX_GET_INDEX(NA_VIEW_STRIDX(na)[i]))
307
+ #define NA_STRIDE_AT(na,i) (SDX_GET_STRIDE(NA_VIEW_STRIDX(na)[i]))
308
+
309
+ #define NA_FILEMAP_PTR(na) (((narray_filemap_t*)na)->ptr)
310
+
311
+
312
+ #define NA_FL0_TEST(x,f) (NA_FLAG0(x)&(f))
313
+ #define NA_FL1_TEST(x,f) (NA_FLAG1(x)&(f))
314
+
315
+ #define NA_FL0_SET(x,f) do {NA_FLAG0(x) |= (f);} while(0)
316
+ #define NA_FL1_SET(x,f) do {NA_FLAG1(x) |= (f);} while(0)
317
+
318
+ #define NA_FL0_UNSET(x,f) do {NA_FLAG0(x) &= ~(f);} while(0)
319
+ #define NA_FL1_UNSET(x,f) do {NA_FLAG1(x) &= ~(f);} while(0)
320
+
321
+ #define NA_FL0_REVERSE(x,f) do {NA_FLAG0(x) ^= (f);} while(0)
322
+ #define NA_FL1_REVERSE(x,f) do {NA_FLAG1(x) ^= (f);} while(0)
323
+
324
+
325
+ /* FLAGS
326
+ - row-major / column-major
327
+ - Overwrite or not
328
+ - byteswapp
329
+ - Extensible?
330
+ - matrix or not
331
+ */
332
+
333
+ #define NA_FL0_BIG_ENDIAN (0x1<<0)
334
+ #define NA_FL0_COLUMN_MAJOR (0x1<<1)
335
+ #define NA_FL1_LOCK (0x1<<0)
336
+ #define NA_FL1_INPLACE (0x1<<1)
337
+
338
+ #define TEST_COLUMN_MAJOR(x) NA_FL0_TEST(x,NA_FL0_COLUMN_MAJOR)
339
+ #define SET_COLUMN_MAJOR(x) NA_FL0_SET(x,NA_FL0_COLUMN_MAJOR)
340
+ #define UNSET_COLUMN_MAJOR(x) NA_FL0_UNSET(x,NA_FL0_COLUMN_MAJOR)
341
+
342
+ #define TEST_ROW_MAJOR(x) (!TEST_COLUMN_MAJOR(x))
343
+ #define SET_ROW_MAJOR(x) UNSET_COLUMN_MAJOR(x)
344
+ #define UNSET_ROW_MAJOR(x) SET_COLUMN_MAJOR(x)
345
+
346
+ #define TEST_BIG_ENDIAN(x) NA_FL0_TEST(x,NA_FL0_BIG_ENDIAN)
347
+ #define SET_BIG_ENDIAN(x) NA_FL0_SET(x,NA_FL0_BIG_ENDIAN)
348
+ #define UNSET_BIG_ENDIAN(x) NA_FL0_UNSET(x,NA_FL0_BIG_ENDIAN)
349
+
350
+ #define TEST_LITTLE_ENDIAN(x) (!TEST_BIG_ENDIAN(x))
351
+ #define SET_LITTLE_ENDIAN(x) UNSET_BIG_ENDIAN(x)
352
+ #define UNSET_LITTLE_ENDIAN(x) SET_BIG_ENDIAN(x)
353
+
354
+ #define REVERSE_ENDIAN(x) NA_FL0_REVERSE((x),NA_FL0_BIG_ENDIAN)
355
+
356
+ #define TEST_LOCK(x) NA_FL1_TEST(x,NA_FL1_LOCK)
357
+ #define SET_LOCK(x) NA_FL1_SET(x,NA_FL1_LOCK)
358
+ #define UNSET_LOCK(x) NA_FL1_UNSET(x,NA_FL1_LOCK)
359
+
360
+ #define TEST_INPLACE(x) NA_FL1_TEST(x,NA_FL1_INPLACE)
361
+ #define SET_INPLACE(x) NA_FL1_SET(x,NA_FL1_INPLACE)
362
+ #define UNSET_INPLACE(x) NA_FL1_UNSET(x,NA_FL1_INPLACE)
363
+
364
+ #ifdef DYNAMIC_ENDIAN
365
+ // not supported
366
+ #else
367
+ #ifdef WORDS_BIGENDIAN
368
+ #define TEST_HOST_ORDER(x) TEST_BIG_ENDIAN(x)
369
+ #define SET_HOST_ORDER(x) SET_BIG_ENDIAN(x)
370
+ #define UNSET_HOST_ORDER(x) UNSET_BIG_ENDIAN(x)
371
+ #define TEST_BYTE_SWAPPED(x) TEST_LITTLE_ENDIAN(x)
372
+ #define SET_BYTE_SWAPPED(x) SET_LITTLE_ENDIAN(x)
373
+ #define UNSET_BYTE_SWAPPED(x) UNSET_LITTLE_ENDIAN(x)
374
+ #define NA_FL0_INIT NA_FL0_BIG_ENDIAN
375
+ #else // LITTLE ENDIAN
376
+ #define TEST_HOST_ORDER(x) TEST_LITTLE_ENDIAN(x)
377
+ #define SET_HOST_ORDER(x) SET_LITTLE_ENDIAN(x)
378
+ #define UNSET_HOST_ORDER(x) UNSET_LITTLE_ENDIAN(x)
379
+ #define TEST_BYTE_SWAPPED(x) TEST_BIG_ENDIAN(x)
380
+ #define SET_BYTE_SWAPPED(x) SET_BIG_ENDIAN(x)
381
+ #define UNSET_BYTE_SWAPPED(x) UNSET_BIG_ENDIAN(x)
382
+ #define NA_FL0_INIT 0
383
+ #endif
384
+ #endif
385
+ #define NA_FL1_INIT 0
386
+
387
+
388
+ #define IsNArray(obj) (rb_obj_is_kind_of(obj,cNArray)==Qtrue)
389
+
390
+ #define DEBUG_PRINT(v) puts(StringValueCStr(rb_funcall(v,rb_intern("inspect"),0)))
391
+
392
+ #define NA_IsNArray(obj) \
393
+ (rb_obj_is_kind_of(obj,cNArray)==Qtrue)
394
+ #define NA_IsArray(obj) \
395
+ (TYPE(obj)==T_ARRAY || rb_obj_is_kind_of(obj,cNArray)==Qtrue)
396
+
397
+ #define NUM2REAL(v) NUM2DBL( rb_funcall((v),na_id_real,0) )
398
+ #define NUM2IMAG(v) NUM2DBL( rb_funcall((v),na_id_imag,0) )
399
+
400
+ #define NA_MAX_DIMENSION (int)(sizeof(VALUE)*8-2)
401
+ #define NA_MAX_ELMSZ 65535
402
+
403
+ typedef unsigned int BIT_DIGIT;
404
+ //#define BYTE_BIT_DIGIT sizeof(BIT_DIGIT)
405
+ #define NB (sizeof(BIT_DIGIT)*8)
406
+ #define BALL (~(BIT_DIGIT)0)
407
+ #define SLB(n) (((n)==NB)?~(BIT_DIGIT)0:(~(~(BIT_DIGIT)0<<(n))))
408
+
409
+ #define ELEMENT_BIT_SIZE "ELEMENT_BIT_SIZE"
410
+ #define ELEMENT_BYTE_SIZE "ELEMENT_BYTE_SIZE"
411
+ #define CONTIGUOUS_STRIDE "CONTIGUOUS_STRIDE"
412
+
413
+
414
+ #ifdef RUBY_INTEGER_UNIFICATION
415
+ #define IS_INTEGER_CLASS(c) ((c)==rb_cInteger)
416
+ #else
417
+ #define IS_INTEGER_CLASS(c) ((c)==rb_cFixnum||(c)==rb_cBignum)
418
+ #endif
419
+
420
+ #include "numo/ndloop.h"
421
+ #include "numo/intern.h"
422
+
423
+ #if defined(__cplusplus)
424
+ #if 0
425
+ { /* satisfy cc-mode */
426
+ #endif
427
+ } /* extern "C" { */
428
+ #endif
429
+
430
+ #endif /* ifndef NARRAY_H */