redshift 1.3.24 → 1.3.30

Sign up to get free protection for your applications and to get access to all the features.
Files changed (69) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +49 -0
  3. data/RELEASE-NOTES +34 -3
  4. data/Rakefile +81 -0
  5. data/bench/bench +1 -1
  6. data/bench/formula.rb +2 -3
  7. data/examples/pid.rb +6 -6
  8. data/examples/robots/lib/shell-world.rb +1 -1
  9. data/examples/scheduler.rb +6 -1
  10. data/ext/redshift/dvector-float/dvector-float.c +184 -0
  11. data/ext/redshift/dvector-float/dvector-float.h +36 -0
  12. data/ext/redshift/dvector-float/dvector-float.rb +33 -0
  13. data/ext/redshift/dvector-float/extconf.rb +2 -0
  14. data/lib/redshift/redshift.rb +5 -5
  15. data/lib/redshift/target/c/component-gen.rb +1 -1
  16. data/lib/redshift/target/c/flow-gen.rb +1 -1
  17. data/lib/redshift/target/c/flow/delay.rb +1 -1
  18. data/lib/redshift/target/c/world-gen.rb +101 -101
  19. data/lib/redshift/util/isaac.rb +2 -2
  20. data/lib/redshift/util/tkar-driver.rb +1 -1
  21. data/test/test.rb +1 -1
  22. data/test/test_buffer.rb +2 -2
  23. data/test/test_connect.rb +2 -2
  24. data/test/test_connect_parallel.rb +2 -2
  25. data/test/test_connect_strict.rb +4 -5
  26. data/test/test_constant.rb +2 -4
  27. data/test/test_delay.rb +2 -2
  28. data/test/test_derivative.rb +2 -2
  29. data/test/test_discrete.rb +2 -4
  30. data/test/test_discrete_isolated.rb +2 -4
  31. data/test/test_dvector-float.rb +110 -0
  32. data/test/test_dvector.rb +15 -17
  33. data/test/test_exit.rb +2 -4
  34. data/test/test_flow.rb +2 -4
  35. data/test/test_flow_link.rb +2 -4
  36. data/test/test_flow_sub.rb +2 -4
  37. data/test/test_flow_trans.rb +23 -25
  38. data/test/test_inherit.rb +2 -4
  39. data/test/test_inherit_event.rb +2 -4
  40. data/test/test_inherit_flow.rb +2 -4
  41. data/test/test_inherit_link.rb +2 -4
  42. data/test/test_inherit_setup.rb +2 -4
  43. data/test/test_inherit_state.rb +2 -4
  44. data/test/test_inherit_transition.rb +2 -4
  45. data/test/test_numerics.rb +2 -4
  46. data/test/test_queue.rb +3 -3
  47. data/test/test_queue_alone.rb +2 -2
  48. data/test/test_reset.rb +2 -4
  49. data/test/test_setup.rb +2 -4
  50. data/test/test_strict_continuity.rb +2 -4
  51. data/test/test_strict_reset_error.rb +2 -4
  52. data/test/test_strictness_error.rb +2 -4
  53. data/test/test_sync.rb +2 -2
  54. data/test/test_world.rb +2 -4
  55. metadata +80 -106
  56. data/.bnsignore +0 -27
  57. data/.gitignore +0 -9
  58. data/README +0 -25
  59. data/TODO +0 -431
  60. data/bench/aug17-ruby19.bench +0 -86
  61. data/bench/aug17.bench +0 -86
  62. data/bench/aug7.bench +0 -86
  63. data/bench/prof.html +0 -0
  64. data/examples/robots/README +0 -49
  65. data/ext/redshift/util/isaac/extconf.rb +0 -2
  66. data/ext/redshift/util/isaac/isaac.c +0 -129
  67. data/ext/redshift/util/isaac/rand.c +0 -140
  68. data/ext/redshift/util/isaac/rand.h +0 -61
  69. data/rakefile +0 -50
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 0e70ddb9e9f05b61a7b15acd4a9a59ad94f8fb43771aa0619f0d69400f691ba2
4
+ data.tar.gz: 0a2087fa620759313761ddcfc788dc44fcc204f5b0d2d8ceb5e73681102c2f75
5
+ SHA512:
6
+ metadata.gz: '059c869b96c8e837ebdd1762e28859f5fdf6d4f4f76afa6a955149a93013b11d262d849b30e472523ce9ca9d09f0b255efc6ae64bcf9488703a0431e6af5cbe5'
7
+ data.tar.gz: 25438a65be067f8b8a5df3dbd9f276722e2de0468bfa06273041fd7dc3e56f79677a1382c75633783c4d46c436845df1873ec34b72c89d83beb87be1638c814a
data/README.md ADDED
@@ -0,0 +1,49 @@
1
+ # RedShift #
2
+
3
+ A framework for simulation of networks of hybrid automata, similar to SHIFT and Lambda-SHIFT. Includes ruby-based DSL for defining simulation components, and ruby/C code generation and runtime.
4
+
5
+ RedShift is for simulating digital devices operating in an analog world. It's also for simulating any system that exhibits a mix of discrete and continuous behavior.
6
+
7
+ There's not much [documentation](doc) yet, but plenty of [examples](examples). Some of the original SHIFT papers are available: [Shift: A Formalism and a Programming Language for Dynamic Networks of Hybrid Automata]( http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.32.5913&rep=rep1&type=pdf).
8
+
9
+ My [2009 rubyconf talk](http://confreaks.com/videos/165-rubyconf2009-dsls-code-generation-and-new-domains-for-ruby) discusses RedShift.
10
+
11
+ ## Requirements ##
12
+
13
+ RedShift needs ruby (1.8, 1.9, 2.0, 2.1) and a compatible C compiler. If you can build native gems, you're all set.
14
+
15
+ Some of the examples also use Ruby/Tk and the tkar gem.
16
+
17
+ ## Installation ##
18
+
19
+ Install as gem:
20
+
21
+ gem install redshift
22
+
23
+ ## Env vars ##
24
+
25
+ If you have a multicore system and are using the gnu toolchain, set
26
+
27
+ REDSHIFT_MAKE_ARGS='-j -l2'
28
+
29
+ or some variation. You'll find that rebuilds of your simulation code go faster.
30
+
31
+
32
+ What is RedShift?
33
+ -----------------
34
+
35
+ RedShift combines *dataflow* programming (for continuous variables) with *actor*-based programming (for discrete events, states, and variables, and also for continuous variables).
36
+
37
+ Examples
38
+ --------
39
+
40
+ See the [examples](examples) dir.
41
+
42
+ The [RedCloud](https://github.com/vjoel/redcloud) project is based on RedShift.
43
+
44
+
45
+ ----
46
+
47
+ Copyright (C) 2001-2014, Joel VanderWerf, mailto:vjoel@users.sourceforge.net
48
+ Distributed under the Ruby license. See www.ruby-lang.org.
49
+
data/RELEASE-NOTES CHANGED
@@ -1,3 +1,34 @@
1
+ redshift 1.3.28
2
+
3
+ - gemspec specifies dvector-float extension
4
+
5
+ redshift 1.3.28
6
+
7
+ - update .gitignore
8
+ - fixes for ruby 2.1
9
+ - modernize rakefile: release tasks
10
+ - use -march=native in formula benchmark
11
+
12
+ redshift 1.3.27
13
+
14
+ - cleanup
15
+ - use minitest
16
+ - remove local isaac ext and depend on prng-isaac gem instead
17
+
18
+ redshift 1.3.26
19
+
20
+ - updated to ruby 2.0
21
+
22
+ - fixed gemspec
23
+
24
+ redshift 1.3.25
25
+
26
+ - added DVectorFloat
27
+
28
+ - added 'rake ex' task to build extensions for current ruby version
29
+
30
+ - minor bug fixes and updates
31
+
1
32
  redshift 1.3.24
2
33
 
3
34
  - fixed include path problem when running from installed gem
@@ -7,7 +38,7 @@ redshift 1.3.23
7
38
  - compatible with ruby-1.9.2
8
39
 
9
40
  - renamed Random module to RandomDistribution to avoid conflict
10
-
41
+
11
42
  redshift 1.3.22
12
43
 
13
44
  - handling of env vars is more consistent
@@ -100,7 +131,7 @@ redshift 1.3.13
100
131
 
101
132
  redshift 1.3.12
102
133
 
103
- - Fixed bug with explicitly assigning an array to a
134
+ - Fixed bug with explicitly assigning an array to a
104
135
  delay buffer.
105
136
 
106
137
  - Raise if time_step changes while using delay.
@@ -185,7 +216,7 @@ redshift 1.2.41
185
216
  - Improved message for reset type error.
186
217
 
187
218
  - Link vars can now be declared without a type (defaults to Component).
188
-
219
+
189
220
  - Fixed Queue behavior when #unpop called with SimultaneousQueueEntries.
190
221
 
191
222
  - Added examples and tests.
data/Rakefile ADDED
@@ -0,0 +1,81 @@
1
+ require 'rake'
2
+
3
+ PRJ = "redshift"
4
+
5
+ def cur_ruby
6
+ require 'rbconfig'
7
+ @cur_ruby ||= RbConfig::CONFIG["RUBY_INSTALL_NAME"]
8
+ end
9
+
10
+ def version
11
+ @version ||= begin
12
+ require 'redshift/redshift'
13
+ warn "RedShift::VERSION not a string" unless
14
+ RedShift::VERSION.kind_of? String
15
+ RedShift::VERSION
16
+ end
17
+ end
18
+
19
+ def tag
20
+ @tag ||= "#{PRJ}-#{version}"
21
+ end
22
+
23
+ desc "Run unit tests"
24
+ task :test do |t|
25
+ sh "cd test && RUBYLIB=../lib:../ext:$RUBYLIB ./test.rb"
26
+ end
27
+
28
+ desc "build extensions for current ruby: #{RUBY_VERSION}"
29
+ task :build_ext do
30
+ require 'find'
31
+ Find.find('ext/redshift') do |f|
32
+ next unless File.basename(f) == "extconf.rb"
33
+ d = File.dirname(f)
34
+ Dir.chdir d do
35
+ sh "make distclean || true"
36
+ ruby "extconf.rb"
37
+ sh "make"
38
+ end
39
+ end
40
+ end
41
+
42
+ desc "Commit, tag, and push repo; build and push gem"
43
+ task :release => "release:is_new_version" do
44
+ require 'tempfile'
45
+
46
+ sh "gem build #{PRJ}.gemspec"
47
+
48
+ file = Tempfile.new "template"
49
+ begin
50
+ file.puts "release #{version}"
51
+ file.close
52
+ sh "git commit --allow-empty -a -v -t #{file.path}"
53
+ ensure
54
+ file.close unless file.closed?
55
+ file.unlink
56
+ end
57
+
58
+ sh "git tag #{tag}"
59
+ sh "git push"
60
+ sh "git push --tags"
61
+
62
+ sh "gem push #{tag}.gem"
63
+ end
64
+
65
+ namespace :release do
66
+ desc "Diff to latest release"
67
+ task :diff do
68
+ latest = `git describe --abbrev=0 --tags --match '#{PRJ}-*'`.chomp
69
+ sh "git diff #{latest}"
70
+ end
71
+
72
+ desc "Log to latest release"
73
+ task :log do
74
+ latest = `git describe --abbrev=0 --tags --match '#{PRJ}-*'`.chomp
75
+ sh "git log #{latest}.."
76
+ end
77
+
78
+ task :is_new_version do
79
+ abort "#{tag} exists; update version!" unless `git tag -l #{tag}`.empty?
80
+ end
81
+ end
data/bench/bench CHANGED
@@ -9,7 +9,7 @@ def bench_one(name)
9
9
  puts "#{name}:"
10
10
  #{name.split(/[-_]/).map {|w|w.capitalize}.join}.do_bench {|l| puts l}
11
11
  }
12
- ruby = Config::CONFIG["RUBY_INSTALL_NAME"]
12
+ ruby = RbConfig::CONFIG["RUBY_INSTALL_NAME"]
13
13
  system ruby, "-r" , "./bench", "-r", lib, "-e", cmd
14
14
  end
15
15
 
data/bench/formula.rb CHANGED
@@ -6,11 +6,10 @@
6
6
  # with
7
7
  # CFLAGS = -fPIC -O2 -march=i686 -msse2 -mfpmath=sse
8
8
 
9
+ $CFLAGS="-fPIC -O2 -march=native"
9
10
  case RUBY_PLATFORM
10
11
  when /x86_64/
11
- $CFLAGS="-fPIC -O2 -march=k8 -msse2 -mfpmath=sse"
12
- else
13
- $CFLAGS="-fPIC -O2 -march=i686 -msse2 -mfpmath=sse"
12
+ $CFLAGS << " -msse2 -mfpmath=sse"
14
13
  end
15
14
 
16
15
  require 'redshift'
data/examples/pid.rb CHANGED
@@ -9,14 +9,14 @@ srand(12345)
9
9
  # Variable with discrete and continuous perturbation.
10
10
  class Plant < Component
11
11
  continuous :x => 0, :t => 1
12
-
12
+
13
13
  link :control => :Control # fwd ref to undefined class Control
14
-
14
+
15
15
  flow do
16
16
  diff " x' = control.output + sin(t) "
17
17
  diff " t' = -1 "
18
18
  end
19
-
19
+
20
20
  transition do
21
21
  guard "t <= 0"
22
22
  action do
@@ -30,14 +30,14 @@ end
30
30
  class Control < Component
31
31
  continuous :set_point => 2.0
32
32
  continuous :p_out, :i_out, :d_out, :output
33
-
33
+
34
34
  # Gains
35
35
  constant :k_p => 1.0,
36
36
  :k_i => 1.0,
37
37
  :k_d => 1.0
38
-
38
+
39
39
  link :plant => Plant
40
-
40
+
41
41
  flow do
42
42
  algebraic " error = set_point - plant.x "
43
43
  algebraic " p_out = k_p * error "
@@ -34,7 +34,7 @@ module ShellWorld
34
34
 
35
35
  loop do
36
36
  evolve 100000000 do
37
- puts "clock: #{clock}"
37
+ printf "clock: %10.3f\n" % clock
38
38
  #sleep 0.01
39
39
  end
40
40
  end
@@ -18,10 +18,14 @@ class Scheduler < RedShift::Component
18
18
 
19
19
  class Request
20
20
  include Comparable
21
+
21
22
  attr_accessor :time, :queue, :message
23
+
22
24
  def <=>(other)
23
25
  self.time <=> other.time
24
26
  end
27
+
28
+ # Scheduled request: at given +time+, send +message+ to +queue+.
25
29
  def initialize time, queue, message
26
30
  @time, @queue, @message = time, queue, message
27
31
  end
@@ -29,6 +33,7 @@ class Scheduler < RedShift::Component
29
33
 
30
34
  EPSILON = 1E-12 # float fuzziness, if timestep is 0.1, for example
31
35
 
36
+ # Schedule the sending of +message+ to +queue+, after +delta_t+ elapses.
32
37
  # Returns the request object for use with unschedule
33
38
  def schedule_message delta_t, queue, message
34
39
  req = Request.new(time + delta_t - EPSILON, queue, message)
@@ -82,7 +87,7 @@ class Client < RedShift::Component
82
87
 
83
88
  transition Enter => Waiting do
84
89
  action do
85
- puts "scheduling message at #{world.clock} sec"
90
+ puts "scheduling message at #{world.clock} sec to run after #{delay} sec"
86
91
  scheduler.schedule_message delay, wakeup, "Time to wake up, snoozebrain!"
87
92
  end
88
93
  end
@@ -0,0 +1,184 @@
1
+ #include "dvector-float.h"
2
+ #include <math.h>
3
+
4
+ static VALUE dvf_alloc(VALUE klass)
5
+ {
6
+ VALUE self;
7
+ RS_DVectorFloat *dvf;
8
+
9
+ self = Data_Make_Struct(klass, RS_DVectorFloat, 0, -1, dvf);
10
+
11
+ dvf->len = 0;
12
+ dvf->capa = 0;
13
+ dvf->ptr = 0;
14
+
15
+ return self;
16
+ }
17
+
18
+ void rs_dvf_grow(RS_DVectorFloat *dvf)
19
+ {
20
+ if (!dvf->ptr) {
21
+ dvf->capa = 16;
22
+ dvf->ptr = ALLOC_N(float, dvf->capa);
23
+ }
24
+ else if (dvf->len == dvf->capa) {
25
+ dvf->capa *= 2;
26
+ REALLOC_N(dvf->ptr, float, dvf->capa);
27
+ }
28
+ }
29
+
30
+ void rs_dvf_shrink(RS_DVectorFloat *dvf)
31
+ {
32
+ if (dvf->ptr && dvf->len < dvf->capa) {
33
+ REALLOC_N(dvf->ptr, float, dvf->len);
34
+ dvf->capa = dvf->len;
35
+ }
36
+ }
37
+
38
+ static VALUE dvf_method_push(int argc, VALUE *argv, VALUE self)
39
+ {
40
+ int i;
41
+ RS_DVectorFloat *dvf;
42
+
43
+ Data_Get_Struct(self, RS_DVectorFloat, dvf);
44
+
45
+ for (i = 0; i < argc; i++) {
46
+ rs_dvf_push(dvf, NUM2DBL(argv[i]));
47
+ }
48
+
49
+ return self;
50
+ }
51
+
52
+ static VALUE dvf_method_pop(VALUE self)
53
+ {
54
+ RS_DVectorFloat *dvf;
55
+
56
+ Data_Get_Struct(self, RS_DVectorFloat, dvf);
57
+
58
+ return dvf->len == 0 ? Qnil : rb_float_new(rs_dvf_pop(dvf));
59
+ }
60
+
61
+ static VALUE dvf_method_each(VALUE self)
62
+ {
63
+ long i;
64
+ RS_DVectorFloat *dvf;
65
+
66
+ Data_Get_Struct(self, RS_DVectorFloat, dvf);
67
+
68
+ RETURN_ENUMERATOR(self, 0, 0);
69
+ for (i=0; i < dvf->len; i++) {
70
+ rb_yield(rb_float_new(dvf->ptr[i]));
71
+ }
72
+
73
+ return self;
74
+ }
75
+
76
+ static VALUE dvf_method_to_a(VALUE self)
77
+ {
78
+ int i;
79
+ RS_DVectorFloat *dvf;
80
+ VALUE ary;
81
+
82
+ Data_Get_Struct(self, RS_DVectorFloat, dvf);
83
+
84
+ ary = rb_ary_new();
85
+ if (!dvf->ptr) return ary;
86
+
87
+ for (i=0; i < dvf->len; i++) {
88
+ rb_ary_push(ary, rb_float_new(dvf->ptr[i]));
89
+ }
90
+
91
+ return ary;
92
+ }
93
+
94
+ static VALUE dvf_method_length(VALUE self)
95
+ {
96
+ RS_DVectorFloat *dvf;
97
+ Data_Get_Struct(self, RS_DVectorFloat, dvf);
98
+ return INT2NUM(dvf->len);
99
+ }
100
+
101
+ static VALUE dvf_method_equal(VALUE self, VALUE other)
102
+ {
103
+ int i;
104
+ RS_DVectorFloat *dvf1, *dvf2;
105
+
106
+ if (self == other) return Qtrue;
107
+ if (CLASS_OF(self) != CLASS_OF(other)) return Qfalse;
108
+
109
+ Data_Get_Struct(self, RS_DVectorFloat, dvf1);
110
+ Data_Get_Struct(other, RS_DVectorFloat, dvf2);
111
+ if (dvf1->len != dvf2->len) return Qfalse;
112
+
113
+ for (i=0; i < dvf1->len; i++) {
114
+ if (dvf1->ptr[i] != dvf2->ptr[i]) return Qfalse;
115
+ }
116
+ return Qtrue;
117
+ }
118
+
119
+ static VALUE dvf_method_hash(VALUE self)
120
+ {
121
+ long i, h;
122
+ RS_DVectorFloat *dvf;
123
+
124
+ Data_Get_Struct(self, RS_DVectorFloat, dvf);
125
+
126
+ h = dvf->len;
127
+ for (i=0; i < dvf->len; i++) {
128
+ int hash;
129
+ unsigned int j;
130
+ char *c;
131
+ float f;
132
+
133
+ h = (h << 1) | (h<0 ? 1 : 0);
134
+ f = dvf->ptr[i];
135
+
136
+ if (f == 0) f = fabs(f);
137
+ c = (char*)&f;
138
+ for (hash=0, j=0; j<sizeof(float); j++) {
139
+ hash = (hash * 971) ^ (unsigned char)c[j];
140
+ }
141
+ if (hash < 0) hash = -hash;
142
+
143
+ h ^= hash;
144
+ }
145
+ return LONG2FIX(h);
146
+ }
147
+
148
+ static VALUE dvf_method_load_data(VALUE self, VALUE from_array)
149
+ {
150
+ long i;
151
+ RS_DVectorFloat *dvf;
152
+ Data_Get_Struct(self, RS_DVectorFloat, dvf);
153
+
154
+ for (i=0; i < RARRAY_LEN(from_array); i++) {
155
+ rs_dvf_push(dvf, NUM2DBL(RARRAY_PTR(from_array)[i]));
156
+ }
157
+
158
+ return self;
159
+ }
160
+
161
+ VALUE rs_cDVectorFloat;
162
+
163
+ void
164
+ Init_dvector_float(void)
165
+ {
166
+ rs_cDVectorFloat = rb_path2class("RedShift::DVectorFloat");
167
+
168
+ rb_define_alloc_func(rs_cDVectorFloat, dvf_alloc);
169
+ rb_define_method(rs_cDVectorFloat, "push", dvf_method_push, -1);
170
+ rb_define_method(rs_cDVectorFloat, "pop", dvf_method_pop, 0);
171
+ rb_define_alias(rs_cDVectorFloat, "<<", "push");
172
+
173
+ rb_define_method(rs_cDVectorFloat, "each", dvf_method_each, 0);
174
+ rb_define_method(rs_cDVectorFloat, "to_a", dvf_method_to_a, 0);
175
+ rb_define_method(rs_cDVectorFloat, "length", dvf_method_length, 0);
176
+ rb_define_alias(rs_cDVectorFloat, "size", "length");
177
+
178
+ rb_define_method(rs_cDVectorFloat, "==", dvf_method_equal, 1);
179
+ rb_define_method(rs_cDVectorFloat, "eql?", dvf_method_equal, 1);
180
+ rb_define_method(rs_cDVectorFloat, "hash", dvf_method_hash, 0);
181
+
182
+ rb_define_method(rs_cDVectorFloat, "_load_data", dvf_method_load_data, 1);
183
+ rb_define_method(rs_cDVectorFloat, "_dump_data", dvf_method_to_a, 0);
184
+ }