numo-linalg 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8a9f783654dbe1c2f8ca4dbeef4030053d8e8c92a99f1ccf0fa0ecd551c90f90
4
- data.tar.gz: 46f3558e6bd86db15fb0d3ab9558e1f4e565ca844db500684b9eaac0e6ec57c7
3
+ metadata.gz: 611dd9ee204102a1688b5d199b95b5376d32239b04dfcf69a605da54599a9310
4
+ data.tar.gz: d999af4188a239e9a78b7f071d850f2b11bf822c1c0237c592c6ebe4979b278d
5
5
  SHA512:
6
- metadata.gz: f1c478c9f017babd63b40dcea4eeb6ca9222226f680cc6870b01c1f305f54080ab7c035ead34c39d9f70aa431c6b2d9c9ec1be0642f7bab66eea32ee97c39363
7
- data.tar.gz: d55a4aa11afffbb51486007df3c80b221a3e90e990e86b76eadc996d560abf8d3e16df62aa25e3f65f050175dca8d45d5b791c1dc4b2b2c3ac66031da0c70c7b
6
+ metadata.gz: 7da7684a72653a7718de5ff51a99ed14ca0dc3770cae735139487cf56ef2610f292cf3e970abd681320d8a1847664dcb7d6e9ac2c936867abb2cae1154bdb053
7
+ data.tar.gz: 70bb3e22d41fee16e953b53467b709cdd96bd80adceb526123df941335782054823bb84a9d703ca45e73e88c0ad34b7cbf90699a53cc7e7dcb1277251e7fa0e6
@@ -259,11 +259,17 @@ numo_cblas_check_func(void **func, const char *name)
259
259
  s = alloca(strlen(blas_prefix)+strlen(name)+1);
260
260
  strcpy(s,blas_prefix);
261
261
  strcat(s,name);
262
+ #if defined(HAVE_DLFCN_H)
262
263
  dlerror();
264
+ #endif
263
265
  *func = dlsym(blas_handle, s);
264
- error = dlerror();
265
- if (error != NULL) {
266
- rb_raise(rb_eRuntimeError, "%s", error);
266
+ #if defined(HAVE_DLFCN_H)
267
+ if ((error = dlerror()) != 0) {
268
+ func = 0;
269
+ }
270
+ #endif
271
+ if ( !func ) {
272
+ rb_raise(rb_eRuntimeError, "unknown symbol \"%s\"", s);
267
273
  }
268
274
  }
269
275
  }
@@ -291,14 +297,22 @@ blas_s_dlopen(int argc, VALUE *argv, VALUE mod)
291
297
  if (i==2) {
292
298
  f = NUM2INT(flag);
293
299
  } else {
294
- f = RTLD_LAZY | RTLD_LOCAL;
300
+ f = RTLD_LAZY;
295
301
  }
302
+ #if defined(HAVE_DLFCN_H)
296
303
  dlerror();
304
+ #endif
297
305
  handle = dlopen(StringValueCStr(lib), f);
298
- error = dlerror();
299
- if (error != NULL) {
306
+ #if defined(HAVE_DLFCN_H)
307
+ if ( !handle && (error = dlerror()) ) {
300
308
  rb_raise(rb_eRuntimeError, "%s", error);
301
309
  }
310
+ #else
311
+ if ( !handle ) {
312
+ error = dlerror();
313
+ rb_raise(rb_eRuntimeError, "%s", error);
314
+ }
315
+ #endif
302
316
  blas_handle = handle;
303
317
  return Qnil;
304
318
  }
@@ -1,35 +1,6 @@
1
1
  require 'mkmf'
2
2
  require 'numo/narray'
