numo-linalg 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (85) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +4 -0
  3. data/README.md +80 -0
  4. data/Rakefile +18 -0
  5. data/ext/numo/linalg/blas/blas.c +352 -0
  6. data/ext/numo/linalg/blas/cblas.h +575 -0
  7. data/ext/numo/linalg/blas/cblas_t.h +563 -0
  8. data/ext/numo/linalg/blas/depend.erb +23 -0
  9. data/ext/numo/linalg/blas/extconf.rb +67 -0
  10. data/ext/numo/linalg/blas/gen/cogen.rb +72 -0
  11. data/ext/numo/linalg/blas/gen/decl.rb +203 -0
  12. data/ext/numo/linalg/blas/gen/desc.rb +8138 -0
  13. data/ext/numo/linalg/blas/gen/erbpp2.rb +339 -0
  14. data/ext/numo/linalg/blas/gen/replace_cblas_h.rb +27 -0
  15. data/ext/numo/linalg/blas/gen/spec.rb +93 -0
  16. data/ext/numo/linalg/blas/numo_blas.h +41 -0
  17. data/ext/numo/linalg/blas/tmpl/axpy.c +75 -0
  18. data/ext/numo/linalg/blas/tmpl/copy.c +57 -0
  19. data/ext/numo/linalg/blas/tmpl/def_c.c +3 -0
  20. data/ext/numo/linalg/blas/tmpl/def_d.c +3 -0
  21. data/ext/numo/linalg/blas/tmpl/def_s.c +3 -0
  22. data/ext/numo/linalg/blas/tmpl/def_z.c +3 -0
  23. data/ext/numo/linalg/blas/tmpl/dot.c +68 -0
  24. data/ext/numo/linalg/blas/tmpl/ger.c +114 -0
  25. data/ext/numo/linalg/blas/tmpl/init_class.c +20 -0
  26. data/ext/numo/linalg/blas/tmpl/init_module.c +12 -0
  27. data/ext/numo/linalg/blas/tmpl/lib.c +40 -0
  28. data/ext/numo/linalg/blas/tmpl/mm.c +214 -0
  29. data/ext/numo/linalg/blas/tmpl/module.c +9 -0
  30. data/ext/numo/linalg/blas/tmpl/mv.c +194 -0
  31. data/ext/numo/linalg/blas/tmpl/nrm2.c +79 -0
  32. data/ext/numo/linalg/blas/tmpl/rot.c +65 -0
  33. data/ext/numo/linalg/blas/tmpl/rotm.c +82 -0
  34. data/ext/numo/linalg/blas/tmpl/scal.c +69 -0
  35. data/ext/numo/linalg/blas/tmpl/sdsdot.c +77 -0
  36. data/ext/numo/linalg/blas/tmpl/set_prefix.c +16 -0
  37. data/ext/numo/linalg/blas/tmpl/swap.c +57 -0
  38. data/ext/numo/linalg/blas/tmpl/syr.c +102 -0
  39. data/ext/numo/linalg/blas/tmpl/syr2.c +110 -0
  40. data/ext/numo/linalg/blas/tmpl/syr2k.c +129 -0
  41. data/ext/numo/linalg/blas/tmpl/syrk.c +132 -0
  42. data/ext/numo/linalg/lapack/depend.erb +23 -0
  43. data/ext/numo/linalg/lapack/extconf.rb +45 -0
  44. data/ext/numo/linalg/lapack/gen/cogen.rb +74 -0
  45. data/ext/numo/linalg/lapack/gen/desc.rb +151278 -0
  46. data/ext/numo/linalg/lapack/gen/replace_lapacke_h.rb +32 -0
  47. data/ext/numo/linalg/lapack/gen/spec.rb +104 -0
  48. data/ext/numo/linalg/lapack/lapack.c +387 -0
  49. data/ext/numo/linalg/lapack/lapacke.h +16425 -0
  50. data/ext/numo/linalg/lapack/lapacke_config.h +119 -0
  51. data/ext/numo/linalg/lapack/lapacke_mangling.h +17 -0
  52. data/ext/numo/linalg/lapack/lapacke_t.h +10550 -0
  53. data/ext/numo/linalg/lapack/numo_lapack.h +42 -0
  54. data/ext/numo/linalg/lapack/tmpl/def_c.c +3 -0
  55. data/ext/numo/linalg/lapack/tmpl/def_d.c +7 -0
  56. data/ext/numo/linalg/lapack/tmpl/def_s.c +7 -0
  57. data/ext/numo/linalg/lapack/tmpl/def_z.c +3 -0
  58. data/ext/numo/linalg/lapack/tmpl/fact.c +179 -0
  59. data/ext/numo/linalg/lapack/tmpl/geev.c +123 -0
  60. data/ext/numo/linalg/lapack/tmpl/gels.c +232 -0
  61. data/ext/numo/linalg/lapack/tmpl/gesv.c +149 -0
  62. data/ext/numo/linalg/lapack/tmpl/gesvd.c +189 -0
  63. data/ext/numo/linalg/lapack/tmpl/ggev.c +138 -0
  64. data/ext/numo/linalg/lapack/tmpl/gqr.c +121 -0
  65. data/ext/numo/linalg/lapack/tmpl/init_class.c +20 -0
  66. data/ext/numo/linalg/lapack/tmpl/init_module.c +12 -0
  67. data/ext/numo/linalg/lapack/tmpl/lange.c +79 -0
  68. data/ext/numo/linalg/lapack/tmpl/lib.c +40 -0
  69. data/ext/numo/linalg/lapack/tmpl/module.c +9 -0
  70. data/ext/numo/linalg/lapack/tmpl/syev.c +91 -0
  71. data/ext/numo/linalg/lapack/tmpl/sygv.c +104 -0
  72. data/ext/numo/linalg/lapack/tmpl/trf.c +276 -0
  73. data/ext/numo/linalg/numo_linalg.h +115 -0
  74. data/lib/numo/linalg.rb +3 -0
  75. data/lib/numo/linalg/function.rb +1008 -0
  76. data/lib/numo/linalg/linalg.rb +7 -0
  77. data/lib/numo/linalg/loader.rb +174 -0
  78. data/lib/numo/linalg/use/atlas.rb +3 -0
  79. data/lib/numo/linalg/use/lapack.rb +3 -0
  80. data/lib/numo/linalg/use/mkl.rb +3 -0
  81. data/lib/numo/linalg/use/openblas.rb +3 -0
  82. data/lib/numo/linalg/version.rb +5 -0
  83. data/numo-linalg.gemspec +26 -0
  84. data/spec/lapack_spec.rb +13 -0
  85. metadata +172 -0
