home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / cellsim / v2_5 / doc / cm.fcn < prev    next >
Encoding:
Text File  |  1990-02-26  |  17.2 KB  |  386 lines

  1.             Documentation for Cellsim v2.5
  2.          Connection Machine Update-Functions
  3.                 February, 1990
  4.  
  5.  
  6.            ****************************************
  7.              LOOKUP TABLE RULES ON THE CM
  8.  
  9.   To make lookup table (LT) rules for the CM, you just do what you do to
  10. make them for the Sun.  The CM uses the exact same format for LTs as
  11. the Sun.  See the "Rule_README" file for information on generating LTs.
  12. (The "Rule_README" also describes how to generate computed-function rules
  13. for the Sun, since it's almost identical to generating lookup-tables).
  14.  
  15.  
  16.            ****************************************
  17.              COMPUTED-FUNCTIONS ON THE CM
  18.                ("Ah, Paris...")
  19.  
  20.   It would be nice to have some kind of program around that you could
  21. feed a normal C update-function, and it would turn it into Paris.
  22. Well, nobody's done that yet, that we know of.  So if you want to use
  23. computed-function rules on the CM, you have to use C/Paris.  C/Paris
  24. is just Paris when you call it from C.  Paris, if you aren't familiar
  25. with it, is something a little above an assembly-level language (it
  26. stands for "PARallel Instruction Set").  You mostly deal with simple
  27. operations like "move", "add", "compare", "set context flag", although
  28. you don't have to worry about normal assembly-language stuff like how
  29. far you can jump, or which register an instruction has to use.  If you
  30. don't have one already, you should get a copy of the Paris manual, which
  31. has a brief introduction to CM memory fields and context flags and the
  32. various instructions at the beginning, and which is mostly made up of a
  33. dictionary of Paris instructions.  Your Thinking Machines representative
  34. should be able to help you get the documentation you need, or speak to
  35. the people who gave you your CM account.
  36.  
  37.   So, anyways, those of you who know Paris probably skipped most of that.
  38. Here are the details of what happens with computed-function rules on the
  39. CM.  Cellsim (or rather cm_cell, the program running on the CMFE) keeps
  40. the current image in an 8-bit field called "cell"; just before calling
  41. your update function, Cellsim will look up the neighbors of the current
  42. neighborhood, and store them in a set of fields.  Your update-function
  43. is then called.  You can do whatever operations you like using these
  44. neighbor-fields, as long as your store the final result back in the
  45. 8-bit "cell" field.
  46.  
  47.   Those of you familiar with Paris realize that you often need some
  48. scratch space to do things, such as to keep intermediate results.  Cellsim
  49. lets you allocate extra fields on the CM (as much as you need, as long
  50. as you don't run out of memory) to use within your update-function.
  51. So, let's go on and get more specific about all of this.
  52.  
  53.  
  54.            ****************************************
  55.           WRITING AN ACTUAL UPDATE-FUNCTION
  56.  
  57.   First, just as with writing Sun update-functions, you need the Makefile.
  58. The Makefile is in the "CM2/Rule_src" directory; you should make a copy
  59. of it in whatever directory you'll be keeping your Paris rules.  Once
  60. you've made your copy, be sure the definition of "CM_MDIR" points to the
  61. full pathname of the "CM2/Rule_mdir" directory, which is where the include
  62. file and stuff are, that your rule needs.
  63.  
  64.   Ok, let's see how to make our Paris function.  The first thing in your
  65. file, besides any comments, must be the line:
  66.  
  67. #include "CMnborhood.h"
  68.  
  69. which defines the fields containing neighbors, the "cell" field, some
  70. macros useful if you have to do NEWS operations, the global parameters
  71. parm1 and parm2, and the current value of the phase variable (if you're
  72. using the Margolus neighborhood).  CMnborhood.h also includes the CM
  73. include-file <cm/paris.h>.
  74.  
  75.   Next, you need to declare two functions: your update function, and your
  76. exit function.  The update-function is the routine which will compute the
  77. new values of the cells, and the exit-function is something you have to
  78. write to clean up, or deallocate any local CM fields your routine creates.
  79. Both should be declared to return a byte (which has been typedef'ed to be
  80. unsigned char).  While in practice, you don't return  anything from either
  81. function, future releases might want a return-value for some reason.
  82. So, for example, you might declare these function as follows:
  83.  
  84.      byte  my_update_func(), my_exit_func();
  85.  
  86.   Next, declare any static (private) variables you want, such as CM fields.
  87. For example, the heat.m256 rule has the following declaration:
  88.  
  89.      static CM_field_id_t heat_sum;
  90.  
  91. where the heat_sum field will be used to add up the values of the neighbors.
  92.  
  93.   Then, after you've declared any local stuff you need, you should write
  94. your init_function.  This must set the value of update_function to point
  95. to your update function declared above, as well as initialize any local
  96. variables or the global parameters if you like.  This includes allocating
  97. space for your CM fields.  So, for example, the init_function for the
  98. heat.m256 rule looks like this:
  99.  
  100. void
  101. init_function()
  102. {
  103.  
  104.     update_function = heat;
  105.     parm2 = 2;
  106.     heat_sum = CM_allocate_heap_field(12);
  107. }
  108.  
  109. We tell Cellsim that our update_function is "heat" (which we declared
  110. above, and which will be implemented below); we initialize the global
  111. parameter parm2 to a default value appropriate for this rule, and then
  112. we allocate 12 bits for our heat_sum field.  You should allocate your
  113. private CM fields as heap fields.
  114.  
  115.   Next comes the update function itself.  Here's what the update-function
  116. looks like for the heat.m256 rule:
  117.  
  118. byte
  119. heat()
  120. {
  121.     CM_u_move_2L(heat_sum, MNorth, 12, 8);
  122.     CM_u_add_3_3L(heat_sum, heat_sum, MSouth, 12, 12, 8);
  123.     CM_u_add_3_3L(heat_sum, heat_sum, MEast, 12, 12, 8);
  124.     CM_u_add_3_3L(heat_sum, heat_sum, MWest, 12, 12, 8);
  125.     CM_u_add_3_3L(heat_sum, heat_sum, MNW, 12, 12, 8);
  126.     CM_u_add_3_3L(heat_sum, heat_sum, MSW, 12, 12, 8);
  127.     CM_u_add_3_3L(heat_sum, heat_sum, MNE, 12, 12, 8);
  128.     CM_u_add_3_3L(heat_sum, heat_sum, MSE, 12, 12, 8);
  129.     CM_u_add_3_3L(heat_sum, heat_sum, MCenter, 12, 12, 8);
  130.     CM_u_truncate_constant_3_1L(heat_sum, heat_sum, 9, 12);
  131.     CM_u_add_constant_2_1L(heat_sum, parm2, 8);
  132.     CM_u_move_1L(cell, heat_sum, 8);
  133. }
  134.  
  135.   (Things like "MSouth", "MNW", and so on will be explained later.  They
  136. are the Moore neighbors).
  137.  
  138.   Basically, we add up the values of the Moore neighbors into the heat_sum
  139. field; we then divide it by 9 (using truncation integer division), and
  140. add in the value of parm2.  Finally, we move the result into the "cell"
  141. field.  Note that we don't need take the result modulo 256, because we
  142. are using 8-bit arithmetic, so the modulo-256 happens automatically.
  143.  
  144.   The last thing you need to write is your exit function.  This is necessary
  145. to deallocate any fields your rule created, otherwise we'd run out of
  146. memory on the CM if we kept loading new rules.  In the heat.m256 rule, this
  147. is all that's in the exit function:
  148.  
  149. byte
  150. heat_exit()
  151. {
  152.     CM_deallocate_heap_field(heat_sum);
  153. }
  154.  
  155. We simply deallocate the heat_sum field, since that's all we allocated.
  156. If you allocated several fields in the init_function, you should deallocate
  157. them all.  If you allocated any front-end variables, e.g with malloc(),
  158. you should use free() to free up the memory here as well.
  159.  
  160.   Now that we ran through an example without too many details, let's back
  161. up for some important info that we skipped over.
  162.  
  163.  
  164.            ****************************************
  165.            NEIGHBOR-FIELDS THAT ARE DEFINED FOR YOU
  166.  
  167.   In the heat.m256 rule, we used such CM fields as MSouth, and you may
  168. have been thinking "what is MSouth?"  It is one of the standard Moore
  169. neighbors.  You get the following neighors with the following neighborhoods:
  170.  
  171.  Moore:  MCenter, MNorth, MSouth, MEast, MWest, MNW, MSW, MNE, MSE
  172.  Von-N:  VCenter, VNorth, VSouth, VEast, VWest
  173.  Margolus: MargCenter, MargCcw, MargOpp, MargCw, MargPhase
  174.  Linear radius1: LR1_L, LR1_C, LR1_R
  175.  Linear radius2: LR2_LL, LR2_L, LR2_C, LR2_R, LR2_RR,
  176.  Linear radius3: LR3_LLL, LR3_LL, LR3_L, LR3_C, LR3_R, LR3_RR, LR3_RRR
  177.  
  178. (Note that in Margolus neighborhood, you get the phase variable both as
  179. a CM field, and as an integer on the front-end.  Sometimes it is useful
  180. to have it in either of those places.)
  181.  
  182.   While the naming scheme may not be the best, if you really hate those
  183. names, you can #define new names in your update-function file, e.g.
  184.     #define MooreNorthWest MNW
  185. if you like to be verbose.
  186.  
  187.   All of these neighbor fields are 8-bit fields.
  188.  
  189.            ****************************************
  190.           AN UNFORTUNATE BUT IMPORTANT NOTE
  191.  
  192.   Due to some problems with dynamic-linking that we haven't worked out
  193. yet, there is one unfortunate step you might have to take when writing a
  194. Paris update-function.  Namely, when your function gets linked in, if there
  195. are any paris calls you use that Cellsim doesn't use, they will not have been
  196. linked in to the cm_cell executable.  Despite attempts at manually linking
  197. in the Paris libraries, this problem still persists.
  198.  
  199.   Therefore, if you use a Paris call that isn't already in Cellsim, you'll
  200. have to add it into Cellsim and recompile cm_cell.  Here's how you do this:
  201. in the file "dummy.c", there is a single function called "dummy".  It never
  202. gets called; however, it contains calls to very many Paris routines (extracted
  203. from the Paris library of one of the CMs I use).  If you're using a Paris
  204. call in your update-function that isn't listed in dummy(), you should add
  205. a call to that Paris function in the dummy function, and recompile cm_cell
  206. by typing "make".
  207.  
  208.   We really hate having to do this, and are working on fixing it.  If you
  209. find a way around it, please let us know.  Once we get it fixed, we'll
  210. distribute a patch that you can apply to solve the problem.  Hopefully,
  211. in the rules you write, you won't encounter this.  However, if the CM
  212. software libraries get upgraded, that may cause problems with this scheme
  213. (requiring us to distribute a new dummy.c, if we haven't implemented a
  214. more elegant solution by then).
  215.  
  216.  
  217.            ****************************************
  218.          A SPECIAL NOTE ABOUT THE LINEAR NEIGHBORHOOD
  219.  
  220.   A special note must be made about the linear neighborhood.  In order to
  221. let your rule know which VPs have a cell that is on the bottom row (the
  222. current row), there are 2 more CM fields defined.  They are "bottom" and
  223. "not_bottom", and they are each 1-bit fields.
  224.  
  225.   In the linear neighborhood, when your update-function is called, the
  226. context-flag has automatically been loaded with "bottom", so only processors
  227. on the bottom row are active.  However, if you do some fiddling with the
  228. context-flag in your rule, you should be careful to always AND the context-flag
  229. with the "bottom" field before altering the value of "cell".  After your
  230. update-function returns, Cellsim will then activate the rest of the processors,
  231. to scroll the rest of the image up a row.
  232.  
  233.   You can deliberately violate these guidelines if you wish, and I did so
  234. once by accident with interesting results; but you should keep it in mind
  235. so you won't wonder why your 1-D rule is acting so strangely.
  236.  
  237.  
  238.            ****************************************
  239.        A LITTLE HELP IF YOU NEED TO DO NEWS OPERATIONS
  240.  
  241.   If you have some private CM fields in your rule, you may need to get them
  242. from neighbors using NEWS operations.  Since I always get confused about
  243. which way is up when using NEWS ops, and since it's really up to the user
  244. which way is up, you need to know how I've defined things.  Basically, you
  245. need to know which axis is horizontal and which is vertical, and then
  246. which direction is positive and negative along each access.
  247.  
  248.   To avoid having to always remember stuff like that, I just define macros
  249. to keep track of it for me.  There are 4 macros:  North, South, East, and
  250. West.  Each macro holds the axis and sign (positive or negative, or upward
  251. and downward in CM terms) of the corresponding direction, in a form that
  252. can be used in a NEWS call.  So, to get the value of "mycell" from your
  253. north neighbor and store it in "northval", you'd do the following NEWS call:
  254.   get_from_news_1L(northval, mycell, North, 8)
  255. (assuming they are 8-bit fields).  If you look at the "CMnborhood.h" file,
  256. you'll see that "North" is really defined to be "1, CM_downward", but
  257. there's no sense in remembering that.
  258.  
  259.   If you want to get the NEWS coordinate of each processor along a particular
  260. axis, I've defined the words VERTICAL and HORIZONTAL to be the respective
  261. axes.  So you could do:
  262.   CM_my_news_coordinate_1L(my_coord, VERTICAL, 9)
  263. to get your vertical coordinate and store it in the "my_coord" field.
  264. Note that coordinates are 9 bits, because the maximum image-size is 512.
  265.  
  266.  
  267.            ****************************************
  268.   A NOTE ABOUT WHEN YOUR INIT-FUNCTION AND EXIT-FUNCTION GET CALLED
  269.  
  270.   Your init-function and exit-function actually get called in circumstances
  271. other then when you just load your rule or when another rule gets loaded.
  272. Since Cellsim has the ability to change its image-size, when that happens
  273. on the CM, all fields are deallocated, and the current VP set is destroyed,
  274. so a new one with the new geometry (array-size) can be created.
  275.  
  276.   So whenever you change image-size on the CM, your exit-function will be
  277. called to deallocate the local CM fields in your rule, the new VP set will
  278. be created, and then your init-function will be called to create your local
  279. fields again on the new VP set.
  280.  
  281.   This shouldn't cause any problems, but it's just something you might like
  282. to be aware of.  (If your init-function or exit-function print any
  283. diagnostic messages, you might have been surprised to see that happen
  284. whenever you changed image-size).
  285.  
  286.  
  287.            ****************************************
  288.            YOU CAN DO MORE THAN JUST 8 BITS
  289.           ("Hey, a floating-point CA rule!")
  290.  
  291.   It turns out that you can really run more than 8-bit rules, provided you
  292. keep the data in one of your local fields, and as long as you have some
  293. way of getting data from Cellsim into your local fields.  While I haven't
  294. done much with this yet, I did write a floating-point rule the other day.
  295.  
  296.   My init-function allocates a local 32-bit field to hold the floating-point
  297. value; the init-function also copies the "cell" field into my FP field,
  298. after re-scaling the values.  So, the current image on the CM gets transferred
  299. into my FP field only when I load the rule; if I want to start with a
  300. different image, I send that new image to the CM, and re-load my rule.
  301.  
  302.   Then, after every time-step, I do a few extra Paris calls to re-scale the
  303. value in my FP field and store it in the "cell" field; so when my rule
  304. runs, you can actually get an image, although the image has far less precision
  305. per cell than the actual rule.
  306.  
  307.   While this may not thrill some of you (I'm not crazy about floating-point
  308. myself, I just wanted to see if it could be done), it should be very exciting
  309. to some of you.  This means you can really run any type of 2-D Paris rule,
  310. and use Cellsim as a simple interface.  For example, I have an 80-bit model
  311. of something like a cross between particles in a potential field, and simple
  312. flocking behavior.  I originally wrote this as a stand-alone program using
  313. Paris, but now I can take the main update-function, and wrap it in Cellsim.
  314. While the Cellsim user-interface isn't very good for doing things this
  315. general yet, the potential is there to turn it into a very general and useful
  316. interface for using the CM, not just for cellular automata rules.
  317.  
  318.   So, if you have some ideas about all of this, we'd like to hear your
  319. comments.  We already have some ideas for useful mechanisms to facilitate
  320. communication between the user and the update-rule, but more ideas would
  321. be appreciated.
  322.  
  323.  
  324.            ****************************************
  325.               SOME SAMPLE FILES
  326.  
  327.   Examples usually make things much simpler and easier to follow.  So, some
  328. sample rules are included, in the "CM2/Rule_src" directory.  Looking through
  329. them may clear up any confusion you have after reading this file.  They are:
  330.  
  331.   heat.m256.c : simple heat rule
  332.   heat.l256r2.c : heat rule in linear neighborhood
  333.   heatb.l256r2.c : linear heat-rule where I accidentally added in the
  334.            increment to *all* rows, instead of just the bottom row.
  335.   bz.m256.c : Belousov-Zhabotinski style rule
  336.   cycle.v256.c : Griffeath's "cyclic" CA rule
  337.   cml.v256.c : the floating-point rule mentioned above
  338.  
  339. Read the comments at the beginning of each file for more information on
  340. the rules.
  341.  
  342.  
  343. /*
  344.  *
  345.  * Cellsim copyright 1989, 1990 by Chris Langton and Dave Hiebeler
  346.  * (cgl@lanl.gov, hiebeler@heretic.lanl.gov)
  347.  *
  348.  * This package may be freely distributed, as long as you don't:
  349.  * - remove this notice
  350.  * - try to make money by doing so
  351.  * - prevent others from copying it freely
  352.  * - distribute modified versions without clearly documenting your changes
  353.  *   and notifying us
  354.  *
  355.  * Please contact either of the authors listed above if you have questions
  356.  * or feel an exception to any of the above restrictions is in order.
  357.  *
  358.  * If you make changes to the code, or have suggestions for changes,
  359.  * let us know!  If we use your suggestion, you will receive full credit
  360.  * of course.
  361.  */
  362.  
  363. /*****
  364.  * Cellsim history:
  365.  *
  366.  * Cellsim was originally written on Apollo workstations by Chris Langton.
  367.  *
  368.  * Sun versions:
  369.  *
  370.  * - version 1.0
  371.  *   by C. Ferenbaugh and C. Langton
  372.  *   released 09/02/88
  373.  *
  374.  * - version 1.5
  375.  *   by Dave Hiebeler and C. Langton  May - June 1989
  376.  *   released 07/03/89
  377.  *
  378.  * - version 2.0
  379.  *   by Dave Hiebeler and C. Langton  July - August 1989
  380.  *   never officially released (unofficially released 09/08/89)
  381.  *
  382.  * - version 2.5
  383.  *   by Dave Hiebeler and C. Langton  September '89 - February 1990
  384.  *   released 02/26/90
  385.  *****/
  386.