3
- require 'erb'
4
-
5
- ldirs = [
6
- dir_config("mkl")[1],
7
- dir_config("openblas")[1],
8
- dir_config("atlas")[1],
9
- dir_config("blas")[1],
10
- dir_config("lapack")[1],
11
- ]
12
- bked = with_config("backend")
13
-
14
- FileUtils.mkdir_p "lib"
15
- open("lib/site_conf.rb","w"){|f| f.write "
16
- module Numo
17
- module Linalg
18
- BACKEND = #{bked.inspect}
19
- MKL_LIBPATH = #{ldirs[0].inspect}
20
- OPENBLAS_LIBPATH = #{ldirs[1].inspect}
21
- ATLAS_LIBPATH = #{ldirs[2].inspect}
22
- BLAS_LIBPATH = #{ldirs[3].inspect}
23
- LAPACK_LIBPATH = #{ldirs[4].inspect}
24
- end
25
- end"}
26
-
27
- $LOAD_PATH.each do |x|
28
- if File.exist? File.join(x,'numo/numo/narray.h')
29
- $INCFLAGS = "-I#{x}/numo " + $INCFLAGS
30
- break
31
- end
32
- end
3
+ require_relative '../mkmf_linalg'
33
4
 
34
5
  srcs = %w(
35
6
  blas
@@ -40,13 +11,24 @@ blas_z
40
11
  )
41
12
  $objs = srcs.collect{|i| i+".o"}
42
13
 
43
- if !have_header('numo/narray.h')
14
+ dir_config("narray")
15
+
16
+ find_narray_h
17
+ if !have_header("numo/narray.h")
44
18
  puts "
45
19
  Header numo/narray.h was not found. Give pathname as follows:
46
20
  % ruby extconf.rb --with-narray-include=narray_h_dir"
47
21
  exit(1)
48
22
  end
49
23
 
24
+ if RUBY_PLATFORM =~ /cygwin|mingw/
25
+ find_libnarray_a
26
+ unless have_library("narray","nary_new")
27
+ puts "libnarray.a not found"
28
+ exit(1)
29
+ end
30
+ end
31
+
50
32
  if have_header("dlfcn.h")
51
33
  exit(1) unless have_library("dl")
52
34
  exit(1) unless have_func("dlopen")
@@ -54,14 +36,6 @@ elsif have_header("windows.h")
54
36
  exit(1) unless have_func("LoadLibrary")
55
37
  end
56
38
 
57
- dep_path = File.join(__dir__, "depend")
58
- File.open(dep_path, "w") do |dep|
59
- dep_erb_path = File.join(__dir__, "depend.erb")
60
- File.open(dep_erb_path, "r") do |dep_erb|
61
- erb = ERB.new(dep_erb.read)
62
- erb.filename = dep_erb_path
63
- dep.print(erb.result)
64
- end
65
- end
66
-
39
+ create_site_conf
40
+ create_depend
67
41
  create_makefile('numo/linalg/blas')
@@ -1,13 +1,6 @@
1
1
  require 'mkmf'
2
2
  require 'numo/narray'
3
- require 'erb'
4
-
5
- $LOAD_PATH.each do |x|
6
- if File.exist? File.join(x,'numo/numo/narray.h')
7
- $INCFLAGS = "-I#{x}/numo " + $INCFLAGS
8
- break
9
- end
10
- end
3
+ require_relative '../mkmf_linalg'
11
4
 
12
5
  srcs = %w(
13
6
  lapack
@@ -18,13 +11,24 @@ lapack_z
18
11
  )
19
12
  $objs = srcs.collect{|i| i+".o"}
20
13
 
21
- if !have_header('numo/narray.h')
14
+ dir_config("narray")
15
+
16
+ find_narray_h
17
+ if !have_header("numo/narray.h")
22
18
  puts "
23
19
  Header numo/narray.h was not found. Give pathname as follows:
24
20
  % ruby extconf.rb --with-narray-include=narray_h_dir"
25
21
  exit(1)
26
22
  end
27
23
 
24
+ if RUBY_PLATFORM =~ /cygwin|mingw/
25
+ find_libnarray_a
26
+ unless have_library("narray","nary_new")
27
+ puts "libnarray.a not found"
28
+ exit(1)
29
+ end
30
+ end
31
+
28
32
  if have_header("dlfcn.h")
29
33
  exit(1) unless have_library("dl")
30
34
  exit(1) unless have_func("dlopen")
@@ -32,14 +36,5 @@ elsif have_header("windows.h")
32
36
  exit(1) unless have_func("LoadLibrary")
33
37
  end
34
38
 
35
- dep_path = File.join(__dir__, "depend")
36
- File.open(dep_path, "w") do |dep|
37
- dep_erb_path = File.join(__dir__, "depend.erb")
38
- File.open(dep_erb_path, "r") do |dep_erb|
39
- erb = ERB.new(dep_erb.read)
40
- erb.filename = dep_erb_path
41
- dep.print(erb.result)
42
- end
43
- end
44
-
39
+ create_depend
45
40
  create_makefile('numo/linalg/lapack')
@@ -295,11 +295,17 @@ numo_lapacke_check_func(void **func, const char *name)
295
295
  s = alloca(strlen(lapack_prefix)+strlen(name)+1);
296
296
  strcpy(s,lapack_prefix);
297
297
  strcat(s,name);
298
+ #if defined(HAVE_DLFCN_H)
298
299
  dlerror();
300
+ #endif
299
301
  *func = dlsym(lapack_handle, s);
300
- error = dlerror();
301
- if (error != NULL) {
302
- rb_raise(rb_eRuntimeError, "%s", error);
302
+ #if defined(HAVE_DLFCN_H)
303
+ if ((error = dlerror()) != 0) {
304
+ func = 0;
305
+ }
306
+ #endif
307
+ if ( !func ) {
308
+ rb_raise(rb_eRuntimeError, "unknown symbol \"%s\"", s);
303
309
  }
304
310
  }
305
311
  }
