narray_ffi 1.0.0 → 1.1.0

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
  SHA1:
3
- metadata.gz: 24dc8afd919a97d1e88cb9b78e6dcc4ed75216a6
4
- data.tar.gz: 557a19fb76f957c21c1fc64dd13297bb3eb0b727
3
+ metadata.gz: 1d1fd3b17183842cb75ec77fef2754c7fc9266a8
4
+ data.tar.gz: d652418d88d7ce7df105336d628f2a0cdbf6619e
5
5
  SHA512:
6
- metadata.gz: 32b05125aea7dffe69f1256ce0f517496db5d4b71853bb01f7b9e16e0634bba38d081482b7e6cc3b04bb7f8c1ae10b91752f85f31734e1ea01028035d30ee115
7
- data.tar.gz: bafb2f7a636c78f9ef9d0707a516ce73aed295839ef5ce262e204858a632ad0ef57186386c2c30494d4478119989377c0d7db993e192626c3c18dd5335a3db2f
6
+ metadata.gz: 362cab7657d9a7a00a1b84933a1c436395694c68a244904b8eb5fa9af4edb71191852f23578fb13c713554729a21d07b5fb03b061469f1f3aa407dfbd8df7e1f
7
+ data.tar.gz: 91d3336406830ddee1a4c1c458cd5df888a73666522ea56ac970c40306cfb1a92fa3a5555cc1013dc05fb46284d7953215a5e20b107696b9acb51ce207764d26
@@ -1,7 +1,9 @@
1
1
  #include "ruby.h"
2
2
  #include "narray.h"
3
3
 
