segmentlib.dox 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*! \page segmentlib GRASS Segment Library
  2. <!-- doxygenized from "GRASS 5 Programmer's Manual"
  3. by M. Neteler 8/2005
  4. -->
  5. \section segmentintro Segment Library
  6. <P>
  7. Authors: CERL
  8. <P>
  9. Large data files which contain data in a matrix format often need to be
  10. accessed in a nonsequential or random manner. This requirement complicates
  11. the programming.
  12. <P>
  13. Methods for accessing the data are to:
  14. <P>
  15. (1) read the entire data file into memory and process the data as a
  16. two-dimensional matrix,
  17. <P>
  18. (2) perform direct access i/o to the data file for every data value to be
  19. accessed, or
  20. <P>
  21. (3) read only portions of the data file into memory as needed.
  22. <P>
  23. Method (1) greatly simplifies the programming effort since i/o is done once
  24. and data access is simple array referencing. However, it has the
  25. disadvantage that large amounts of memory may be required to hold the data.
  26. The memory may not be available, or if it is, system paging of the module
  27. may severely degrade performance. Method (2) is not much more complicated to
  28. code and requires no significant amount of memory to hold the data. But the
  29. i/o involved will certainly degrade performance. Method (3) is a mixture of
  30. (1) and (2) . Memory requirements are fixed and data is read from the data
  31. file only when not already in memory. However the programming is more
  32. complex.
  33. <P>
  34. The routines provided in this library are an implementation of method (3) .
  35. They are based on the idea that if the original matrix were segmented or
  36. partitioned into smaller matrices these segments could be managed to reduce
  37. both the memory required and the i/o. Data access along connected paths
  38. through the matrix, (i.e., moving up or down one row and left or right one
  39. column) should benefit.
  40. <P>
  41. In most applications, the original data is not in the segmented format. The
  42. data must be transformed from the nonsegmented format to the segmented
  43. format. This means reading the original data matrix row by row and writing
  44. each row to a new file with the segmentation organization. This step
  45. corresponds to the i/o step of method (1) .
  46. <P>
  47. Then data can be retrieved from the segment file through routines by
  48. specifying the row and column of the original matrix. Behind the scenes, the
  49. data is paged into memory as needed and the requested data is returned to
  50. the caller.
  51. <P>
  52. <B>Note:</B> All routines and global variables in this library, documented
  53. or undocumented, start with the prefix <B>segment_.</B> To avoid name
  54. conflicts, programmers should not create variables or routines in their own
  55. modules which use this prefix.
  56. \section Segment_Routines Segment Routines
  57. <P>
  58. The routines in the <I>Segment Library</I> are described below, more or
  59. less in the order they would logically be used in a module. They use a data
  60. structure called SEGMENT which is defined in the header file
  61. <grass/segment.h> that must be included in any code using these
  62. routines: [footnote]
  63. \verbatim
  64. #include <grass/segment.h>
  65. \endverbatim
  66. <P>
  67. The first step is to create a file which is properly formatted for use by
  68. the <I>Segment Library</I> routines:
  69. <P>
  70. <I>int segment_format (int fd, int nrows, int ncols, int srows, int scols,
  71. int len)</I>, format a segment file
  72. <P>
  73. The segmentation routines require a disk file to be used for paging
  74. segments in and out of memory. This routine formats the file open for
  75. write on file descriptor <B>fd</B> for use as a segment file. A segment
  76. file must be formatted before it can be processed by other segment
  77. routines. The configuration parameters <B>nrows, ncols, srows, scols</B>,
  78. and <B>len</B> are written to the beginning of the segment file which is
  79. then filled with zeros.
  80. <P>
  81. The corresponding nonsegmented data matrix, which is to be transferred to the
  82. segment file, is <B>nrows</B> by <B>ncols.</B> The segment file is to be
  83. formed of segments which are <B>srows</B> by <B>scols.</B> The data items
  84. have length <B>len</B> bytes. For example, if the <I>data type is int</I>,
  85. <B><I>len</I> </B><I>is sizeof(int) .</I>
  86. <P>
  87. Return codes are: 1 ok; else -1 could not seek or write <I>fd</I>, or -3
  88. illegal configuration parameter(s) .
  89. <P>
  90. The next step is to initialize a SEGMENT structure to be associated with a
  91. segment file formatted by <I>segment_format.</I>
  92. <P>
  93. <I>int segment_init (SEGMENT *seg, int fd, int nsegs)</I>, initialize segment
  94. structure
  95. <P>
  96. Initializes the <B>seg</B> structure. The file on <B>fd</B> is
  97. a segment file created by <I>segment_format</I> and must be open for
  98. reading and writing. The segment file configuration parameters <I>nrows,
  99. ncols, srows, scols</I>, and <I>len</I>, as written to the file by
  100. <I>segment_format</I>, are read from the file and stored in the
  101. <B>seg</B> structure. <B>Nsegs</B> specifies the number of segments that
  102. will be retained in memory. The minimum value allowed is 1.
  103. <P>
  104. <B>Note.</B> The size of a segment is <I>scols*srows*len</I> plus a few
  105. bytes for managing each segment.
  106. <P>
  107. Return codes are: 1 if ok; else -1 could not seek or read segment file, or -2 out of memory.
  108. <P>
  109. Then data can be written from another file to the segment file row by row:
  110. <P>
  111. <I>int segment_put_row (SEGMENT *seg, char *buf, int row)</I>, write row to
  112. segment file
  113. <P>
  114. Transfers nonsegmented matrix data, row by row, into a segment
  115. file. <B>Seg</B> is the segment structure that was configured from a call
  116. to <I>segment_init.</I> <B>Buf</B> should contain <I>ncols*len</I>
  117. bytes of data to be transferred to the segment file. <B>Row</B> specifies
  118. the row from the data matrix being transferred.
  119. <P>
  120. Return codes are: 1 if ok; else -1 could not seek or write segment file.
  121. <P>
  122. Then data can be read or written to the segment file randomly:
  123. <P>
  124. <I>int segment_get (SEGMENT *seg, char *value, int row, int col)</I>, get value
  125. from segment file
  126. <P>
  127. Provides random read access to the segmented data. It gets
  128. <I>len</I> bytes of data into <B>value</B> from the segment file
  129. <B>seg</B> for the corresponding <B>row</B> and <B>col</B> in the
  130. original data matrix.
  131. <P>
  132. Return codes are: 1 if ok; else -1 could not seek or read segment file.
  133. <P>
  134. <I>int segment_put (SEGMENT *seg, char *value, int row, int col)</I>, put
  135. value to segment file
  136. <P>
  137. Provides random write access to the segmented data. It
  138. copies <I>len</I> bytes of data from <B>value</B> into the segment
  139. structure <B>seg</B> for the corresponding <B>row</B> and <B>col</B> in
  140. the original data matrix.
  141. <P>
  142. The data is not written to disk immediately. It is stored in a memory segment
  143. until the segment routines decide to page the segment to disk.
  144. <P>
  145. Return codes are: 1 if ok; else -1 could not seek or write segment file.
  146. <P>
  147. After random reading and writing is finished, the pending updates must be
  148. flushed to disk:
  149. <P>
  150. <I>int segment_flush (SEGMENT *seg)</I>, flush pending updates to disk
  151. <P>
  152. Forces all pending updates generated by <I>segment_put()</I> to be
  153. written to the segment file <B>seg.</B> Must be called after the final
  154. segment_put() to force all pending updates to disk. Must also be called
  155. before the first call to <I>segment_get_row.</I>
  156. <P>
  157. Now the data in segment file can be read row by row and transferred to a normal
  158. sequential data file:
  159. <P>
  160. <I>int segment_get_row (SEGMENT *seg, char *buf, int row)</I>, read row from
  161. segment file
  162. <P>
  163. Transfers data from a segment file, row by row, into memory
  164. (which can then be written to a regular matrix file) . <B>Seg</B> is the
  165. segment structure that was configured from a call to <I>segment_init.</I>
  166. <B>Buf</B> will be filled with <I>ncols*len</I> bytes of data
  167. corresponding to the <B>row</B> in the data matrix.
  168. <P>
  169. Return codes are: 1 if ok; else -1 could not seek or read segment file.
  170. <P>
  171. Finally, memory allocated in the SEGMENT structure is freed:
  172. <P>
  173. <I>int segment_release (SEGMENT *seg)</I>, free allocated memory
  174. <P>
  175. Releases the allocated memory associated with the segment file
  176. <B>seg.</B> Does not close the file. Does not flush the data which may
  177. be pending from previous <I>segment_put()</I> calls.
  178. <P>
  179. \section How_to_Use_the_Library_Routines How to Use the Library Routines
  180. The following should provide the programmer with a good idea of how to use the
  181. <I>Segment Library</I> routines. The examples assume that the data is integer.
  182. The first step is the creation and formatting of a segment file. A file is
  183. created, formatted and then closed:
  184. \verbatim
  185. fd = creat (file, 0666);
  186. segment_format (fd, nrows, ncols, srows, scols, sizeof(int));
  187. close(fd);
  188. \endverbatim
  189. <P>
  190. The next step is the conversion of the nonsegmented matrix data into segment
  191. file format. The segment file is reopened for read and write, initialized, and
  192. then data read row by row from the original data file and put into the segment
  193. file:
  194. \verbatim
  195. #include <fcntl.h>
  196. int buf[NCOLS];
  197. SEGMENT seg;
  198. fd = open (file, O_RDWR);
  199. segment_init (&seg, fd, nseg);
  200. for (row = 0; row < nrows; row++)
  201. {
  202. <code to get original matrix data for row into buf>
  203. segment_put_row (&seg, buf, row);
  204. }
  205. \endverbatim
  206. <P>
  207. Of course if the intention is only to add new values rather than update existing
  208. values, the step which transfers data from the original matrix to the segment
  209. file, using segment_put_row() , could be omitted, since
  210. <I>segment_format</I> will fill the segment file with zeros.
  211. <P>
  212. The data can now be accessed directly using <I>segment_get.</I> For example,
  213. to get the value at a given row and column:
  214. \verbatim
  215. int value;
  216. SEGMENT seg;
  217. segment_get (&seg, &value, row, col);
  218. \endverbatim
  219. <P>
  220. Similarly <I>segment_put()</I> can be used to change data values in the
  221. segment file:
  222. \verbatim
  223. int value;
  224. SEGMENT seg;
  225. value = 10;
  226. segment_put (&seg, &value, row, col);
  227. \endverbatim
  228. <P>
  229. <B>WARNING:</B> It is an easy mistake to pass a value directly to
  230. segment_put(). The following should be avoided:
  231. \verbatim
  232. segment_put (&seg, 10, row, col); /* this will not work */
  233. \endverbatim
  234. <P>
  235. Once the random access processing is complete, the data would be extracted
  236. from the segment file and written to a nonsegmented matrix data file as
  237. follows:
  238. \verbatim
  239. segment_flush (&seg);
  240. for (row = 0; row < nrows; row++)
  241. {
  242. segment_get_row (&seg, buf, row);
  243. <code to put buf into a matrix data file for row>
  244. }
  245. \endverbatim
  246. <P>
  247. Finally, the memory allocated for use by the segment routines would be
  248. released and the file closed:
  249. \verbatim
  250. segment_release (&seg);
  251. close (fd);
  252. \endverbatim
  253. <P>
  254. <B>Note:</B> The <I>Segment Library</I> does not know the name of the
  255. segment file. It does not attempt to remove the file. If the file is only
  256. temporary, the programmer should remove the file after closing it.
  257. <P>
  258. \section Segment_Library_Performance Segment Library Performance
  259. Performance of the <I>Segment Library</I> routines can be improved by
  260. about 10% if <B>srows, scols</B> are each powers of 2; in this case a
  261. faster alternative is used to access the segment file. An additional
  262. improvement can be achieved if <B>len</B> is also a power of 2. For
  263. highly random and scattered access to a large dataset, smaller segments,
  264. i.e. values for <B>srows, scols</B> of 32, 64, or 128 seem to provide
  265. better performance than e.g. srows = nrows / 4 + 1.
  266. \section Loading_the_Segment_Library Loading the Segment Library
  267. <P>
  268. The library is loaded by specifying
  269. \verbatim
  270. $(SEGMENTLIB)
  271. \endverbatim
  272. in the Makefile.
  273. <P>
  274. See \ref Compiling_and_Installing_GRASS_Modules for a complete
  275. discussion of Makefiles.
  276. */