@@ -327,14 +333,22 @@ lapack_s_dlopen(int argc, VALUE *argv, VALUE mod)
327
333
  if (i==2) {
328
334
  f = NUM2INT(flag);
329
335
  } else {
330
- f = RTLD_LAZY | RTLD_LOCAL;
336
+ f = RTLD_LAZY;
331
337
  }
338
+ #if defined(HAVE_DLFCN_H)
332
339
  dlerror();
340
+ #endif
333
341
  handle = dlopen(StringValueCStr(lib), f);
334
- error = dlerror();
335
- if (error != NULL) {
342
+ #if defined(HAVE_DLFCN_H)
343
+ if ( !handle && (error = dlerror()) ) {
336
344
  rb_raise(rb_eRuntimeError, "%s", error);
337
345
  }
346
+ #else
347
+ if ( !handle ) {
348
+ error = dlerror();
349
+ rb_raise(rb_eRuntimeError, "%s", error);
350
+ }
351
+ #endif
338
352
  lapack_handle = handle;
339
353
  return Qnil;
340
354
  }
@@ -0,0 +1,101 @@
1
+ require 'mkmf'
2
+
3
+ def create_site_conf
4
+ ldirs = [
5
+ dir_config("mkl")[1],
6
+ dir_config("openblas")[1],
7
+ dir_config("atlas")[1],
8
+ dir_config("blas")[1],
9
+ dir_config("lapack")[1],
10
+ ]
11
+ bked = with_config("backend")
12
+
13
+ require 'fiddle'
14
+
15
+ message "creating lib/site_conf.rb\n"
16
+
17
+ FileUtils.mkdir_p "lib"
18
+
19
+ ext = detect_library_extension
20
+ need_version = false
21
+ if ext == 'so'
22
+ begin
23
+ Fiddle.dlopen "libm.so"
24
+ rescue
25
+ (5..7).each do |i|
26
+ begin
27
+ Fiddle.dlopen "libm.so.#{i}"
28
+ need_version = true
29
+ break
30
+ rescue
31
+ end
32
+ end
33
+ if !need_version
34
+ raise "failed to check whether dynamically linked shared object needs version suffix"
35
+ end
36
+ end
37
+ end
38
+
39
+ open("lib/site_conf.rb","w"){|f| f.write "
40
+ module Numo
41
+ module Linalg
42
+
43
+ BACKEND = #{bked.inspect}
44
+ MKL_LIBPATH = #{ldirs[0].inspect}
45
+ OPENBLAS_LIBPATH = #{ldirs[1].inspect}
46
+ ATLAS_LIBPATH = #{ldirs[2].inspect}
47
+ BLAS_LIBPATH = #{ldirs[3].inspect}
48
+ LAPACK_LIBPATH = #{ldirs[4].inspect}
49
+
50
+ module Loader
51
+ EXT = '#{ext}'
52
+ NEED_VERSION_SUFFIX = #{need_version}
53
+ end
54
+
55
+ end
56
+ end"
57
+ }
58
+ end
59
+
60
+ def detect_library_extension
61
+ case RbConfig::CONFIG['host_os']
62
+ when /mswin|msys|mingw|cygwin/
63
+ 'dll'
64
+ when /darwin|mac os/
65
+ 'dylib'
66
+ else
67
+ 'so'
68
+ end
69
+ end
70
+
71
+ def find_narray_h
72
+ $LOAD_PATH.each do |x|
73
+ if File.exist? File.join(x,'numo/numo/narray.h')
74
+ $INCFLAGS = "-I#{x}/numo " + $INCFLAGS
75
+ break
76
+ end
77
+ end
78
+ end
79
+
80
+ def find_libnarray_a
81
+ $LOAD_PATH.each do |x|
82
+ if File.exist? File.join(x,'numo/libnarray.a')
83
+ $LDFLAGS = "-L#{x}/numo " + $LDFLAGS
84
+ break
85
+ end
86
+ end
87
+ end
88
+
89
+ def create_depend
90
+ require 'erb'
91
+ message "creating depend\n"
92
+ dep_path = File.join(Dir.pwd, "depend")
93
+ File.open(dep_path, "w") do |dep|
94
+ dep_erb_path = File.join(Dir.pwd, "depend.erb")
95
+ File.open(dep_erb_path, "r") do |dep_erb|
96
+ erb = ERB.new(dep_erb.read)
97
+ erb.filename = dep_erb_path
98
+ dep.print(erb.result)
99
+ end
100
+ end
101
+ end
@@ -12,32 +12,48 @@ module Numo
12
12
  @@libs
