fast_statistics 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 77ab0c69365318bdd1f68d562c0460ed573d12bf1301542e65fe7ff204f70118
4
- data.tar.gz: ff64565f990ea2d6f77c52afbb63d7271f25737744be191146d5935d69727a30
3
+ metadata.gz: f74e28e9d460ef4ebcab5b0efa21ef0bd7aa1bc17ec398292dbc90fb2f759c10
4
+ data.tar.gz: 1ab07eeea3e7e139b0305b0d4109f780d2d7f372a0c3d8998ffc297a690f8eef
5
5
  SHA512:
6
- metadata.gz: 898331717252505cff2e972ee2fb380709b070209197bf56374fe3c398aa906945c71a543f435d493aa5f5c5f7e7c65100979772f56bd5d28eb68967788fa6d3
7
- data.tar.gz: e725e1d41c7749b8bc14eca78e46f574d477e12ddce3c8cd09817af82b4581ca3dcf24f2f2e2ea06128c2c65fe906ba365c38c7e911d20210b474b698065a01b
6
+ metadata.gz: 7ce73ccecf92df45a42789a45dab9d00ea0642d5b08904e07ecb43eb75148c33f4d645737cf1999fd2b9b7931e80b5efc9f3882756b62820464481ae8a954bfe
7
+ data.tar.gz: 8e9385b1776b36d9d45c402ccc3015ecc01c4c52a95fae4321f3f6409ebc050921192d2c049cb8b453c49a46defd899d25a84491dbb811f9e10ae3bc2caa2b60
@@ -6,21 +6,32 @@ namespace array_2d
6
6
 
7
7
  DFloat::~DFloat()
8
8
  {
9
- free(entries);
10
- delete[] stats;
9
+ if (data_initialized) {
10
+ free(entries);
11
+ delete[] stats;
12
+ }
11
13
  }
12
14
 
