Module: Numo::Linalg::Loader

Defined in:
lib/numo/linalg/loader.rb

Constant Summary

@@libs =
nil

Class Method Summary collapse

Class Method Details

.dlopen(recvr, base, dir = nil, ver = nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/numo/linalg/loader.rb', line 15

def dlopen(recvr,base,dir=nil,ver=nil)
  if dir && !dir.empty?
    base = File.join(dir,base)
  end
  if ver && EXT=='so'
    begin
      path = "#{base}.#{EXT}.#{ver}"
      recvr.send(:dlopen,path)
      return path
    rescue
    end
  end
  path = "#{base}.#{EXT}"
  recvr.send(:dlopen,path)
  path
end

.libsObject



11
12
13
# File 'lib/numo/linalg/loader.rb', line 11

def libs
  @@libs
end

.load_atlas(*dirs, exc: true) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/numo/linalg/loader.rb', line 95

def load_atlas(*dirs,exc:true)
  if dirs.empty?
    dirs = [ATLAS_LIBPATH,""].compact
  end
  dirs.each do |d|
    %w[ libtatlas
        libatlas
        libsatlas ].each do |f|
      begin
        f_atlas = dlopen(Fiddle,f,d,3)
        f_atlas = dlopen(Blas,f,d,3)
        f_lapacke = dlopen(Lapack,"liblapacke",LAPACK_LIBPATH,3)
        @@libs = [ f_atlas, f_lapacke ]
        if $DEBUG
          $stderr.puts "Numo::Linalg: use #{f}"
        end
        return true
      rescue
      end
    end
  end
  if exc
    raise RuntimeError, "cannot find ATLAS library"
  end
  false
end

.load_lapack(*dirs, exc: true) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/numo/linalg/loader.rb', line 122

def load_lapack(*dirs,exc:true)
  if dirs.empty?
    b = BLAS_LIBPATH || LAPACK_LIBPATH
    l = LAPACK_LIBPATH || BLAS_LIBPATH
    dirs = (b ? [[b,l]] : []) + ["",""]
  end
  dirs.each do |arg|
    b,l = *arg
    l = b if l.nil?
    begin
      f_blas = dlopen(Fiddle,"libblas",b,3)
      f_lapack = dlopen(Fiddle,"liblapack",l,3)
      f_cblas = dlopen(Blas,"libcblas",b,3)
      f_lapacke = dlopen(Lapack,"liblapacke",l,3)
      @@libs = [ f_blas, f_lapack, f_cblas, f_lapacke ]
      if $DEBUG
        $stderr.puts "Numo::Linalg: use #{f_blas} and #{f_lapack}"
      end
      return true
    rescue
    end
  end
  if exc
    raise RuntimeError, "cannot find BLAS/LAPABK library"
  end
  false
end

.load_libraryObject

Raises:

  • (RuntimeError)


150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/numo/linalg/loader.rb', line 150

def load_library
  case BACKEND
  when /mkl/i         ; return if load_mkl(exc:false)
  when /^openblas/i   ; return if load_openblas(exc:false)
  when /^atlas/i      ; return if load_atlas(exc:false)
  when /lapack|blas/i ; return if load_lapack(exc:false)
  else
    return if load_mkl(exc:false)
    return if load_openblas(exc:false)
    return if load_atlas(exc:false)
    return if load_lapack(exc:false)
  end
  raise RuntimeError, "cannot find backend library for Numo::Linalg"
end

.load_mkl(*dirs, exc: true) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/numo/linalg/loader.rb', line 33

def load_mkl(*dirs,exc:true)
  if dirs.empty?
    dirs = [MKL_LIBPATH,""].compact
  end
  dirs.each do |d|
    if d.empty?
      d = e = nil
    else
      e = d.sub(/\/mkl\//, "/")
    end
    begin
      f_iomp5 = dlopen(Fiddle,"libiomp5",e)
      f_mkl_core = dlopen(Fiddle,"libmkl_core",d)
      f_intel_thread = dlopen(Fiddle,"libmkl_intel_thread",d)
      f_intel_lp64 = dlopen(Blas,"libmkl_intel_lp64",d)
      f_intel_lp64 = dlopen(Lapack,"libmkl_intel_lp64",d)
      @@libs = [ f_iomp5, f_mkl_core, f_intel_thread, f_intel_lp64 ]
      if $DEBUG
        $stderr.puts "Numo::Linalg: use #{f_intel_lp64}"
      end
      return true
    rescue
    end
  end
  if exc
    raise RuntimeError, "cannot find MKL library"
  end
  false
end

.load_openblas(*dirs, exc: true) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/numo/linalg/loader.rb', line 63

def load_openblas(*dirs,exc:true)
  if dirs.empty?
    dirs = [OPENBLAS_LIBPATH,""].compact
  end
  dirs.each do |d|
    %w[ libopenblaso
        libopenblasp
        libopenblas  ].each do |f|
      begin
        f_openblas = dlopen(Blas,f,d,0)
        f_openblas = dlopen(Fiddle,f,d,0)
        f_lapacke = nil
        begin
          f_lapacke = dlopen(Lapack,"liblapacke",d,3)
        rescue
          f_openblas = dlopen(Lapack,f,d,0)
        end
        @@libs = [ f_openblas, f_lapacke ].compact
        if $DEBUG
          $stderr.puts "Numo::Linalg: use #{f}"
        end
        return true
      rescue
      end
    end
  end
  if exc
    raise RuntimeError, "cannot find OpenBLAS library"
  end
  false
end