13
13
  end
14
14
 
15
- def load_mkl(path=nil,exc:true)
16
- a = case path
17
- when String; [path]
18
- when Array ; path
19
- when NilClass; [MKL_LIBPATH,""].compact
20
- else raise ArgumentError,"invalid path"
15
+ def dlopen(recvr,base,dir=nil,ver=nil)
16
+ if dir && !dir.empty?
17
+ base = File.join(dir,base)
18
+ end
19
+ path = base+".#{EXT}"
20
+ if NEED_VERSION_SUFFIX
21
+ if ver
22
+ path += ".#{ver}"
23
+ end
24
+ recvr.send(:dlopen,path)
25
+ else
26
+ begin
27
+ recvr.send(:dlopen,path)
28
+ rescue => e
29
+ if ver
30
+ path += ".#{ver}"
31
+ recvr.send(:dlopen,path)
32
+ else
33
+ raise e
21
34
  end
22
- a.each do |d|
35
+ end
36
+ end
37
+ path
38
+ end
39
+ private :dlopen
40
+
41
+ def load_mkl(*dirs,exc:true)
42
+ if dirs.empty?
43
+ dirs = [MKL_LIBPATH,""].compact
44
+ end
45
+ dirs.each do |d|
23
46
  if d.empty?
24
- f_iomp5 = "libiomp5.so"
25
- f_mkl_core = "libmkl_core.so"
26
- f_intel_thread = "libmkl_intel_thread.so"
27
- f_intel_lp64 = "libmkl_intel_lp64.so"
47
+ d = e = nil
28
48
  else
