carray-ffi 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 366c240e4ca4b1690d8db085f8b9208cecd80490b8ba0ee982b884c594df8ab0
4
+ data.tar.gz: 5e86c1fa5bf5214762247b5a09f78922a2f1389d7b47aeb13110a33953020ec6
5
+ SHA512:
6
+ metadata.gz: 5ee2346c24706ace95e31ea2c77d428b976a92bca4ff647e15199e34a8a75bb359df37d0ecf67a2c194044724f5664743f0bb283288697f7b7591bc80ee03eaa
7
+ data.tar.gz: 1f8777b307ac745a7d7558a974c7c76791926c7d7d2cbc517938283d8ff4750ffc616d99b256a5796e80564f1115315a974f43bd9bcb44fdf195615d8c5efc57
File without changes
@@ -0,0 +1,11 @@
1
+ GEMSPEC = "carray-ffi.gemspec"
2
+
3
+ task :install do
4
+ spec = eval File.read(GEMSPEC)
5
+ system %{
6
+ gem build #{GEMSPEC}; gem install #{spec.full_name}.gem
7
+ }
8
+ end
9
+
10
+ require 'rspec/core/rake_task'
11
+ RSpec::Core::RakeTask.new
@@ -0,0 +1,22 @@
1
+
2
+ Gem::Specification::new do |s|
3
+ version = "0.9.0"
4
+ files = Dir.glob("**/*") - [
5
+ Dir.glob("carray*.gem"),
6
+ ].flatten
7
+ s.platform = Gem::Platform::RUBY
8
+ s.name = "carray-ffi"
9
+ s.summary = "Extension for inteface between CArray and FFI"
10
+ s.description = <<-HERE
11
+ Extension for inteface between CArray and FFI
12
+ HERE
13
+ s.version = version
14
+ s.author = "Hiroki Motoyoshi"
15
+ s.email = ""
16
+ s.homepage = 'https://github.com/himotoyoshi/carray-ffi'
17
+ s.files = files
18
+ s.extensions = [ "ext/extconf.rb" ]
19
+ s.required_ruby_version = ">= 2.4.0"
20
+ s.add_runtime_dependency 'carray', '~> 1.4.0', ">=1.4.0"
21
+ s.add_runtime_dependency 'ffi', '~> 1.13.0', ">=1.13.0"
22
+ end
@@ -0,0 +1,35 @@
1
+ FFI経由でFortranのルーチンを呼び出す
2
+ -------------------------------
3
+
4
+ ネームマングリング
5
+ ---------------
6
+
7
+
8
+ bind(c)を用いて、C言語から呼ぶことができるようにネームマングリングする。
9
+
10
+ bind(c, name = 'arrange')
11
+
12
+ としてもよいが、Fortran関数名の小文字にしたものがデフォルトなので、本来はname指定はいらない。
13
+
14
+ 値渡し
15
+ -----
16
+
17
+ 値渡しのためには value という指示が必要。
18
+
19
+ subroutine arange(n, a) bind(c, name = 'arange')
20
+ implicit none
21
+ integer, intent(IN), value :: n
22
+ double precision, intent(IN OUT) :: a(n)
23
+ integer :: k
24
+ do k=1,n
25
+ a(k) = k
26
+ end do
27
+ end subroutine
28
+
29
+
30
+ 引数の n を値渡しししたいので、変数宣言にvalueというキーワードを入れている。
31
+
32
+ コンパイル
33
+ --------
34
+
35
+ gfortran --shared -o test.so test.f90
@@ -0,0 +1,26 @@
1
+ integer function aaa(i) bind(c, name = 'aaa')
2
+ implicit none
3
+ integer, value :: i
4
+ aaa = i * i
5
+ return
6
+ end function aaa
7
+
8
+ subroutine arange(n, a) bind(c, name = 'arange')
9
+ implicit none
10
+ integer, intent(IN), value :: n
11
+ double precision, intent(IN OUT) :: a(n)
12
+ integer :: k
13
+ do k=1,n
14
+ a(k) = k
15
+ end do
16
+ end subroutine
17
+
18
+ subroutine trans(a,n,m) bind(c, name = 'trans')
19
+ implicit none
20
+ integer, intent(IN), value :: n, m
21
+ double precision, intent(IN OUT) :: a(n,m)
22
+ write (6,*) a
23
+ a = transpose(a)
24
+ end subroutine
25
+
26
+
@@ -0,0 +1,29 @@
1
+ require "ffi"
2
+ require "carray"
3
+ #require "carray-ffi"
4
+
5
+ module FooLib
6
+ extend FFI::Library
7
+ ffi_lib "test.so"
8
+
9
+ attach_function :aaa, [:int], :int
10
+ attach_function :arange, [:int, :pointer], :void
11
+ attach_function :trans, [:pointer, :int, :int], :void
12
+ end
13
+
14
+ b = FFI::MemoryPointer.new(:double,1)
15
+ FooLib.arange(1, b)
16
+ p b.ca.refer(:double,[1])
17
+
18
+ a = CArray.double(100,100)
19
+ FooLib.arange(a.size, a.mp)
20
+
21
+ p a
22
+
23
+ a = CArray.double(3,3).seq
24
+
25
+ p a
26
+
27
+ FooLib.trans(a.mp, *a.dim.reverse)
28
+
29
+ p a
Binary file
@@ -0,0 +1,74 @@
1
+ #include <ruby.h>
2
+ #include <stdbool.h>
3
+ #include "carray.h"
4
+
5
+ typedef struct AbstractMemory_ AbstractMemory;
6
+
7
+ struct AbstractMemory_ {
8
+ char* address; /* Use char* instead of void* to ensure adding to it works correctly */
9
+ long size;
10
+ int flags;
11
+ int typeSize;
12
+ };
13
+
14
+ typedef struct Pointer {
15
+ AbstractMemory memory;
16
+ VALUE rbParent;
17
+ char* storage; /* start of malloc area */
18
+ bool autorelease;
19
+ bool allocated;
20
+ } Pointer;
21
+
22
+
23
+ static VALUE rb_mFFI, rb_cMemoryPointer;
24
+
25
+ static VALUE
26
+ rb_ca_mp (VALUE self)
27
+ {
28
+ volatile VALUE obj;
29
+ CArray *ca;
30
+ Pointer *p;
31
+ Data_Get_Struct(self, CArray, ca);
32
+
33
+ if ( ! ca_is_entity(ca) ) {
34
+ rb_raise(rb_eArgError, "carray should has entity to convert to memory pointer.");
35
+ }
36
+
37
+ {
38
+ VALUE args[] = { INT2FIX(1), LONG2NUM(0), Qfalse };
39
+ obj = rb_class_new_instance(3, args, rb_cMemoryPointer);
40
+ }
41
+
42
+ Data_Get_Struct(obj, Pointer, p);
43
+
44
+ p->memory.size = ca->bytes;
45
+ p->memory.address = ca->ptr;
46
+
47
+ rb_ivar_set(obj, rb_intern("referred_object"), self);
48
+
49
+ return obj;
50
+ }
51
+
52
+ static VALUE
53
+ rb_mp_ca (VALUE self)
54
+ {
55
+ volatile VALUE obj;
56
+ Pointer *p;
57
+ Data_Get_Struct(self, Pointer, p);
58
+
59
+ obj = rb_carray_wrap_ptr(CA_UINT8, 1, &(p->memory.size),
60
+ 0, NULL, p->memory.address, self);
61
+
62
+ return obj;
63
+ }
64
+
65
+ void
66
+ Init_carray_ffi ()
67
+ {
68
+ rb_mFFI = rb_const_get(rb_cObject, rb_intern("FFI"));
69
+ rb_cMemoryPointer = rb_const_get(rb_mFFI, rb_intern("MemoryPointer"));
70
+
71
+ rb_define_method(rb_cCArray, "mp", rb_ca_mp, 0);
72
+ rb_define_method(rb_cMemoryPointer, "ca", rb_mp_ca, 0);
73
+
74
+ }
@@ -0,0 +1,12 @@
1
+ require 'mkmf'
2
+ require "carray/mkmf"
3
+
4
+ $CFLAGS += " -Wall"
5
+
6
+ pkg_config("libffi") ||
7
+ have_header("ffi.h") ||
8
+ find_header("ffi.h", "/usr/local/include", "/usr/include/ffi")
9
+
10
+ if have_carray()
11
+ create_makefile("carray_ffi")
12
+ end
@@ -0,0 +1,3 @@
1
+ require "ffi"
2
+ require "carray"
3
+ require "carray_ffi"
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: carray-ffi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.0
5
+ platform: ruby
6
+ authors:
7
+ - Hiroki Motoyoshi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-06-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: carray
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.4.0
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.4.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: 1.4.0
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 1.4.0
33
+ - !ruby/object:Gem::Dependency
34
+ name: ffi
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: 1.13.0
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 1.13.0
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: 1.13.0
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 1.13.0
53
+ description: " Extension for inteface between CArray and FFI\n"
54
+ email: ''
55
+ executables: []
56
+ extensions:
57
+ - ext/extconf.rb
58
+ extra_rdoc_files: []
59
+ files:
60
+ - README.md
61
+ - Rakefile
62
+ - carray-ffi.gemspec
63
+ - examples/fortran/README.txt
64
+ - examples/fortran/test.f90
65
+ - examples/fortran/test.rb
66
+ - examples/fortran/test.so
67
+ - ext/caffi.c
68
+ - ext/extconf.rb
69
+ - lib/carray-ffi.rb
70
+ homepage: https://github.com/himotoyoshi/carray-ffi
71
+ licenses: []
72
+ metadata: {}
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: 2.4.0
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubyforge_project:
89
+ rubygems_version: 2.7.7
90
+ signing_key:
91
+ specification_version: 4
92
+ summary: Extension for inteface between CArray and FFI
93
+ test_files: []