segmen2d_parallel.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /*!
  2. * \file segmen2d.c
  3. *
  4. * \author H. Mitasova, I. Kosinovsky, D. Gerdes
  5. *
  6. * \copyright
  7. * (C) 1993 by Helena Mitasova and the GRASS Development Team
  8. *
  9. * \copyright
  10. * This program is free software under the
  11. * GNU General Public License (>=v2).
  12. * Read the file COPYING that comes with GRASS
  13. * for details.
  14. *
  15. */
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <math.h>
  19. #include <omp.h>
  20. #include <grass/gis.h>
  21. #include <grass/glocale.h>
  22. #include <grass/interpf.h>
  23. #include <grass/gmath.h>
  24. static int cut_tree(struct multtree *, struct multtree **, int *);
  25. /*!
  26. * See documentation for IL_interp_segments_2d.
  27. * This is a parallel processing implementation.
  28. */
  29. int IL_interp_segments_2d_parallel(struct interp_params *params, struct tree_info *info, /*!< info for the quad tree */
  30. struct multtree *tree, /*!< current leaf of the quad tree */
  31. struct BM *bitmask, /*!< bitmask */
  32. double zmin, double zmax, /*!< min and max input z-values */
  33. double *zminac, double *zmaxac, /*!< min and max interp. z-values */
  34. double *gmin, double *gmax, /*!< min and max inperp. slope val. */
  35. double *c1min, double *c1max, /*!< min and max interp. curv. val. */
  36. double *c2min, double *c2max, /*!< min and max interp. curv. val. */
  37. double *ertot, /*!< total interplating func. error */
  38. int totsegm, /*!< total number of segments */
  39. off_t offset1, /*!< offset for temp file writing */
  40. double dnorm, int threads)
  41. {
  42. int some_thread_failed = 0;
  43. int tid;
  44. int i = 0;
  45. int j = 0;
  46. int i_cnt;
  47. int cursegm = 0;
  48. double smseg;
  49. double ***matrix = NULL;
  50. int **indx = NULL;
  51. double **b = NULL;
  52. double **A = NULL;
  53. struct quaddata **data_local;
  54. struct multtree **all_leafs;
  55. all_leafs =
  56. (struct multtree **)G_malloc(sizeof(struct multtree *) * totsegm);
  57. data_local =
  58. (struct quaddata **)G_malloc(sizeof(struct quaddata *) * threads);
  59. matrix = (double ***)G_malloc(sizeof(double **) * threads);
  60. indx = (int **)G_malloc(sizeof(int *) * threads);
  61. b = (double **)G_malloc(sizeof(double *) * threads);
  62. A = (double **)G_malloc(sizeof(double *) * threads);
  63. for (i_cnt = 0; i_cnt < threads; i_cnt++) {
  64. if (!
  65. (matrix[i_cnt] =
  66. G_alloc_matrix(params->KMAX2 + 1, params->KMAX2 + 1))) {
  67. G_fatal_error(_("Out of memory"));
  68. return -1;
  69. }
  70. }
  71. for (i_cnt = 0; i_cnt < threads; i_cnt++) {
  72. if (!(indx[i_cnt] = G_alloc_ivector(params->KMAX2 + 1))) {
  73. G_fatal_error(_("Out of memory"));
  74. return -1;
  75. }
  76. }
  77. for (i_cnt = 0; i_cnt < threads; i_cnt++) {
  78. if (!(b[i_cnt] = G_alloc_vector(params->KMAX2 + 3))) {
  79. G_fatal_error(_("Out of memory"));
  80. return -1;
  81. }
  82. }
  83. for (i_cnt = 0; i_cnt < threads; i_cnt++) {
  84. if (!
  85. (A[i_cnt] =
  86. G_alloc_vector((params->KMAX2 + 2) * (params->KMAX2 + 2) + 1))) {
  87. G_fatal_error(_("Out of memory"));
  88. return -1;
  89. }
  90. }
  91. smseg = smallest_segment(tree, 4);
  92. cut_tree(tree, all_leafs, &i);
  93. G_message(_("Starting parallel work"));
  94. #pragma omp parallel firstprivate(tid, i, j, zmin, zmax, tree, totsegm, offset1, dnorm, smseg, ertot, params, info, all_leafs, bitmask, b, indx, matrix, data_local, A) shared(cursegm, threads, some_thread_failed, zminac, zmaxac, gmin, gmax, c1min, c1max, c2min, c2max) default(none)
  95. {
  96. #pragma omp for schedule(dynamic)
  97. for (i_cnt = 0; i_cnt < totsegm; i_cnt++) {
  98. /* Obtain thread id */
  99. #if defined(_OPENMP)
  100. tid = omp_get_thread_num();
  101. #endif
  102. double xmn, xmx, ymn, ymx, distx, disty, distxp, distyp, temp1,
  103. temp2;
  104. int npt, nptprev, MAXENC;
  105. double ew_res, ns_res;
  106. int MINPTS;
  107. double pr;
  108. struct triple *point;
  109. struct triple skip_point;
  110. int m_skip, skip_index, k, segtest;
  111. double xx, yy, zz;
  112. //struct quaddata *data_local;
  113. ns_res = (((struct quaddata *)(tree->data))->ymax -
  114. ((struct quaddata *)(tree->data))->y_orig) /
  115. params->nsizr;
  116. ew_res =
  117. (((struct quaddata *)(tree->data))->xmax -
  118. ((struct quaddata *)(tree->data))->x_orig) / params->nsizc;
  119. if (all_leafs[i_cnt] == NULL) {
  120. some_thread_failed = -1;
  121. continue;
  122. }
  123. if (all_leafs[i_cnt]->data == NULL) {
  124. some_thread_failed = -1;
  125. continue;
  126. }
  127. if (((struct quaddata *)(all_leafs[i_cnt]->data))->points == NULL) {
  128. continue;
  129. }
  130. else {
  131. distx =
  132. (((struct quaddata *)(all_leafs[i_cnt]->data))->n_cols *
  133. ew_res) * 0.1;
  134. disty =
  135. (((struct quaddata *)(all_leafs[i_cnt]->data))->n_rows *
  136. ns_res) * 0.1;
  137. distxp = 0;
  138. distyp = 0;
  139. xmn = ((struct quaddata *)(all_leafs[i_cnt]->data))->x_orig;
  140. xmx = ((struct quaddata *)(all_leafs[i_cnt]->data))->xmax;
  141. ymn = ((struct quaddata *)(all_leafs[i_cnt]->data))->y_orig;
  142. ymx = ((struct quaddata *)(all_leafs[i_cnt]->data))->ymax;
  143. i = 0;
  144. MAXENC = 0;
  145. /* data is a window with zero points; some fields don't make sence in this case
  146. so they are zero (like resolution,dimentions */
  147. /* CHANGE */
  148. /* Calcutaing kmin for surrent segment (depends on the size) */
  149. /*****if (smseg <= 0.00001) MINPTS=params->kmin; else {} ***/
  150. pr = pow(2., (xmx - xmn) / smseg - 1.);
  151. MINPTS =
  152. params->kmin * (pr /
  153. (1 + params->kmin * pr / params->KMAX2));
  154. /* fprintf(stderr,"MINPTS=%d, KMIN=%d, KMAX=%d, pr=%lf, smseg=%lf, DX=%lf \n", MINPTS,params->kmin,params->KMAX2,pr,smseg,xmx-xmn); */
  155. data_local[tid] =
  156. (struct quaddata *)quad_data_new(xmn - distx, ymn - disty,
  157. xmx + distx, ymx + disty,
  158. 0, 0, 0, params->KMAX2);
  159. npt =
  160. MT_region_data(info, tree, data_local[tid], params->KMAX2,
  161. 4);
  162. while ((npt < MINPTS) || (npt > params->KMAX2)) {
  163. if (i >= 70) {
  164. G_warning(_("Taking too long to find points for interpolation - "
  165. "please change the region to area where your points are. "
  166. "Continuing calculations..."));
  167. break;
  168. }
  169. i++;
  170. if (npt > params->KMAX2)
  171. /* decrease window */
  172. {
  173. MAXENC = 1;
  174. nptprev = npt;
  175. temp1 = distxp;
  176. distxp = distx;
  177. distx = distxp - fabs(distx - temp1) * 0.5;
  178. temp2 = distyp;
  179. distyp = disty;
  180. disty = distyp - fabs(disty - temp2) * 0.5;
  181. /* decrease by 50% of a previous change in window */
  182. }
  183. else {
  184. nptprev = npt;
  185. temp1 = distyp;
  186. distyp = disty;
  187. temp2 = distxp;
  188. distxp = distx;
  189. if (MAXENC) {
  190. disty = fabs(disty - temp1) * 0.5 + distyp;
  191. distx = fabs(distx - temp2) * 0.5 + distxp;
  192. }
  193. else {
  194. distx += distx;
  195. disty += disty;
  196. }
  197. /* decrease by 50% of extra distance */
  198. }
  199. data_local[tid]->x_orig = xmn - distx; /* update window */
  200. data_local[tid]->y_orig = ymn - disty;
  201. data_local[tid]->xmax = xmx + distx;
  202. data_local[tid]->ymax = ymx + disty;
  203. data_local[tid]->n_points = 0;
  204. npt =
  205. MT_region_data(info, tree, data_local[tid],
  206. params->KMAX2, 4);
  207. }
  208. if (totsegm != 0 && tid == 0) {
  209. G_percent(cursegm, totsegm, 1);
  210. }
  211. data_local[tid]->n_rows =
  212. ((struct quaddata *)(all_leafs[i_cnt]->data))->n_rows;
  213. data_local[tid]->n_cols =
  214. ((struct quaddata *)(all_leafs[i_cnt]->data))->n_cols;
  215. /* for printing out overlapping segments */
  216. ((struct quaddata *)(all_leafs[i_cnt]->data))->x_orig =
  217. xmn - distx;
  218. ((struct quaddata *)(all_leafs[i_cnt]->data))->y_orig =
  219. ymn - disty;
  220. ((struct quaddata *)(all_leafs[i_cnt]->data))->xmax =
  221. xmx + distx;
  222. ((struct quaddata *)(all_leafs[i_cnt]->data))->ymax =
  223. ymx + disty;
  224. data_local[tid]->x_orig = xmn;
  225. data_local[tid]->y_orig = ymn;
  226. data_local[tid]->xmax = xmx;
  227. data_local[tid]->ymax = ymx;
  228. /* allocate memory for CV points only if cv is performed */
  229. if (params->cv) {
  230. if (!
  231. (point =
  232. (struct triple *)G_malloc(sizeof(struct triple) *
  233. data_local[tid]->
  234. n_points))) {
  235. G_warning(_("Out of memory"));
  236. some_thread_failed = -1;
  237. continue;
  238. }
  239. }
  240. /*normalize the data so that the side of average segment is about 1m */
  241. /* put data_points into point only if CV is performed */
  242. for (i = 0; i < data_local[tid]->n_points; i++) {
  243. data_local[tid]->points[i].x =
  244. (data_local[tid]->points[i].x -
  245. data_local[tid]->x_orig) / dnorm;
  246. data_local[tid]->points[i].y =
  247. (data_local[tid]->points[i].y -
  248. data_local[tid]->y_orig) / dnorm;
  249. if (params->cv) {
  250. point[i].x = data_local[tid]->points[i].x; /*cv stuff */
  251. point[i].y = data_local[tid]->points[i].y; /*cv stuff */
  252. point[i].z = data_local[tid]->points[i].z; /*cv stuff */
  253. }
  254. /* commented out by Helena january 1997 as this is not necessary
  255. although it may be useful to put normalization of z back?
  256. data->points[i].z = data->points[i].z / dnorm;
  257. this made smoothing self-adjusting based on dnorm
  258. if (params->rsm < 0.) data->points[i].sm = data->points[i].sm / dnorm;
  259. */
  260. }
  261. /* cv stuff */
  262. if (params->cv) {
  263. m_skip = data_local[tid]->n_points;
  264. }
  265. else {
  266. m_skip = 1;
  267. }
  268. /* remove after cleanup - this is just for testing */
  269. skip_point.x = 0.;
  270. skip_point.y = 0.;
  271. skip_point.z = 0.;
  272. for (skip_index = 0; skip_index < m_skip; skip_index++) {
  273. if (params->cv) {
  274. segtest = 0;
  275. j = 0;
  276. xx = point[skip_index].x * dnorm +
  277. data_local[tid]->x_orig + params->x_orig;
  278. yy = point[skip_index].y * dnorm +
  279. data_local[tid]->y_orig + params->y_orig;
  280. zz = point[skip_index].z;
  281. if (xx >= data_local[tid]->x_orig + params->x_orig &&
  282. xx <= data_local[tid]->xmax + params->x_orig &&
  283. yy >= data_local[tid]->y_orig + params->y_orig &&
  284. yy <= data_local[tid]->ymax + params->y_orig) {
  285. segtest = 1;
  286. skip_point.x = point[skip_index].x;
  287. skip_point.y = point[skip_index].y;
  288. skip_point.z = point[skip_index].z;
  289. for (k = 0; k < m_skip; k++) {
  290. if (k != skip_index && params->cv) {
  291. data_local[tid]->points[j].x = point[k].x;
  292. data_local[tid]->points[j].y = point[k].y;
  293. data_local[tid]->points[j].z = point[k].z;
  294. j++;
  295. }
  296. }
  297. } /* segment area test */
  298. }
  299. if (!params->cv) {
  300. if ( /* params */
  301. IL_matrix_create_alloc(params,
  302. data_local[tid]->points,
  303. data_local[tid]->
  304. n_points, matrix[tid],
  305. indx[tid],
  306. A[tid]) < 0) {
  307. some_thread_failed = -1;
  308. continue;
  309. }
  310. }
  311. else if (segtest == 1) {
  312. if ( /* params */
  313. IL_matrix_create_alloc(params,
  314. data_local[tid]->points,
  315. data_local[tid]->
  316. n_points - 1,
  317. matrix[tid], indx[tid],
  318. A[tid]) < 0) {
  319. some_thread_failed = -1;
  320. continue;
  321. }
  322. }
  323. if (!params->cv) {
  324. for (i = 0; i < data_local[tid]->n_points; i++) {
  325. b[tid][i + 1] = data_local[tid]->points[i].z;
  326. }
  327. b[tid][0] = 0.;
  328. G_lubksb(matrix[tid], data_local[tid]->n_points + 1,
  329. indx[tid], b[tid]);
  330. /* put here condition to skip error if not needed */
  331. params->check_points(params, data_local[tid], b[tid],
  332. ertot, zmin, dnorm, skip_point);
  333. }
  334. else if (segtest == 1) {
  335. for (i = 0; i < data_local[tid]->n_points - 1; i++) {
  336. b[tid][i + 1] = data_local[tid]->points[i].z;
  337. }
  338. b[tid][0] = 0.;
  339. G_lubksb(matrix[tid], data_local[tid]->n_points,
  340. indx[tid], b[tid]);
  341. params->check_points(params, data_local[tid], b[tid],
  342. ertot, zmin, dnorm, skip_point);
  343. }
  344. } /*end of cv loop */
  345. if (!params->cv) {
  346. if ((params->Tmp_fd_z != NULL) ||
  347. (params->Tmp_fd_dx != NULL) ||
  348. (params->Tmp_fd_dy != NULL) ||
  349. (params->Tmp_fd_xx != NULL) ||
  350. (params->Tmp_fd_yy != NULL) ||
  351. (params->Tmp_fd_xy != NULL)) {
  352. #pragma omp critical
  353. {
  354. if (params->grid_calc
  355. (params, data_local[tid], bitmask, zmin, zmax,
  356. zminac, zmaxac, gmin, gmax, c1min, c1max,
  357. c2min, c2max, ertot, b[tid], offset1,
  358. dnorm) < 0) {
  359. some_thread_failed = -1;
  360. }
  361. }
  362. }
  363. }
  364. /* show after to catch 100% */
  365. #pragma omp atomic
  366. cursegm++;
  367. if (totsegm < cursegm) {
  368. G_debug(1, "%d %d", totsegm, cursegm);
  369. }
  370. if (totsegm != 0 && tid == 0) {
  371. G_percent(cursegm, totsegm, 1);
  372. }
  373. /*
  374. G_free_matrix(matrix);
  375. G_free_ivector(indx);
  376. G_free_vector(b);
  377. */
  378. G_free(data_local[tid]->points);
  379. G_free(data_local[tid]);
  380. }
  381. }
  382. } /* All threads join master thread and terminate */
  383. for (i_cnt = 0; i_cnt < threads; i_cnt++) {
  384. G_free(matrix[i_cnt]);
  385. G_free(indx[i_cnt]);
  386. G_free(b[i_cnt]);
  387. G_free(A[i_cnt]);
  388. }
  389. G_free(all_leafs);
  390. G_free(data_local);
  391. G_free(matrix);
  392. G_free(indx);
  393. G_free(b);
  394. G_free(A);
  395. if (some_thread_failed != 0) {
  396. return -1;
  397. }
  398. return 1;
  399. }
  400. /* cut given tree into separate leafs */
  401. int cut_tree(struct multtree *tree, /* tree we want to cut */
  402. struct multtree **cut_leafs, /* array of leafs */
  403. int *where_to_add /* index of leaf which will be next */ )
  404. {
  405. if (tree == NULL)
  406. return -1;
  407. if (tree->data == NULL)
  408. return -1;
  409. if (((struct quaddata *)(tree->data))->points == NULL) {
  410. int i;
  411. for (i = 0; i < 4; i++) {
  412. cut_tree(tree->leafs[i], cut_leafs, where_to_add);
  413. }
  414. return 1;
  415. }
  416. else {
  417. cut_leafs[*where_to_add] = tree;
  418. (*where_to_add)++;
  419. return 1;
  420. }
  421. }