cumo 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +27 -0
- data/.travis.yml +5 -0
- data/3rd_party/mkmf-cu/.gitignore +36 -0
- data/3rd_party/mkmf-cu/Gemfile +3 -0
- data/3rd_party/mkmf-cu/LICENSE +21 -0
- data/3rd_party/mkmf-cu/README.md +36 -0
- data/3rd_party/mkmf-cu/Rakefile +11 -0
- data/3rd_party/mkmf-cu/bin/mkmf-cu-nvcc +4 -0
- data/3rd_party/mkmf-cu/lib/mkmf-cu.rb +32 -0
- data/3rd_party/mkmf-cu/lib/mkmf-cu/cli.rb +80 -0
- data/3rd_party/mkmf-cu/lib/mkmf-cu/nvcc.rb +157 -0
- data/3rd_party/mkmf-cu/mkmf-cu.gemspec +16 -0
- data/3rd_party/mkmf-cu/test/test_mkmf-cu.rb +67 -0
- data/CODE_OF_CONDUCT.md +46 -0
- data/Gemfile +8 -0
- data/LICENSE.txt +82 -0
- data/README.md +252 -0
- data/Rakefile +43 -0
- data/bench/broadcast_fp32.rb +138 -0
- data/bench/cumo_bench.rb +193 -0
- data/bench/numo_bench.rb +138 -0
- data/bench/reduction_fp32.rb +117 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/cumo.gemspec +32 -0
- data/ext/cumo/cuda/cublas.c +278 -0
- data/ext/cumo/cuda/driver.c +421 -0
- data/ext/cumo/cuda/memory_pool.cpp +185 -0
- data/ext/cumo/cuda/memory_pool_impl.cpp +308 -0
- data/ext/cumo/cuda/memory_pool_impl.hpp +370 -0
- data/ext/cumo/cuda/memory_pool_impl_test.cpp +554 -0
- data/ext/cumo/cuda/nvrtc.c +207 -0
- data/ext/cumo/cuda/runtime.c +167 -0
- data/ext/cumo/cumo.c +148 -0
- data/ext/cumo/depend.erb +58 -0
- data/ext/cumo/extconf.rb +179 -0
- data/ext/cumo/include/cumo.h +25 -0
- data/ext/cumo/include/cumo/compat.h +23 -0
- data/ext/cumo/include/cumo/cuda/cublas.h +153 -0
- data/ext/cumo/include/cumo/cuda/cumo_thrust.hpp +187 -0
- data/ext/cumo/include/cumo/cuda/cumo_thrust_complex.hpp +79 -0
- data/ext/cumo/include/cumo/cuda/driver.h +22 -0
- data/ext/cumo/include/cumo/cuda/memory_pool.h +28 -0
- data/ext/cumo/include/cumo/cuda/nvrtc.h +22 -0
- data/ext/cumo/include/cumo/cuda/runtime.h +40 -0
- data/ext/cumo/include/cumo/indexer.h +238 -0
- data/ext/cumo/include/cumo/intern.h +142 -0
- data/ext/cumo/include/cumo/intern_fwd.h +38 -0
- data/ext/cumo/include/cumo/intern_kernel.h +6 -0
- data/ext/cumo/include/cumo/narray.h +429 -0
- data/ext/cumo/include/cumo/narray_kernel.h +149 -0
- data/ext/cumo/include/cumo/ndloop.h +95 -0
- data/ext/cumo/include/cumo/reduce_kernel.h +126 -0
- data/ext/cumo/include/cumo/template.h +158 -0
- data/ext/cumo/include/cumo/template_kernel.h +77 -0
- data/ext/cumo/include/cumo/types/bit.h +40 -0
- data/ext/cumo/include/cumo/types/bit_kernel.h +34 -0
- data/ext/cumo/include/cumo/types/complex.h +402 -0
- data/ext/cumo/include/cumo/types/complex_kernel.h +414 -0
- data/ext/cumo/include/cumo/types/complex_macro.h +382 -0
- data/ext/cumo/include/cumo/types/complex_macro_kernel.h +186 -0
- data/ext/cumo/include/cumo/types/dcomplex.h +46 -0
- data/ext/cumo/include/cumo/types/dcomplex_kernel.h +13 -0
- data/ext/cumo/include/cumo/types/dfloat.h +47 -0
- data/ext/cumo/include/cumo/types/dfloat_kernel.h +14 -0
- data/ext/cumo/include/cumo/types/float_def.h +34 -0
- data/ext/cumo/include/cumo/types/float_def_kernel.h +39 -0
- data/ext/cumo/include/cumo/types/float_macro.h +191 -0
- data/ext/cumo/include/cumo/types/float_macro_kernel.h +158 -0
- data/ext/cumo/include/cumo/types/int16.h +24 -0
- data/ext/cumo/include/cumo/types/int16_kernel.h +23 -0
- data/ext/cumo/include/cumo/types/int32.h +24 -0
- data/ext/cumo/include/cumo/types/int32_kernel.h +19 -0
- data/ext/cumo/include/cumo/types/int64.h +24 -0
- data/ext/cumo/include/cumo/types/int64_kernel.h +19 -0
- data/ext/cumo/include/cumo/types/int8.h +24 -0
- data/ext/cumo/include/cumo/types/int8_kernel.h +19 -0
- data/ext/cumo/include/cumo/types/int_macro.h +67 -0
- data/ext/cumo/include/cumo/types/int_macro_kernel.h +48 -0
- data/ext/cumo/include/cumo/types/real_accum.h +486 -0
- data/ext/cumo/include/cumo/types/real_accum_kernel.h +101 -0
- data/ext/cumo/include/cumo/types/robj_macro.h +80 -0
- data/ext/cumo/include/cumo/types/robj_macro_kernel.h +0 -0
- data/ext/cumo/include/cumo/types/robject.h +27 -0
- data/ext/cumo/include/cumo/types/robject_kernel.h +7 -0
- data/ext/cumo/include/cumo/types/scomplex.h +46 -0
- data/ext/cumo/include/cumo/types/scomplex_kernel.h +13 -0
- data/ext/cumo/include/cumo/types/sfloat.h +48 -0
- data/ext/cumo/include/cumo/types/sfloat_kernel.h +14 -0
- data/ext/cumo/include/cumo/types/uint16.h +25 -0
- data/ext/cumo/include/cumo/types/uint16_kernel.h +20 -0
- data/ext/cumo/include/cumo/types/uint32.h +25 -0
- data/ext/cumo/include/cumo/types/uint32_kernel.h +20 -0
- data/ext/cumo/include/cumo/types/uint64.h +25 -0
- data/ext/cumo/include/cumo/types/uint64_kernel.h +20 -0
- data/ext/cumo/include/cumo/types/uint8.h +25 -0
- data/ext/cumo/include/cumo/types/uint8_kernel.h +20 -0
- data/ext/cumo/include/cumo/types/uint_macro.h +58 -0
- data/ext/cumo/include/cumo/types/uint_macro_kernel.h +38 -0
- data/ext/cumo/include/cumo/types/xint_macro.h +169 -0
- data/ext/cumo/include/cumo/types/xint_macro_kernel.h +88 -0
- data/ext/cumo/narray/SFMT-params.h +97 -0
- data/ext/cumo/narray/SFMT-params19937.h +46 -0
- data/ext/cumo/narray/SFMT.c +620 -0
- data/ext/cumo/narray/SFMT.h +167 -0
- data/ext/cumo/narray/array.c +638 -0
- data/ext/cumo/narray/data.c +961 -0
- data/ext/cumo/narray/gen/cogen.rb +56 -0
- data/ext/cumo/narray/gen/cogen_kernel.rb +58 -0
- data/ext/cumo/narray/gen/def/bit.rb +37 -0
- data/ext/cumo/narray/gen/def/dcomplex.rb +39 -0
- data/ext/cumo/narray/gen/def/dfloat.rb +37 -0
- data/ext/cumo/narray/gen/def/int16.rb +36 -0
- data/ext/cumo/narray/gen/def/int32.rb +36 -0
- data/ext/cumo/narray/gen/def/int64.rb +36 -0
- data/ext/cumo/narray/gen/def/int8.rb +36 -0
- data/ext/cumo/narray/gen/def/robject.rb +37 -0
- data/ext/cumo/narray/gen/def/scomplex.rb +39 -0
- data/ext/cumo/narray/gen/def/sfloat.rb +37 -0
- data/ext/cumo/narray/gen/def/uint16.rb +36 -0
- data/ext/cumo/narray/gen/def/uint32.rb +36 -0
- data/ext/cumo/narray/gen/def/uint64.rb +36 -0
- data/ext/cumo/narray/gen/def/uint8.rb +36 -0
- data/ext/cumo/narray/gen/erbpp2.rb +346 -0
- data/ext/cumo/narray/gen/narray_def.rb +268 -0
- data/ext/cumo/narray/gen/spec.rb +425 -0
- data/ext/cumo/narray/gen/tmpl/accum.c +86 -0
- data/ext/cumo/narray/gen/tmpl/accum_binary.c +121 -0
- data/ext/cumo/narray/gen/tmpl/accum_binary_kernel.cu +61 -0
- data/ext/cumo/narray/gen/tmpl/accum_index.c +119 -0
- data/ext/cumo/narray/gen/tmpl/accum_index_kernel.cu +66 -0
- data/ext/cumo/narray/gen/tmpl/accum_kernel.cu +12 -0
- data/ext/cumo/narray/gen/tmpl/alloc_func.c +107 -0
- data/ext/cumo/narray/gen/tmpl/allocate.c +37 -0
- data/ext/cumo/narray/gen/tmpl/aref.c +66 -0
- data/ext/cumo/narray/gen/tmpl/aref_cpu.c +50 -0
- data/ext/cumo/narray/gen/tmpl/aset.c +56 -0
- data/ext/cumo/narray/gen/tmpl/binary.c +162 -0
- data/ext/cumo/narray/gen/tmpl/binary2.c +70 -0
- data/ext/cumo/narray/gen/tmpl/binary2_kernel.cu +15 -0
- data/ext/cumo/narray/gen/tmpl/binary_kernel.cu +31 -0
- data/ext/cumo/narray/gen/tmpl/binary_s.c +45 -0
- data/ext/cumo/narray/gen/tmpl/binary_s_kernel.cu +15 -0
- data/ext/cumo/narray/gen/tmpl/bincount.c +181 -0
- data/ext/cumo/narray/gen/tmpl/cast.c +44 -0
- data/ext/cumo/narray/gen/tmpl/cast_array.c +13 -0
- data/ext/cumo/narray/gen/tmpl/class.c +9 -0
- data/ext/cumo/narray/gen/tmpl/class_kernel.cu +6 -0
- data/ext/cumo/narray/gen/tmpl/clip.c +121 -0
- data/ext/cumo/narray/gen/tmpl/coerce_cast.c +10 -0
- data/ext/cumo/narray/gen/tmpl/complex_accum_kernel.cu +129 -0
- data/ext/cumo/narray/gen/tmpl/cond_binary.c +68 -0
- data/ext/cumo/narray/gen/tmpl/cond_binary_kernel.cu +18 -0
- data/ext/cumo/narray/gen/tmpl/cond_unary.c +46 -0
- data/ext/cumo/narray/gen/tmpl/cum.c +50 -0
- data/ext/cumo/narray/gen/tmpl/each.c +47 -0
- data/ext/cumo/narray/gen/tmpl/each_with_index.c +70 -0
- data/ext/cumo/narray/gen/tmpl/ewcomp.c +79 -0
- data/ext/cumo/narray/gen/tmpl/ewcomp_kernel.cu +19 -0
- data/ext/cumo/narray/gen/tmpl/extract.c +22 -0
- data/ext/cumo/narray/gen/tmpl/extract_cpu.c +26 -0
- data/ext/cumo/narray/gen/tmpl/extract_data.c +53 -0
- data/ext/cumo/narray/gen/tmpl/eye.c +105 -0
- data/ext/cumo/narray/gen/tmpl/eye_kernel.cu +19 -0
- data/ext/cumo/narray/gen/tmpl/fill.c +52 -0
- data/ext/cumo/narray/gen/tmpl/fill_kernel.cu +29 -0
- data/ext/cumo/narray/gen/tmpl/float_accum_kernel.cu +106 -0
- data/ext/cumo/narray/gen/tmpl/format.c +62 -0
- data/ext/cumo/narray/gen/tmpl/format_to_a.c +49 -0
- data/ext/cumo/narray/gen/tmpl/frexp.c +38 -0
- data/ext/cumo/narray/gen/tmpl/gemm.c +203 -0
- data/ext/cumo/narray/gen/tmpl/init_class.c +20 -0
- data/ext/cumo/narray/gen/tmpl/init_module.c +12 -0
- data/ext/cumo/narray/gen/tmpl/inspect.c +21 -0
- data/ext/cumo/narray/gen/tmpl/lib.c +50 -0
- data/ext/cumo/narray/gen/tmpl/lib_kernel.cu +24 -0
- data/ext/cumo/narray/gen/tmpl/logseq.c +102 -0
- data/ext/cumo/narray/gen/tmpl/logseq_kernel.cu +31 -0
- data/ext/cumo/narray/gen/tmpl/map_with_index.c +98 -0
- data/ext/cumo/narray/gen/tmpl/median.c +66 -0
- data/ext/cumo/narray/gen/tmpl/minmax.c +47 -0
- data/ext/cumo/narray/gen/tmpl/module.c +9 -0
- data/ext/cumo/narray/gen/tmpl/module_kernel.cu +1 -0
- data/ext/cumo/narray/gen/tmpl/new_dim0.c +15 -0
- data/ext/cumo/narray/gen/tmpl/new_dim0_kernel.cu +8 -0
- data/ext/cumo/narray/gen/tmpl/poly.c +50 -0
- data/ext/cumo/narray/gen/tmpl/pow.c +97 -0
- data/ext/cumo/narray/gen/tmpl/pow_kernel.cu +29 -0
- data/ext/cumo/narray/gen/tmpl/powint.c +17 -0
- data/ext/cumo/narray/gen/tmpl/qsort.c +212 -0
- data/ext/cumo/narray/gen/tmpl/rand.c +168 -0
- data/ext/cumo/narray/gen/tmpl/rand_norm.c +121 -0
- data/ext/cumo/narray/gen/tmpl/real_accum_kernel.cu +75 -0
- data/ext/cumo/narray/gen/tmpl/seq.c +112 -0
- data/ext/cumo/narray/gen/tmpl/seq_kernel.cu +43 -0
- data/ext/cumo/narray/gen/tmpl/set2.c +57 -0
- data/ext/cumo/narray/gen/tmpl/sort.c +48 -0
- data/ext/cumo/narray/gen/tmpl/sort_index.c +111 -0
- data/ext/cumo/narray/gen/tmpl/store.c +41 -0
- data/ext/cumo/narray/gen/tmpl/store_array.c +187 -0
- data/ext/cumo/narray/gen/tmpl/store_array_kernel.cu +58 -0
- data/ext/cumo/narray/gen/tmpl/store_bit.c +86 -0
- data/ext/cumo/narray/gen/tmpl/store_bit_kernel.cu +66 -0
- data/ext/cumo/narray/gen/tmpl/store_from.c +81 -0
- data/ext/cumo/narray/gen/tmpl/store_from_kernel.cu +58 -0
- data/ext/cumo/narray/gen/tmpl/store_kernel.cu +3 -0
- data/ext/cumo/narray/gen/tmpl/store_numeric.c +9 -0
- data/ext/cumo/narray/gen/tmpl/to_a.c +43 -0
- data/ext/cumo/narray/gen/tmpl/unary.c +132 -0
- data/ext/cumo/narray/gen/tmpl/unary2.c +60 -0
- data/ext/cumo/narray/gen/tmpl/unary_kernel.cu +72 -0
- data/ext/cumo/narray/gen/tmpl/unary_ret2.c +34 -0
- data/ext/cumo/narray/gen/tmpl/unary_s.c +86 -0
- data/ext/cumo/narray/gen/tmpl/unary_s_kernel.cu +58 -0
- data/ext/cumo/narray/gen/tmpl_bit/allocate.c +24 -0
- data/ext/cumo/narray/gen/tmpl_bit/aref.c +54 -0
- data/ext/cumo/narray/gen/tmpl_bit/aref_cpu.c +57 -0
- data/ext/cumo/narray/gen/tmpl_bit/aset.c +56 -0
- data/ext/cumo/narray/gen/tmpl_bit/binary.c +98 -0
- data/ext/cumo/narray/gen/tmpl_bit/bit_count.c +64 -0
- data/ext/cumo/narray/gen/tmpl_bit/bit_count_cpu.c +88 -0
- data/ext/cumo/narray/gen/tmpl_bit/bit_count_kernel.cu +76 -0
- data/ext/cumo/narray/gen/tmpl_bit/bit_reduce.c +133 -0
- data/ext/cumo/narray/gen/tmpl_bit/each.c +48 -0
- data/ext/cumo/narray/gen/tmpl_bit/each_with_index.c +70 -0
- data/ext/cumo/narray/gen/tmpl_bit/extract.c +30 -0
- data/ext/cumo/narray/gen/tmpl_bit/extract_cpu.c +29 -0
- data/ext/cumo/narray/gen/tmpl_bit/fill.c +69 -0
- data/ext/cumo/narray/gen/tmpl_bit/format.c +64 -0
- data/ext/cumo/narray/gen/tmpl_bit/format_to_a.c +51 -0
- data/ext/cumo/narray/gen/tmpl_bit/inspect.c +21 -0
- data/ext/cumo/narray/gen/tmpl_bit/mask.c +136 -0
- data/ext/cumo/narray/gen/tmpl_bit/none_p.c +14 -0
- data/ext/cumo/narray/gen/tmpl_bit/store_array.c +108 -0
- data/ext/cumo/narray/gen/tmpl_bit/store_bit.c +70 -0
- data/ext/cumo/narray/gen/tmpl_bit/store_from.c +60 -0
- data/ext/cumo/narray/gen/tmpl_bit/to_a.c +47 -0
- data/ext/cumo/narray/gen/tmpl_bit/unary.c +81 -0
- data/ext/cumo/narray/gen/tmpl_bit/where.c +90 -0
- data/ext/cumo/narray/gen/tmpl_bit/where2.c +95 -0
- data/ext/cumo/narray/index.c +880 -0
- data/ext/cumo/narray/kwargs.c +153 -0
- data/ext/cumo/narray/math.c +142 -0
- data/ext/cumo/narray/narray.c +1948 -0
- data/ext/cumo/narray/ndloop.c +2105 -0
- data/ext/cumo/narray/rand.c +45 -0
- data/ext/cumo/narray/step.c +474 -0
- data/ext/cumo/narray/struct.c +886 -0
- data/lib/cumo.rb +3 -0
- data/lib/cumo/cuda.rb +11 -0
- data/lib/cumo/cuda/compile_error.rb +36 -0
- data/lib/cumo/cuda/compiler.rb +161 -0
- data/lib/cumo/cuda/device.rb +47 -0
- data/lib/cumo/cuda/link_state.rb +31 -0
- data/lib/cumo/cuda/module.rb +40 -0
- data/lib/cumo/cuda/nvrtc_program.rb +27 -0
- data/lib/cumo/linalg.rb +12 -0
- data/lib/cumo/narray.rb +2 -0
- data/lib/cumo/narray/extra.rb +1278 -0
- data/lib/erbpp.rb +294 -0
- data/lib/erbpp/line_number.rb +137 -0
- data/lib/erbpp/narray_def.rb +381 -0
- data/numo-narray-version +1 -0
- data/run.gdb +7 -0
- metadata +353 -0
@@ -0,0 +1,167 @@
|
|
1
|
+
/**
|
2
|
+
* @file SFMT.h
|
3
|
+
*
|
4
|
+
* @brief SIMD oriented Fast Mersenne Twister(SFMT) pseudorandom
|
5
|
+
* number generator
|
6
|
+
*
|
7
|
+
* @author Mutsuo Saito (Hiroshima University)
|
8
|
+
* @author Makoto Matsumoto (Hiroshima University)
|
9
|
+
*
|
10
|
+
* Copyright (C) 2006, 2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima
|
11
|
+
* University. All rights reserved.
|
12
|
+
*
|
13
|
+
* The new BSD License is applied to this software.
|
14
|
+
* see LICENSE.txt
|
15
|
+
*
|
16
|
+
* @note We assume that your system has inttypes.h. If your system
|
17
|
+
* doesn't have inttypes.h, you have to typedef uint32_t and uint64_t,
|
18
|
+
* and you have to define PRIu64 and PRIx64 in this file as follows:
|
19
|
+
* @verbatim
|
20
|
+
typedef unsigned int uint32_t
|
21
|
+
typedef unsigned long long uint64_t
|
22
|
+
#define PRIu64 "llu"
|
23
|
+
#define PRIx64 "llx"
|
24
|
+
@endverbatim
|
25
|
+
* uint32_t must be exactly 32-bit unsigned integer type (no more, no
|
26
|
+
* less), and uint64_t must be exactly 64-bit unsigned integer type.
|
27
|
+
* PRIu64 and PRIx64 are used for printf function to print 64-bit
|
28
|
+
* unsigned int and 64-bit unsigned int in hexadecimal format.
|
29
|
+
*/
|
30
|
+
|
31
|
+
#ifndef SFMT_H
|
32
|
+
#define SFMT_H
|
33
|
+
|
34
|
+
#include <stdio.h>
|
35
|
+
|
36
|
+
#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
|
37
|
+
#include <inttypes.h>
|
38
|
+
#elif defined(_MSC_VER) || defined(__BORLANDC__)
|
39
|
+
typedef unsigned int uint32_t;
|
40
|
+
typedef unsigned __int64 uint64_t;
|
41
|
+
#define inline __inline
|
42
|
+
#else
|
43
|
+
#include <inttypes.h>
|
44
|
+
#if defined(__GNUC__)
|
45
|
+
#define inline __inline__
|
46
|
+
#endif
|
47
|
+
#endif
|
48
|
+
|
49
|
+
#ifndef PRIu64
|
50
|
+
#if defined(_MSC_VER) || defined(__BORLANDC__)
|
51
|
+
#define PRIu64 "I64u"
|
52
|
+
#define PRIx64 "I64x"
|
53
|
+
#else
|
54
|
+
#define PRIu64 "llu"
|
55
|
+
#define PRIx64 "llx"
|
56
|
+
#endif
|
57
|
+
#endif
|
58
|
+
|
59
|
+
#if defined(__GNUC__)
|
60
|
+
#define ALWAYSINLINE __attribute__((always_inline))
|
61
|
+
#else
|
62
|
+
#define ALWAYSINLINE
|
63
|
+
#endif
|
64
|
+
|
65
|
+
#if defined(_MSC_VER)
|
66
|
+
#if _MSC_VER >= 1200
|
67
|
+
#define PRE_ALWAYS __forceinline
|
68
|
+
#else
|
69
|
+
#define PRE_ALWAYS inline
|
70
|
+
#endif
|
71
|
+
#else
|
72
|
+
#define PRE_ALWAYS inline
|
73
|
+
#endif
|
74
|
+
|
75
|
+
/* Add cumo_ prefix to avoid symbol colision with numo */
|
76
|
+
#define gen_rand32 cumo_gen_rand32
|
77
|
+
uint32_t cumo_gen_rand32(void);
|
78
|
+
#define gen_rand64 cumo_gen_rand64
|
79
|
+
uint64_t cumo_gen_rand64(void);
|
80
|
+
#define fill_array32 cumo_fill_array32
|
81
|
+
void cumo_fill_array32(uint32_t* array, int size);
|
82
|
+
#define fill_array64 cumo_fill_array64
|
83
|
+
void cumo_fill_array64(uint64_t* array, int size);
|
84
|
+
#define init_gen_rand cumo_init_gen_rand
|
85
|
+
void cumo_init_gen_rand(uint32_t seed);
|
86
|
+
#define init_by_array cumo_init_by_array
|
87
|
+
void cumo_init_by_array(uint32_t* init_key, int key_length);
|
88
|
+
#define get_idstring cumo_get_idstring
|
89
|
+
const char* cumo_get_idstring(void);
|
90
|
+
#define get_min_array_size32 cumo_get_min_array_size32
|
91
|
+
int cumo_get_min_array_size32(void);
|
92
|
+
#define get_min_array_size64 cumo_get_min_array_size64
|
93
|
+
int cumo_get_min_array_size64(void);
|
94
|
+
|
95
|
+
/* These real versions are due to Isaku Wada */
|
96
|
+
/** generates a random number on [0,1]-real-interval */
|
97
|
+
inline static double to_real1(uint32_t v)
|
98
|
+
{
|
99
|
+
return v * (1.0/4294967295.0);
|
100
|
+
/* divided by 2^32-1 */
|
101
|
+
}
|
102
|
+
|
103
|
+
/** generates a random number on [0,1]-real-interval */
|
104
|
+
inline static double genrand_real1(void)
|
105
|
+
{
|
106
|
+
return to_real1(gen_rand32());
|
107
|
+
}
|
108
|
+
|
109
|
+
/** generates a random number on [0,1)-real-interval */
|
110
|
+
inline static double to_real2(uint32_t v)
|
111
|
+
{
|
112
|
+
return v * (1.0/4294967296.0);
|
113
|
+
/* divided by 2^32 */
|
114
|
+
}
|
115
|
+
|
116
|
+
/** generates a random number on [0,1)-real-interval */
|
117
|
+
inline static double genrand_real2(void)
|
118
|
+
{
|
119
|
+
return to_real2(gen_rand32());
|
120
|
+
}
|
121
|
+
|
122
|
+
/** generates a random number on (0,1)-real-interval */
|
123
|
+
inline static double to_real3(uint32_t v)
|
124
|
+
{
|
125
|
+
return (((double)v) + 0.5)*(1.0/4294967296.0);
|
126
|
+
/* divided by 2^32 */
|
127
|
+
}
|
128
|
+
|
129
|
+
/** generates a random number on (0,1)-real-interval */
|
130
|
+
inline static double genrand_real3(void)
|
131
|
+
{
|
132
|
+
return to_real3(gen_rand32());
|
133
|
+
}
|
134
|
+
/** These real versions are due to Isaku Wada */
|
135
|
+
|
136
|
+
/** generates a random number on [0,1) with 53-bit resolution*/
|
137
|
+
inline static double to_res53(uint64_t v)
|
138
|
+
{
|
139
|
+
return v * (1.0/18446744073709551616.0L);
|
140
|
+
}
|
141
|
+
|
142
|
+
/** generates a random number on [0,1) with 53-bit resolution from two
|
143
|
+
* 32 bit integers */
|
144
|
+
inline static double to_res53_mix(uint32_t x, uint32_t y)
|
145
|
+
{
|
146
|
+
return to_res53(x | ((uint64_t)y << 32));
|
147
|
+
}
|
148
|
+
|
149
|
+
/** generates a random number on [0,1) with 53-bit resolution
|
150
|
+
*/
|
151
|
+
inline static double genrand_res53(void)
|
152
|
+
{
|
153
|
+
return to_res53(gen_rand64());
|
154
|
+
}
|
155
|
+
|
156
|
+
/** generates a random number on [0,1) with 53-bit resolution
|
157
|
+
using 32bit integer.
|
158
|
+
*/
|
159
|
+
inline static double genrand_res53_mix(void)
|
160
|
+
{
|
161
|
+
uint32_t x, y;
|
162
|
+
|
163
|
+
x = gen_rand32();
|
164
|
+
y = gen_rand32();
|
165
|
+
return to_res53_mix(x, y);
|
166
|
+
}
|
167
|
+
#endif
|
@@ -0,0 +1,638 @@
|
|
1
|
+
#include <ruby.h>
|
2
|
+
#include "cumo/narray.h"
|
3
|
+
|
4
|
+
// mdai: Multi-Dimensional Array Investigation
|
5
|
+
typedef struct {
|
6
|
+
size_t shape;
|
7
|
+
VALUE val;
|
8
|
+
} na_mdai_item_t;
|
9
|
+
|
10
|
+
typedef struct {
|
11
|
+
int capa;
|
12
|
+
na_mdai_item_t *item;
|
13
|
+
int type; // Ruby numeric type - investigated separately
|
14
|
+
VALUE na_type; // NArray type
|
15
|
+
VALUE int_max;
|
16
|
+
} na_mdai_t;
|
17
|
+
|
18
|
+
// Order of Ruby object.
|
19
|
+
enum { NA_NONE, NA_BIT, NA_INT32, NA_INT64, NA_RATIONAL,
|
20
|
+
NA_DFLOAT, NA_DCOMPLEX, NA_ROBJ, NA_NTYPES };
|
21
|
+
|
22
|
+
static ID id_begin;
|
23
|
+
static ID id_end;
|
24
|
+
static ID id_step;
|
25
|
+
static ID id_abs;
|
26
|
+
static ID id_cast;
|
27
|
+
static ID id_le;
|
28
|
+
static ID id_Complex;
|
29
|
+
|
30
|
+
|
31
|
+
static VALUE
|
32
|
+
na_object_type(int type, VALUE v)
|
33
|
+
{
|
34
|
+
static VALUE int32_max = Qnil;
|
35
|
+
if (NIL_P(int32_max))
|
36
|
+
int32_max = ULONG2NUM(2147483647);
|
37
|
+
|
38
|
+
switch(TYPE(v)) {
|
39
|
+
|
40
|
+
case T_TRUE:
|
41
|
+
case T_FALSE:
|
42
|
+
if (type<NA_BIT)
|
43
|
+
return NA_BIT;
|
44
|
+
return type;
|
45
|
+
|
46
|
+
#if SIZEOF_LONG == 4
|
47
|
+
case T_FIXNUM:
|
48
|
+
if (type<NA_INT32)
|
49
|
+
return NA_INT32;
|
50
|
+
return type;
|
51
|
+
case T_BIGNUM:
|
52
|
+
if (type<NA_INT64) {
|
53
|
+
v = rb_funcall(v,id_abs,0);
|
54
|
+
if (RTEST(rb_funcall(v,id_le,1,int32_max))) {
|
55
|
+
if (type<NA_INT32)
|
56
|
+
return NA_INT32;
|
57
|
+
} else {
|
58
|
+
return NA_INT64;
|
59
|
+
}
|
60
|
+
}
|
61
|
+
return type;
|
62
|
+
|
63
|
+
#elif SIZEOF_LONG == 8
|
64
|
+
case T_FIXNUM:
|
65
|
+
if (type<NA_INT64) {
|
66
|
+
long x = NUM2LONG(v);
|
67
|
+
if (x<0) x=-x;
|
68
|
+
if (x<=2147483647) {
|
69
|
+
if (type<NA_INT32)
|
70
|
+
return NA_INT32;
|
71
|
+
} else {
|
72
|
+
return NA_INT64;
|
73
|
+
}
|
74
|
+
}
|
75
|
+
return type;
|
76
|
+
case T_BIGNUM:
|
77
|
+
if (type<NA_INT64)
|
78
|
+
return NA_INT64;
|
79
|
+
return type;
|
80
|
+
#else
|
81
|
+
case T_FIXNUM:
|
82
|
+
case T_BIGNUM:
|
83
|
+
if (type<NA_INT64) {
|
84
|
+
v = rb_funcall(v,id_abs,0);
|
85
|
+
if (RTEST(rb_funcall(v,id_le,1,int32_max))) {
|
86
|
+
if (type<NA_INT32)
|
87
|
+
return NA_INT32;
|
88
|
+
} else {
|
89
|
+
return NA_INT64;
|
90
|
+
}
|
91
|
+
}
|
92
|
+
return type;
|
93
|
+
#endif
|
94
|
+
|
95
|
+
case T_FLOAT:
|
96
|
+
if (type<NA_DFLOAT)
|
97
|
+
return NA_DFLOAT;
|
98
|
+
return type;
|
99
|
+
|
100
|
+
case T_NIL:
|
101
|
+
return type;
|
102
|
+
|
103
|
+
default:
|
104
|
+
if (CLASS_OF(v) == rb_const_get( rb_cObject, id_Complex )) {
|
105
|
+
return NA_DCOMPLEX;
|
106
|
+
}
|
107
|
+
}
|
108
|
+
return NA_ROBJ;
|
109
|
+
}
|
110
|
+
|
111
|
+
|
112
|
+
#define MDAI_ATTR_TYPE(tp,v,attr) \
|
113
|
+
{tp = na_object_type(tp,rb_funcall(v,id_##attr,0));}
|
114
|
+
|
115
|
+
static int na_mdai_object_type(int type, VALUE v)
|
116
|
+
{
|
117
|
+
if (rb_obj_is_kind_of(v, rb_cRange)) {
|
118
|
+
MDAI_ATTR_TYPE(type,v,begin);
|
119
|
+
MDAI_ATTR_TYPE(type,v,end);
|
120
|
+
} else if (rb_obj_is_kind_of(v, na_cStep)) {
|
121
|
+
MDAI_ATTR_TYPE(type,v,begin);
|
122
|
+
MDAI_ATTR_TYPE(type,v,end);
|
123
|
+
MDAI_ATTR_TYPE(type,v,step);
|
124
|
+
} else {
|
125
|
+
type = na_object_type(type,v);
|
126
|
+
}
|
127
|
+
return type;
|
128
|
+
}
|
129
|
+
|
130
|
+
|
131
|
+
static na_mdai_t *
|
132
|
+
na_mdai_alloc(VALUE ary)
|
133
|
+
{
|
134
|
+
int i, n=4;
|
135
|
+
na_mdai_t *mdai;
|
136
|
+
|
137
|
+
mdai = ALLOC(na_mdai_t);
|
138
|
+
mdai->capa = n;
|
139
|
+
mdai->item = ALLOC_N( na_mdai_item_t, n );
|
140
|
+
for (i=0; i<n; i++) {
|
141
|
+
mdai->item[i].shape = 0;
|
142
|
+
mdai->item[i].val = Qnil;
|
143
|
+
}
|
144
|
+
mdai->item[0].val = ary;
|
145
|
+
mdai->type = NA_NONE;
|
146
|
+
mdai->na_type = Qnil;
|
147
|
+
|
148
|
+
return mdai;
|
149
|
+
}
|
150
|
+
|
151
|
+
static void
|
152
|
+
na_mdai_realloc(na_mdai_t *mdai, int n_extra)
|
153
|
+
{
|
154
|
+
int i, n;
|
155
|
+
|
156
|
+
i = mdai->capa;
|
157
|
+
mdai->capa += n_extra;
|
158
|
+
n = mdai->capa;
|
159
|
+
REALLOC_N( mdai->item, na_mdai_item_t, n );
|
160
|
+
for (; i<n; i++) {
|
161
|
+
mdai->item[i].shape = 0;
|
162
|
+
mdai->item[i].val = Qnil;
|
163
|
+
}
|
164
|
+
}
|
165
|
+
|
166
|
+
static void
|
167
|
+
na_mdai_free(void *ptr)
|
168
|
+
{
|
169
|
+
na_mdai_t *mdai = (na_mdai_t*)ptr;
|
170
|
+
xfree(mdai->item);
|
171
|
+
xfree(mdai);
|
172
|
+
}
|
173
|
+
|
174
|
+
|
175
|
+
/* investigate ndim, shape, type of Array */
|
176
|
+
static int
|
177
|
+
na_mdai_investigate(na_mdai_t *mdai, int ndim)
|
178
|
+
{
|
179
|
+
ssize_t i;
|
180
|
+
int j;
|
181
|
+
size_t len, length;
|
182
|
+
double dbeg, dstep;
|
183
|
+
VALUE v;
|
184
|
+
VALUE val;
|
185
|
+
|
186
|
+
val = mdai->item[ndim-1].val;
|
187
|
+
len = RARRAY_LEN(val);
|
188
|
+
|
189
|
+
for (i=0; i < RARRAY_LEN(val); i++) {
|
190
|
+
v = RARRAY_AREF(val,i);
|
191
|
+
|
192
|
+
if (TYPE(v) == T_ARRAY) {
|
193
|
+
/* check recursive array */
|
194
|
+
for (j=0; j<ndim; j++) {
|
195
|
+
if (mdai->item[j].val == v)
|
196
|
+
rb_raise(rb_eStandardError,
|
197
|
+
"cannot convert from a recursive Array to NArray");
|
198
|
+
}
|
199
|
+
if ( ndim >= mdai->capa ) {
|
200
|
+
na_mdai_realloc(mdai,4);
|
201
|
+
}
|
202
|
+
mdai->item[ndim].val = v;
|
203
|
+
if ( na_mdai_investigate(mdai,ndim+1) ) {
|
204
|
+
len--; /* Array is empty */
|
205
|
+
}
|
206
|
+
}
|
207
|
+
else
|
208
|
+
if (rb_obj_is_kind_of(v, rb_cRange) || rb_obj_is_kind_of(v, na_cStep)) {
|
209
|
+
nary_step_sequence(v,&length,&dbeg,&dstep);
|
210
|
+
len += length-1;
|
211
|
+
mdai->type = na_mdai_object_type(mdai->type, v);
|
212
|
+
}
|
213
|
+
else if (IsNArray(v)) {
|
214
|
+
int r;
|
215
|
+
narray_t *na;
|
216
|
+
GetNArray(v,na);
|
217
|
+
if ( na->ndim == 0 ) {
|
218
|
+
len--; /* NArray is empty */
|
219
|
+
} else {
|
220
|
+
if ( ndim+na->ndim > mdai->capa ) {
|
221
|
+
na_mdai_realloc(mdai,((na->ndim-1)/4+1)*4);
|
222
|
+
}
|
223
|
+
for ( j=0,r=ndim; j < na->ndim ; j++,r++ ) {
|
224
|
+
if ( mdai->item[r].shape < na->shape[j] )
|
225
|
+
mdai->item[r].shape = na->shape[j];
|
226
|
+
}
|
227
|
+
}
|
228
|
+
// type
|
229
|
+
if (NIL_P(mdai->na_type)) {
|
230
|
+
mdai->na_type = CLASS_OF(v);
|
231
|
+
} else {
|
232
|
+
mdai->na_type = na_upcast(CLASS_OF(v), mdai->na_type);
|
233
|
+
}
|
234
|
+
} else {
|
235
|
+
mdai->type = na_mdai_object_type(mdai->type, v);
|
236
|
+
}
|
237
|
+
}
|
238
|
+
|
239
|
+
if (len==0) return 1; /* this array is empty */
|
240
|
+
if (mdai->item[ndim-1].shape < len) {
|
241
|
+
mdai->item[ndim-1].shape = len;
|
242
|
+
}
|
243
|
+
return 0;
|
244
|
+
}
|
245
|
+
|
246
|
+
|
247
|
+
static inline int
|
248
|
+
na_mdai_ndim(na_mdai_t *mdai)
|
249
|
+
{
|
250
|
+
int i;
|
251
|
+
// Dimension
|
252
|
+
for (i=0; i < mdai->capa && mdai->item[i].shape > 0; i++) ;
|
253
|
+
return i;
|
254
|
+
}
|
255
|
+
|
256
|
+
static inline void
|
257
|
+
na_mdai_shape(na_mdai_t *mdai, int ndim, size_t *shape)
|
258
|
+
{
|
259
|
+
int i;
|
260
|
+
for (i=0; i<ndim; i++) {
|
261
|
+
shape[i] = mdai->item[i].shape;
|
262
|
+
}
|
263
|
+
}
|
264
|
+
|
265
|
+
static VALUE
|
266
|
+
na_mdai_dtype_numeric(int type)
|
267
|
+
{
|
268
|
+
VALUE tp;
|
269
|
+
// DataType
|
270
|
+
switch(type) {
|
271
|
+
case NA_BIT:
|
272
|
+
tp = cumo_cBit;
|
273
|
+
break;
|
274
|
+
case NA_INT32:
|
275
|
+
tp = cumo_cInt32;
|
276
|
+
break;
|
277
|
+
case NA_INT64:
|
278
|
+
tp = cumo_cInt64;
|
279
|
+
break;
|
280
|
+
case NA_DFLOAT:
|
281
|
+
tp = cumo_cDFloat;
|
282
|
+
break;
|
283
|
+
case NA_DCOMPLEX:
|
284
|
+
tp = cumo_cDComplex;
|
285
|
+
break;
|
286
|
+
case NA_ROBJ:
|
287
|
+
tp = cumo_cRObject;
|
288
|
+
break;
|
289
|
+
default:
|
290
|
+
tp = Qnil;
|
291
|
+
}
|
292
|
+
return tp;
|
293
|
+
}
|
294
|
+
|
295
|
+
static VALUE
|
296
|
+
na_mdai_dtype(na_mdai_t *mdai)
|
297
|
+
{
|
298
|
+
VALUE tp;
|
299
|
+
|
300
|
+
tp = na_mdai_dtype_numeric(mdai->type);
|
301
|
+
|
302
|
+
if (!NIL_P(mdai->na_type)) {
|
303
|
+
if (NIL_P(tp)) {
|
304
|
+
tp = mdai->na_type;
|
305
|
+
} else {
|
306
|
+
tp = na_upcast(mdai->na_type,tp);
|
307
|
+
}
|
308
|
+
}
|
309
|
+
return tp;
|
310
|
+
}
|
311
|
+
|
312
|
+
|
313
|
+
static inline VALUE
|
314
|
+
update_type(VALUE *ptype, VALUE dtype)
|
315
|
+
{
|
316
|
+
if (ptype) {
|
317
|
+
if (*ptype == cNArray || !RTEST(*ptype)) {
|
318
|
+
*ptype = dtype;
|
319
|
+
} else {
|
320
|
+
dtype = *ptype;
|
321
|
+
}
|
322
|
+
}
|
323
|
+
return dtype;
|
324
|
+
}
|
325
|
+
|
326
|
+
static inline void
|
327
|
+
check_subclass_of_narray(VALUE dtype)
|
328
|
+
{
|
329
|
+
if (RTEST(rb_obj_is_kind_of(dtype, rb_cClass))) {
|
330
|
+
if (RTEST(rb_funcall(dtype, id_le, 1, cNArray))) {
|
331
|
+
return;
|
332
|
+
}
|
333
|
+
}
|
334
|
+
rb_raise(nary_eCastError, "cannot convert to NArray");
|
335
|
+
}
|
336
|
+
|
337
|
+
|
338
|
+
static size_t
|
339
|
+
na_mdai_memsize(const void *ptr)
|
340
|
+
{
|
341
|
+
const na_mdai_t *mdai = (const na_mdai_t*)ptr;
|
342
|
+
|
343
|
+
return sizeof(na_mdai_t) + mdai->capa * sizeof(na_mdai_item_t);
|
344
|
+
}
|
345
|
+
|
346
|
+
static const rb_data_type_t mdai_data_type = {
|
347
|
+
"Cumo::NArray/mdai",
|
348
|
+
{NULL, na_mdai_free, na_mdai_memsize,},
|
349
|
+
0, 0, RUBY_TYPED_FREE_IMMEDIATELY|RUBY_TYPED_WB_PROTECTED
|
350
|
+
};
|
351
|
+
|
352
|
+
|
353
|
+
static void
|
354
|
+
na_composition3_ary(VALUE ary, VALUE *ptype, VALUE *pshape, VALUE *pnary)
|
355
|
+
{
|
356
|
+
VALUE vmdai;
|
357
|
+
na_mdai_t *mdai;
|
358
|
+
int i, ndim;
|
359
|
+
size_t *shape;
|
360
|
+
VALUE dtype, dshape;
|
361
|
+
|
362
|
+
mdai = na_mdai_alloc(ary);
|
363
|
+
vmdai = TypedData_Wrap_Struct(rb_cData, &mdai_data_type, (void*)mdai);
|
364
|
+
if ( na_mdai_investigate(mdai, 1) ) {
|
365
|
+
// empty
|
366
|
+
dtype = update_type(ptype, cumo_cInt32);
|
367
|
+
if (pshape) {
|
368
|
+
*pshape = rb_ary_new3(1, INT2FIX(0));
|
369
|
+
}
|
370
|
+
if (pnary) {
|
371
|
+
check_subclass_of_narray(dtype);
|
372
|
+
shape = ALLOCA_N(size_t, 1);
|
373
|
+
shape[0] = 0;
|
374
|
+
*pnary = nary_new(dtype, 1, shape);
|
375
|
+
}
|
376
|
+
} else {
|
377
|
+
ndim = na_mdai_ndim(mdai);
|
378
|
+
shape = ALLOCA_N(size_t, ndim);
|
379
|
+
na_mdai_shape(mdai, ndim, shape);
|
380
|
+
dtype = update_type(ptype, na_mdai_dtype(mdai));
|
381
|
+
if (pshape) {
|
382
|
+
dshape = rb_ary_new2(ndim);
|
383
|
+
for (i=0; i<ndim; i++) {
|
384
|
+
rb_ary_push(dshape, SIZET2NUM(shape[i]));
|
385
|
+
}
|
386
|
+
*pshape = dshape;
|
387
|
+
}
|
388
|
+
if (pnary) {
|
389
|
+
check_subclass_of_narray(dtype);
|
390
|
+
*pnary = nary_new(dtype, ndim, shape);
|
391
|
+
}
|
392
|
+
}
|
393
|
+
RB_GC_GUARD(vmdai);
|
394
|
+
}
|
395
|
+
|
396
|
+
|
397
|
+
static void
|
398
|
+
na_composition3(VALUE obj, VALUE *ptype, VALUE *pshape, VALUE *pnary)
|
399
|
+
{
|
400
|
+
VALUE dtype, dshape;
|
401
|
+
|
402
|
+
if (TYPE(obj) == T_ARRAY) {
|
403
|
+
na_composition3_ary(obj, ptype, pshape, pnary);
|
404
|
+
}
|
405
|
+
else if (RTEST(rb_obj_is_kind_of(obj,rb_cNumeric))) {
|
406
|
+
dtype = na_mdai_dtype_numeric(na_mdai_object_type(NA_NONE, obj));
|
407
|
+
dtype = update_type(ptype, dtype);
|
408
|
+
if (pshape) {
|
409
|
+
*pshape = rb_ary_new();
|
410
|
+
}
|
411
|
+
if (pnary) {
|
412
|
+
check_subclass_of_narray(dtype);
|
413
|
+
*pnary = nary_new(dtype, 0, 0);
|
414
|
+
}
|
415
|
+
}
|
416
|
+
else if (IsNArray(obj)) {
|
417
|
+
int i, ndim;
|
418
|
+
narray_t *na;
|
419
|
+
GetNArray(obj,na);
|
420
|
+
ndim = na->ndim;
|
421
|
+
dtype = update_type(ptype, CLASS_OF(obj));
|
422
|
+
if (pshape) {
|
423
|
+
dshape = rb_ary_new2(ndim);
|
424
|
+
for (i=0; i<ndim; i++) {
|
425
|
+
rb_ary_push(dshape, SIZET2NUM(na->shape[i]));
|
426
|
+
}
|
427
|
+
*pshape = dshape;
|
428
|
+
}
|
429
|
+
if (pnary) {
|
430
|
+
*pnary = nary_new(dtype, ndim, na->shape);
|
431
|
+
}
|
432
|
+
} else {
|
433
|
+
rb_raise(rb_eTypeError,"invalid type for NArray: %s",
|
434
|
+
rb_class2name(CLASS_OF(obj)));
|
435
|
+
}
|
436
|
+
}
|
437
|
+
|
438
|
+
|
439
|
+
static VALUE
|
440
|
+
na_s_array_shape(VALUE mod, VALUE ary)
|
441
|
+
{
|
442
|
+
VALUE shape;
|
443
|
+
|
444
|
+
if (TYPE(ary) != T_ARRAY) {
|
445
|
+
// 0-dimension
|
446
|
+
return rb_ary_new();
|
447
|
+
}
|
448
|
+
na_composition3(ary, 0, &shape, 0);
|
449
|
+
return shape;
|
450
|
+
}
|
451
|
+
|
452
|
+
|
453
|
+
/*
|
454
|
+
Generate new unallocated NArray instance with shape and type defined from obj.
|
455
|
+
Cumo::NArray.new_like(obj) returns instance whose type is defined from obj.
|
456
|
+
Cumo::DFloat.new_like(obj) returns DFloat instance.
|
457
|
+
|
458
|
+
@overload new_like(obj)
|
459
|
+
@param [Numeric,Array,Cumo::NArray] obj
|
460
|
+
@return [Cumo::NArray]
|
461
|
+
@example
|
462
|
+
Cumo::NArray.new_like([[1,2,3],[4,5,6]])
|
463
|
+
=> Cumo::Int32#shape=[2,3](empty)
|
464
|
+
Cumo::DFloat.new_like([[1,2],[3,4]])
|
465
|
+
=> Cumo::DFloat#shape=[2,2](empty)
|
466
|
+
Cumo::NArray.new_like([1,2i,3])
|
467
|
+
=> Cumo::DComplex#shape=[3](empty)
|
468
|
+
*/
|
469
|
+
VALUE
|
470
|
+
na_s_new_like(VALUE type, VALUE obj)
|
471
|
+
{
|
472
|
+
VALUE newary;
|
473
|
+
|
474
|
+
na_composition3(obj, &type, 0, &newary);
|
475
|
+
return newary;
|
476
|
+
}
|
477
|
+
|
478
|
+
|
479
|
+
VALUE
|
480
|
+
na_ary_composition_dtype(VALUE ary)
|
481
|
+
{
|
482
|
+
VALUE type = Qnil;
|
483
|
+
|
484
|
+
na_composition3(ary, &type, 0, 0);
|
485
|
+
return type;
|
486
|
+
}
|
487
|
+
|
488
|
+
static VALUE
|
489
|
+
na_s_array_type(VALUE mod, VALUE ary)
|
490
|
+
{
|
491
|
+
return na_ary_composition_dtype(ary);
|
492
|
+
}
|
493
|
+
|
494
|
+
|
495
|
+
/*
|
496
|
+
Generate NArray object. NArray datatype is automatically selected.
|
497
|
+
@overload [](elements)
|
498
|
+
@param [Numeric,Array] elements
|
499
|
+
@return [NArray]
|
500
|
+
*/
|
501
|
+
static VALUE
|
502
|
+
nary_s_bracket(VALUE klass, VALUE ary)
|
503
|
+
{
|
504
|
+
VALUE dtype=Qnil;
|
505
|
+
|
506
|
+
if (TYPE(ary)!=T_ARRAY) {
|
507
|
+
rb_bug("Argument is not array");
|
508
|
+
}
|
509
|
+
dtype = na_ary_composition_dtype(ary);
|
510
|
+
check_subclass_of_narray(dtype);
|
511
|
+
return rb_funcall(dtype, id_cast, 1, ary);
|
512
|
+
}
|
513
|
+
|
514
|
+
|
515
|
+
//VALUE
|
516
|
+
//nst_check_compatibility(VALUE self, VALUE ary);
|
517
|
+
|
518
|
+
|
519
|
+
/* investigate ndim, shape, type of Array */
|
520
|
+
/*
|
521
|
+
static int
|
522
|
+
na_mdai_for_struct(na_mdai_t *mdai, int ndim)
|
523
|
+
{
|
524
|
+
size_t i;
|
525
|
+
int j, r;
|
526
|
+
size_t len;
|
527
|
+
VALUE v;
|
528
|
+
VALUE val;
|
529
|
+
narray_t *na;
|
530
|
+
|
531
|
+
//fprintf(stderr,"ndim=%d\n",ndim); rb_p(mdai->na_type);
|
532
|
+
if (ndim>4) { abort(); }
|
533
|
+
val = mdai->item[ndim].val;
|
534
|
+
|
535
|
+
//fpintf(stderr,"val = "); rb_p(val);
|
536
|
+
|
537
|
+
if (CLASS_OF(val) == mdai->na_type) {
|
538
|
+
GetNArray(val,na);
|
539
|
+
if ( ndim+na->ndim > mdai->capa ) {
|
540
|
+
abort();
|
541
|
+
na_mdai_realloc(mdai,((na->ndim-1)/4+1)*4);
|
542
|
+
}
|
543
|
+
for ( j=0,r=ndim; j < na->ndim; j++,r++ ) {
|
544
|
+
if ( mdai->item[r].shape < na->shape[j] )
|
545
|
+
mdai->item[r].shape = na->shape[j];
|
546
|
+
}
|
547
|
+
return 1;
|
548
|
+
}
|
549
|
+
|
550
|
+
if (TYPE(val) == T_ARRAY) {
|
551
|
+
// check recursive array
|
552
|
+
for (j=0; j<ndim-1; j++) {
|
553
|
+
if (mdai->item[j].val == val)
|
554
|
+
rb_raise(rb_eStandardError,
|
555
|
+
"cannot convert from a recursive Array to NArray");
|
556
|
+
}
|
557
|
+
//fprintf(stderr,"check:"); rb_p(val);
|
558
|
+
// val is a Struct recort
|
559
|
+
if (RTEST( nst_check_compatibility(mdai->na_type, val) )) {
|
560
|
+
//fputs("compati\n",stderr);
|
561
|
+
return 1;
|
562
|
+
}
|
563
|
+
// otherwise, multi-dimention
|
564
|
+
if (ndim >= mdai->capa) {
|
565
|
+
//fprintf(stderr,"exeed capa\n"); abort();
|
566
|
+
na_mdai_realloc(mdai,4);
|
567
|
+
}
|
568
|
+
// finally, multidimension-check
|
569
|
+
len = RARRAY_LEN(val);
|
570
|
+
for (i=0; i < len; i++) {
|
571
|
+
v = RARRAY_AREF(val,i);
|
572
|
+
if (TYPE(v) != T_ARRAY) {
|
573
|
+
//abort();
|
574
|
+
return 0;
|
575
|
+
}
|
576
|
+
}
|
577
|
+
for (i=0; i < len; i++) {
|
578
|
+
v = RARRAY_AREF(val,i);
|
579
|
+
//fprintf(stderr,"check:"); rb_p(v);
|
580
|
+
mdai->item[ndim+1].val = v;
|
581
|
+
if ( na_mdai_for_struct( mdai, ndim+1 ) == 0 ) {
|
582
|
+
//fprintf(stderr,"not struct:"); rb_p(v);
|
583
|
+
//abort();
|
584
|
+
return 0;
|
585
|
+
}
|
586
|
+
}
|
587
|
+
if (mdai->item[ndim].shape < len) {
|
588
|
+
mdai->item[ndim].shape = len;
|
589
|
+
}
|
590
|
+
return 1;
|
591
|
+
}
|
592
|
+
|
593
|
+
//fprintf(stderr,"invalid for struct:"); rb_p(val); abort();
|
594
|
+
return 0;
|
595
|
+
}
|
596
|
+
*/
|
597
|
+
|
598
|
+
|
599
|
+
/*
|
600
|
+
VALUE
|
601
|
+
na_ary_composition_for_struct(VALUE nstruct, VALUE ary)
|
602
|
+
{
|
603
|
+
volatile VALUE vmdai, vnc;
|
604
|
+
na_mdai_t *mdai;
|
605
|
+
na_compose_t *nc;
|
606
|
+
|
607
|
+
mdai = na_mdai_alloc(ary);
|
608
|
+
mdai->na_type = nstruct;
|
609
|
+
vmdai = TypedData_Wrap_Struct(rb_cData, &mdai_data_type, (void*)mdai);
|
610
|
+
na_mdai_for_struct(mdai, 0);
|
611
|
+
nc = na_compose_alloc();
|
612
|
+
vnc = WrapCompose(nc);
|
613
|
+
na_mdai_result(mdai, nc);
|
614
|
+
//fprintf(stderr,"nc->ndim=%d\n",nc->ndim);
|
615
|
+
rb_gc_force_recycle(vmdai);
|
616
|
+
return vnc;
|
617
|
+
}
|
618
|
+
*/
|
619
|
+
|
620
|
+
|
621
|
+
|
622
|
+
void
|
623
|
+
Init_cumo_nary_array()
|
624
|
+
{
|
625
|
+
rb_define_singleton_method(cNArray, "array_shape", na_s_array_shape, 1);
|
626
|
+
rb_define_singleton_method(cNArray, "array_type", na_s_array_type, 1);
|
627
|
+
rb_define_singleton_method(cNArray, "new_like", na_s_new_like, 1);
|
628
|
+
|
629
|
+
rb_define_singleton_method(cNArray, "[]", nary_s_bracket, -2);
|
630
|
+
|
631
|
+
id_begin = rb_intern("begin");
|
632
|
+
id_end = rb_intern("end");
|
633
|
+
id_step = rb_intern("step");
|
634
|
+
id_cast = rb_intern("cast");
|
635
|
+
id_abs = rb_intern("abs");
|
636
|
+
id_le = rb_intern("<=");
|
637
|
+
id_Complex = rb_intern("Complex");
|
638
|
+
}
|