@@ -0,0 +1,41 @@
1
+ #include "cblas.h"
2
+ #include "cblas_t.h"
3
+ #include "../numo_linalg.h"
4
+
5
+ typedef int blasint;
6
+
7
+ #define option_value numo_cblas_option_value
8
+ extern VALUE numo_cblas_option_value(VALUE value, VALUE default_value);
9
+
10
+ #define option_order numo_cblas_option_order
11
+ extern enum CBLAS_ORDER numo_cblas_option_order(VALUE order);
12
+
13
+ #define option_trans numo_cblas_option_trans
14
+ extern enum CBLAS_TRANSPOSE numo_cblas_option_trans(VALUE trans);
15
+
16
+ #define option_uplo numo_cblas_option_uplo
17
+ extern enum CBLAS_UPLO numo_cblas_option_uplo(VALUE uplo);
18
+
19
+ #define option_diag numo_cblas_option_diag
20
+ extern enum CBLAS_DIAG numo_cblas_option_diag(VALUE diag);
21
+
22
+ #define option_side numo_cblas_option_side
23
+ extern enum CBLAS_SIDE numo_cblas_option_side(VALUE side);
24
+
25
+ #define check_func numo_cblas_check_func
26
+ extern void numo_cblas_check_func(void **func, const char *name);
27
+
28
+ #define SWAP_IFCOL(order,a,b,tmp) \
29
+ { if ((order)==CblasColMajor) {(tmp)=(a);(a)=(b);(b)=(tmp);} }
30
+
31
+ #define SWAP_IFROW(order,a,b,tmp) \
32
+ { if ((order)==CblasRowMajor) {(tmp)=(a);(a)=(b);(b)=(tmp);} }
33
+
34
+ #define SWAP_IFCOLTR(order,trans,a,b,tmp) \
35
+ { if (((order)==CblasRowMajor && (trans)!=CblasNoTrans) || \
36
+ ((order)!=CblasRowMajor && (trans)==CblasNoTrans)) \
37
+ {(tmp)=(a);(a)=(b);(b)=(tmp);} \
38
+ }
39
+
40
+ #define CHECK_FUNC(fptr, fname) \
41
+ { if ((fptr)==0) { check_func((void*)(&(fptr)),fname); } }
@@ -0,0 +1,75 @@
1
+ #define args_t <%=name%>_args_t
2
+
3
+ typedef struct {
4
+ dtype alpha;
5
+ } args_t;
6
+
7
+ #define func_p <%=func_name%>_p
8
+ static <%=func_name%>_t func_p = 0;
9
+
10
+ static void
11
+ <%=c_iter%>(na_loop_t *const lp)
12
+ {
13
+ char *p1, *p2;
14
+ size_t n;
15
+ ssize_t s1, s2;
16
+ dtype *g;
17
+
18
+ INIT_COUNTER(lp,n);
19
+ INIT_PTR(lp,0,p1,s1);
20
+ INIT_PTR(lp,1,p2,s2);
21
+ g = (dtype*)(lp->opt_ptr);
22
+
23
+ (*func_p)(n, DP(*g), (dtype*)p1, s1/sizeof(dtype),
24
+ (dtype*)p2, s2/sizeof(dtype));
25
+ }
26
+
27
+ /*<%
28
+ params = [
29
+ vec("x"),
30
+ vec("y",:inplace),
31
+ opt("alpha"),
32
+ ].select{|x| x}.join("\n ")
33
+ %>
34
+ @overload <%=name%>( x, y, [alpha:1] )
35
+ <%=params%>
36
+ @return [<%=class_name%>] y = alpha * x + y
37
+
38
+ <%=description%>
39
+
40
+ */
41
+ static VALUE
42
+ <%=c_func(-1)%>(int argc, VALUE const argv[], VALUE UNUSED(mod))
43
+ {
44
+ VALUE x, y, alpha;
45
+ narray_t *na1, *na2;
46
+ ndfunc_arg_in_t ain[2] = {{cT,0},{OVERWRITE,0}};
47
+ ndfunc_t ndf = {<%=c_iter%>, STRIDE_LOOP, 2, 0, ain, 0};
48
+
49
+ dtype g;
50
+ VALUE kw_hash = Qnil;
51
+ ID kw_table[1] = {id_alpha};
52
+ VALUE opts[1] = {Qundef};
53
+
54
+ CHECK_FUNC(func_p,"<%=func_name%>");
55
+
56
+ rb_scan_args(argc, argv, "2:", &x, &y, &kw_hash);
57
+ rb_get_kwargs(kw_hash, kw_table, 0, 1, opts);
58
+ alpha = option_value(opts[0],Qnil);
59
+ g = RTEST(alpha) ? m_num_to_data(alpha) : m_one;
60
+
61
+ COPY_OR_CAST_TO(y,cT);
62
+ GetNArray(x,na1);
63
+ GetNArray(y,na2);
64
+ CHECK_DIM_GE(na1,1);
65
+ CHECK_DIM_GE(na2,1);
66
+ CHECK_NON_EMPTY(na1);
67
+ CHECK_NON_EMPTY(na2);
68
+ CHECK_SAME_SHAPE(na1,na2);
69
+
70
+ na_ndloop3(&ndf, &g, 2, x, y);
71
+ return y;
72
+ }
73
+
74
+ #undef func_p
75
+ #undef args_t
@@ -0,0 +1,57 @@
1
+ #define func_p <%=name%>_p
2
+
3
+ static <%=func_name%>_t func_p = 0;
4
+
5
+ static void
6
+ <%=c_iter%>(na_loop_t *const lp)
7
+ {
8
+ char *p1, *p2;
9
+ size_t n;
10
+ ssize_t s1, s2;
11
+
12
+ INIT_COUNTER(lp,n);
13
+ INIT_PTR(lp,0,p1,s1);
14
+ INIT_PTR(lp,1,p2,s2);
15
+
16
+ (*func_p)(n, (dtype*)p1, s1/sizeof(dtype),
17
+ (dtype*)p2, s2/sizeof(dtype));
18
+ }
19
+
20
+ /*<%
21
+ params = [
22
+ vec("x"),
23
+ vec("y"),
24
+ ].select{|x| x}.join("\n ")
25
+ %>
26
+ @overload <%=name%>( x, y )
27
+ <%=params%>
28
+ @return [nil]
29
+
30
+ <%=description%>
31
+
32
+ */
33
+ static VALUE
34
+ <%=c_func(2)%>(VALUE UNUSED(mod), VALUE x, VALUE y)
35
+ {
36
+ narray_t *na1, *na2;
37
+ ndfunc_arg_in_t ain[2] = {{cT,0},{OVERWRITE,0}};
38
+ ndfunc_t ndf = {<%=c_iter%>, STRIDE_LOOP, 2,0, ain,0};
39
+
40
+ CHECK_FUNC(func_p,"<%=func_name%>");
41
+
42
+ CHECK_NARRAY_TYPE(x,cT);
43
+ CHECK_NARRAY_TYPE(y,cT);
44
+ GetNArray(x,na1);
45
+ GetNArray(y,na2);
46
+ CHECK_DIM_GE(na1,1);
47
+ CHECK_DIM_GE(na2,1);
48
+ CHECK_NON_EMPTY(na1);
49
+ CHECK_NON_EMPTY(na2);
50
+ CHECK_SAME_SHAPE(na1,na2);
51
+
52
+ na_ndloop(&ndf, 2, x, y);
53
+
54
+ return Qnil;
55
+ }
56
+
57
+ #undef func_p
@@ -0,0 +1,3 @@
1
+ #include "numo/types/scomplex.h"
2
+
3
+ #define DP(a) (&(a))
@@ -0,0 +1,3 @@
1
+ #include "numo/types/dfloat.h"
2
+
3
+ #define DP(a) (a)
@@ -0,0 +1,3 @@
1
+ #include "numo/types/sfloat.h"
2
+
3
+ #define DP(a) (a)
@@ -0,0 +1,3 @@
1
+ #include "numo/types/dcomplex.h"
2
+
3
+ #define DP(a) (&(a))
@@ -0,0 +1,68 @@
1
+ #define func_p <%=func_name%>_p
2
+
3
+ static <%=func_name%>_t func_p = 0;
4
+
5
+ #undef result_dtype
6
+ #define result_dtype <%=result_dtype%>
7
+
8
+ static void
9
+ <%=c_iter%>(na_loop_t *const lp)
10
+ {
11
+ char *p1, *p2, *p3;
12
+ size_t n;
13
+ ssize_t s1, s2;
14
+
15
+ INIT_PTR(lp,0,p1,s1);
16
+ INIT_PTR(lp,1,p2,s2);
17
+ p3 = NDL_PTR(lp,2);
18
+ n = NDL_SHAPE(lp,0)[0];
19
+
20
+ <% if /[cz]/ =~ blas_char %>
21
+ (*func_p)(n, (dtype*)p1, s1/sizeof(dtype),
22
+ (dtype*)p2, s2/sizeof(dtype), (result_dtype*)p3);
23
+ <% else %>
24
+ *(result_dtype*)p3 = (*func_p)(n, (dtype*)p1, s1/sizeof(dtype),
25
+ (dtype*)p2, s2/sizeof(dtype));
26
+ <% end %>
27
+ }
28
+
29
+ /*<%
30
+ params = [
31
+ vec("x"),
32
+ vec("y"),
33
+ ].select{|x| x}.join("\n ")
34
+ %>
35
+ @overload <%=name%>( x, y )
36
+ <%=params%>
37
+ @return [<%=class_name%>] op(x) dot y
38
+
39
+ <%=description%>
40
+
41
+ */
42
+ static VALUE
43
+ <%=c_func(2)%>(VALUE mod, VALUE x, VALUE y)
44
+ {
45
+ VALUE ans;
46
+ narray_t *na1, *na2;
47
+ size_t nx, ny, shape[1]={1};
48
+ ndfunc_arg_in_t ain[2] = {{cT,1},{cT,1}};
49
+ ndfunc_arg_out_t aout[1] = {{<%=result_class%>,0,shape}};
50
+ ndfunc_t ndf = {<%=c_iter%>, NDF_EXTRACT, 2,1, ain,aout};
51
+
52
+ CHECK_FUNC(func_p,"<%=func_name%>");
53
+
54
+ GetNArray(x,na1);
55
+ GetNArray(y,na2);
56
+ CHECK_DIM_GE(na1,1);
57
+ CHECK_DIM_GE(na2,1);
58
+ CHECK_NON_EMPTY(na1);
59
+ CHECK_NON_EMPTY(na2);
60
+ nx = COL_SIZE(na1);
61
+ ny = COL_SIZE(na2);
62
+ CHECK_SIZE_EQ(nx,ny);
63
+
64
+ ans = na_ndloop(&ndf, 2, x, y);
65
+
66
+ return ans;
67
+ }
68
+ #undef func_p
@@ -0,0 +1,114 @@
1
+ #define args_t <%=name%>_args_t
2
+
3
+ typedef struct {
4
+ enum CBLAS_ORDER order;
5
+ dtype alpha;
6
+ blasint m, n;
7
+ } args_t;
8
+
9
+ #define func_p <%=func_name%>_p
10
+
11
+ static <%=func_name%>_t func_p = 0;
12
+
13
+ static void
14
+ <%=c_iter%>(na_loop_t *const lp)
15
+ {
16
+ dtype *a;
17
+ char *p1, *p2;
18
+ ssize_t s1, s2;
19
+ args_t *g;
20
+ int lda;
21
+
22
+ INIT_PTR(lp,0,p1,s1);
23
+ INIT_PTR(lp,1,p2,s2);
24
+ a = (dtype*)NDL_PTR(lp,2);
25
+ g = (args_t*)(lp->opt_ptr);
26
+
27
+ lda = NDL_STEP(lp,2) / sizeof(dtype);
28
+
29
+ (*func_p)(g->order, g->m, g->n,
30
+ DP(g->alpha), (dtype*)p1, s1/sizeof(dtype),
31
+ (dtype*)p2, s2/sizeof(dtype), a, lda);
32
+ }
33
+
34
+ /*<%
35
+ params = [
36
+ vec("x"),
37
+ vec("y"),
38
+ mat("a","m-by-n symmetric matrix","optional",:inplace),
39
+ opt("alpha"),
40
+ opt("order"),
41
+ ].select{|x| x}.join("\n ")
42
+ %>
43
+ @overload <%=name%>( x, y, [a, alpha:1, order:'R'] )
44
+ <%=params%>
45
+ @return [<%=class_name%>] returns a.
46
+
47
+ <%=description%>
48
+
49
+ */
50
+ static VALUE
51
+ <%=c_func(-1)%>(int argc, VALUE const argv[], VALUE UNUSED(mod))
52
+ {
53
+ VALUE ans;
54
+ VALUE x, y, a=Qnil, alpha;
55
+ narray_t *na1, *na2;
56
+ blasint mx, ny, tmp;
57
+ size_t shape[2];
58
+ ndfunc_arg_in_t ain[4] = {{cT,1},{cT,1},{OVERWRITE,2},{sym_init,0}};
59
+ ndfunc_arg_out_t aout[1] = {{cT,2,shape}};
60
+ ndfunc_t ndf = {<%=c_iter%>, NO_LOOP, 3, 0, ain, aout};
61
+
62
+ args_t g;
63
+ VALUE kw_hash = Qnil;
64
+ ID kw_table[2] = {id_alpha,id_order};
65
+ VALUE opts[2] = {Qundef};
66
+
67
+ CHECK_FUNC(func_p,"<%=func_name%>");
68
+
69
+ rb_scan_args(argc, argv, "21:", &x, &y, &a, &kw_hash);
70
+ rb_get_kwargs(kw_hash, kw_table, 0, 2, opts);
71
+ alpha = option_value(opts[0],Qnil);
72
+ g.alpha = RTEST(alpha) ? m_num_to_data(alpha) : m_one;
73
+ g.order = option_order(opts[1]);
74
+
75
+ GetNArray(x,na1);
76
+ GetNArray(y,na2);
77
+ CHECK_DIM_GE(na1,1);
78
+ CHECK_DIM_GE(na2,1);
79
+ mx = COL_SIZE(na1); // m
80
+ ny = COL_SIZE(na2); // n
81
+ g.m = mx;
82
+ g.n = ny;
83
+
84
+ SWAP_IFCOL(g.order, mx,ny, tmp);
85
+
86
+ if (a == Qnil) { // c is not given.
87
+ ndf.nout = 1;
88
+ ain[2] = ain[3];
89
+ a = INT2FIX(0);
90
+ shape[0] = mx;
91
+ shape[1] = ny;
92
+ } else {
93
+ narray_t *na3;
94
+ blasint ma, na;
95
+ COPY_OR_CAST_TO(a,cT);
96
+ GetNArray(a,na3);
97
+ CHECK_DIM_GE(na3,2);
98
+ ma = ROW_SIZE(na3); // m
99
+ na = COL_SIZE(na3); // n (lda)
100
+ CHECK_SIZE_EQ(ma,mx);
101
+ CHECK_SIZE_EQ(na,ny);
102
+ }
103
+
104
+ ans = na_ndloop3(&ndf, &g, 3, x, y, a);
105
+
106
+ if (ndf.nout = 1) { // a is not given.
107
+ return ans;
108
+ } else {
109
+ return a;
110
+ }
111
+ }
112
+
113
+ #undef func_p
114
+ #undef args_t
@@ -0,0 +1,20 @@
1
+ /*
2
+ Document-class: <%= full_class_name %>
3
+ <%= description %>
4
+ */
5
+ cT = rb_define_class_under(<%=ns_var%>, "<%=class_name%>", cNArray);
6
+
7
+ <% for x in class_alias %>
8
+ // alias of <%=class_name%>
9
+ rb_define_const(<%=ns_var%>, "<%=x%>", <%=type_var%>);
10
+ <% end %>
11
+
12
+ hCast = rb_hash_new();
13
+ rb_define_const(cT, "UPCAST", hCast);
14
+ rb_hash_aset(hCast, rb_cArray, cT);
15
+ <% for x in upcast %>
16
+ <%= x %><% end %>
17
+
18
+ <% @children.each do |m| %>
19
+ <%= m.init_def %><% end %>
20
+ rb_define_singleton_method(cT, "[]", <%=find("cast").c_func%>, -2);
@@ -0,0 +1,12 @@
1
+ /*
2
+ Document-module: <%= full_module_name %>
3
+ <%= description %>
4
+ */
5
+ <% if module_var != ns_var %>
6
+ <%=module_var%> = rb_define_module_under(<%=ns_var%>, "<%=module_name%>");
7
+ <% end %>
8
+ <% @children.each do |m| %>
9
+ <%= m.init_def %><% end %>
10
+
11
+ // how to do this?
12
+ //rb_extend_object(cT, mTM);
@@ -0,0 +1,40 @@
1
+ /*
2
+ BLAS wrapper for Ruby/Numo
3
+ (C) Copyright 2017 by Masahiro TANAKA
4
+
5
+ This program is free software.
6
+ NO WARRANTY.
7
+ */
8
+
9
+ #include <assert.h>
10
+ #include <ruby.h>
11
+ #include "numo/narray.h"
12
+ #include "numo/template.h"
13
+ #include "numo_blas.h"
14
+
15
+ <% id_decl.each do |x| %>
16
+ <%= x %>
17
+ <% end %>
18
+
19
+ <% include_files.each do |f| %>
20
+ #include <<%=f%>>
21
+ <% end %>
22
+
23
+ <% children.each do |c|%>
24
+ <%= c.result+"\n\n" %>
25
+ <% end %>
26
+
27
+ void
28
+ Init_<%=lib_name%>(void)
29
+ {
30
+ VALUE <%=ns_var%>;
31
+
32
+ <%=ns_var%> = rb_define_module("Numo");
33
+
34
+ <% id_assign.each do |x| %>
35
+ <%= x %><% end %>
36
+
37
+ <% children.each do |c| %>
38
+ <%= c.init_def %>
39
+ <% end %>
40
+ }