4
- VALUE na_address(VALUE self) {
4
+ static VALUE
5
+ na_address(VALUE self)
6
+ {
5
7
  struct NARRAY *ary;
6
8
  void * ptr;
7
9
  VALUE ret;
@@ -12,11 +14,161 @@ VALUE na_address(VALUE self) {
12
14
  return ret;
13
15
  }
14
16
 
17
+ static struct NARRAY*
18
+ na_alloc_struct_empty(int type, int rank, int *shape)
19
+ {
20
+ int total=1, total_bak;
21
+ int i, memsz;
22
+ struct NARRAY *ary;
23
+
24
+ for (i=0; i<rank; ++i) {
25
+ if (shape[i] < 0) {
26
+ rb_raise(rb_eArgError, "negative array size");
27
+ } else if (shape[i] == 0) {
28
+ total = 0;
29
+ break;
30
+ }
31
+ total_bak = total;
32
+ total *= shape[i];
33
+ if (total < 1 || total > 2147483647 || total/shape[i] != total_bak) {
34
+ rb_raise(rb_eArgError, "array size is too large");
35
+ }
36
+ }
37
+
38
+ if (rank<=0 || total<=0) {
39
+ /* empty array */
40
+ ary = ALLOC(struct NARRAY);
41
+ ary->rank =
42
+ ary->total = 0;
43
+ ary->shape = NULL;
44
+ ary->ptr = NULL;
45
+ ary->type = type;
46
+ }
47
+ else {
48
+ memsz = na_sizeof[type] * total;
49
+
50
+ if (memsz < 1 || memsz > 2147483647 || memsz/na_sizeof[type] != total) {
51
+ rb_raise(rb_eArgError, "allocation size is too large");
52
+ }
53
+
54
+ /* Garbage Collection */
55
+ #ifdef NARRAY_GC
56
+ mem_count += memsz;
57
+ if ( mem_count > na_gc_freq ) { rb_gc(); mem_count=0; }
58
+ #endif
59
+
60
+ ary = ALLOC(struct NARRAY);
61
+ ary->shape = ALLOC_N(int, rank);
62
+
63
+ ary->rank = rank;
64
+ ary->total = total;
65
+ ary->type = type;
66
+ for (i=0; i<rank; ++i)
67
+ ary->shape[i] = shape[i];
68
+ }
69
+ ary->ref = Qtrue;
70
+ return ary;
71
+ }
72
+
73
+ static void
74
+ na_free_empty(struct NARRAY* ary)
75
+ {
76
+ if ( ary->total > 0 ) {
77
+ xfree(ary->shape);
78
+ }
79
+ xfree(ary);
80
+ }
81
+
82
+ static void
83
+ na_mark_ref_empty(struct NARRAY *ary)
84
+ {
85
+ struct NARRAY *a2;
86
+
87
+ rb_gc_mark( ary->ref );
88
+
89
+ }
90
+
91
+ static VALUE
92
+ na_make_object_empty(int type, int rank, int *shape, VALUE klass, VALUE pointer)
93
+ {
94
+ struct NARRAY *na;
95
+
96
+ na = na_alloc_struct_empty(type, rank, shape);
97
+ na->ref = pointer;
98
+
99
+ return Data_Wrap_Struct(klass, na_mark_ref_empty, na_free_empty, na);
100
+ }
101
+
102
+
103
+ static VALUE
104
+ na_pointer_to_na(int argc, VALUE *argv, VALUE pointer)
105
+ {
106
+ struct NARRAY *ary;
107
+ VALUE v;
108
+ void * ptr;
109
+ VALUE address;
110
+ int i, type, len=1, pointer_len, *shape, rank=argc-1;
111
+
112
+ if (argc < 1)
113
+ rb_raise(rb_eArgError, "Type and Size Arguments required");
114
+
115
+ type = na_get_typecode(argv[0]);
116
+ if (type==NA_ROBJ)
117
+ rb_raise(rb_eArgError, "Invalid type");
118
+
119
+ address = rb_funcall(pointer, rb_intern("address"), 0);
120
+ ptr = sizeof(ptr) == 4 ? (void *) NUM2ULONG(address) : (void *) NUM2ULL(address);
121
+
122
+ pointer_len = NUM2INT(rb_funcall(pointer, rb_intern("size"), 0));
123
+
124
+ if (argc == 1) {
125
+ rank = 1;
126
+ shape = ALLOCA_N(int,rank);
127
+ if ( pointer_len % na_sizeof[type] != 0 )
128
+ rb_raise(rb_eArgError, "pointer size mismatch");
129
+ shape[0] = pointer_len / na_sizeof[type];
130
+ }
131
+ else {
132
+ shape = ALLOCA_N(int,rank);
133
+ for (i=0; i<rank; i++)
134
+ len *= shape[i] = NUM2INT(argv[i+1]);
135
+ len *= na_sizeof[type];
136
+ if ( len != pointer_len )
137
+ rb_raise(rb_eArgError, "pointer size mismatch");
138
+ }
139
+
140
+ v = na_make_object_empty( type, rank, shape, cNArray, pointer );
141
+ GetNArray(v,ary);
142
+ ary->ptr = ptr;
143
+
144
+ return v;
145
+ }
146
+
147
+ static VALUE
148
+ na_s_to_na_pointer(int argc, VALUE *argv, VALUE klass)
149
+ {
150
+ VALUE mod;
151
+ VALUE klass_p;
152
+ if (argc < 1){
153
+ rb_raise(rb_eArgError, "Argument is required");
154
+ }
155
+ mod = rb_const_get(rb_cObject, rb_intern("FFI"));
156
+ if (mod != Qnil) {
157
+ klass_p = rb_const_get(mod, rb_intern("Pointer"));
158
+ if ( rb_funcall(argv[0], rb_intern("kind_of?"), 1, klass_p) == Qtrue ){
159
+ return na_pointer_to_na(argc-1,argv+1,argv[0]);
160
+ }
161
+ }
162
+ rb_funcall2(klass, rb_intern("to_na_old"), argc, argv);
163
+ }
164
+
15
165
  void Init_narray_ffi_c() {
16
166
  ID id;
17
167
  VALUE klass;
18
168
  id = rb_intern("NArray");
19
169
  klass = rb_const_get(rb_cObject, id);
20
170
  rb_define_private_method(klass, "address", na_address, 0);
171
+ rb_define_singleton_method(klass, "to_na", na_s_to_na_pointer, -1);
172
+ rb_define_singleton_method(klass, "to_narray", na_s_to_na_pointer, -1);
21
173
  }
22
174
 
data/lib/narray_ffi.rb CHANGED
@@ -1,9 +1,14 @@
1
1
  require "narray"
2
2
  require "ffi"
3
+ class NArray
4
+ class << self
5
+ alias to_na_old to_na
6
+ end
7
+ end
3
8
  require "narray_ffi_c.so"
4
9
 
5
10
  class NArray
6
11
  def to_ptr
7
- return FFI::Pointer::new( address() )
12
+ return FFI::Pointer::new( address() ).slice(0, size*element_size)
8
13
  end
9
14
  end
data/narray_ffi.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'narray_ffi'
3
- s.version = "1.0.0"
3
+ s.version = "1.1.0"
4
4
  s.author = "Brice Videau"
5
5
  s.email = "brice.videau@imag.fr"
6
6
  s.homepage = "https://forge.imag.fr/projects/opencl-ruby/"
@@ -11,6 +11,6 @@ Gem::Specification.new do |s|
11
11
  s.has_rdoc = false
12
12
  s.license = 'BSD'
13
13
  s.required_ruby_version = '>= 1.8.7'
14
- s.add_dependency 'narray', '~> 0.6.0', '>=0.6.0.8'
14
+ s.add_dependency 'narray', '~> 0.6', '>=0.6.0.8'
15
15
  s.add_dependency 'ffi', '~> 1.9', '>=1.9.3'
16
16
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: narray_ffi
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brice Videau
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-21 00:00:00.000000000 Z
11
+ date: 2015-09-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: narray
@@ -16,7 +16,7 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 0.6.0
19
+ version: '0.6'
20
20
  - - ">="
21
21
  - !ruby/object:Gem::Version
22
22
  version: 0.6.0.8
@@ -26,7 +26,7 @@ dependencies:
26
26
  requirements:
27
27
  - - "~>"
28
28
  - !ruby/object:Gem::Version
29
- version: 0.6.0
29
+ version: '0.6'
30
30
  - - ">="
31
31
  - !ruby/object:Gem::Version
32
32
  version: 0.6.0.8