segmentlib.dox 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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. A temporary file needs to be prepared and a SEGMENT structure needs to
  68. be initialized before any data can be transferred to the segment file.
  69. This can be done with the <I>Segment Library</I> routine:
  70. <P>
  71. <I>int segment_open(SEGMENT *SEG, char *fname, off_t nrows, off_t ncols,
  72. int srows, int scols, int len, int nseg), open a new segment structure.
  73. <P>
  74. A new file with full path name <B>fname</B> will be created and
  75. formatted. The original nonsegmented data matrix consists of
  76. <B>nrows</B> and <B>ncols. The segments consist of <B>srows</B> by
  77. <B>scols</B>. The data items have length <B>len</B> bytes. The number
  78. of segments to be retained in memory is given by <B>nseg</B>. This
  79. routine calls segment_format() and segment_init(), see below. If
  80. segment_open() is used, the routines segment_format() and segment_init()
  81. must not be used.
  82. <P>
  83. Return codes are: 1 ok; else a negative number between -1 and -6 encoding
  84. the error type.
  85. <P>
  86. Alternatively, the first step is to create a file which is properly
  87. formatted for use by the <I>Segment Library</I> routines:
  88. <P>
  89. <I>int segment_format (int fd, int nrows, off_t ncols,off_t srows, int scols,
  90. int len)</I>, format a segment file
  91. <P>
  92. The segmentation routines require a disk file to be used for paging
  93. segments in and out of memory. This routine formats the file open for
  94. write on file descriptor <B>fd</B> for use as a segment file. A segment
  95. file must be formatted before it can be processed by other segment
  96. routines. The configuration parameters <B>nrows, ncols, srows, scols</B>,
  97. and <B>len</B> are written to the beginning of the segment file which is
  98. then filled with zeros.
  99. <P>
  100. The corresponding nonsegmented data matrix, which is to be transferred to the
  101. segment file, is <B>nrows</B> by <B>ncols.</B> The segment file is to be
  102. formed of segments which are <B>srows</B> by <B>scols</B>. The data items
  103. have length <B>len</B> bytes. For example, if the data type is <I>int</I>,
  104. <B><I>len</I></B> is <I>sizeof(int)</I>.
  105. <P>
  106. Return codes are: 1 ok; else -1 could not seek or write <I>fd</I>, or -3
  107. illegal configuration parameter(s) .
  108. <P>
  109. The next step is to initialize a SEGMENT structure to be associated with a
  110. segment file formatted by <I>segment_format.</I>
  111. <P>
  112. <I>int segment_init (SEGMENT *seg, int fd, int nsegs)</I>, initialize segment
  113. structure
  114. <P>
  115. Initializes the <B>seg</B> structure. The file on <B>fd</B> is
  116. a segment file created by <I>segment_format</I> and must be open for
  117. reading and writing. The segment file configuration parameters <I>nrows,
  118. ncols, srows, scols</I>, and <I>len</I>, as written to the file by
  119. <I>segment_format</I>, are read from the file and stored in the
  120. <B>seg</B> structure. <B>Nsegs</B> specifies the number of segments that
  121. will be retained in memory. The minimum value allowed is 1.
  122. <P>
  123. <B>Note.</B> The size of a segment is <I>scols*srows*len</I> plus a few
  124. bytes for managing each segment.
  125. <P>
  126. Return codes are: 1 if ok; else -1 could not seek or read segment file, or -2 out of memory.
  127. <P>
  128. Then data can be written from another file to the segment file row by row:
  129. <P>
  130. <I>int segment_put_row (SEGMENT *seg, char *buf, int row)</I>, write row to
  131. segment file
  132. <P>
  133. Transfers nonsegmented matrix data, row by row, into a segment
  134. file. <B>Seg</B> is the segment structure that was configured from a call
  135. to <I>segment_init.</I> <B>Buf</B> should contain <I>ncols*len</I>
  136. bytes of data to be transferred to the segment file. <B>Row</B> specifies
  137. the row from the data matrix being transferred.
  138. <P>
  139. Return codes are: 1 if ok; else -1 could not seek or write segment file.
  140. <P>
  141. Then data can be read or written to the segment file randomly:
  142. <P>
  143. <I>int segment_get (SEGMENT *seg, char *value, int row, int col)</I>, get value
  144. from segment file
  145. <P>
  146. Provides random read access to the segmented data. It gets
  147. <I>len</I> bytes of data into <B>value</B> from the segment file
  148. <B>seg</B> for the corresponding <B>row</B> and <B>col</B> in the
  149. original data matrix.
  150. <P>
  151. Return codes are: 1 if ok; else -1 could not seek or read segment file.
  152. <P>
  153. <I>int segment_put (SEGMENT *seg, char *value, int row, int col)</I>, put
  154. value to segment file
  155. <P>
  156. Provides random write access to the segmented data. It
  157. copies <I>len</I> bytes of data from <B>value</B> into the segment
  158. structure <B>seg</B> for the corresponding <B>row</B> and <B>col</B> in
  159. the original data matrix.
  160. <P>
  161. The data is not written to disk immediately. It is stored in a memory segment
  162. until the segment routines decide to page the segment to disk.
  163. <P>
  164. Return codes are: 1 if ok; else -1 could not seek or write segment file.
  165. <P>
  166. After random reading and writing is finished, the pending updates must be
  167. flushed to disk:
  168. <P>
  169. <I>int segment_flush (SEGMENT *seg)</I>, flush pending updates to disk
  170. <P>
  171. Forces all pending updates generated by <I>segment_put()</I> to be
  172. written to the segment file <B>seg.</B> Must be called after the final
  173. segment_put() to force all pending updates to disk. Must also be called
  174. before the first call to <I>segment_get_row.</I>
  175. <P>
  176. Now the data in segment file can be read row by row and transferred to a normal
  177. sequential data file:
  178. <P>
  179. <I>int segment_get_row (SEGMENT *seg, char *buf, int row)</I>, read row from
  180. segment file
  181. <P>
  182. Transfers data from a segment file, row by row, into memory
  183. (which can then be written to a regular matrix file) . <B>Seg</B> is the
  184. segment structure that was configured from a call to <I>segment_init.</I>
  185. <B>Buf</B> will be filled with <I>ncols*len</I> bytes of data
  186. corresponding to the <B>row</B> in the data matrix.
  187. <P>
  188. Return codes are: 1 if ok; else -1 could not seek or read segment file.
  189. <P>
  190. Finally, memory allocated in the SEGMENT structure is freed:
  191. <P>
  192. <I>int segment_release (SEGMENT *seg)</I>, free allocated memory
  193. <P>
  194. Releases the allocated memory associated with the segment file
  195. <B>seg.</B> Does not close the file. Does not flush the data which may
  196. be pending from previous <I>segment_put()</I> calls.
  197. <P>
  198. The following routine both deletes the segment file and releases allocated
  199. memory:
  200. <P>
  201. <I>int segment_close (SEGMENT *seg)</I>, close segment structure
  202. <P>
  203. Deletes the segment file and uses segment_release() to release the
  204. allocated memory. No further cleaing up is required.
  205. <P>
  206. \section How_to_Use_the_Library_Routines How to Use the Library Routines
  207. The following should provide the programmer with a good idea of how to use the
  208. <I>Segment Library</I> routines. The examples assume that the data is integer.
  209. Creation of a segment file and initialization of the segment structure
  210. at once:
  211. \verbatim
  212. SEGMENT seg;
  213. segment_open (&seg, G_tempfile(), nrows, ncols, srows, scols, sizeof(int), nseg);
  214. \endverbatim
  215. Alternatively, the first step is the creation and formatting of a segment
  216. file. A file is created, formatted and then closed:
  217. \verbatim
  218. fd = creat (file, 0666);
  219. segment_format (fd, nrows, ncols, srows, scols, sizeof(int));
  220. close(fd);
  221. \endverbatim
  222. <P>
  223. The next step is the conversion of the nonsegmented matrix data into segment
  224. file format. The segment file is reopened for read and write and initialized:
  225. \verbatim
  226. #include <fcntl.h>
  227. SEGMENT seg;
  228. fd = open (file, O_RDWR);
  229. segment_init (&seg, fd, nseg);
  230. \endverbatim
  231. <P>
  232. Both the segment file and the segment structure are now ready to use, and
  233. data can be read row by row from the original data file and put into the
  234. segment file:
  235. \verbatim
  236. for (row = 0; row < nrows; row++)
  237. {
  238. <code to get original matrix data for row into buf>
  239. segment_put_row (&seg, buf, row);
  240. }
  241. \endverbatim
  242. <P>
  243. Of course if the intention is only to add new values rather than update existing
  244. values, the step which transfers data from the original matrix to the segment
  245. file, using segment_put_row() , could be omitted, since
  246. <I>segment_format</I> will fill the segment file with zeros.
  247. <P>
  248. The data can now be accessed directly using <I>segment_get.</I> For example,
  249. to get the value at a given row and column:
  250. \verbatim
  251. int value;
  252. SEGMENT seg;
  253. segment_get (&seg, &value, row, col);
  254. \endverbatim
  255. <P>
  256. Similarly <I>segment_put()</I> can be used to change data values in the
  257. segment file:
  258. \verbatim
  259. int value;
  260. SEGMENT seg;
  261. value = 10;
  262. segment_put (&seg, &value, row, col);
  263. \endverbatim
  264. <P>
  265. <B>WARNING:</B> It is an easy mistake to pass a value directly to
  266. segment_put(). The following should be avoided:
  267. \verbatim
  268. segment_put (&seg, 10, row, col); /* this will not work */
  269. \endverbatim
  270. <P>
  271. Once the random access processing is complete, the data would be extracted
  272. from the segment file and written to a nonsegmented matrix data file as
  273. follows:
  274. \verbatim
  275. segment_flush (&seg);
  276. for (row = 0; row < nrows; row++)
  277. {
  278. segment_get_row (&seg, buf, row);
  279. <code to put buf into a matrix data file for row>
  280. }
  281. \endverbatim
  282. <P>
  283. Finally, the memory allocated for use by the segment routines would be
  284. released and the file closed:
  285. \verbatim
  286. segment_release (&seg);
  287. close (fd);
  288. \endverbatim
  289. <P>
  290. <B>Note:</B> The <I>Segment Library</I> does not know the name of the
  291. segment file. It does not attempt to remove the file. If the file is only
  292. temporary, the programmer should remove the file after closing it.
  293. <P>
  294. \section Segment_Library_Performance Segment Library Performance
  295. Performance of the <I>Segment Library</I> routines can be improved by
  296. about 10% if <B>srows, scols</B> are each powers of 2; in this case a
  297. faster alternative is used to access the segment file. An additional
  298. improvement can be achieved if <B>len</B> is also a power of 2. For
  299. scattered access to a large dataset, smaller segments, i.e. values
  300. for <B>srows, scols</B> of 32, 64, or 128 seem to provide
  301. the best performance. Calculating segment size as a fraction of the
  302. data matrix size, e.g. srows = nrows / 4 + 1, will result in very poor
  303. performance, particularly for larger datasets.
  304. \section Loading_the_Segment_Library Loading the Segment Library
  305. <P>
  306. The library is loaded by specifying
  307. \verbatim
  308. $(SEGMENTLIB)
  309. \endverbatim
  310. in the Makefile.
  311. <P>
  312. See \ref Compiling_and_Installing_GRASS_Modules for a complete
  313. discussion of Makefiles.
  314. */