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,339 @@
1
+ require "erb"
2
+
3
+ class ErbPP
4
+
5
+ def initialize(parent=nil, erb_base=nil, **opts, &block)
6
+ @parent = parent
7
+ @children = []
8
+ @opts = opts
9
+ set erb_base: erb_base if erb_base
10
+ @parent.add_child(self) if @parent
11
+ instance_eval(&block) if block
12
+ end
13
+
14
+ attr_reader :children
15
+ attr_accessor :parent
16
+
17
+ def add_child(child)
18
+ @children.push(child)
19
+ end
20
+
21
+ def set(**opts)
22
+ @opts.merge!(opts)
23
+ end
24
+
25
+ def get(key, *args, &block)
26
+ if respond_to?(key)
27
+ return send(key, *args, &block)
28
+ end
29
+ if args.empty? && block.nil? && @opts.has_key?(key)
30
+ return @opts[key]
31
+ end
32
+ if @parent
33
+ return @parent.get(key, *args, &block)
34
+ end
35
+ nil
36
+ end
37
+
38
+ def description
39
+ if s = @opts[:description] || @opts[:desc]
40
+ s.gsub(/\@\{/,"[").gsub(/\@\}/,"]")
41
+ end
42
+ end
43
+
44
+ alias desc description
45
+
46
+ alias method_missing_alias method_missing
47
+
48
+ def method_missing(_meth_id, *args, &block)
49
+ if args.empty?
50
+ #$stderr.puts _meth_id.inspect
51
+ v = get(_meth_id, *args, &block)
52
+ return v if !v.nil?
53
+ end
54
+ method_missing_alias(_meth_id, *args, &block)
55
+ end
56
+
57
+ # ERB Loader
58
+
59
+ def load_erb(base_name)
60
+ safe_level = nil
61
+ trim_mode = '%<>'
62
+ file = base_name + get(:erb_suffix)
63
+ dirs = get(:erb_dir)
64
+ dirs = [dirs] if !dirs.kind_of?(Array)
65
+ dirs.each do |x|
66
+ Dir.glob(x).each do |dir|
67
+ path = File.join(dir,file)
68
+ if File.exist?(path)
69
+ erb = ERB.new(File.read(path), safe_level, trim_mode)
70
+ erb.filename = path
71
+ return erb
72
+ end
73
+ end
74
+ end
75
+ raise "file not found: #{file.inspect} in #{dirs.inspect}"
76
+ end
77
+
78
+ def run
79
+ if base = @opts[:erb_base]
80
+ load_erb(base).run(binding)
81
+ end
82
+ end
83
+
84
+ def result
85
+ if base = @opts[:erb_base]
86
+ load_erb(base).result(binding)
87
+ end
88
+ end
89
+
90
+ def write(output)
91
+ File.open(output,"wt") do |f|
92
+ f.print(result)
93
+ end
94
+ end
95
+
96
+ def init_def
97
+ end
98
+
99
+ def find_tmpl(name)
100
+ @parent.children.find{|x| x.name == name }
101
+ end
102
+
103
+ def find(name)
104
+ children.find{|x| x.name == name }
105
+ end
106
+ end
107
+
108
+
109
+ class DefLib < ErbPP
110
+ def initialize(parent=nil, **opts, &block)
111
+ opts[:erb_base] ||= 'lib'
112
+ opts[:include_files] ||= []
113
+ super(parent, **opts, &block)
114
+ end
115
+ def id_assign
116
+ ids = []
117
+ @children.each{|c| a=c.get(:id_list); ids.concat(a) if a}
118
+ ids.sort.uniq.map{|x| "id_#{x[1]} = rb_intern(\"#{x[0]}\");"}
119
+ end
120
+ def id_decl
121
+ ids = []
122
+ @children.each{|c| a=c.get(:id_list); ids.concat(a) if a}
123
+ ids.sort.uniq.map{|x| "static ID id_#{x[1]};\n"}
124
+ end
125
+ def def_class(**opts, &block)
126
+ DefClass.new(self, **opts, &block)
127
+ end
128
+ def def_module(**opts, &block)
129
+ DefModule.new(self, **opts, &block)
130
+ end
131
+ end
132
+
133
+ module DeclMethod
134
+ def def_alloc_func(m, erb_path=nil, **opts, &block)
135
+ DefAllocFunc.new(self, erb_path||m, name:m, singleton:true, **opts, &block)
136
+ end
137
+ def undef_alloc_func
138
+ UndefAllocFunc.new(self)
139
+ end
140
+ def def_method(m, erb_path=nil, **opts, &block)
141
+ DefMethod.new(self, erb_path||m, name:m, **opts, &block)
142
+ end
143
+ def undef_method(m)
144
+ UndefMethod.new(self,name:m)
145
+ end
146
+ def def_singleton_method(m, erb_path=nil, **opts, &block)
147
+ DefMethod.new(self, erb_path||m, name:m, singleton:true, **opts, &block)
148
+ end
149
+ def undef_singleton_method(m)
150
+ UndefSingletonMethod.new(self,name:m)
151
+ end
152
+ def def_module_function(m, erb_path=nil, **opts, &block)
153
+ DefModuleFunction.new(self, erb_path||m, name:m, **opts, &block)
154
+ end
155
+ def def_alias(from, to)
156
+ DefAlias.new(self, from:from, to:to)
157
+ end
158
+ def def_const(m, v, **opts, &block)
159
+ DefConst.new(self, name:m, value:v, **opts, &block)
160
+ end
161
+ end
162
+
163
+ class DefModule < ErbPP
164
+ include DeclMethod
165
+ def initialize(parent, **opts, &block)
166
+ eb = opts[:erb_base] || 'module'
167
+ super(parent, erb_base:eb, **opts, &block)
168
+ end
169
+ def id_list
170
+ @id_list ||= []
171
+ end
172
+ def def_id(name,var=nil)
173
+ var = name.gsub(/\?/,"_p").gsub(/\!/,"_bang") if var.nil?
174
+ id_list << [name,var]
175
+ end
176
+ def init_def
177
+ load_erb(init_erb).result(binding)
178
+ end
179
+ def init_erb
180
+ @opts[:init_erb] || "init_module"
181
+ end
182
+ def method_code
183
+ @children.map{|c| c.result}.join("\n")
184
+ end
185
+ def _mod_var
186
+ @opts[:module_var]
187
+ end
188
+ end
189
+
190
+ class DefClass < DefModule
191
+ def initialize(parent, **opts, &block)
192
+ eb = opts[:erb_base] || 'class'
193
+ super(parent, erb_base:eb, **opts, &block)
194
+ end
195
+ def _mod_var
196
+ @opts[:class_var]
197
+ end
198
+ def init_erb
199
+ @opts[:init_erb] || "init_class"
200
+ end
201
+ def super_class
202
+ @opts[:super_class] || "rb_cObject"
203
+ end
204
+ def free_func
205
+ @opts[:free_func] || "gsl_"+get(:name)+"_free"
206
+ end
207
+ end
208
+
209
+ class DefMethod < ErbPP
210
+ include DeclMethod
211
+
212
+ def initialize(parent, erb_base, **opts, &block)
213
+ super(parent, **opts, &block)
214
+ set erb_base: erb_base
215
+ end
216
+
217
+ def id_op
218
+ if op.size == 1
219
+ "'#{op}'"
220
+ else
221
+ "id_#{c_name}"
222
+ end
223
+ end
224
+
225
+ def c_name
226
+ @opts[:name].gsub(/\?/,"_p").gsub(/\!/,"_bang").gsub(/=/,"_set")
227
+ end
228
+
229
+ def op_map
230
+ @opts[:op] || @opts[:name]
231
+ end
232
+
233
+ def c_func(n_arg=nil)
234
+ set n_arg: n_arg if n_arg
235
+ s = (singleton) ? "_s" : ""
236
+ "#{@parent.name}#{s}_#{c_name}"
237
+ end
238
+
239
+ def c_iter
240
+ "iter_#{c_func}"
241
+ end
242
+
243
+ def define_method_args
244
+ "#{_mod_var}, \"#{op_map}\", #{c_func}, #{n_arg}"
245
+ end
246
+
247
+ def init_def
248
+ return if n_arg == :nodef
249
+ s = (singleton) ? "_singleton" : ""
250
+ "rb_define#{s}_method(#{define_method_args});"
251
+ end
252
+
253
+ def singleton
254
+ @opts[:singleton]
255
+ end
256
+ end
257
+
258
+ class DefModuleFunction < DefMethod
259
+ def initialize(parent, erb_base, **opts, &block)
260
+ super(parent, erb_base, **opts, &block)
261
+ set singleton: true
262
+ end
263
+
264
+ def init_def
265
+ return if n_arg == :nodef
266
+ "rb_define_module_function(#{define_method_args});"
267
+ end
268
+ end
269
+
270
+ class DefAlias < ErbPP
271
+ def init_def
272
+ "rb_define_alias(#{_mod_var}, \"#{from}\", \"#{to}\");"
273
+ end
274
+ end
275
+
276
+ class DefAllocFunc < DefMethod
277
+ def init_def
278
+ "rb_define_alloc_func(#{_mod_var}, #{c_func});"
279
+ end
280
+ end
281
+
282
+ class UndefAllocFunc < ErbPP
283
+ def init_def
284
+ "rb_undef_alloc_func(#{_mod_var});"
285
+ end
286
+ end
287
+
288
+ class UndefMethod < ErbPP
289
+ def init_def
290
+ "rb_undef_method(#{_mod_var},\"#{name}\");"
291
+ end
292
+ end
293
+
294
+ class UndefSingletonMethod < ErbPP
295
+ def init_def
296
+ "rb_undef_method(rb_singleton_class(#{_mod_var}),\"#{name}\");"
297
+ end
298
+ end
299
+
300
+ class DefConst < ErbPP
301
+ def init_def
302
+ "/*#{desc}*/
303
+ rb_define_const(#{_mod_var},\"#{name}\",#{value});"
304
+ end
305
+ end
306
+
307
+ class DefError < ErbPP
308
+ def initialize(parent, name, sup_var, **opts, &block)
309
+ super(parent, error_name:name, error_var:"e"+name, super_var:sup_var,
310
+ **opts, &block)
311
+ end
312
+ def result
313
+ "static VALUE #{error_var};"
314
+ end
315
+ def init_def
316
+ "/*#{description}*/
317
+ #{error_var} = rb_define_class_under(#{ns_var},\"#{error_name}\",#{super_var});"
318
+ end
319
+ end
320
+
321
+ class DefStruct < ErbPP
322
+ def method_code
323
+ "static VALUE #{class_var};"
324
+ end
325
+ def init_def
326
+ items = members.map{|s| "\"#{s}\""}.join(",")
327
+ "/*#{description}*/
328
+ #{class_var} = rb_struct_define(\"#{class_name}\",#{items},NULL);"
329
+ end
330
+ end
331
+
332
+ class DefInclueModule < ErbPP
333
+ def initialize(parent=nil, incl_class, incl_module, **opts, &block)
334
+ super(parent,incl_class:incl_class,incl_module:incl_module,**opts,&block)
335
+ end
336
+ def init_def
337
+ "rb_include_module(#{get(:incl_class)}, #{get(:incl_module)});"
338
+ end
339
+ end
@@ -0,0 +1,27 @@
1
+ puts <<EOL
2
+ #ifndef NUMO_CBLAS_T_H
3
+ #define NUMO_CBLAS_T_H
4
+
5
+ EOL
6
+
7
+ ARGF.each_line do |line|
8
+ case line
9
+ when /__cplusplus/
10
+ puts line
11
+ break
12
+ end
13
+ end
14
+
15
+ type, name = nil,nil
16
+
17
+ ARGF.each_line do |line|
18
+ line.sub!(/^(\w+)\s+cblas_(\w+)\s*\(/) do
19
+ type, name = $1,$2
20
+ "typedef #{type} (*#{name}_t)("
21
+ end
22
+ case name
23
+ when /^[id]?z/; line.gsub!(/\bvoid\s*\*/,"dcomplex *")
24
+ when /^[is]?c/; line.gsub!(/\bvoid\s*\*/,"scomplex *")
25
+ end
26
+ puts line
27
+ end
@@ -0,0 +1,93 @@
1
+ def_id "order"
2
+ def_id "trans"
3
+ def_id "transa"
4
+ def_id "transb"
5
+ def_id "uplo"
6
+ def_id "diag"
7
+ def_id "side"
8
+ def_id "alpha"
9
+ def_id "beta"
10
+ def_id "sb"
11
+ def_id "axis"
12
+ def_id "keepdims"
13
+
14
+ if /[cz]/ =~ blas_char
15
+ def_id "real"
16
+ def_id "imag"
17
+ end
18
+
19
+ case blas_char
20
+ when "c"
21
+ real_char = "s"
22
+ when "z"
23
+ real_char = "d"
24
+ end
25
+
26
+ # level-1 blas
27
+
28
+ case blas_char
29
+ when /[sd]/
30
+ decl "?dot"
31
+ decl "?nrm2"
32
+ decl "?asum", "nrm2"
33
+ if "s" == blas_char
34
+ decl "dsdot", "dot"
35
+ decl "sdsdot"
36
+ end
37
+ when /[cz]/
38
+ decl "?dotc", "dot", "?dotc_sub"
39
+ decl "?dotu", "dot", "?dotu_sub"
40
+ decl real_char+"?nrm2", "nrm2"
41
+ decl real_char+"?asum", "nrm2"
42
+ end
43
+
44
+ decl "?swap"
45
+ decl "?copy"
46
+ decl "?axpy"
47
+
48
+ case blas_char
49
+ when /[sd]/
50
+ decl "?rot"
51
+ decl "?rotm"
52
+ else
53
+ #decl blas_char+real_char+"rot", "rot"
54
+ end
55
+
56
+ decl "?scal"
57
+ case blas_char
58
+ when /[cz]/
59
+ decl blas_char+real_char+"scal", "scal"
60
+ end
61
+
62
+ # level-2 blas
63
+
64
+ decl "?gemv", "mv"
65
+ decl "?trmv", "mv"
66
+
67
+ case blas_char
68
+ when /[sd]/
69
+ decl "?symv", "mv" # [cz]symv missing in CBLAS
70
+ decl "?syr" # [cz]syr missing in CBLAS
71
+ decl "?ger"
72
+ decl "?syr2"
73
+ when /[cz]/
74
+ decl "?hemv", "mv"
75
+ decl "?gerc", "ger"
76
+ decl "?geru", "ger"
77
+ decl "?her", "syr"
78
+ decl "?her2", "syr2"
79
+ decl "?herk", "syrk"
80
+ end
81
+
82
+ # level-3 blas
83
+
84
+ decl "?gemm", "mm"
85
+ decl "?symm", "mm"
86
+ decl "?trmm", "mm"
87
+
88
+ case blas_char
89
+ when /[cz]/
90
+ decl "?hemm", "mm"
91
+ end
92
+ decl "?syrk"
93
+ decl "?syr2k"