13
- DFloat::DFloat(VALUE arrays)
15
+ DFloat::DFloat(VALUE arrays, bool initialize_data)
14
16
  {
15
- cols = rb_array_len(arrays);
16
- rows = rb_array_len(rb_ary_entry(arrays, 0));
17
- entries = (double*)malloc(cols * rows * sizeof(double));
18
- stats = NULL;
19
-
20
- for (int j = 0; j < cols; j++) {
21
- for (int i = 0; i < rows; i++) {
22
- entries[j * rows + i] = (double)NUM2DBL(rb_ary_entry(rb_ary_entry(arrays, j), i));
17
+ data_initialized = initialize_data;
18
+
19
+ if (initialize_data) {
20
+ cols = rb_array_len(arrays);
21
+ rows = rb_array_len(rb_ary_entry(arrays, 0));
22
+ entries = (double*)malloc(cols * rows * sizeof(double));
23
+ stats = new Stats[cols];
24
+
25
+ for (int j = 0; j < cols; j++) {
26
+ for (int i = 0; i < rows; i++) {
27
+ entries[j * rows + i] = (double)NUM2DBL(rb_ary_entry(rb_ary_entry(arrays, j), i));
28
+ }
23
29
  }
30
+ } else {
31
+ cols = 0;
32
+ rows = 0;
33
+ entries = NULL;
34
+ stats = NULL;
24
35
  }
25
36
  }
26
37
 
@@ -68,7 +79,7 @@ DFloat::standard_deviation(double* col, double mean)
68
79
  Stats*
69
80
  DFloat::descriptive_statistics()
70
81
  {
71
- stats = new Stats[cols];
82
+ if (!data_initialized) return stats;
72
83
 
73
84
  for (int col = 0; col < cols; col++) {
74
85
  Stats var_stats;
@@ -137,6 +148,7 @@ Stats*
137
148
  DFloat::descriptive_statistics_packed()
138
149
  {
139
150
  stats = new Stats[cols];
151
+ if (!data_initialized) return stats;
140
152
  const int simd_pack_size = 2;
141
153
 
142
154
  __m128d lengths = _mm_set_pd1((double)rows);
@@ -44,10 +44,11 @@ class DFloat
44
44
  public:
45
45
  int cols;
46
46
  int rows;
47
+ bool data_initialized;
47
48
  double* entries;
48
49
  Stats* stats;
49
50
 
50
- DFloat(VALUE ruby_arr);
51
+ DFloat(VALUE ruby_arr, bool initialize_data);
51
52
  ~DFloat();
52
53
 
53
54
  Stats* descriptive_statistics();
@@ -40,17 +40,16 @@ build_results_hashes(Stats* stats, int num_variables)
40
40
 
41
41
  // Common
42
42
  void
43
- free_wrapped_array(void* array)
43
+ free_wrapped_array(void* dfloat)
44
44
  {
45
- ((DFloat*)array)->~DFloat();
45
+ ((DFloat*)dfloat)->~DFloat();
46
+ free(dfloat);
46
47
  }
47
48
 
48
49
  size_t
49
50
  wrapped_array_size(const void* data)
50
51
  {
51
- DFloat* array = (DFloat*)data;
52
- size_t size = sizeof(array->entries) + sizeof(*array);
53
- return size;
52
+ return sizeof(DFloat);
54
53
  }
55
54
 
56
55
  static rb_data_type_t dfloat_wrapper = [] {
@@ -65,21 +64,36 @@ static rb_data_type_t dfloat_wrapper = [] {
65
64
  VALUE
66
65
  cArray2D_alloc(VALUE self)
67
66
  {
68
- void* dfloat = (void*)malloc(sizeof(void*));
67
+ void* dfloat = (void*)malloc(sizeof(DFloat));
69
68
 
70
69
  return TypedData_Wrap_Struct(self, &dfloat_wrapper, dfloat);
71
70
  }
72
71
 
73
- inline bool
74
- cArray2D_check_array_args(VALUE arrays)
72
+ /*
73
+ * def initialize(arrays)
74
+ */
75
+ VALUE
76
+ cArray2D_initialize(VALUE self, VALUE arrays)
75
77
  {
76
- if (TYPE(arrays) == T_ARRAY && TYPE(rb_ary_entry(arrays, 0)) == T_ARRAY) {
77
- return true;
78
- } else {
78
+ // Initialize dfloat structure to store Dfloat in type wrapper
79
+ void* dfloat;
80
+ UNWRAP_DFLOAT(self, dfloat);
81
+
82
+ // Type-check 2D array
83
+ if (TYPE(arrays) != T_ARRAY) {
84
+ new (dfloat) DFloat(arrays, false);
79
85
  Check_Type(arrays, T_ARRAY);
86
+ return false;
87
+ }
88
+
89
+ if (TYPE(rb_ary_entry(arrays, 0)) != T_ARRAY) {
90
+ new (dfloat) DFloat(arrays, false);
80
91
  Check_Type(rb_ary_entry(arrays, 0), T_ARRAY);
81
92
  return false;
82
93
  }
94
+
95
+ new (dfloat) DFloat(arrays, true);
96
+ return self;
83
97
  }
84
98
 
85
99
  //{{{ Unpacked
@@ -89,22 +103,6 @@ simd_disabled(VALUE self)
89
103
  return Qfalse;
90
104
  }
91
105
 
92
- /*
93
- * def initialize(arrays)
94
- */
95
- VALUE
96
- cArray2D_initialize_unpacked(VALUE self, VALUE arrays)
97
- {
98
- // Typecheck 2d array
99
- if (cArray2D_check_array_args(arrays)) {
100
- // Initialize dfloat structure to store Dfloat in type wrapper
101
- void* dfloat;
102
- UNWRAP_DFLOAT(self, dfloat);
103
- new (dfloat) DFloat(arrays);
104
- }
105
- return self;
106
- }
107
-
108
106
  /*
109
107
  * Unpacked descriptive statistics
110
108
  *
@@ -122,7 +120,7 @@ cArray2D_descriptive_statistics_unpacked(VALUE self)
122
120
  }
123
121
  //}}}
124
122
 
125
- // Packed
123
+ //{{{ Packed
126
124
  #ifdef HAVE_XMMINTRIN_H
127
125
  extern "C" VALUE
128
126
  simd_enabled(VALUE self)
@@ -130,22 +128,6 @@ simd_enabled(VALUE self)
130
128
  return Qtrue;
131
129
  }
132
130
 
133
- /*
134
- * def initialize(arrays)
135
- */
136
- VALUE
137
- cArray2D_initialize_packed(VALUE self, VALUE arrays)
138
- {
139
- // Typecheck 2d array
140
- if (cArray2D_check_array_args(arrays)) {
141
- // Initialize dfloat structure to store Dfloat in type wrapper
142
- void* dfloat;
143
- UNWRAP_DFLOAT(self, dfloat);
144
- new (dfloat) DFloat(arrays);
145
- }
146
- return self;
147
- }
148
-
149
131
  /*
150
132
  * Packed descriptive statistics
151
133
  *
@@ -162,6 +144,7 @@ cArray2D_descriptive_statistics_packed(VALUE self)
162
144
  return build_results_hashes(stats, dfloat->cols);
163
145
  }
164
146
  #endif
147
+ //}}}
165
148
 
166
149
  extern "C" void
167
150
  Init_fast_statistics(void)
@@ -169,10 +152,10 @@ Init_fast_statistics(void)
169
152
  mFastStatistics = rb_define_module("FastStatistics");
170
153
  cArray2D = rb_define_class_under(mFastStatistics, "Array2D", rb_cData);
171
154
  rb_define_alloc_func(cArray2D, cArray2D_alloc);
155
+ rb_define_method(cArray2D, "initialize", RUBY_METHOD_FUNC(cArray2D_initialize), 1);
172
156
 
173
157
  #ifdef HAVE_XMMINTRIN_H
174
158
  rb_define_singleton_method(mFastStatistics, "simd_enabled?", RUBY_METHOD_FUNC(simd_enabled), 0);
175
- rb_define_method(cArray2D, "initialize", RUBY_METHOD_FUNC(cArray2D_initialize_packed), 1);
176
159
  rb_define_method(
177
160
  cArray2D,
178
161
  "descriptive_statistics",
@@ -180,7 +163,6 @@ Init_fast_statistics(void)
180
163
  0);
181
164
  #else
182
165
  rb_define_singleton_method(mFastStatistics, "simd_enabled?", RUBY_METHOD_FUNC(simd_disabled), 0);
183
- rb_define_method(cArray2D, "initialize", RUBY_METHOD_FUNC(cArray2D_initialize_unpacked), 1);
184
166
  rb_define_method(
185
167
  cArray2D,
186
168
  "descriptive_statistics",
@@ -1,3 +1,3 @@
1
1
  module FastStatistics
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fast_statistics
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Martin Nyaga
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-03-23 00:00:00.000000000 Z
11
+ date: 2021-03-24 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Fast computation of descriptive statistics in ruby using native code
14
14
  and SIMD