29
49
  e = d.sub(/\/mkl\//, "/")
30
- f_iomp5 = File.join(e,"libiomp5.so")
31
- f_mkl_core = File.join(d,"libmkl_core.so")
32
- f_intel_thread = File.join(d,"libmkl_intel_thread.so")
33
- f_intel_lp64 = File.join(d,"libmkl_intel_lp64.so")
34
50
  end
35
51
  begin
36
- Fiddle.dlopen(f_iomp5)
37
- Fiddle.dlopen(f_mkl_core)
38
- Fiddle.dlopen(f_intel_thread)
39
- Blas.dlopen(f_intel_lp64)
40
- Lapack.dlopen(f_intel_lp64)
52
+ f_iomp5 = dlopen(Fiddle,"libiomp5",e)
53
+ f_mkl_core = dlopen(Fiddle,"libmkl_core",d)
54
+ f_intel_thread = dlopen(Fiddle,"libmkl_intel_thread",d)
55
+ f_intel_lp64 = dlopen(Blas,"libmkl_intel_lp64",d)
56
+ f_intel_lp64 = dlopen(Lapack,"libmkl_intel_lp64",d)
41
57
  @@libs = [ f_iomp5, f_mkl_core, f_intel_thread, f_intel_lp64 ]
42
58
  if $DEBUG
43
59
  $stderr.puts "Numo::Linalg: use #{f_intel_lp64}"
@@ -52,20 +68,24 @@ module Numo
52
68
  false
53
69
  end
54
70
 
55
- def load_openblas(path=nil,exc:true)
56
- a = case path
57
- when String; [path]
58
- when Array ; path
59
- when NilClass; [OPENBLAS_LIBPATH,""].compact
60
- else raise ArgumentError,"invalid path"
61
- end
62
- a.each do |d|
63
- %w[libopenblaso.so libopenblasp.so libopenblas.so].each do |f|
64
- f = File.join(d,f) if !d.empty?
71
+ def load_openblas(*dirs,exc:true)
72
+ if dirs.empty?
73
+ dirs = [OPENBLAS_LIBPATH,""].compact
74
+ end
75
+ dirs.each do |d|
76
+ %w[ libopenblaso
77
+ libopenblasp
78
+ libopenblas ].each do |f|
65
79
  begin
66
- Blas.dlopen(f)
67
- Lapack.dlopen(f)
68
- @@libs = [ f ]
80
+ f_openblas = dlopen(Blas,f,d,0)
81
+ f_openblas = dlopen(Fiddle,f,d,0)
82
+ f_lapacke = nil
83
+ begin
84
+ f_lapacke = dlopen(Lapack,"liblapacke",d,3)
85
+ rescue
86
+ f_openblas = dlopen(Lapack,f,d,0)
87
+ end
88
+ @@libs = [ f_openblas, f_lapacke ].compact
69
89
  if $DEBUG
70
90
  $stderr.puts "Numo::Linalg: use #{f}"
71
91
  end
@@ -80,24 +100,19 @@ module Numo
80
100
  false
81
101
  end
82
102
 
83
- def load_atlas(path=nil,exc:true)
84
- a = case path
85
- when String; [path]
86
- when Array ; path
87
- when NilClass; [ATLAS_LIBPATH,""].compact
88
- else raise ArgumentError,"invalid path"
89
- end
90
- a.each do |d|
91
- %w[libtatlas.so libatlas.so libsatlas.so].each do |f|
92
- f = File.join(d,f) if !d.empty?
103
+ def load_atlas(*dirs,exc:true)
104
+ if dirs.empty?
105
+ dirs = [ATLAS_LIBPATH,""].compact
106
+ end
107
+ dirs.each do |d|
108
+ %w[ libtatlas
109
+ libatlas
110
+ libsatlas ].each do |f|
93
111
  begin
94
- Fiddle.dlopen(f)
95
- Blas.dlopen(f)
96
- l = LAPACK_LIBPATH
97
- e = "liblapacke.so"
98
- e = File.join(l,e) if l && !l.empty?
99
- Lapack.dlopen(e)
100
- @@libs = [ f, e ]
112
+ f_atlas = dlopen(Fiddle,f,d,3)
113
+ f_atlas = dlopen(Blas,f,d,3)
114
+ f_lapacke = dlopen(Lapack,"liblapacke",LAPACK_LIBPATH,3)
115
+ @@libs = [ f_atlas, f_lapacke ]
101
116
  if $DEBUG
102
117
  $stderr.puts "Numo::Linalg: use #{f}"
103
118
  end
@@ -112,34 +127,20 @@ module Numo
112
127
  false
113
128
  end
114
129
 
115
- def load_lapack(path=nil,exc:true)
116
- a = case path
117
- when String; [[path,path]]
118
- when Array ; [path]
119
- when NilClass
120
- b = BLAS_LIBPATH || LAPACK_LIBPATH
121
- l = LAPACK_LIBPATH || BLAS_LIBPATH
122
- (b ? [[b,l]] : []) + ["",""]
123
- else raise ArgumentError,"invalid path"
124
- end
125
- a.each do |arg|
130
+ def load_lapack(*dirs,exc:true)
131
+ if dirs.empty?
132
+ b = BLAS_LIBPATH || LAPACK_LIBPATH
133
+ l = LAPACK_LIBPATH || BLAS_LIBPATH
134
+ dirs = (b ? [[b,l]] : []) + ["",""]
135
+ end
136
+ dirs.each do |arg|
126
137
  b,l = *arg
127
- if b.empty?
128
- f_blas = "libblas.so"
129
- f_cblas = "libcblas.so"
130
- f_lapack = "liblapack.so"
131
- f_lapacke = "liblapacke.so"
132
- else
133
- f_blas = File.join(b,"libblas.so")
134
- f_cblas = File.join(b,"libcblas.so")
135
- f_lapack = File.join(l,"liblapack.so")
136
- f_lapacke = File.join(l,"liblapacke.so")
137
- end
138
+ l = b if l.nil?
138
139
  begin
139
- Fiddle.dlopen(f_blas)
140
- Fiddle.dlopen(f_lapack)
141
- Blas.dlopen(f_cblas)
142
- Lapack.dlopen(f_lapacke)
140
+ f_blas = dlopen(Fiddle,"libblas",b,3)
141
+ f_lapack = dlopen(Fiddle,"liblapack",l,3)
142
+ f_cblas = dlopen(Blas,"libcblas",b,3)
143
+ f_lapacke = dlopen(Lapack,"liblapacke",l,3)
143
144
  @@libs = [ f_blas, f_lapack, f_cblas, f_lapacke ]
144
145
  if $DEBUG
145
146
  $stderr.puts "Numo::Linalg: use #{f_blas} and #{f_lapack}"
@@ -1,5 +1,5 @@
1
1
  module Numo
2
2
  module Linalg
3
- VERSION = "0.1.1"
3
+ VERSION = "0.1.2"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: numo-linalg
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Masahiro TANAKA
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2018-01-30 00:00:00.000000000 Z
12
+ date: 2018-03-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -134,6 +134,7 @@ files:
134
134
  - ext/numo/linalg/lapack/tmpl/syev.c
135
135
  - ext/numo/linalg/lapack/tmpl/sygv.c
136
136
  - ext/numo/linalg/lapack/tmpl/trf.c
137
+ - ext/numo/linalg/mkmf_linalg.rb
137
138
  - ext/numo/linalg/numo_linalg.h
138
139
  - lib/numo/linalg.rb
139
140
  - lib/numo/linalg/autoloader.rb